blob: 7fe33d8e5f4b0fa74247573db6f577979b2428b6 [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
699 cstack.cs_idx = -1;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000700 cstack.cs_looplevel = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000701 cstack.cs_trylevel = 0;
702 cstack.cs_emsg_silent_list = NULL;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000703 cstack.cs_lflags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000704 ga_init2(&lines_ga, (int)sizeof(wcmd_T), 10);
705
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100706 real_cookie = getline_cookie(fgetline, cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000707
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100708 // Inside a function use a higher nesting level.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100709 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000710 if (getline_is_func && ex_nesting_level == func_level(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000711 ++ex_nesting_level;
712
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100713 // Get the function or script name and the address where the next breakpoint
714 // line and the debug tick for a function or script are stored.
Bram Moolenaar05159a02005-02-26 23:04:13 +0000715 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000716 {
717 fname = func_name(real_cookie);
718 breakpoint = func_breakpoint(real_cookie);
719 dbg_tick = func_dbg_tick(real_cookie);
720 }
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100721 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000722 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100723 fname = SOURCING_NAME;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000724 breakpoint = source_breakpoint(real_cookie);
725 dbg_tick = source_dbg_tick(real_cookie);
726 }
727
728 /*
729 * Initialize "force_abort" and "suppress_errthrow" at the top level.
730 */
731 if (!recursive)
732 {
733 force_abort = FALSE;
734 suppress_errthrow = FALSE;
735 }
736
737 /*
738 * If requested, store and reset the global values controlling the
Bram Moolenaar89d40322006-08-29 15:30:07 +0000739 * exception handling (used when debugging). Otherwise clear it to avoid
740 * a bogus compiler warning when the optimizer uses inline functions...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000741 */
Bram Moolenaarb4872942006-05-13 10:32:52 +0000742 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +0000743 save_dbg_stuff(&debug_saved);
Bram Moolenaar89d40322006-08-29 15:30:07 +0000744 else
Bram Moolenaara80faa82020-04-12 19:37:17 +0200745 CLEAR_FIELD(debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000746
747 initial_trylevel = trylevel;
748
749 /*
750 * "did_throw" will be set to TRUE when an exception is being thrown.
751 */
752 did_throw = FALSE;
753#endif
754 /*
755 * "did_emsg" will be set to TRUE when emsg() is used, in which case we
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000756 * cancel the whole command line, and any if/endif or loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000757 * If force_abort is set, we cancel everything.
758 */
Bram Moolenaareeece9e2020-11-20 19:26:48 +0100759#ifdef FEAT_EVAL
760 did_emsg_cumul += did_emsg;
761#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000762 did_emsg = FALSE;
763
764 /*
765 * KeyTyped is only set when calling vgetc(). Reset it here when not
766 * calling vgetc() (sourced command lines).
767 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100768 if (!(flags & DOCMD_KEYTYPED)
769 && !getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770 KeyTyped = FALSE;
771
772 /*
773 * Continue executing command lines:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000774 * - when inside an ":if", ":while" or ":for"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775 * - for multiple commands on one line, separated with '|'
776 * - when repeating until there are no more lines (for ":source")
777 */
778 next_cmdline = cmdline;
779 do
780 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000781#ifdef FEAT_EVAL
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100782 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000783#endif
784
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100785 // stop skipping cmds for an error msg after all endif/while/for
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786 if (next_cmdline == NULL
787#ifdef FEAT_EVAL
788 && !force_abort
789 && cstack.cs_idx < 0
Bram Moolenaar05159a02005-02-26 23:04:13 +0000790 && !(getline_is_func && func_has_abort(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791#endif
792 )
Bram Moolenaareeece9e2020-11-20 19:26:48 +0100793 {
794#ifdef FEAT_EVAL
795 did_emsg_cumul += did_emsg;
796#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000797 did_emsg = FALSE;
Bram Moolenaareeece9e2020-11-20 19:26:48 +0100798 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000799
800 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000801 * 1. If repeating a line in a loop, get a line from lines_ga.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100802 * 2. If no line given: Get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000803 * 3. If a line is given: Make a copy, so we can mess with it.
804 */
805
806#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100807 // 1. If repeating, get a previous line from lines_ga.
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000808 if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000809 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100810 // Each '|' separated command is stored separately in lines_ga, to
811 // be able to jump to it. Don't use next_cmdline now.
Bram Moolenaard23a8232018-02-10 18:45:26 +0100812 VIM_CLEAR(cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100814 // Check if a function has returned or, unless it has an unclosed
815 // try conditional, aborted.
Bram Moolenaar05159a02005-02-26 23:04:13 +0000816 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000817 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000818# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000819 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000820 func_line_end(real_cookie);
821# endif
822 if (func_has_ended(real_cookie))
823 {
824 retval = FAIL;
825 break;
826 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000828#ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000829 else if (do_profiling == PROF_YES
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100830 && getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000831 script_line_end();
832#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000833
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100834 // Check if a sourced file hit a ":finish" command.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100835 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000836 {
837 retval = FAIL;
838 break;
839 }
840
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100841 // If breakpoints have been added/deleted need to check for it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842 if (breakpoint != NULL && dbg_tick != NULL
843 && *dbg_tick != debug_tick)
844 {
845 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100846 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100847 fname, SOURCING_LNUM);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000848 *dbg_tick = debug_tick;
849 }
850
851 next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100852 SOURCING_LNUM = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000853
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100854 // Did we encounter a breakpoint?
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855 if (breakpoint != NULL && *breakpoint != 0
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100856 && *breakpoint <= SOURCING_LNUM)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000857 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100858 dbg_breakpoint(fname, SOURCING_LNUM);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100859 // Find next breakpoint.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000860 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100861 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100862 fname, SOURCING_LNUM);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000863 *dbg_tick = debug_tick;
864 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000865# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000866 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000867 {
868 if (getline_is_func)
869 func_line_start(real_cookie);
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100870 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000871 script_line_start();
872 }
873# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000875#endif
876
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100877 // 2. If no line given, get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878 if (next_cmdline == NULL)
879 {
880 /*
881 * Need to set msg_didout for the first line after an ":if",
882 * otherwise the ":if" will be overwritten.
883 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100884 if (count == 1 && getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885 msg_didout = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100886 if (fgetline == NULL || (next_cmdline = fgetline(':', cookie,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000887#ifdef FEAT_EVAL
888 cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2
889#else
890 0
891#endif
Bram Moolenaar8242ebb2020-12-29 11:15:01 +0100892 , in_vim9script() ? GETLINE_CONCAT_CONTBAR
893 : GETLINE_CONCAT_CONT)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100895 // Don't call wait_return for aborted command line. The NULL
896 // returned for the end of a sourced file or executed function
897 // doesn't do this.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000898 if (KeyTyped && !(flags & DOCMD_REPEAT))
899 need_wait_return = FALSE;
900 retval = FAIL;
901 break;
902 }
903 used_getline = TRUE;
904
905 /*
906 * Keep the first typed line. Clear it when more lines are typed.
907 */
908 if (flags & DOCMD_KEEPLINE)
909 {
910 vim_free(repeat_cmdline);
911 if (count == 0)
912 repeat_cmdline = vim_strsave(next_cmdline);
913 else
914 repeat_cmdline = NULL;
915 }
916 }
917
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100918 // 3. Make a copy of the command so we can mess with it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000919 else if (cmdline_copy == NULL)
920 {
921 next_cmdline = vim_strsave(next_cmdline);
922 if (next_cmdline == NULL)
923 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100924 emsg(_(e_outofmem));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000925 retval = FAIL;
926 break;
927 }
928 }
929 cmdline_copy = next_cmdline;
930
931#ifdef FEAT_EVAL
932 /*
Bram Moolenaard5053d02020-06-28 15:51:16 +0200933 * Inside a while/for loop, and when the command looks like a ":while"
934 * or ":for", the line is stored, because we may need it later when
935 * looping.
936 *
937 * When there is a '|' and another command, it is stored separately,
938 * because we need to be able to jump back to it from an
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000939 * :endwhile/:endfor.
Bram Moolenaard5053d02020-06-28 15:51:16 +0200940 *
941 * Pass a different "fgetline" function to do_one_cmd() below,
942 * that it stores lines in or reads them from "lines_ga". Makes it
943 * possible to define a function inside a while/for loop and handles
944 * line continuation.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000945 */
Bram Moolenaard5053d02020-06-28 15:51:16 +0200946 if ((cstack.cs_looplevel > 0 || has_loop_cmd(next_cmdline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000947 {
Bram Moolenaard5053d02020-06-28 15:51:16 +0200948 cmd_getline = get_loop_line;
949 cmd_cookie = (void *)&cmd_loop_cookie;
950 cmd_loop_cookie.lines_gap = &lines_ga;
951 cmd_loop_cookie.current_line = current_line;
952 cmd_loop_cookie.getline = fgetline;
953 cmd_loop_cookie.cookie = cookie;
954 cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len);
955
956 // Save the current line when encountering it the first time.
957 if (current_line == lines_ga.ga_len
958 && store_loop_line(&lines_ga, next_cmdline) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000959 {
960 retval = FAIL;
961 break;
962 }
Bram Moolenaard5053d02020-06-28 15:51:16 +0200963 current_line_before = current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000964 }
Bram Moolenaard5053d02020-06-28 15:51:16 +0200965 else
966 {
967 cmd_getline = fgetline;
968 cmd_cookie = cookie;
969 }
970
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971 did_endif = FALSE;
972#endif
973
974 if (count++ == 0)
975 {
976 /*
977 * All output from the commands is put below each other, without
978 * waiting for a return. Don't do this when executing commands
979 * from a script or when being called recursive (e.g. for ":e
980 * +command file").
981 */
982 if (!(flags & DOCMD_NOWAIT) && !recursive)
983 {
984 msg_didout_before_start = msg_didout;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100985 msg_didany = FALSE; // no output yet
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986 msg_start();
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100987 msg_scroll = TRUE; // put messages below each other
988 ++no_wait_return; // don't wait for return until finished
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989 ++RedrawingDisabled;
990 did_inc = TRUE;
991 }
992 }
993
Bram Moolenaar823654b2020-05-29 23:03:09 +0200994 if ((p_verbose >= 15 && SOURCING_NAME != NULL) || p_verbose >= 16)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100995 msg_verbose_cmd(SOURCING_LNUM, cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996
997 /*
998 * 2. Execute one '|' separated command.
999 * do_one_cmd() will return NULL if there is no trailing '|'.
1000 * "cmdline_copy" can change, e.g. for '%' and '#' expansion.
1001 */
1002 ++recursive;
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001003 next_cmdline = do_one_cmd(&cmdline_copy, flags,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001004#ifdef FEAT_EVAL
1005 &cstack,
1006#endif
1007 cmd_getline, cmd_cookie);
1008 --recursive;
1009
1010#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001011 if (cmd_cookie == (void *)&cmd_loop_cookie)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001012 // Use "current_line" from "cmd_loop_cookie", it may have been
1013 // incremented when defining a function.
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001014 current_line = cmd_loop_cookie.current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015#endif
1016
1017 if (next_cmdline == NULL)
1018 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01001019 VIM_CLEAR(cmdline_copy);
Bram Moolenaard7663c22019-08-06 21:59:57 +02001020
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021 /*
1022 * If the command was typed, remember it for the ':' register.
1023 * Do this AFTER executing the command to make :@: work.
1024 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001025 if (getline_equal(fgetline, cookie, getexline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026 && new_last_cmdline != NULL)
1027 {
1028 vim_free(last_cmdline);
1029 last_cmdline = new_last_cmdline;
1030 new_last_cmdline = NULL;
1031 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001032 }
1033 else
1034 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001035 // need to copy the command after the '|' to cmdline_copy, for the
1036 // next do_one_cmd()
Bram Moolenaara7241f52008-06-24 20:39:31 +00001037 STRMOVE(cmdline_copy, next_cmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038 next_cmdline = cmdline_copy;
1039 }
1040
1041
1042#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001043 // reset did_emsg for a function that is not aborted by an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00001044 if (did_emsg && !force_abort
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001045 && getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001046 && !func_has_abort(real_cookie))
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001047 {
1048 // did_emsg_cumul is not set here
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049 did_emsg = FALSE;
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001050 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001052 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001053 {
1054 ++current_line;
1055
1056 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001057 * An ":endwhile", ":endfor" and ":continue" is handled here.
1058 * If we were executing commands, jump back to the ":while" or
1059 * ":for".
1060 * If we were not executing commands, decrement cs_looplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001062 if (cstack.cs_lflags & (CSL_HAD_CONT | CSL_HAD_ENDLOOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001064 cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001066 // Jump back to the matching ":while" or ":for". Be careful
1067 // not to use a cs_line[] from an entry that isn't a ":while"
1068 // or ":for": It would make "current_line" invalid and can
1069 // cause a crash.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001070 if (!did_emsg && !got_int && !did_throw
1071 && cstack.cs_idx >= 0
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001072 && (cstack.cs_flags[cstack.cs_idx]
1073 & (CSF_WHILE | CSF_FOR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074 && cstack.cs_line[cstack.cs_idx] >= 0
1075 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE))
1076 {
1077 current_line = cstack.cs_line[cstack.cs_idx];
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001078 // remember we jumped there
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001079 cstack.cs_lflags |= CSL_HAD_LOOP;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001080 line_breakcheck(); // check if CTRL-C typed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001082 // Check for the next breakpoint at or after the ":while"
1083 // or ":for".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084 if (breakpoint != NULL)
1085 {
1086 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001087 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088 fname,
1089 ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1);
1090 *dbg_tick = debug_tick;
1091 }
1092 }
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001093 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001095 // can only get here with ":endwhile" or ":endfor"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001096 if (cstack.cs_idx >= 0)
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001097 rewind_conditionals(&cstack, cstack.cs_idx - 1,
1098 CSF_WHILE | CSF_FOR, &cstack.cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099 }
1100 }
1101
1102 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001103 * For a ":while" or ":for" we need to remember the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001105 else if (cstack.cs_lflags & CSL_HAD_LOOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001106 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001107 cstack.cs_lflags &= ~CSL_HAD_LOOP;
Bram Moolenaard5053d02020-06-28 15:51:16 +02001108 cstack.cs_line[cstack.cs_idx] = current_line_before;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109 }
1110 }
1111
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001112 // Check for the next breakpoint after a watchexpression
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01001113 if (breakpoint != NULL && has_watchexpr())
1114 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001115 *breakpoint = dbg_find_breakpoint(FALSE, fname, SOURCING_LNUM);
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01001116 *dbg_tick = debug_tick;
1117 }
1118
Bram Moolenaar071d4272004-06-13 20:20:40 +00001119 /*
1120 * When not inside any ":while" loop, clear remembered lines.
1121 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001122 if (cstack.cs_looplevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123 {
1124 if (lines_ga.ga_len > 0)
1125 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001126 SOURCING_LNUM =
Bram Moolenaar071d4272004-06-13 20:20:40 +00001127 ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum;
1128 free_cmdlines(&lines_ga);
1129 }
1130 current_line = 0;
1131 }
1132
1133 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001134 * A ":finally" makes did_emsg, got_int, and did_throw pending for
1135 * being restored at the ":endtry". Reset them here and set the
1136 * ACTIVE and FINALLY flags, so that the finally clause gets executed.
1137 * This includes the case where a missing ":endif", ":endwhile" or
1138 * ":endfor" was detected by the ":finally" itself.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001140 if (cstack.cs_lflags & CSL_HAD_FINA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001141 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001142 cstack.cs_lflags &= ~CSL_HAD_FINA;
1143 report_make_pending(cstack.cs_pending[cstack.cs_idx]
1144 & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145 did_throw ? (void *)current_exception : NULL);
1146 did_emsg = got_int = did_throw = FALSE;
1147 cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY;
1148 }
1149
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001150 // Update global "trylevel" for recursive calls to do_cmdline() from
1151 // within this loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152 trylevel = initial_trylevel + cstack.cs_trylevel;
1153
1154 /*
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001155 * If the outermost try conditional (across function calls and sourced
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156 * files) is aborted because of an error, an interrupt, or an uncaught
1157 * exception, cancel everything. If it is left normally, reset
1158 * force_abort to get the non-EH compatible abortion behavior for
1159 * the rest of the script.
1160 */
1161 if (trylevel == 0 && !did_emsg && !got_int && !did_throw)
1162 force_abort = FALSE;
1163
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001164 // Convert an interrupt to an exception if appropriate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165 (void)do_intthrow(&cstack);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001166#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167
1168 }
1169 /*
1170 * Continue executing command lines when:
1171 * - no CTRL-C typed, no aborting error, no exception thrown or try
1172 * conditionals need to be checked for executing finally clauses or
1173 * catching an interrupt exception
1174 * - didn't get an error message or lines are not typed
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001175 * - there is a command after '|', inside a :if, :while, :for or :try, or
Bram Moolenaar071d4272004-06-13 20:20:40 +00001176 * looping for ":source" command or function call.
1177 */
1178 while (!((got_int
1179#ifdef FEAT_EVAL
1180 || (did_emsg && force_abort) || did_throw
1181#endif
1182 )
1183#ifdef FEAT_EVAL
1184 && cstack.cs_trylevel == 0
1185#endif
1186 )
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001187 && !(did_emsg
1188#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001189 // Keep going when inside try/catch, so that the error can be
1190 // deal with, except when it is a syntax error, it may cause
1191 // the :endtry to be missed.
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001192 && (cstack.cs_trylevel == 0 || did_emsg_syntax)
1193#endif
1194 && used_getline
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001195 && (getline_equal(fgetline, cookie, getexmodeline)
1196 || getline_equal(fgetline, cookie, getexline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197 && (next_cmdline != NULL
1198#ifdef FEAT_EVAL
1199 || cstack.cs_idx >= 0
1200#endif
1201 || (flags & DOCMD_REPEAT)));
1202
1203 vim_free(cmdline_copy);
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001204 did_emsg_syntax = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205#ifdef FEAT_EVAL
1206 free_cmdlines(&lines_ga);
1207 ga_clear(&lines_ga);
1208
1209 if (cstack.cs_idx >= 0)
1210 {
1211 /*
1212 * If a sourced file or executed function ran to its end, report the
1213 * unclosed conditional.
1214 */
1215 if (!got_int && !did_throw
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001216 && ((getline_equal(fgetline, cookie, getsourceline)
1217 && !source_finished(fgetline, cookie))
1218 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219 && !func_has_ended(real_cookie))))
1220 {
1221 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001222 emsg(_(e_endtry));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001224 emsg(_(e_endwhile));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001225 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001226 emsg(_(e_endfor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001227 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001228 emsg(_(e_endif));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001229 }
1230
1231 /*
1232 * Reset "trylevel" in case of a ":finish" or ":return" or a missing
1233 * ":endtry" in a sourced file or executed function. If the try
1234 * conditional is in its finally clause, ignore anything pending.
1235 * If it is in a catch clause, finish the caught exception.
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001236 * Also cleanup any "cs_forinfo" structures.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001237 */
1238 do
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001239 {
1240 int idx = cleanup_conditionals(&cstack, 0, TRUE);
1241
Bram Moolenaar89e5d682005-01-17 22:06:23 +00001242 if (idx >= 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001243 --idx; // remove try block not in its finally clause
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001244 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
1245 &cstack.cs_looplevel);
1246 }
1247 while (cstack.cs_idx >= 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001248 trylevel = initial_trylevel;
1249 }
1250
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001251 // If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
1252 // lack was reported above and the error message is to be converted to an
1253 // exception, do this now after rewinding the cstack.
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001254 do_errthrow(&cstack, getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001255 ? (char_u *)"endfunction" : (char_u *)NULL);
1256
1257 if (trylevel == 0)
1258 {
Bram Moolenaar820d55a2020-10-10 15:05:23 +02001259 // Just in case did_throw got set but current_exception wasn't.
1260 if (current_exception == NULL)
1261 did_throw = FALSE;
1262
Bram Moolenaar071d4272004-06-13 20:20:40 +00001263 /*
1264 * When an exception is being thrown out of the outermost try
1265 * conditional, discard the uncaught exception, disable the conversion
1266 * of interrupts or errors to exceptions, and ensure that no more
1267 * commands are executed.
1268 */
1269 if (did_throw)
1270 {
Bram Moolenaar033135e2021-01-14 21:40:22 +01001271 char *p = NULL;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02001272 msglist_T *messages = NULL, *next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273
1274 /*
1275 * If the uncaught exception is a user exception, report it as an
1276 * error. If it is an error exception, display the saved error
1277 * message now. For an interrupt exception, do nothing; the
1278 * interrupt message is given elsewhere.
1279 */
1280 switch (current_exception->type)
1281 {
1282 case ET_USER:
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001283 vim_snprintf((char *)IObuff, IOSIZE,
1284 _("E605: Exception not caught: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285 current_exception->value);
Bram Moolenaar033135e2021-01-14 21:40:22 +01001286 p = (char *)vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001287 break;
1288 case ET_ERROR:
1289 messages = current_exception->messages;
1290 current_exception->messages = NULL;
1291 break;
1292 case ET_INTERRUPT:
1293 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001294 }
1295
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001296 estack_push(ETYPE_EXCEPT, current_exception->throw_name,
1297 current_exception->throw_lnum);
Bram Moolenaare31ee862020-01-07 20:59:34 +01001298 ESTACK_CHECK_SETUP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001299 current_exception->throw_name = NULL;
1300
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001301 discard_current_exception(); // uses IObuff if 'verbose'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001302 suppress_errthrow = TRUE;
1303 force_abort = TRUE;
1304
1305 if (messages != NULL)
1306 {
1307 do
1308 {
1309 next = messages->next;
1310 emsg(messages->msg);
1311 vim_free(messages->msg);
Bram Moolenaar5fbf3bc2020-06-01 21:13:11 +02001312 vim_free(messages->sfile);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313 vim_free(messages);
1314 messages = next;
1315 }
1316 while (messages != NULL);
1317 }
1318 else if (p != NULL)
1319 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01001320 emsg(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321 vim_free(p);
1322 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001323 vim_free(SOURCING_NAME);
Bram Moolenaare31ee862020-01-07 20:59:34 +01001324 ESTACK_CHECK_NOW
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001325 estack_pop();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326 }
1327
1328 /*
1329 * On an interrupt or an aborting error not converted to an exception,
1330 * disable the conversion of errors to exceptions. (Interrupts are not
Bram Moolenaar2196bce2020-04-12 20:01:11 +02001331 * converted anymore, here.) This enables also the interrupt message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332 * when force_abort is set and did_emsg unset in case of an interrupt
1333 * from a finally clause after an error.
1334 */
1335 else if (got_int || (did_emsg && force_abort))
1336 suppress_errthrow = TRUE;
1337 }
1338
1339 /*
1340 * The current cstack will be freed when do_cmdline() returns. An uncaught
1341 * exception will have to be rethrown in the previous cstack. If a function
1342 * has just returned or a script file was just finished and the previous
1343 * cstack belongs to the same function or, respectively, script file, it
1344 * will have to be checked for finally clauses to be executed due to the
1345 * ":return" or ":finish". This is done in do_one_cmd().
1346 */
1347 if (did_throw)
1348 need_rethrow = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001349 if ((getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350 && ex_nesting_level > source_level(real_cookie))
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001351 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352 && ex_nesting_level > func_level(real_cookie) + 1))
1353 {
1354 if (!did_throw)
1355 check_cstack = TRUE;
1356 }
1357 else
1358 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001359 // When leaving a function, reduce nesting level.
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001360 if (getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361 --ex_nesting_level;
1362 /*
1363 * Go to debug mode when returning from a function in which we are
1364 * single-stepping.
1365 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001366 if ((getline_equal(fgetline, cookie, getsourceline)
1367 || getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001368 && ex_nesting_level + 1 <= debug_break_level)
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001369 do_debug(getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370 ? (char_u *)_("End of sourced file")
1371 : (char_u *)_("End of function"));
1372 }
1373
1374 /*
1375 * Restore the exception environment (done after returning from the
1376 * debugger).
1377 */
1378 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001379 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380
1381 msg_list = saved_msg_list;
Bram Moolenaar292b90d2020-03-18 15:23:16 +01001382
1383 // Cleanup if "cs_emsg_silent_list" remains.
1384 if (cstack.cs_emsg_silent_list != NULL)
1385 {
1386 eslist_T *elem, *temp;
1387
1388 for (elem = cstack.cs_emsg_silent_list; elem != NULL; elem = temp)
1389 {
1390 temp = elem->next;
1391 vim_free(elem);
1392 }
1393 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001394#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395
1396 /*
1397 * If there was too much output to fit on the command line, ask the user to
1398 * hit return before redrawing the screen. With the ":global" command we do
1399 * this only once after the command is finished.
1400 */
1401 if (did_inc)
1402 {
1403 --RedrawingDisabled;
1404 --no_wait_return;
1405 msg_scroll = FALSE;
1406
1407 /*
1408 * When just finished an ":if"-":else" which was typed, no need to
1409 * wait for hit-return. Also for an error situation.
1410 */
1411 if (retval == FAIL
1412#ifdef FEAT_EVAL
1413 || (did_endif && KeyTyped && !did_emsg)
1414#endif
1415 )
1416 {
1417 need_wait_return = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001418 msg_didany = FALSE; // don't wait when restarting edit
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419 }
1420 else if (need_wait_return)
1421 {
1422 /*
1423 * The msg_start() above clears msg_didout. The wait_return we do
1424 * here should not overwrite the command that may be shown before
1425 * doing that.
1426 */
1427 msg_didout |= msg_didout_before_start;
1428 wait_return(FALSE);
1429 }
1430 }
1431
Bram Moolenaar2a942252012-11-28 23:03:07 +01001432#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001433 did_endif = FALSE; // in case do_cmdline used recursively
Bram Moolenaar2a942252012-11-28 23:03:07 +01001434#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001435 /*
1436 * Reset if_level, in case a sourced script file contains more ":if" than
1437 * ":endif" (could be ":if x | foo | endif").
1438 */
1439 if_level = 0;
Bram Moolenaar2e18a122012-11-28 19:10:54 +01001440#endif
Bram Moolenaard4ad0d42012-11-28 17:34:48 +01001441
Bram Moolenaar071d4272004-06-13 20:20:40 +00001442 --call_depth;
1443 return retval;
1444}
1445
1446#ifdef FEAT_EVAL
1447/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001448 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001449 */
1450 static char_u *
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001451get_loop_line(int c, void *cookie, int indent, getline_opt_T options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001452{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001453 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001454 wcmd_T *wp;
1455 char_u *line;
1456
1457 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1458 {
1459 if (cp->repeating)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001460 return NULL; // trying to read past ":endwhile"/":endfor"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001461
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001462 // First time inside the ":while"/":for": get line normally.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001463 if (cp->getline == NULL)
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001464 line = getcmdline(c, 0L, indent, options);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001465 else
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001466 line = cp->getline(c, cp->cookie, indent, options);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001467 if (line != NULL && store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001468 ++cp->current_line;
1469
1470 return line;
1471 }
1472
1473 KeyTyped = FALSE;
1474 ++cp->current_line;
1475 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001476 SOURCING_LNUM = wp->lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001477 return vim_strsave(wp->line);
1478}
1479
1480/*
1481 * Store a line in "gap" so that a ":while" loop can execute it again.
1482 */
1483 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001484store_loop_line(garray_T *gap, char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001485{
1486 if (ga_grow(gap, 1) == FAIL)
1487 return FAIL;
1488 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001489 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = SOURCING_LNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001491 return OK;
1492}
1493
1494/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001495 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001496 */
1497 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001498free_cmdlines(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001499{
1500 while (gap->ga_len > 0)
1501 {
1502 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1503 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001504 }
1505}
1506#endif
1507
1508/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001509 * If "fgetline" is get_loop_line(), return TRUE if the getline it uses equals
1510 * "func". * Otherwise return TRUE when "fgetline" equals "func".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001512 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001513getline_equal(
Bram Moolenaar66250c92020-08-20 15:02:42 +02001514 char_u *(*fgetline)(int, void *, int, getline_opt_T),
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001515 void *cookie UNUSED, // argument for fgetline()
Bram Moolenaar66250c92020-08-20 15:02:42 +02001516 char_u *(*func)(int, void *, int, getline_opt_T))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517{
1518#ifdef FEAT_EVAL
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001519 char_u *(*gp)(int, void *, int, getline_opt_T);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001520 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001521
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001522 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1523 // function that's originally used to obtain the lines. This may be
1524 // nested several levels.
Bram Moolenaar89d40322006-08-29 15:30:07 +00001525 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001526 cp = (struct loop_cookie *)cookie;
1527 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001528 {
1529 gp = cp->getline;
1530 cp = cp->cookie;
1531 }
1532 return gp == func;
1533#else
Bram Moolenaar89d40322006-08-29 15:30:07 +00001534 return fgetline == func;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001535#endif
1536}
1537
Bram Moolenaar071d4272004-06-13 20:20:40 +00001538/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001539 * If "fgetline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001540 * getline function. Otherwise return "cookie".
1541 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542 void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001543getline_cookie(
Bram Moolenaar66250c92020-08-20 15:02:42 +02001544 char_u *(*fgetline)(int, void *, int, getline_opt_T) UNUSED,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001545 void *cookie) // argument for fgetline()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001546{
Bram Moolenaar13505972019-01-24 15:04:48 +01001547#ifdef FEAT_EVAL
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001548 char_u *(*gp)(int, void *, int, getline_opt_T);
Bram Moolenaard5053d02020-06-28 15:51:16 +02001549 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001550
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001551 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1552 // cookie that's originally used to obtain the lines. This may be nested
1553 // several levels.
Bram Moolenaar89d40322006-08-29 15:30:07 +00001554 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001555 cp = (struct loop_cookie *)cookie;
1556 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001557 {
1558 gp = cp->getline;
1559 cp = cp->cookie;
1560 }
1561 return cp;
Bram Moolenaar13505972019-01-24 15:04:48 +01001562#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001563 return cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001564#endif
Bram Moolenaar13505972019-01-24 15:04:48 +01001565}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001566
Bram Moolenaard5053d02020-06-28 15:51:16 +02001567#if defined(FEAT_EVAL) || defined(PROT)
1568/*
1569 * Get the next line source line without advancing.
1570 */
1571 char_u *
1572getline_peek(
Bram Moolenaar66250c92020-08-20 15:02:42 +02001573 char_u *(*fgetline)(int, void *, int, getline_opt_T) UNUSED,
Bram Moolenaard5053d02020-06-28 15:51:16 +02001574 void *cookie) // argument for fgetline()
1575{
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001576 char_u *(*gp)(int, void *, int, getline_opt_T);
Bram Moolenaard5053d02020-06-28 15:51:16 +02001577 struct loop_cookie *cp;
1578 wcmd_T *wp;
1579
1580 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1581 // cookie that's originally used to obtain the lines. This may be nested
1582 // several levels.
1583 gp = fgetline;
1584 cp = (struct loop_cookie *)cookie;
1585 while (gp == get_loop_line)
1586 {
1587 if (cp->current_line + 1 < cp->lines_gap->ga_len)
1588 {
1589 // executing lines a second time, use the stored copy
1590 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line + 1;
1591 return wp->line;
1592 }
1593 gp = cp->getline;
1594 cp = cp->cookie;
1595 }
1596 if (gp == getsourceline)
1597 return source_nextline(cp);
1598 return NULL;
1599}
1600#endif
1601
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001602
1603/*
1604 * Helper function to apply an offset for buffer commands, i.e. ":bdelete",
1605 * ":bwipeout", etc.
1606 * Returns the buffer number.
1607 */
1608 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001609compute_buffer_local_count(int addr_type, int lnum, int offset)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001610{
1611 buf_T *buf;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001612 buf_T *nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001613 int count = offset;
1614
1615 buf = firstbuf;
1616 while (buf->b_next != NULL && buf->b_fnum < lnum)
1617 buf = buf->b_next;
1618 while (count != 0)
1619 {
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001620 count += (offset < 0) ? 1 : -1;
1621 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1622 if (nextbuf == NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001623 break;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001624 buf = nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001625 if (addr_type == ADDR_LOADED_BUFFERS)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001626 // skip over unloaded buffers
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001627 while (buf->b_ml.ml_mfp == NULL)
1628 {
1629 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1630 if (nextbuf == NULL)
1631 break;
1632 buf = nextbuf;
1633 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001634 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001635 // we might have gone too far, last buffer is not loadedd
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001636 if (addr_type == ADDR_LOADED_BUFFERS)
1637 while (buf->b_ml.ml_mfp == NULL)
1638 {
1639 nextbuf = (offset >= 0) ? buf->b_prev : buf->b_next;
1640 if (nextbuf == NULL)
1641 break;
1642 buf = nextbuf;
1643 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001644 return buf->b_fnum;
1645}
1646
Bram Moolenaarb7316892019-05-01 18:08:42 +02001647/*
1648 * Return the window number of "win".
1649 * When "win" is NULL return the number of windows.
1650 */
Bram Moolenaarf240e182014-11-27 18:33:02 +01001651 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001652current_win_nr(win_T *win)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001653{
1654 win_T *wp;
1655 int nr = 0;
1656
Bram Moolenaar29323592016-07-24 22:04:11 +02001657 FOR_ALL_WINDOWS(wp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001658 {
1659 ++nr;
1660 if (wp == win)
1661 break;
1662 }
1663 return nr;
1664}
1665
1666 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001667current_tab_nr(tabpage_T *tab)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001668{
1669 tabpage_T *tp;
1670 int nr = 0;
1671
Bram Moolenaar29323592016-07-24 22:04:11 +02001672 FOR_ALL_TABPAGES(tp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001673 {
1674 ++nr;
1675 if (tp == tab)
1676 break;
1677 }
1678 return nr;
1679}
1680
Bram Moolenaar6e36b1c2020-07-17 20:47:51 +02001681 static int
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001682comment_start(char_u *p, int starts_with_colon UNUSED)
1683{
1684#ifdef FEAT_EVAL
1685 if (in_vim9script())
Bram Moolenaar93f82cb2020-12-12 21:25:56 +01001686 return p[0] == '#' && !starts_with_colon;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001687#endif
1688 return *p == '"';
1689}
1690
Bram Moolenaarf240e182014-11-27 18:33:02 +01001691# define CURRENT_WIN_NR current_win_nr(curwin)
1692# define LAST_WIN_NR current_win_nr(NULL)
1693# define CURRENT_TAB_NR current_tab_nr(curtab)
1694# define LAST_TAB_NR current_tab_nr(NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001695
Bram Moolenaar071d4272004-06-13 20:20:40 +00001696/*
1697 * Execute one Ex command.
1698 *
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001699 * If "flags" has DOCMD_VERBOSE, the command will be included in the error
1700 * message.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701 *
1702 * 1. skip comment lines and leading space
1703 * 2. handle command modifiers
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001704 * 3. find the command
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001705 * 4. parse range
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001706 * 5. Parse the command.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001707 * 6. parse arguments
1708 * 7. switch on command name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001709 *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001710 * Note: "fgetline" can be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001711 *
1712 * This function may be called recursively!
1713 */
1714#if (_MSC_VER == 1200)
1715/*
Bram Moolenaared203462004-06-16 11:19:22 +00001716 * Avoid optimisation bug in VC++ version 6.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001717 */
Bram Moolenaar281bdce2005-01-25 21:53:18 +00001718 #pragma optimize( "g", off )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001719#endif
1720 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001721do_one_cmd(
Bram Moolenaarddef1292019-12-16 17:10:33 +01001722 char_u **cmdlinep,
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001723 int flags,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001724#ifdef FEAT_EVAL
Bram Moolenaarddef1292019-12-16 17:10:33 +01001725 cstack_T *cstack,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001726#endif
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001727 char_u *(*fgetline)(int, void *, int, getline_opt_T),
Bram Moolenaarddef1292019-12-16 17:10:33 +01001728 void *cookie) // argument for fgetline()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001729{
Bram Moolenaarddef1292019-12-16 17:10:33 +01001730 char_u *p;
1731 linenr_T lnum;
1732 long n;
1733 char *errormsg = NULL; // error message
1734 char_u *after_modifier = NULL;
1735 exarg_T ea; // Ex command arguments
Bram Moolenaarddef1292019-12-16 17:10:33 +01001736 cmdmod_T save_cmdmod;
1737 int save_reg_executing = reg_executing;
1738 int ni; // set when Not Implemented
1739 char_u *cmd;
Bram Moolenaar6e36b1c2020-07-17 20:47:51 +02001740 int starts_with_colon = FALSE;
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001741#ifdef FEAT_EVAL
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02001742 int may_have_range;
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001743 int vim9script = in_vim9script();
Bram Moolenaarb4bcea42020-10-28 13:53:50 +01001744 int did_set_expr_line = FALSE;
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001745#endif
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001746 int sourcing = flags & DOCMD_VERBOSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001747
Bram Moolenaara80faa82020-04-12 19:37:17 +02001748 CLEAR_FIELD(ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749 ea.line1 = 1;
1750 ea.line2 = 1;
1751#ifdef FEAT_EVAL
1752 ++ex_nesting_level;
1753#endif
1754
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001755 // When the last file has not been edited :q has to be typed twice.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001756 if (quitmore
1757#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001758 // avoid that a function call in 'statusline' does this
Bram Moolenaar89d40322006-08-29 15:30:07 +00001759 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001760#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001761 // avoid that an autocommand, e.g. QuitPre, does this
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001762 && !getline_equal(fgetline, cookie, getnextac))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001763 --quitmore;
1764
1765 /*
1766 * Reset browse, confirm, etc.. They are restored when returning, for
1767 * recursive calls.
1768 */
1769 save_cmdmod = cmdmod;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001770
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001771 // "#!anything" is handled like a comment.
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001772 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1773 goto doend;
1774
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02001775/*
1776 * 1. Skip comment lines and leading white space and colons.
1777 * 2. Handle command modifiers.
1778 */
1779 // The "ea" structure holds the arguments that can be used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780 ea.cmd = *cmdlinep;
Bram Moolenaareffed932018-08-14 13:38:17 +02001781 ea.cmdlinep = cmdlinep;
1782 ea.getline = fgetline;
1783 ea.cookie = cookie;
1784#ifdef FEAT_EVAL
1785 ea.cstack = cstack;
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001786 starts_with_colon = *skipwhite(ea.cmd) == ':';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787#endif
Bram Moolenaare1004402020-10-24 20:49:43 +02001788 if (parse_command_modifiers(&ea, &errormsg, &cmdmod, FALSE) == FAIL)
Bram Moolenaareffed932018-08-14 13:38:17 +02001789 goto doend;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02001790 apply_cmdmod(&cmdmod);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001792 after_modifier = ea.cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793
1794#ifdef FEAT_EVAL
1795 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
1796 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
1797#else
1798 ea.skip = (if_level > 0);
1799#endif
1800
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001802 * 3. Skip over the range to find the command. Let "p" point to after it.
1803 *
1804 * We need the command to know what kind of range it uses.
1805 */
1806 cmd = ea.cmd;
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001807#ifdef FEAT_EVAL
Bram Moolenaare88c8e82020-11-01 17:03:37 +01001808 // In Vim9 script a colon is required before the range. This may also be
1809 // after command modifiers.
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001810 if (vim9script && (flags & DOCMD_RANGEOK) == 0)
Bram Moolenaare88c8e82020-11-01 17:03:37 +01001811 {
1812 may_have_range = FALSE;
1813 for (p = ea.cmd; p >= *cmdlinep; --p)
1814 {
1815 if (*p == ':')
1816 may_have_range = TRUE;
1817 if (p < ea.cmd && !VIM_ISWHITE(*p))
1818 break;
1819 }
1820 }
1821 else
1822 may_have_range = TRUE;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02001823 if (may_have_range)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001824#endif
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02001825 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001826
1827#ifdef FEAT_EVAL
Bram Moolenaare88c8e82020-11-01 17:03:37 +01001828 if (vim9script && !may_have_range)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02001829 {
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02001830 if (ea.cmd == cmd + 1 && *cmd == '$')
1831 // should be "$VAR = val"
1832 --ea.cmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001833 p = find_ex_command(&ea, NULL, lookup_scriptvar, NULL);
Bram Moolenaar36113e42020-11-02 21:08:47 +01001834 if (ea.cmdidx == CMD_SIZE)
1835 {
1836 char_u *ar = skip_range(ea.cmd, TRUE, NULL);
1837
1838 // If a ':' before the range is missing, give a clearer error
1839 // message.
1840 if (ar > ea.cmd)
1841 {
Bram Moolenaar6e2c2c52020-12-25 19:25:45 +01001842 semsg(_(e_colon_required_before_range_str), ea.cmd);
Bram Moolenaar36113e42020-11-02 21:08:47 +01001843 goto doend;
1844 }
1845 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02001846 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001847 else
1848#endif
1849 p = find_ex_command(&ea, NULL, NULL, NULL);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001850
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001851#ifdef FEAT_EVAL
1852# ifdef FEAT_PROFILE
1853 // Count this line for profiling if skip is TRUE.
1854 if (do_profiling == PROF_YES
1855 && (!ea.skip || cstack->cs_idx == 0 || (cstack->cs_idx > 0
1856 && (cstack->cs_flags[cstack->cs_idx - 1] & CSF_ACTIVE))))
1857 {
1858 int skip = did_emsg || got_int || did_throw;
1859
1860 if (ea.cmdidx == CMD_catch)
1861 skip = !skip && !(cstack->cs_idx >= 0
1862 && (cstack->cs_flags[cstack->cs_idx] & CSF_THROWN)
1863 && !(cstack->cs_flags[cstack->cs_idx] & CSF_CAUGHT));
1864 else if (ea.cmdidx == CMD_else || ea.cmdidx == CMD_elseif)
1865 skip = skip || !(cstack->cs_idx >= 0
1866 && !(cstack->cs_flags[cstack->cs_idx]
1867 & (CSF_ACTIVE | CSF_TRUE)));
1868 else if (ea.cmdidx == CMD_finally)
1869 skip = FALSE;
1870 else if (ea.cmdidx != CMD_endif
1871 && ea.cmdidx != CMD_endfor
1872 && ea.cmdidx != CMD_endtry
1873 && ea.cmdidx != CMD_endwhile)
1874 skip = ea.skip;
1875
1876 if (!skip)
1877 {
1878 if (getline_equal(fgetline, cookie, get_func_line))
1879 func_line_exec(getline_cookie(fgetline, cookie));
1880 else if (getline_equal(fgetline, cookie, getsourceline))
1881 script_line_exec();
1882 }
1883 }
1884# endif
1885
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001886 // May go to debug mode. If this happens and the ">quit" debug command is
1887 // used, throw an interrupt exception and skip the next command.
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001888 dbg_check_breakpoint(&ea);
1889 if (!ea.skip && got_int)
1890 {
1891 ea.skip = TRUE;
1892 (void)do_intthrow(cstack);
1893 }
1894#endif
1895
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001896/*
1897 * 4. parse a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898 *
1899 * where 'addr' is:
1900 *
1901 * % (entire file)
1902 * $ [+-NUM]
1903 * 'x [+-NUM] (where x denotes a currently defined mark)
1904 * . [+-NUM]
1905 * [+-NUM]..
1906 * NUM
1907 *
1908 * The ea.cmd pointer is updated to point to the first character following the
1909 * range spec. If an initial address is found, but no second, the upper bound
1910 * is equal to the lower.
1911 */
1912
Bram Moolenaar25190db2019-05-04 15:05:28 +02001913 // ea.addr_type for user commands is set by find_ucmd
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001914 if (!IS_USER_CMDIDX(ea.cmdidx))
1915 {
1916 if (ea.cmdidx != CMD_SIZE)
1917 ea.addr_type = cmdnames[(int)ea.cmdidx].cmd_addr_type;
1918 else
1919 ea.addr_type = ADDR_LINES;
1920
Bram Moolenaar25190db2019-05-04 15:05:28 +02001921 // :wincmd range depends on the argument.
Bram Moolenaar164f3262015-01-14 21:22:01 +01001922 if (ea.cmdidx == CMD_wincmd && p != NULL)
1923 get_wincmd_addr_type(skipwhite(p), &ea);
Bram Moolenaar25190db2019-05-04 15:05:28 +02001924#ifdef FEAT_QUICKFIX
1925 // :.cc in quickfix window uses line number
1926 if ((ea.cmdidx == CMD_cc || ea.cmdidx == CMD_ll) && bt_quickfix(curbuf))
1927 ea.addr_type = ADDR_OTHER;
1928#endif
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001929 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001930
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001931 ea.cmd = cmd;
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001932#ifdef FEAT_EVAL
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02001933 if (!may_have_range)
1934 ea.line1 = ea.line2 = default_address(&ea);
1935 else
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001936#endif
1937 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
1938 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001939
Bram Moolenaar071d4272004-06-13 20:20:40 +00001940/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001941 * 5. Parse the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001942 */
1943
1944 /*
1945 * Skip ':' and any white space
1946 */
1947 ea.cmd = skipwhite(ea.cmd);
1948 while (*ea.cmd == ':')
1949 ea.cmd = skipwhite(ea.cmd + 1);
1950
1951 /*
1952 * If we got a line, but no command, then go to the line.
1953 * If we find a '|' or '\n' we set ea.nextcmd.
1954 */
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001955 if (*ea.cmd == NUL || comment_start(ea.cmd, starts_with_colon)
1956 || (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001957 {
1958 /*
1959 * strange vi behaviour:
1960 * ":3" jumps to line 3
1961 * ":3|..." prints line 3
1962 * ":|" prints current line
1963 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001964 if (ea.skip) // skip this if inside :if
Bram Moolenaar071d4272004-06-13 20:20:40 +00001965 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001966 if (*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001967 {
1968 ea.cmdidx = CMD_print;
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001969 ea.argt = EX_RANGE+EX_COUNT+EX_TRLBAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001970 if ((errormsg = invalid_range(&ea)) == NULL)
1971 {
1972 correct_range(&ea);
1973 ex_print(&ea);
1974 }
1975 }
1976 else if (ea.addr_count != 0)
1977 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001978 if (ea.line2 > curbuf->b_ml.ml_line_count)
1979 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001980 // With '-' in 'cpoptions' a line number past the file is an
1981 // error, otherwise put it at the end of the file.
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001982 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
1983 ea.line2 = -1;
1984 else
1985 ea.line2 = curbuf->b_ml.ml_line_count;
1986 }
1987
1988 if (ea.line2 < 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001989 errormsg = _(e_invrange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990 else
1991 {
1992 if (ea.line2 == 0)
1993 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001994 else
1995 curwin->w_cursor.lnum = ea.line2;
1996 beginline(BL_SOL | BL_FIX);
1997 }
1998 }
1999 goto doend;
2000 }
2001
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002002 // If this looks like an undefined user command and there are CmdUndefined
2003 // autocommands defined, trigger the matching autocommands.
Bram Moolenaard5005162014-08-22 23:05:54 +02002004 if (p != NULL && ea.cmdidx == CMD_SIZE && !ea.skip
2005 && ASCII_ISUPPER(*ea.cmd)
2006 && has_cmdundefined())
2007 {
Bram Moolenaard5005162014-08-22 23:05:54 +02002008 int ret;
2009
Bram Moolenaar5a31b462014-08-23 14:16:20 +02002010 p = ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02002011 while (ASCII_ISALNUM(*p))
2012 ++p;
Bram Moolenaar158ea172020-06-18 17:28:39 +02002013 p = vim_strnsave(ea.cmd, p - ea.cmd);
Bram Moolenaard5005162014-08-22 23:05:54 +02002014 ret = apply_autocmds(EVENT_CMDUNDEFINED, p, p, TRUE, NULL);
2015 vim_free(p);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002016 // If the autocommands did something and didn't cause an error, try
2017 // finding the command again.
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002018 p = (ret
2019#ifdef FEAT_EVAL
2020 && !aborting()
Bram Moolenaard5005162014-08-22 23:05:54 +02002021#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002022 ) ? find_ex_command(&ea, NULL, NULL, NULL) : ea.cmd;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002023 }
Bram Moolenaard5005162014-08-22 23:05:54 +02002024
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025 if (p == NULL)
2026 {
2027 if (!ea.skip)
Bram Moolenaard1510ee2021-01-04 16:15:58 +01002028 errormsg = _(e_ambiguous_use_of_user_defined_command);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002029 goto doend;
2030 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002031 // Check for wrong commands.
Bram Moolenaar6f9a4762017-06-22 20:39:17 +02002032 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78
2033 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002034 {
2035 errormsg = uc_fun_cmd();
2036 goto doend;
2037 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002038
Bram Moolenaar071d4272004-06-13 20:20:40 +00002039 if (ea.cmdidx == CMD_SIZE)
2040 {
2041 if (!ea.skip)
2042 {
2043 STRCPY(IObuff, _("E492: Not an editor command"));
2044 if (!sourcing)
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002045 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002046 // If the modifier was parsed OK the error must be in the
2047 // following command
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002048 if (after_modifier != NULL)
2049 append_command(after_modifier);
2050 else
2051 append_command(*cmdlinep);
2052 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002053 errormsg = (char *)IObuff;
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02002054 did_emsg_syntax = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002055 }
2056 goto doend;
2057 }
2058
Bram Moolenaar958636c2014-10-21 20:01:58 +02002059 ni = (!IS_USER_CMDIDX(ea.cmdidx)
2060 && (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00002061#ifdef HAVE_EX_SCRIPT_NI
2062 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
2063#endif
2064 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002065
2066#ifndef FEAT_EVAL
2067 /*
2068 * When the expression evaluation is disabled, recognize the ":if" and
2069 * ":endif" commands and ignore everything in between it.
2070 */
2071 if (ea.cmdidx == CMD_if)
2072 ++if_level;
2073 if (if_level)
2074 {
2075 if (ea.cmdidx == CMD_endif)
2076 --if_level;
2077 goto doend;
2078 }
2079
2080#endif
2081
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002082 // forced commands
Bram Moolenaar196b3b02008-06-20 16:51:41 +00002083 if (*p == '!' && ea.cmdidx != CMD_substitute
2084 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002085 {
2086 ++p;
2087 ea.forceit = TRUE;
2088 }
2089 else
2090 ea.forceit = FALSE;
2091
2092/*
Bram Moolenaarb7316892019-05-01 18:08:42 +02002093 * 6. Parse arguments. Then check for errors.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002094 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02002095 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002096 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002097
2098 if (!ea.skip)
2099 {
2100#ifdef HAVE_SANDBOX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002101 if (sandbox != 0 && !(ea.argt & EX_SBOXOK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002102 {
Bram Moolenaar8c62a082019-02-08 14:34:10 +01002103 // Command not allowed in sandbox.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002104 errormsg = _(e_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002105 goto doend;
2106 }
2107#endif
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002108 if (restricted != 0 && (ea.argt & EX_RESTRICT))
Bram Moolenaar8c62a082019-02-08 14:34:10 +01002109 {
2110 errormsg = _("E981: Command not allowed in rvim");
2111 goto doend;
2112 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002113 if (!curbuf->b_p_ma && (ea.argt & EX_MODIFY))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002114 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002115 // Command not allowed in non-'modifiable' buffer
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002116 errormsg = _(e_modifiable);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002117 goto doend;
2118 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002119
Bram Moolenaar37394ff2020-07-25 19:38:18 +02002120 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002121 {
Bram Moolenaar37394ff2020-07-25 19:38:18 +02002122#ifdef FEAT_CMDWIN
2123 if (cmdwin_type != 0 && !(ea.argt & EX_CMDWIN))
2124 {
2125 // Command not allowed in the command line window
2126 errormsg = _(e_cmdwin);
2127 goto doend;
2128 }
2129#endif
2130 if (text_locked() && !(ea.argt & EX_LOCK_OK))
2131 {
2132 // Command not allowed when text is locked
2133 errormsg = _(get_text_locked_msg());
2134 goto doend;
2135 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002136 }
Bram Moolenaar379fb762018-08-30 15:58:28 +02002137
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002138 // Disallow editing another buffer when "curbuf_lock" is set.
2139 // Do allow ":checktime" (it is postponed).
2140 // Do allow ":edit" (check for an argument later).
2141 // Do allow ":file" with no arguments (check for an argument later).
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02002142 if (!(ea.argt & (EX_CMDWIN | EX_LOCK_OK))
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002143 && ea.cmdidx != CMD_checktime
Bram Moolenaar379fb762018-08-30 15:58:28 +02002144 && ea.cmdidx != CMD_edit
2145 && ea.cmdidx != CMD_file
Bram Moolenaar958636c2014-10-21 20:01:58 +02002146 && !IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002147 && curbuf_locked())
2148 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002149
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002150 if (!ni && !(ea.argt & EX_RANGE) && ea.addr_count > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002151 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002152 // no range allowed
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002153 errormsg = _(e_norange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002154 goto doend;
2155 }
2156 }
2157
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002158 if (!ni && !(ea.argt & EX_BANG) && ea.forceit) // no <!> allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002159 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002160 errormsg = _(e_nobang);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002161 goto doend;
2162 }
2163
2164 /*
2165 * Don't complain about the range if it is not used
2166 * (could happen if line_count is accidentally set to 0).
2167 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002168 if (!ea.skip && !ni && (ea.argt & EX_RANGE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002169 {
2170 /*
2171 * If the range is backwards, ask for confirmation and, if given, swap
2172 * ea.line1 & ea.line2 so it's forwards again.
2173 * When global command is busy, don't ask, will fail below.
2174 */
2175 if (!global_busy && ea.line1 > ea.line2)
2176 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002177 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002179 if (sourcing || exmode_active)
2180 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002181 errormsg = _("E493: Backwards range given");
Bram Moolenaara5792f52005-11-23 21:25:05 +00002182 goto doend;
2183 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002184 if (ask_yesno((char_u *)
2185 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002186 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187 }
2188 lnum = ea.line1;
2189 ea.line1 = ea.line2;
2190 ea.line2 = lnum;
2191 }
2192 if ((errormsg = invalid_range(&ea)) != NULL)
2193 goto doend;
2194 }
2195
Bram Moolenaarb7316892019-05-01 18:08:42 +02002196 if ((ea.addr_type == ADDR_OTHER) && ea.addr_count == 0)
2197 // default is 1, not cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002198 ea.line2 = 1;
2199
2200 correct_range(&ea);
2201
2202#ifdef FEAT_FOLDING
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002203 if (((ea.argt & EX_WHOLEFOLD) || ea.addr_count >= 2) && !global_busy
Bram Moolenaara3306952016-01-02 21:41:06 +01002204 && ea.addr_type == ADDR_LINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002205 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002206 // Put the first line at the start of a closed fold, put the last line
2207 // at the end of a closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208 (void)hasFolding(ea.line1, &ea.line1, NULL);
2209 (void)hasFolding(ea.line2, NULL, &ea.line2);
2210 }
2211#endif
2212
2213#ifdef FEAT_QUICKFIX
2214 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002215 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002216 * option here, so things like % get expanded.
2217 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002218 p = replace_makeprg(&ea, p, cmdlinep);
2219 if (p == NULL)
2220 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221#endif
2222
2223 /*
2224 * Skip to start of argument.
2225 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2226 */
2227 if (ea.cmdidx == CMD_bang)
2228 ea.arg = p;
2229 else
2230 ea.arg = skipwhite(p);
2231
Bram Moolenaar379fb762018-08-30 15:58:28 +02002232 // ":file" cannot be run with an argument when "curbuf_lock" is set
2233 if (ea.cmdidx == CMD_file && *ea.arg != NUL && curbuf_locked())
2234 goto doend;
2235
Bram Moolenaar071d4272004-06-13 20:20:40 +00002236 /*
2237 * Check for "++opt=val" argument.
2238 * Must be first, allow ":w ++enc=utf8 !cmd"
2239 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002240 if (ea.argt & EX_ARGOPT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002241 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2242 if (getargopt(&ea) == FAIL && !ni)
2243 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002244 errormsg = _(e_invarg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002245 goto doend;
2246 }
2247
2248 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2249 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002250 if (*ea.arg == '>') // append
Bram Moolenaar071d4272004-06-13 20:20:40 +00002251 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002252 if (*++ea.arg != '>') // typed wrong
Bram Moolenaar071d4272004-06-13 20:20:40 +00002253 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002254 errormsg = _("E494: Use w or w>>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002255 goto doend;
2256 }
2257 ea.arg = skipwhite(ea.arg + 1);
2258 ea.append = TRUE;
2259 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002260 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) // :w !filter
Bram Moolenaar071d4272004-06-13 20:20:40 +00002261 {
2262 ++ea.arg;
2263 ea.usefilter = TRUE;
2264 }
2265 }
2266
2267 if (ea.cmdidx == CMD_read)
2268 {
2269 if (ea.forceit)
2270 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002271 ea.usefilter = TRUE; // :r! filter if ea.forceit
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272 ea.forceit = FALSE;
2273 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002274 else if (*ea.arg == '!') // :r !filter
Bram Moolenaar071d4272004-06-13 20:20:40 +00002275 {
2276 ++ea.arg;
2277 ea.usefilter = TRUE;
2278 }
2279 }
2280
2281 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2282 {
2283 ea.amount = 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002284 while (*ea.arg == *ea.cmd) // count number of '>' or '<'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285 {
2286 ++ea.arg;
2287 ++ea.amount;
2288 }
2289 ea.arg = skipwhite(ea.arg);
2290 }
2291
2292 /*
2293 * Check for "+command" argument, before checking for next command.
2294 * Don't do this for ":read !cmd" and ":write !cmd".
2295 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002296 if ((ea.argt & EX_CMDARG) && !ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2298
2299 /*
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002300 * Check for '|' to separate commands and '"' or '#' to start comments.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301 * Don't do this for ":read !cmd" and ":write !cmd".
2302 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002303 if ((ea.argt & EX_TRLBAR) && !ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304 separate_nextcmd(&ea);
2305
2306 /*
2307 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002308 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2309 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002310 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002311 else if (ea.cmdidx == CMD_bang
Bram Moolenaar67883b42017-07-27 22:57:00 +02002312 || ea.cmdidx == CMD_terminal
Bram Moolenaardf177f62005-02-22 08:39:57 +00002313 || ea.cmdidx == CMD_global
2314 || ea.cmdidx == CMD_vglobal
2315 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002316 {
2317 for (p = ea.arg; *p; ++p)
2318 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002319 // Remove one backslash before a newline, so that it's possible to
2320 // pass a newline to the shell and also a newline that is preceded
2321 // with a backslash. This makes it impossible to end a shell
2322 // command in a backslash, but that doesn't appear useful.
2323 // Halving the number of backslashes is incompatible with previous
2324 // versions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002326 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327 else if (*p == '\n')
2328 {
2329 ea.nextcmd = p + 1;
2330 *p = NUL;
2331 break;
2332 }
2333 }
2334 }
2335
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002336 if ((ea.argt & EX_DFLALL) && ea.addr_count == 0)
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02002337 address_default_all(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002338
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002339 // accept numbered register only when no count allowed (:put)
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002340 if ( (ea.argt & EX_REGSTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341 && *ea.arg != NUL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002342 // Do not allow register = for user commands
Bram Moolenaar958636c2014-10-21 20:01:58 +02002343 && (!IS_USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002344 && !((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002346#ifndef FEAT_CLIPBOARD
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002347 // check these explicitly for a more specific error message
Bram Moolenaar85de2062011-05-05 14:26:41 +02002348 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002349 {
Bram Moolenaar99b12722019-01-14 20:16:40 +01002350 errormsg = _(e_invalidreg);
Bram Moolenaar85de2062011-05-05 14:26:41 +02002351 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352 }
2353#endif
Bram Moolenaar958636c2014-10-21 20:01:58 +02002354 if (valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2355 && !IS_USER_CMDIDX(ea.cmdidx))))
Bram Moolenaar85de2062011-05-05 14:26:41 +02002356 {
2357 ea.regname = *ea.arg++;
2358#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002359 // for '=' register: accept the rest of the line as an expression
Bram Moolenaar85de2062011-05-05 14:26:41 +02002360 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2361 {
Bram Moolenaarb4bcea42020-10-28 13:53:50 +01002362 set_expr_line(vim_strsave(ea.arg), &ea);
Bram Moolenaar85de2062011-05-05 14:26:41 +02002363 ea.arg += STRLEN(ea.arg);
Bram Moolenaarb4bcea42020-10-28 13:53:50 +01002364 did_set_expr_line = TRUE;
Bram Moolenaar85de2062011-05-05 14:26:41 +02002365 }
2366#endif
2367 ea.arg = skipwhite(ea.arg);
2368 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369 }
2370
2371 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002372 * Check for a count. When accepting a EX_BUFNAME, don't use "123foo" as a
Bram Moolenaar071d4272004-06-13 20:20:40 +00002373 * count, it's a buffer name.
2374 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002375 if ((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)
2376 && (!(ea.argt & EX_BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01002377 || VIM_ISWHITE(*p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002378 {
2379 n = getdigits(&ea.arg);
2380 ea.arg = skipwhite(ea.arg);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002381 if (n <= 0 && !ni && (ea.argt & EX_ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002382 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002383 errormsg = _(e_zerocount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002384 goto doend;
2385 }
Bram Moolenaarb7316892019-05-01 18:08:42 +02002386 if (ea.addr_type != ADDR_LINES) // e.g. :buffer 2, :sleep 3
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387 {
2388 ea.line2 = n;
2389 if (ea.addr_count == 0)
2390 ea.addr_count = 1;
2391 }
2392 else
2393 {
2394 ea.line1 = ea.line2;
2395 ea.line2 += n - 1;
2396 ++ea.addr_count;
2397 /*
2398 * Be vi compatible: no error message for out of range.
2399 */
Bram Moolenaarb7316892019-05-01 18:08:42 +02002400 if (ea.line2 > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401 ea.line2 = curbuf->b_ml.ml_line_count;
2402 }
2403 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002404
2405 /*
2406 * Check for flags: 'l', 'p' and '#'.
2407 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002408 if (ea.argt & EX_FLAGS)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002409 get_flags(&ea);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002410 if (!ni && !(ea.argt & EX_EXTRA) && *ea.arg != NUL
2411 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & EX_TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002413 // no arguments allowed but there is something
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02002414 errormsg = ex_errmsg(e_trailing_arg, ea.arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415 goto doend;
2416 }
2417
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002418 if (!ni && (ea.argt & EX_NEEDARG) && *ea.arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002420 errormsg = _(e_argreq);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421 goto doend;
2422 }
2423
2424#ifdef FEAT_EVAL
2425 /*
2426 * Skip the command when it's not going to be executed.
2427 * The commands like :if, :endif, etc. always need to be executed.
2428 * Also make an exception for commands that handle a trailing command
2429 * themselves.
2430 */
2431 if (ea.skip)
2432 {
2433 switch (ea.cmdidx)
2434 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002435 // commands that need evaluation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002436 case CMD_while:
2437 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002438 case CMD_for:
2439 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002440 case CMD_if:
2441 case CMD_elseif:
2442 case CMD_else:
2443 case CMD_endif:
2444 case CMD_try:
2445 case CMD_catch:
2446 case CMD_finally:
2447 case CMD_endtry:
2448 case CMD_function:
Bram Moolenaarab55c682020-03-01 19:41:43 +01002449 case CMD_def:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002450 break;
2451
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002452 // Commands that handle '|' themselves. Check: A command should
2453 // either have the EX_TRLBAR flag, appear in this list or appear in
2454 // the list at ":help :bar".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002455 case CMD_aboveleft:
2456 case CMD_and:
2457 case CMD_belowright:
2458 case CMD_botright:
2459 case CMD_browse:
2460 case CMD_call:
2461 case CMD_confirm:
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01002462 case CMD_const:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463 case CMD_delfunction:
2464 case CMD_djump:
2465 case CMD_dlist:
2466 case CMD_dsearch:
2467 case CMD_dsplit:
2468 case CMD_echo:
2469 case CMD_echoerr:
2470 case CMD_echomsg:
2471 case CMD_echon:
Bram Moolenaara76b3152020-02-16 16:20:21 +01002472 case CMD_eval:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473 case CMD_execute:
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002474 case CMD_filter:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002475 case CMD_final:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002476 case CMD_help:
2477 case CMD_hide:
2478 case CMD_ijump:
2479 case CMD_ilist:
2480 case CMD_isearch:
2481 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002482 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483 case CMD_keepjumps:
2484 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01002485 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486 case CMD_leftabove:
2487 case CMD_let:
2488 case CMD_lockmarks:
Bram Moolenaarcc4423a2019-11-26 17:05:00 +01002489 case CMD_lockvar:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002490 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002492 case CMD_mzscheme:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01002493 case CMD_noautocmd:
2494 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495 case CMD_perl:
2496 case CMD_psearch:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002497 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002498 case CMD_python3:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002499 case CMD_python:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002500 case CMD_return:
2501 case CMD_rightbelow:
2502 case CMD_ruby:
2503 case CMD_silent:
2504 case CMD_smagic:
2505 case CMD_snomagic:
2506 case CMD_substitute:
2507 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002508 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509 case CMD_tcl:
2510 case CMD_throw:
2511 case CMD_tilde:
2512 case CMD_topleft:
2513 case CMD_unlet:
Bram Moolenaarcc4423a2019-11-26 17:05:00 +01002514 case CMD_unlockvar:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002515 case CMD_var:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002516 case CMD_verbose:
2517 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002518 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002519 break;
2520
2521 default: goto doend;
2522 }
2523 }
2524#endif
2525
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002526 if (ea.argt & EX_XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527 {
2528 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2529 goto doend;
2530 }
2531
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532 /*
2533 * Accept buffer name. Cannot be used at the same time with a buffer
2534 * number. Don't do this for a user command.
2535 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002536 if ((ea.argt & EX_BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
Bram Moolenaar958636c2014-10-21 20:01:58 +02002537 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538 {
2539 /*
2540 * :bdelete, :bwipeout and :bunload take several arguments, separated
2541 * by spaces: find next space (skipping over escaped characters).
2542 * The others take one argument: ignore trailing spaces.
2543 */
2544 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2545 || ea.cmdidx == CMD_bunload)
2546 p = skiptowhite_esc(ea.arg);
2547 else
2548 {
2549 p = ea.arg + STRLEN(ea.arg);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002550 while (p > ea.arg && VIM_ISWHITE(p[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002551 --p;
2552 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002553 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & EX_BUFUNL) != 0,
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002554 FALSE, FALSE);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002555 if (ea.line2 < 0) // failed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002556 goto doend;
2557 ea.addr_count = 1;
2558 ea.arg = skipwhite(p);
2559 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002560
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002561 // The :try command saves the emsg_silent flag, reset it here when
2562 // ":silent! try" was used, it should only apply to :try itself.
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002563 if (ea.cmdidx == CMD_try && cmdmod.cmod_did_esilent > 0)
Bram Moolenaar2be57332018-02-13 18:05:18 +01002564 {
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002565 emsg_silent -= cmdmod.cmod_did_esilent;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002566 if (emsg_silent < 0)
2567 emsg_silent = 0;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002568 cmdmod.cmod_did_esilent = 0;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002569 }
2570
Bram Moolenaar071d4272004-06-13 20:20:40 +00002571/*
Bram Moolenaar2be57332018-02-13 18:05:18 +01002572 * 7. Execute the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002573 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002574
Bram Moolenaar958636c2014-10-21 20:01:58 +02002575 if (IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576 {
2577 /*
2578 * Execute a user-defined command.
2579 */
2580 do_ucmd(&ea);
2581 }
2582 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002583 {
2584 /*
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002585 * Call the function to execute the builtin command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002586 */
2587 ea.errmsg = NULL;
2588 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2589 if (ea.errmsg != NULL)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02002590 errormsg = ea.errmsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002591 }
2592
2593#ifdef FEAT_EVAL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002594 // Set flag that any command was executed, used by ex_vim9script().
Bram Moolenaar16531552020-02-08 16:00:46 +01002595 if (getline_equal(ea.getline, ea.cookie, getsourceline)
2596 && current_sctx.sc_sid > 0)
Bram Moolenaar2b327002020-12-26 15:39:31 +01002597 SCRIPT_ITEM(current_sctx.sc_sid)->sn_state = SN_STATE_HAD_COMMAND;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002598
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599 /*
2600 * If the command just executed called do_cmdline(), any throw or ":return"
2601 * or ":finish" encountered there must also check the cstack of the still
2602 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2603 * exception, or reanimate a returned function or finished script file and
2604 * return or finish it again.
2605 */
2606 if (need_rethrow)
2607 do_throw(cstack);
2608 else if (check_cstack)
2609 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002610 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002611 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002612 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002613 && current_func_returned())
2614 do_return(&ea, TRUE, FALSE, NULL);
2615 }
2616 need_rethrow = check_cstack = FALSE;
2617#endif
2618
2619doend:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002620 if (curwin->w_cursor.lnum == 0) // can happen with zero line number
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002621 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002622 curwin->w_cursor.lnum = 1;
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002623 curwin->w_cursor.col = 0;
2624 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002625
2626 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2627 {
2628 if (sourcing)
2629 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002630 if (errormsg != (char *)IObuff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631 {
2632 STRCPY(IObuff, errormsg);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002633 errormsg = (char *)IObuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002634 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002635 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002636 }
2637 emsg(errormsg);
2638 }
2639#ifdef FEAT_EVAL
2640 do_errthrow(cstack,
Bram Moolenaar958636c2014-10-21 20:01:58 +02002641 (ea.cmdidx != CMD_SIZE && !IS_USER_CMDIDX(ea.cmdidx))
2642 ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
Bram Moolenaarb4bcea42020-10-28 13:53:50 +01002643
2644 if (did_set_expr_line)
2645 set_expr_line(NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002646#endif
2647
Bram Moolenaare1004402020-10-24 20:49:43 +02002648 undo_cmdmod(&cmdmod);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649 cmdmod = save_cmdmod;
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01002650 reg_executing = save_reg_executing;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002652 if (ea.nextcmd && *ea.nextcmd == NUL) // not really a next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00002653 ea.nextcmd = NULL;
2654
2655#ifdef FEAT_EVAL
2656 --ex_nesting_level;
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002657 vim_free(ea.cmdline_tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658#endif
2659
2660 return ea.nextcmd;
2661}
2662#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00002663 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664#endif
2665
Bram Moolenaar8930caa2020-07-23 16:37:03 +02002666static char ex_error_buf[MSG_BUF_LEN];
2667
2668/*
2669 * Return an error message with argument included.
2670 * Uses a static buffer, only the last error will be kept.
2671 * "msg" will be translated, caller should use N_().
2672 */
2673 char *
2674ex_errmsg(char *msg, char_u *arg)
2675{
2676 vim_snprintf(ex_error_buf, MSG_BUF_LEN, _(msg), arg);
2677 return ex_error_buf;
2678}
2679
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680/*
Bram Moolenaareffed932018-08-14 13:38:17 +02002681 * Parse and skip over command modifiers:
2682 * - update eap->cmd
Bram Moolenaare1004402020-10-24 20:49:43 +02002683 * - store flags in "cmod".
Bram Moolenaareffed932018-08-14 13:38:17 +02002684 * - Set ex_pressedreturn for an empty command line.
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002685 * When "skip_only" is TRUE the global variables are not changed, except for
2686 * "cmdmod".
Bram Moolenaare1004402020-10-24 20:49:43 +02002687 * When "skip_only" is FALSE then undo_cmdmod() must be called later to free
2688 * any cmod_filter_regmatch.regprog.
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002689 * Call apply_cmdmod() to get the side effects of the modifiers:
2690 * - Increment "sandbox" for ":sandbox"
2691 * - set p_verbose for ":verbose"
Bram Moolenaare1004402020-10-24 20:49:43 +02002692 * - set msg_silent for ":silent"
2693 * - set 'eventignore' to "all" for ":noautocmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002694 * Return FAIL when the command is not to be executed.
2695 * May set "errormsg" to an error message.
2696 */
2697 int
Bram Moolenaare1004402020-10-24 20:49:43 +02002698parse_command_modifiers(
2699 exarg_T *eap,
2700 char **errormsg,
2701 cmdmod_T *cmod,
2702 int skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002703{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002704 char_u *p;
2705 int starts_with_colon = FALSE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002706
Bram Moolenaare1004402020-10-24 20:49:43 +02002707 CLEAR_POINTER(cmod);
Bram Moolenaareffed932018-08-14 13:38:17 +02002708
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002709 // Repeat until no more command modifiers are found.
Bram Moolenaareffed932018-08-14 13:38:17 +02002710 for (;;)
2711 {
Bram Moolenaareffed932018-08-14 13:38:17 +02002712 while (*eap->cmd == ' ' || *eap->cmd == '\t' || *eap->cmd == ':')
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002713 {
2714 if (*eap->cmd == ':')
2715 starts_with_colon = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002716 ++eap->cmd;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002717 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002718
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002719 // in ex mode, an empty line works like :+
Bram Moolenaareffed932018-08-14 13:38:17 +02002720 if (*eap->cmd == NUL && exmode_active
2721 && (getline_equal(eap->getline, eap->cookie, getexmodeline)
2722 || getline_equal(eap->getline, eap->cookie, getexline))
2723 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2724 {
2725 eap->cmd = (char_u *)"+";
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002726 if (!skip_only)
2727 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002728 }
2729
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002730 // ignore comment and empty lines
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002731 if (comment_start(eap->cmd, starts_with_colon))
Bram Moolenaareffed932018-08-14 13:38:17 +02002732 return FAIL;
2733 if (*eap->cmd == NUL)
2734 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002735 if (!skip_only)
2736 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002737 return FAIL;
2738 }
2739
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02002740 p = skip_range(eap->cmd, TRUE, NULL);
Bram Moolenaar17126b12021-01-07 22:03:02 +01002741
2742 // In Vim9 script a variable can shadow a command modifier:
2743 // verbose = 123
2744 // verbose += 123
2745 // silent! verbose = func()
2746 // verbose.member = 2
2747 // verbose[expr] = 2
Bram Moolenaar9e0f8832021-01-09 12:09:22 +01002748 // But not:
2749 // verbose [a, b] = list
Bram Moolenaar17126b12021-01-07 22:03:02 +01002750 if (in_vim9script())
2751 {
Bram Moolenaar9e0f8832021-01-09 12:09:22 +01002752 char_u *s, *n;
Bram Moolenaar17126b12021-01-07 22:03:02 +01002753
2754 for (s = p; ASCII_ISALPHA(*s); ++s)
2755 ;
Bram Moolenaar9e0f8832021-01-09 12:09:22 +01002756 n = skipwhite(s);
2757 if (vim_strchr((char_u *)".=", *n) != NULL
2758 || *s == '['
2759 || (*n != NUL && n[1] == '='))
Bram Moolenaar17126b12021-01-07 22:03:02 +01002760 break;
2761 }
2762
Bram Moolenaareffed932018-08-14 13:38:17 +02002763 switch (*p)
2764 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002765 // When adding an entry, also modify cmd_exists().
Bram Moolenaareffed932018-08-14 13:38:17 +02002766 case 'a': if (!checkforcmd(&eap->cmd, "aboveleft", 3))
2767 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002768 cmod->cmod_split |= WSP_ABOVE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002769 continue;
2770
2771 case 'b': if (checkforcmd(&eap->cmd, "belowright", 3))
2772 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002773 cmod->cmod_split |= WSP_BELOW;
Bram Moolenaareffed932018-08-14 13:38:17 +02002774 continue;
2775 }
2776 if (checkforcmd(&eap->cmd, "browse", 3))
2777 {
2778#ifdef FEAT_BROWSE_CMD
Bram Moolenaare1004402020-10-24 20:49:43 +02002779 cmod->cmod_flags |= CMOD_BROWSE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002780#endif
2781 continue;
2782 }
2783 if (!checkforcmd(&eap->cmd, "botright", 2))
2784 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002785 cmod->cmod_split |= WSP_BOT;
Bram Moolenaareffed932018-08-14 13:38:17 +02002786 continue;
2787
2788 case 'c': if (!checkforcmd(&eap->cmd, "confirm", 4))
2789 break;
2790#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02002791 cmod->cmod_flags |= CMOD_CONFIRM;
Bram Moolenaareffed932018-08-14 13:38:17 +02002792#endif
2793 continue;
2794
2795 case 'k': if (checkforcmd(&eap->cmd, "keepmarks", 3))
2796 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002797 cmod->cmod_flags |= CMOD_KEEPMARKS;
Bram Moolenaareffed932018-08-14 13:38:17 +02002798 continue;
2799 }
2800 if (checkforcmd(&eap->cmd, "keepalt", 5))
2801 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002802 cmod->cmod_flags |= CMOD_KEEPALT;
Bram Moolenaareffed932018-08-14 13:38:17 +02002803 continue;
2804 }
2805 if (checkforcmd(&eap->cmd, "keeppatterns", 5))
2806 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002807 cmod->cmod_flags |= CMOD_KEEPPATTERNS;
Bram Moolenaareffed932018-08-14 13:38:17 +02002808 continue;
2809 }
2810 if (!checkforcmd(&eap->cmd, "keepjumps", 5))
2811 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002812 cmod->cmod_flags |= CMOD_KEEPJUMPS;
Bram Moolenaareffed932018-08-14 13:38:17 +02002813 continue;
2814
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002815 case 'f': // only accept ":filter {pat} cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002816 {
2817 char_u *reg_pat;
2818
2819 if (!checkforcmd(&p, "filter", 4)
2820 || *p == NUL || ends_excmd(*p))
2821 break;
2822 if (*p == '!')
2823 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002824 cmod->cmod_filter_force = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002825 p = skipwhite(p + 1);
2826 if (*p == NUL || ends_excmd(*p))
2827 break;
2828 }
Bram Moolenaar1e624c92020-07-11 14:08:04 +02002829#ifdef FEAT_EVAL
Bram Moolenaarb074e8b2020-07-11 13:40:45 +02002830 // Avoid that "filter(arg)" is recognized.
Bram Moolenaar4f6b6ed2020-10-29 20:24:34 +01002831 if (in_vim9script() && !VIM_ISWHITE(p[-1]))
Bram Moolenaarb074e8b2020-07-11 13:40:45 +02002832 break;
Bram Moolenaar1e624c92020-07-11 14:08:04 +02002833#endif
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002834 if (skip_only)
2835 p = skip_vimgrep_pat(p, NULL, NULL);
2836 else
2837 // NOTE: This puts a NUL after the pattern.
2838 p = skip_vimgrep_pat(p, &reg_pat, NULL);
Bram Moolenaareffed932018-08-14 13:38:17 +02002839 if (p == NULL || *p == NUL)
2840 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002841 if (!skip_only)
2842 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002843 cmod->cmod_filter_regmatch.regprog =
Bram Moolenaareffed932018-08-14 13:38:17 +02002844 vim_regcomp(reg_pat, RE_MAGIC);
Bram Moolenaare1004402020-10-24 20:49:43 +02002845 if (cmod->cmod_filter_regmatch.regprog == NULL)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002846 break;
2847 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002848 eap->cmd = p;
2849 continue;
2850 }
2851
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002852 // ":hide" and ":hide | cmd" are not modifiers
Bram Moolenaareffed932018-08-14 13:38:17 +02002853 case 'h': if (p != eap->cmd || !checkforcmd(&p, "hide", 3)
2854 || *p == NUL || ends_excmd(*p))
2855 break;
2856 eap->cmd = p;
Bram Moolenaare1004402020-10-24 20:49:43 +02002857 cmod->cmod_flags |= CMOD_HIDE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002858 continue;
2859
2860 case 'l': if (checkforcmd(&eap->cmd, "lockmarks", 3))
2861 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002862 cmod->cmod_flags |= CMOD_LOCKMARKS;
Bram Moolenaareffed932018-08-14 13:38:17 +02002863 continue;
2864 }
2865
2866 if (!checkforcmd(&eap->cmd, "leftabove", 5))
2867 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002868 cmod->cmod_split |= WSP_ABOVE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002869 continue;
2870
2871 case 'n': if (checkforcmd(&eap->cmd, "noautocmd", 3))
2872 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002873 cmod->cmod_flags |= CMOD_NOAUTOCMD;
Bram Moolenaareffed932018-08-14 13:38:17 +02002874 continue;
2875 }
2876 if (!checkforcmd(&eap->cmd, "noswapfile", 3))
2877 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002878 cmod->cmod_flags |= CMOD_NOSWAPFILE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002879 continue;
2880
2881 case 'r': if (!checkforcmd(&eap->cmd, "rightbelow", 6))
2882 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002883 cmod->cmod_split |= WSP_BELOW;
Bram Moolenaareffed932018-08-14 13:38:17 +02002884 continue;
2885
2886 case 's': if (checkforcmd(&eap->cmd, "sandbox", 3))
2887 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002888 cmod->cmod_flags |= CMOD_SANDBOX;
Bram Moolenaareffed932018-08-14 13:38:17 +02002889 continue;
2890 }
2891 if (!checkforcmd(&eap->cmd, "silent", 3))
2892 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002893 cmod->cmod_flags |= CMOD_SILENT;
Bram Moolenaareffed932018-08-14 13:38:17 +02002894 if (*eap->cmd == '!' && !VIM_ISWHITE(eap->cmd[-1]))
2895 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002896 // ":silent!", but not "silent !cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002897 eap->cmd = skipwhite(eap->cmd + 1);
Bram Moolenaare1004402020-10-24 20:49:43 +02002898 cmod->cmod_flags |= CMOD_ERRSILENT;
Bram Moolenaareffed932018-08-14 13:38:17 +02002899 }
2900 continue;
2901
2902 case 't': if (checkforcmd(&p, "tab", 3))
2903 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002904 if (!skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002905 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002906 long tabnr = get_address(eap, &eap->cmd,
2907 ADDR_TABS, eap->skip,
2908 skip_only, FALSE, 1);
2909 if (tabnr == MAXLNUM)
Bram Moolenaare1004402020-10-24 20:49:43 +02002910 cmod->cmod_tab = tabpage_index(curtab) + 1;
Bram Moolenaar548e5982018-12-26 21:45:00 +01002911 else
Bram Moolenaareffed932018-08-14 13:38:17 +02002912 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002913 if (tabnr < 0 || tabnr > LAST_TAB_NR)
2914 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002915 *errormsg = _(e_invrange);
Bram Moolenaar548e5982018-12-26 21:45:00 +01002916 return FAIL;
2917 }
Bram Moolenaare1004402020-10-24 20:49:43 +02002918 cmod->cmod_tab = tabnr + 1;
Bram Moolenaareffed932018-08-14 13:38:17 +02002919 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002920 }
2921 eap->cmd = p;
2922 continue;
2923 }
2924 if (!checkforcmd(&eap->cmd, "topleft", 2))
2925 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002926 cmod->cmod_split |= WSP_TOP;
Bram Moolenaareffed932018-08-14 13:38:17 +02002927 continue;
2928
2929 case 'u': if (!checkforcmd(&eap->cmd, "unsilent", 3))
2930 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002931 cmod->cmod_flags |= CMOD_UNSILENT;
Bram Moolenaareffed932018-08-14 13:38:17 +02002932 continue;
2933
2934 case 'v': if (checkforcmd(&eap->cmd, "vertical", 4))
2935 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002936 cmod->cmod_split |= WSP_VERT;
Bram Moolenaareffed932018-08-14 13:38:17 +02002937 continue;
2938 }
2939 if (!checkforcmd(&p, "verbose", 4))
2940 break;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002941 if (vim_isdigit(*eap->cmd))
Bram Moolenaare1004402020-10-24 20:49:43 +02002942 cmod->cmod_verbose = atoi((char *)eap->cmd);
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002943 else
Bram Moolenaare1004402020-10-24 20:49:43 +02002944 cmod->cmod_verbose = 1;
Bram Moolenaareffed932018-08-14 13:38:17 +02002945 eap->cmd = p;
2946 continue;
2947 }
2948 break;
2949 }
2950
2951 return OK;
2952}
2953
2954/*
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002955 * Apply the command modifiers. Saves current state in "cmdmod", call
2956 * undo_cmdmod() later.
2957 */
2958 void
2959apply_cmdmod(cmdmod_T *cmod)
2960{
2961#ifdef HAVE_SANDBOX
2962 if ((cmod->cmod_flags & CMOD_SANDBOX) && !cmod->cmod_did_sandbox)
2963 {
2964 ++sandbox;
2965 cmod->cmod_did_sandbox = TRUE;
2966 }
2967#endif
2968 if (cmod->cmod_verbose > 0)
2969 {
2970 if (cmod->cmod_verbose_save == 0)
2971 cmod->cmod_verbose_save = p_verbose + 1;
2972 p_verbose = cmod->cmod_verbose;
2973 }
2974
2975 if ((cmod->cmod_flags & (CMOD_SILENT | CMOD_UNSILENT))
2976 && cmod->cmod_save_msg_silent == 0)
Bram Moolenaare1004402020-10-24 20:49:43 +02002977 {
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002978 cmod->cmod_save_msg_silent = msg_silent + 1;
Bram Moolenaare1004402020-10-24 20:49:43 +02002979 cmod->cmod_save_msg_scroll = msg_scroll;
2980 }
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002981 if (cmod->cmod_flags & CMOD_SILENT)
2982 ++msg_silent;
2983 if (cmod->cmod_flags & CMOD_UNSILENT)
2984 msg_silent = 0;
2985
2986 if (cmod->cmod_flags & CMOD_ERRSILENT)
2987 {
2988 ++emsg_silent;
2989 ++cmod->cmod_did_esilent;
2990 }
2991
Bram Moolenaare1004402020-10-24 20:49:43 +02002992 if ((cmod->cmod_flags & CMOD_NOAUTOCMD) && cmod->cmod_save_ei == NULL)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002993 {
2994 // Set 'eventignore' to "all".
2995 // First save the existing option value for restoring it later.
Bram Moolenaare1004402020-10-24 20:49:43 +02002996 cmod->cmod_save_ei = vim_strsave(p_ei);
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002997 set_string_option_direct((char_u *)"ei", -1,
2998 (char_u *)"all", OPT_FREE, SID_NONE);
2999 }
3000}
3001
3002/*
Bram Moolenaare1004402020-10-24 20:49:43 +02003003 * Undo and free contents of "cmod".
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003004 */
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003005 void
Bram Moolenaare1004402020-10-24 20:49:43 +02003006undo_cmdmod(cmdmod_T *cmod)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003007{
Bram Moolenaare1004402020-10-24 20:49:43 +02003008 if (cmod->cmod_verbose_save > 0)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003009 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003010 p_verbose = cmod->cmod_verbose_save - 1;
3011 cmod->cmod_verbose_save = 0;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003012 }
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003013
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003014#ifdef HAVE_SANDBOX
Bram Moolenaare1004402020-10-24 20:49:43 +02003015 if (cmod->cmod_did_sandbox)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003016 {
3017 --sandbox;
Bram Moolenaare1004402020-10-24 20:49:43 +02003018 cmod->cmod_did_sandbox = FALSE;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003019 }
3020#endif
3021
Bram Moolenaare1004402020-10-24 20:49:43 +02003022 if (cmod->cmod_save_ei != NULL)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003023 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003024 // Restore 'eventignore' to the value before ":noautocmd".
Bram Moolenaare1004402020-10-24 20:49:43 +02003025 set_string_option_direct((char_u *)"ei", -1, cmod->cmod_save_ei,
3026 OPT_FREE, SID_NONE);
3027 free_string_option(cmod->cmod_save_ei);
3028 cmod->cmod_save_ei = NULL;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003029 }
3030
Bram Moolenaarcaf73dc2020-10-26 21:39:13 +01003031 vim_regfree(cmod->cmod_filter_regmatch.regprog);
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003032
Bram Moolenaare1004402020-10-24 20:49:43 +02003033 if (cmod->cmod_save_msg_silent > 0)
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003034 {
3035 // messages could be enabled for a serious error, need to check if the
3036 // counters don't become negative
Bram Moolenaare1004402020-10-24 20:49:43 +02003037 if (!did_emsg || msg_silent > cmod->cmod_save_msg_silent - 1)
3038 msg_silent = cmod->cmod_save_msg_silent - 1;
3039 emsg_silent -= cmod->cmod_did_esilent;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003040 if (emsg_silent < 0)
3041 emsg_silent = 0;
3042 // Restore msg_scroll, it's set by file I/O commands, even when no
3043 // message is actually displayed.
Bram Moolenaare1004402020-10-24 20:49:43 +02003044 msg_scroll = cmod->cmod_save_msg_scroll;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003045
3046 // "silent reg" or "silent echo x" inside "redir" leaves msg_col
3047 // somewhere in the line. Put it back in the first column.
3048 if (redirecting())
3049 msg_col = 0;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003050
Bram Moolenaare1004402020-10-24 20:49:43 +02003051 cmod->cmod_save_msg_silent = 0;
3052 cmod->cmod_did_esilent = 0;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003053 }
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003054}
3055
3056/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003057 * Parse the address range, if any, in "eap".
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003058 * May set the last search pattern, unless "silent" is TRUE.
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003059 * Return FAIL and set "errormsg" or return OK.
3060 */
3061 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003062parse_cmd_address(exarg_T *eap, char **errormsg, int silent)
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003063{
3064 int address_count = 1;
3065 linenr_T lnum;
3066
3067 // Repeat for all ',' or ';' separated addresses.
3068 for (;;)
3069 {
3070 eap->line1 = eap->line2;
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02003071 eap->line2 = default_address(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003072 eap->cmd = skipwhite(eap->cmd);
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003073 lnum = get_address(eap, &eap->cmd, eap->addr_type, eap->skip, silent,
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003074 eap->addr_count == 0, address_count++);
3075 if (eap->cmd == NULL) // error detected
3076 return FAIL;
3077 if (lnum == MAXLNUM)
3078 {
3079 if (*eap->cmd == '%') // '%' - all lines
3080 {
3081 ++eap->cmd;
3082 switch (eap->addr_type)
3083 {
3084 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003085 case ADDR_OTHER:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003086 eap->line1 = 1;
3087 eap->line2 = curbuf->b_ml.ml_line_count;
3088 break;
3089 case ADDR_LOADED_BUFFERS:
3090 {
3091 buf_T *buf = firstbuf;
3092
3093 while (buf->b_next != NULL
3094 && buf->b_ml.ml_mfp == NULL)
3095 buf = buf->b_next;
3096 eap->line1 = buf->b_fnum;
3097 buf = lastbuf;
3098 while (buf->b_prev != NULL
3099 && buf->b_ml.ml_mfp == NULL)
3100 buf = buf->b_prev;
3101 eap->line2 = buf->b_fnum;
3102 break;
3103 }
3104 case ADDR_BUFFERS:
3105 eap->line1 = firstbuf->b_fnum;
3106 eap->line2 = lastbuf->b_fnum;
3107 break;
3108 case ADDR_WINDOWS:
3109 case ADDR_TABS:
3110 if (IS_USER_CMDIDX(eap->cmdidx))
3111 {
3112 eap->line1 = 1;
3113 eap->line2 = eap->addr_type == ADDR_WINDOWS
3114 ? LAST_WIN_NR : LAST_TAB_NR;
3115 }
3116 else
3117 {
3118 // there is no Vim command which uses '%' and
3119 // ADDR_WINDOWS or ADDR_TABS
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003120 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003121 return FAIL;
3122 }
3123 break;
3124 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003125 case ADDR_UNSIGNED:
3126 case ADDR_QUICKFIX:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003127 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003128 return FAIL;
3129 case ADDR_ARGUMENTS:
3130 if (ARGCOUNT == 0)
3131 eap->line1 = eap->line2 = 0;
3132 else
3133 {
3134 eap->line1 = 1;
3135 eap->line2 = ARGCOUNT;
3136 }
3137 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003138 case ADDR_QUICKFIX_VALID:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003139#ifdef FEAT_QUICKFIX
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003140 eap->line1 = 1;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003141 eap->line2 = qf_get_valid_size(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003142 if (eap->line2 == 0)
3143 eap->line2 = 1;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003144#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003145 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003146 case ADDR_NONE:
3147 // Will give an error later if a range is found.
3148 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003149 }
3150 ++eap->addr_count;
3151 }
3152 else if (*eap->cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
3153 {
3154 pos_T *fp;
3155
3156 // '*' - visual area
3157 if (eap->addr_type != ADDR_LINES)
3158 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003159 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003160 return FAIL;
3161 }
3162
3163 ++eap->cmd;
3164 if (!eap->skip)
3165 {
3166 fp = getmark('<', FALSE);
3167 if (check_mark(fp) == FAIL)
3168 return FAIL;
3169 eap->line1 = fp->lnum;
3170 fp = getmark('>', FALSE);
3171 if (check_mark(fp) == FAIL)
3172 return FAIL;
3173 eap->line2 = fp->lnum;
3174 ++eap->addr_count;
3175 }
3176 }
3177 }
3178 else
3179 eap->line2 = lnum;
3180 eap->addr_count++;
3181
3182 if (*eap->cmd == ';')
3183 {
3184 if (!eap->skip)
3185 {
3186 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar0e717042020-04-27 19:29:01 +02003187 // Don't leave the cursor on an illegal line or column, but do
3188 // accept zero as address, so 0;/PATTERN/ works correctly.
3189 if (eap->line2 > 0)
3190 check_cursor();
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003191 }
3192 }
3193 else if (*eap->cmd != ',')
3194 break;
3195 ++eap->cmd;
3196 }
3197
3198 // One address given: set start and end lines.
3199 if (eap->addr_count == 1)
3200 {
3201 eap->line1 = eap->line2;
3202 // ... but only implicit: really no address given
3203 if (lnum == MAXLNUM)
3204 eap->addr_count = 0;
3205 }
3206 return OK;
3207}
3208
3209/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003210 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003211 * If there is a match advance "pp" to the argument and return TRUE.
3212 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003213 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003214checkforcmd(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003215 char_u **pp, // start of command
3216 char *cmd, // name of command
3217 int len) // required length
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218{
3219 int i;
3220
3221 for (i = 0; cmd[i] != NUL; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003222 if (((char_u *)cmd)[i] != (*pp)[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223 break;
Bram Moolenaar7a66a172020-10-16 19:56:12 +02003224 if (i >= len && !isalpha((*pp)[i]) && (*pp)[i] != '_')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003225 {
3226 *pp = skipwhite(*pp + i);
3227 return TRUE;
3228 }
3229 return FALSE;
3230}
3231
3232/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003233 * Append "cmd" to the error message in IObuff.
3234 * Takes care of limiting the length and handling 0xa0, which would be
3235 * invisible otherwise.
3236 */
3237 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003238append_command(char_u *cmd)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003239{
3240 char_u *s = cmd;
3241 char_u *d;
3242
3243 STRCAT(IObuff, ": ");
3244 d = IObuff + STRLEN(IObuff);
3245 while (*s != NUL && d - IObuff < IOSIZE - 7)
3246 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003247 if (enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) : *s == 0xa0)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003248 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003249 s += enc_utf8 ? 2 : 1;
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003250 STRCPY(d, "<a0>");
3251 d += 4;
3252 }
3253 else
3254 MB_COPY_CHAR(s, d);
3255 }
3256 *d = NUL;
3257}
3258
3259/*
Bram Moolenaar2e800952020-08-23 19:34:48 +02003260 * If "start" points "&opt", "&l:opt", "&g:opt" or "$ENV" return a pointer to
3261 * the name. Otherwise just return "start".
3262 */
3263 char_u *
3264skip_option_env_lead(char_u *start)
3265{
3266 char_u *name = start;
3267
3268 if (*start == '&')
3269 {
3270 if ((start[1] == 'l' || start[1] == 'g') && start[2] == ':')
3271 name += 3;
3272 else
3273 name += 1;
3274 }
3275 else if (*start == '$')
3276 name += 1;
3277 return name;
3278}
3279
3280/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003281 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003282 * Start of the name can be found at eap->cmd.
Bram Moolenaar25190db2019-05-04 15:05:28 +02003283 * Sets eap->cmdidx and returns a pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003284 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003285 *
3286 * If "lookup" is not NULL recognize expression without "eval" or "call" and
3287 * assignment without "let". Sets eap->cmdidx to the command while returning
3288 * "eap->cmd".
3289 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003290 * Returns NULL for an ambiguous user command.
3291 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003292 char_u *
3293find_ex_command(
3294 exarg_T *eap,
Bram Moolenaara494f562020-04-18 17:45:38 +02003295 int *full UNUSED,
Bram Moolenaar709664c2020-12-12 14:33:41 +01003296 int (*lookup)(char_u *, size_t, void *, cctx_T *) UNUSED,
Bram Moolenaara494f562020-04-18 17:45:38 +02003297 cctx_T *cctx UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003298{
3299 int len;
3300 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003301 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003302
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003303#ifdef FEAT_EVAL
3304 /*
3305 * Recognize a Vim9 script function/method call and assignment:
3306 * "lvar = value", "lvar(arg)", "[1, 2 3]->Func()"
3307 */
Bram Moolenaar0c6ceaf2020-02-22 18:36:32 +01003308 p = eap->cmd;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003309 if (lookup != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003310 {
Bram Moolenaar2e800952020-08-23 19:34:48 +02003311 char_u *pskip = skip_option_env_lead(eap->cmd);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003312
Bram Moolenaar83144542020-08-02 20:40:43 +02003313 if (vim_strchr((char_u *)"{('[\"@", *p) != NULL
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003314 || ((p = to_name_const_end(pskip)) > eap->cmd && *p != NUL))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003315 {
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003316 int oplen;
3317 int heredoc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003318
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003319 if (
3320 // "(..." is an expression.
3321 // "funcname(" is always a function call.
3322 *p == '('
3323 || (p == eap->cmd
3324 ? (
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003325 // "{..." is a dict expression or block start.
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003326 *eap->cmd == '{'
3327 // "'string'->func()" is an expression.
3328 || *eap->cmd == '\''
Bram Moolenaar10409562020-07-29 20:00:38 +02003329 // '"string"->func()' is an expression.
3330 || *eap->cmd == '"'
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003331 // "g:varname" is an expression.
3332 || eap->cmd[1] == ':'
3333 )
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003334 // "varname->func()" is an expression.
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003335 : (*p == '-' && p[1] == '>')))
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003336 {
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003337 if (*eap->cmd == '{' && ends_excmd(*skipwhite(eap->cmd + 1)))
3338 {
3339 // "{" by itself is the start of a block.
3340 eap->cmdidx = CMD_block;
3341 return eap->cmd + 1;
3342 }
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003343 eap->cmdidx = CMD_eval;
3344 return eap->cmd;
3345 }
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003346
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003347 if (p != eap->cmd && (
3348 // "varname[]" is an expression.
3349 *p == '['
3350 // "varname.key" is an expression.
3351 || (*p == '.' && ASCII_ISALPHA(p[1]))))
3352 {
3353 char_u *after = p;
3354
3355 // When followed by "=" or "+=" then it is an assignment.
3356 ++emsg_silent;
Bram Moolenaar8f22f5c2020-12-19 22:10:13 +01003357 if (*after == '.')
3358 after = skipwhite(after + 1);
Bram Moolenaarce2c5442020-11-28 21:21:17 +01003359 if (skip_expr(&after, NULL) == OK)
3360 after = skipwhite(after);
3361 else
3362 after = (char_u *)"";
3363 if (*after == '=' || (*after != NUL && after[1] == '=')
3364 || (after[0] == '.' && after[1] == '.'
3365 && after[2] == '='))
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003366 eap->cmdidx = CMD_var;
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003367 else
3368 eap->cmdidx = CMD_eval;
3369 --emsg_silent;
3370 return eap->cmd;
3371 }
3372
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003373 // "[...]->Method()" is a list expression, but "[a, b] = Func()" is
3374 // an assignment.
3375 // If there is no line break inside the "[...]" then "p" is
3376 // advanced to after the "]" by to_name_const_end(): check if a "="
3377 // follows.
3378 // If "[...]" has a line break "p" still points at the "[" and it
3379 // can't be an assignment.
3380 if (*eap->cmd == '[')
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003381 {
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003382 p = to_name_const_end(eap->cmd);
Bram Moolenaarda7c20c2020-11-30 21:12:19 +01003383 if (p == eap->cmd && *p == '[')
3384 {
3385 int count = 0;
3386 int semicolon = FALSE;
3387
3388 p = skip_var_list(eap->cmd, TRUE, &count, &semicolon, TRUE);
3389 }
3390 if (p == NULL || p == eap->cmd || *skipwhite(p) != '=')
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003391 {
3392 eap->cmdidx = CMD_eval;
3393 return eap->cmd;
3394 }
3395 if (p > eap->cmd && *skipwhite(p) == '=')
3396 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003397 eap->cmdidx = CMD_var;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003398 return eap->cmd;
3399 }
3400 }
3401
3402 // Recognize an assignment if we recognize the variable name:
3403 // "g:var = expr"
3404 // "var = expr" where "var" is a local var name.
Bram Moolenaar83144542020-08-02 20:40:43 +02003405 if (*eap->cmd == '@')
3406 p = eap->cmd + 2;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003407 oplen = assignment_len(skipwhite(p), &heredoc);
3408 if (oplen > 0)
3409 {
3410 if (((p - eap->cmd) > 2 && eap->cmd[1] == ':')
3411 || *eap->cmd == '&'
3412 || *eap->cmd == '$'
3413 || *eap->cmd == '@'
Bram Moolenaar709664c2020-12-12 14:33:41 +01003414 || lookup(eap->cmd, p - eap->cmd, NULL, cctx) == OK)
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003415 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003416 eap->cmdidx = CMD_var;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003417 return eap->cmd;
3418 }
3419 }
3420
3421 // Recognize using a type for a w:, b:, t: or g: variable:
3422 // "w:varname: number = 123".
3423 if (eap->cmd[1] == ':' && *p == ':')
3424 {
3425 eap->cmdidx = CMD_eval;
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003426 return eap->cmd;
3427 }
Bram Moolenaard2ef6b32020-07-02 21:11:34 +02003428 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003429 }
3430#endif
3431
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432 /*
3433 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00003434 * Exceptions:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435 * - the 'k' command can directly be followed by any character.
3436 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003437 * but :sre[wind] is another command, as are :scr[iptnames],
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00003439 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440 */
3441 p = eap->cmd;
3442 if (*p == 'k')
3443 {
3444 eap->cmdidx = CMD_k;
3445 ++p;
3446 }
3447 else if (p[0] == 's'
Bram Moolenaar204b93f2015-08-04 22:02:51 +02003448 && ((p[1] == 'c' && (p[2] == NUL || (p[2] != 's' && p[2] != 'r'
3449 && (p[3] == NUL || (p[3] != 'i' && p[4] != 'p')))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450 || p[1] == 'g'
3451 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
3452 || p[1] == 'I'
3453 || (p[1] == 'r' && p[2] != 'e')))
3454 {
3455 eap->cmdidx = CMD_substitute;
3456 ++p;
3457 }
3458 else
3459 {
3460 while (ASCII_ISALPHA(*p))
3461 ++p;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003462 // for python 3.x support ":py3", ":python3", ":py3file", etc.
Bram Moolenaar55d5c032010-07-17 23:52:29 +02003463 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003464 {
Bram Moolenaarb6590522010-07-21 16:00:43 +02003465 while (ASCII_ISALNUM(*p))
3466 ++p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003467 }
3468 else if (*p == '9' && STRNCMP("vim9", eap->cmd, 4) == 0)
3469 {
3470 // include "9" for "vim9script"
3471 ++p;
3472 while (ASCII_ISALPHA(*p))
3473 ++p;
3474 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02003475
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003476 // check for non-alpha command
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003477 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#}", *p) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478 ++p;
3479 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003480 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
3481 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003482 // Check for ":dl", ":dell", etc. to ":deletel": that's
3483 // :delete with the 'l' flag. Same for 'p'.
Bram Moolenaardf177f62005-02-22 08:39:57 +00003484 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003485 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00003486 break;
3487 if (i == len - 1)
3488 {
3489 --len;
3490 if (p[-1] == 'l')
3491 eap->flags |= EXFLAG_LIST;
3492 else
3493 eap->flags |= EXFLAG_PRINT;
3494 }
3495 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003497 if (ASCII_ISLOWER(eap->cmd[0]))
3498 {
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003499 int c1 = eap->cmd[0];
Bram Moolenaar94f82cb2019-07-24 22:30:27 +02003500 int c2 = len == 1 ? NUL : eap->cmd[1];
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003501
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003502 if (command_count != (int)CMD_SIZE)
3503 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003504 iemsg(_("E943: Command table needs to be updated, run 'make cmdidxs'"));
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003505 getout(1);
3506 }
3507
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003508 // Use a precomputed index for fast look-up in cmdnames[]
3509 // taking into account the first 2 letters of eap->cmd.
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003510 eap->cmdidx = cmdidxs1[CharOrdLow(c1)];
3511 if (ASCII_ISLOWER(c2))
3512 eap->cmdidx += cmdidxs2[CharOrdLow(c1)][CharOrdLow(c2)];
3513 }
Bram Moolenaara494f562020-04-18 17:45:38 +02003514 else if (ASCII_ISUPPER(eap->cmd[0]))
3515 eap->cmdidx = CMD_Next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516 else
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003517 eap->cmdidx = CMD_bang;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518
3519 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
3520 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
3521 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
3522 (size_t)len) == 0)
3523 {
3524#ifdef FEAT_EVAL
3525 if (full != NULL
3526 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
3527 *full = TRUE;
3528#endif
3529 break;
3530 }
3531
Bram Moolenaar95388e32020-11-20 21:07:00 +01003532 // Not not recognize ":*" as the star command unless '*' is in
3533 // 'cpoptions'.
3534 if (eap->cmdidx == CMD_star && vim_strchr(p_cpo, CPO_STAR) == NULL)
3535 p = eap->cmd;
3536
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003537 // Look for a user defined command as a last resort. Let ":Print" be
3538 // overruled by a user defined command.
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01003539 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
3540 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541 {
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003542 // User defined commands may contain digits.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543 while (ASCII_ISALNUM(*p))
3544 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003545 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546 }
Bram Moolenaar1c0aa972020-12-16 21:43:54 +01003547 if (p == NULL || p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548 eap->cmdidx = CMD_SIZE;
3549 }
3550
Bram Moolenaar373863e2020-09-26 17:20:53 +02003551 // ":fina" means ":finally" for backwards compatibility.
3552 if (eap->cmdidx == CMD_final && p - eap->cmd == 4)
3553 eap->cmdidx = CMD_finally;
3554
Bram Moolenaar2a3cd3a2020-12-13 19:22:27 +01003555#ifdef FEAT_EVAL
Bram Moolenaard1510ee2021-01-04 16:15:58 +01003556 if (eap->cmdidx < CMD_SIZE
3557 && in_vim9script()
Bram Moolenaarf8103f22020-12-25 17:36:27 +01003558 && !IS_WHITE_OR_NUL(*p) && *p != '\n' && *p != '!'
Bram Moolenaard1510ee2021-01-04 16:15:58 +01003559 && (eap->cmdidx < 0 ||
3560 (cmdnames[eap->cmdidx].cmd_argt & EX_NONWHITE_OK) == 0))
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003561 {
3562 semsg(_(e_command_not_followed_by_white_space_str), eap->cmd);
3563 eap->cmdidx = CMD_SIZE;
3564 }
Bram Moolenaar2a3cd3a2020-12-13 19:22:27 +01003565#endif
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003566
Bram Moolenaar071d4272004-06-13 20:20:40 +00003567 return p;
3568}
3569
3570#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003571static struct cmdmod
3572{
3573 char *name;
3574 int minlen;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003575 int has_count; // :123verbose :3tab
Bram Moolenaared53fb92007-11-24 20:50:24 +00003576} cmdmods[] = {
3577 {"aboveleft", 3, FALSE},
3578 {"belowright", 3, FALSE},
3579 {"botright", 2, FALSE},
3580 {"browse", 3, FALSE},
3581 {"confirm", 4, FALSE},
Bram Moolenaar7b668e82016-08-23 23:51:21 +02003582 {"filter", 4, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003583 {"hide", 3, FALSE},
3584 {"keepalt", 5, FALSE},
3585 {"keepjumps", 5, FALSE},
3586 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01003587 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003588 {"leftabove", 5, FALSE},
3589 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003590 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003591 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003592 {"rightbelow", 6, FALSE},
3593 {"sandbox", 3, FALSE},
3594 {"silent", 3, FALSE},
3595 {"tab", 3, TRUE},
3596 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003597 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003598 {"verbose", 4, TRUE},
3599 {"vertical", 4, FALSE},
3600};
3601
3602/*
3603 * Return length of a command modifier (including optional count).
3604 * Return zero when it's not a modifier.
3605 */
3606 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003607modifier_len(char_u *cmd)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003608{
3609 int i, j;
3610 char_u *p = cmd;
3611
3612 if (VIM_ISDIGIT(*cmd))
3613 p = skipwhite(skipdigits(cmd));
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003614 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003615 {
3616 for (j = 0; p[j] != NUL; ++j)
3617 if (p[j] != cmdmods[i].name[j])
3618 break;
Bram Moolenaar2d473ab2013-06-12 17:12:24 +02003619 if (!ASCII_ISALPHA(p[j]) && j >= cmdmods[i].minlen
Bram Moolenaared53fb92007-11-24 20:50:24 +00003620 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003621 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003622 }
3623 return 0;
3624}
3625
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626/*
3627 * Return > 0 if an Ex command "name" exists.
3628 * Return 2 if there is an exact match.
3629 * Return 3 if there is an ambiguous match.
3630 */
3631 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003632cmd_exists(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633{
3634 exarg_T ea;
3635 int full = FALSE;
3636 int i;
3637 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003638 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003640 // Check command modifiers.
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003641 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642 {
3643 for (j = 0; name[j] != NUL; ++j)
3644 if (name[j] != cmdmods[i].name[j])
3645 break;
3646 if (name[j] == NUL && j >= cmdmods[i].minlen)
3647 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3648 }
3649
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003650 // Check built-in commands and user defined commands.
3651 // For ":2match" and ":3match" we need to skip the number.
Bram Moolenaara9587612006-05-04 21:47:50 +00003652 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003653 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003654 p = find_ex_command(&ea, &full, NULL, NULL);
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003655 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003656 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003657 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3658 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003659 if (*skipwhite(p) != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003660 return 0; // trailing garbage
Bram Moolenaar071d4272004-06-13 20:20:40 +00003661 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3662}
3663#endif
3664
Bram Moolenaard0190392019-08-23 21:17:35 +02003665 cmdidx_T
3666excmd_get_cmdidx(char_u *cmd, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667{
Bram Moolenaard0190392019-08-23 21:17:35 +02003668 cmdidx_T idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003669
Bram Moolenaard0190392019-08-23 21:17:35 +02003670 for (idx = (cmdidx_T)0; (int)idx < (int)CMD_SIZE;
3671 idx = (cmdidx_T)((int)idx + 1))
3672 if (STRNCMP(cmdnames[(int)idx].cmd_name, cmd, (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673 break;
3674
Bram Moolenaard0190392019-08-23 21:17:35 +02003675 return idx;
3676}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003677
Bram Moolenaard0190392019-08-23 21:17:35 +02003678 long
3679excmd_get_argt(cmdidx_T idx)
3680{
3681 return (long)cmdnames[(int)idx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003682}
3683
3684/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003685 * Skip a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00003686 *
3687 * Backslashed delimiters after / or ? will be skipped, and commands will
3688 * not be expanded between /'s and ?'s or after "'".
3689 *
Bram Moolenaardf069ee2020-06-22 23:02:51 +02003690 * Also skip white space and ":" characters after the range.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003691 * Returns the "cmd" pointer advanced to beyond the range.
3692 */
3693 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003694skip_range(
3695 char_u *cmd,
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02003696 int skip_star, // skip "*" used for Visual range
3697 int *ctx) // pointer to xp_context or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003698{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003699 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003700
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01003701 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;\\", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003702 {
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01003703 if (*cmd == '\\')
3704 {
3705 if (cmd[1] == '?' || cmd[1] == '/' || cmd[1] == '&')
3706 ++cmd;
3707 else
3708 break;
3709 }
3710 else if (*cmd == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711 {
3712 if (*++cmd == NUL && ctx != NULL)
3713 *ctx = EXPAND_NOTHING;
3714 }
3715 else if (*cmd == '/' || *cmd == '?')
3716 {
3717 delim = *cmd++;
3718 while (*cmd != NUL && *cmd != delim)
3719 if (*cmd++ == '\\' && *cmd != NUL)
3720 ++cmd;
3721 if (*cmd == NUL && ctx != NULL)
3722 *ctx = EXPAND_NOTHING;
3723 }
3724 if (*cmd != NUL)
3725 ++cmd;
3726 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00003727
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003728 // Skip ":" and white space.
Bram Moolenaardf177f62005-02-22 08:39:57 +00003729 while (*cmd == ':')
3730 cmd = skipwhite(cmd + 1);
3731
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02003732 // Skip "*" used for Visual range.
3733 if (skip_star && *cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
3734 cmd = skipwhite(cmd + 1);
3735
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736 return cmd;
3737}
3738
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003739 static void
3740addr_error(cmd_addr_T addr_type)
3741{
3742 if (addr_type == ADDR_NONE)
3743 emsg(_(e_norange));
3744 else
3745 emsg(_(e_invrange));
3746}
3747
Bram Moolenaar071d4272004-06-13 20:20:40 +00003748/*
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02003749 * Return the default address for an address type.
3750 */
3751 static linenr_T
3752default_address(exarg_T *eap)
3753{
3754 linenr_T lnum = 0;
3755
3756 switch (eap->addr_type)
3757 {
3758 case ADDR_LINES:
3759 case ADDR_OTHER:
3760 // Default is the cursor line number. Avoid using an invalid
3761 // line number though.
3762 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
3763 lnum = curbuf->b_ml.ml_line_count;
3764 else
3765 lnum = curwin->w_cursor.lnum;
3766 break;
3767 case ADDR_WINDOWS:
3768 lnum = CURRENT_WIN_NR;
3769 break;
3770 case ADDR_ARGUMENTS:
3771 lnum = curwin->w_arg_idx + 1;
3772 if (lnum > ARGCOUNT)
3773 lnum = ARGCOUNT;
3774 break;
3775 case ADDR_LOADED_BUFFERS:
3776 case ADDR_BUFFERS:
3777 lnum = curbuf->b_fnum;
3778 break;
3779 case ADDR_TABS:
3780 lnum = CURRENT_TAB_NR;
3781 break;
3782 case ADDR_TABS_RELATIVE:
3783 case ADDR_UNSIGNED:
3784 lnum = 1;
3785 break;
3786 case ADDR_QUICKFIX:
3787#ifdef FEAT_QUICKFIX
3788 lnum = qf_get_cur_idx(eap);
3789#endif
3790 break;
3791 case ADDR_QUICKFIX_VALID:
3792#ifdef FEAT_QUICKFIX
3793 lnum = qf_get_cur_valid_idx(eap);
3794#endif
3795 break;
3796 case ADDR_NONE:
3797 // Will give an error later if a range is found.
3798 break;
3799 }
3800 return lnum;
3801}
3802
3803/*
Bram Moolenaar198cb662018-09-06 21:44:17 +02003804 * Get a single EX address.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003805 *
3806 * Set ptr to the next character after the part that was interpreted.
3807 * Set ptr to NULL when an error is encountered.
Bram Moolenaar198cb662018-09-06 21:44:17 +02003808 * This may set the last used search pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003809 *
3810 * Return MAXLNUM when no Ex address was found.
3811 */
3812 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003813get_address(
3814 exarg_T *eap UNUSED,
3815 char_u **ptr,
Bram Moolenaar1b03a192019-12-08 17:08:29 +01003816 cmd_addr_T addr_type,
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003817 int skip, // only skip the address, don't use it
3818 int silent, // no errors or side effects
3819 int to_other_file, // flag: may jump to other file
3820 int address_count UNUSED) // 1 for first address, >1 after comma
Bram Moolenaar071d4272004-06-13 20:20:40 +00003821{
3822 int c;
3823 int i;
3824 long n;
3825 char_u *cmd;
3826 pos_T pos;
3827 pos_T *fp;
3828 linenr_T lnum;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003829 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830
3831 cmd = skipwhite(*ptr);
3832 lnum = MAXLNUM;
3833 do
3834 {
3835 switch (*cmd)
3836 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003837 case '.': // '.' - Cursor position
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003838 ++cmd;
3839 switch (addr_type)
3840 {
3841 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003842 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003843 lnum = curwin->w_cursor.lnum;
3844 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003845 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003846 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003847 break;
3848 case ADDR_ARGUMENTS:
3849 lnum = curwin->w_arg_idx + 1;
3850 break;
3851 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003852 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003853 lnum = curbuf->b_fnum;
3854 break;
3855 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003856 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003857 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003858 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003859 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003860 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003861 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003862 cmd = NULL;
3863 goto error;
3864 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003865 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003866#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02003867 lnum = qf_get_cur_idx(eap);
3868#endif
3869 break;
3870 case ADDR_QUICKFIX_VALID:
3871#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003872 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02003873#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003874 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003875 }
3876 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003877
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003878 case '$': // '$' - last line
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003879 ++cmd;
3880 switch (addr_type)
3881 {
3882 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003883 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884 lnum = curbuf->b_ml.ml_line_count;
3885 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003886 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003887 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003888 break;
3889 case ADDR_ARGUMENTS:
3890 lnum = ARGCOUNT;
3891 break;
3892 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003893 buf = lastbuf;
3894 while (buf->b_ml.ml_mfp == NULL)
3895 {
3896 if (buf->b_prev == NULL)
3897 break;
3898 buf = buf->b_prev;
3899 }
3900 lnum = buf->b_fnum;
3901 break;
3902 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003903 lnum = lastbuf->b_fnum;
3904 break;
3905 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003906 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003907 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003908 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003909 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003910 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003911 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003912 cmd = NULL;
3913 goto error;
3914 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003915 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003916#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003917 lnum = qf_get_size(eap);
3918 if (lnum == 0)
3919 lnum = 1;
Bram Moolenaare906c502015-09-09 21:10:39 +02003920#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003921 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003922 case ADDR_QUICKFIX_VALID:
3923#ifdef FEAT_QUICKFIX
3924 lnum = qf_get_valid_size(eap);
3925 if (lnum == 0)
3926 lnum = 1;
3927#endif
3928 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003929 }
3930 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003932 case '\'': // ''' - mark
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003933 if (*++cmd == NUL)
3934 {
3935 cmd = NULL;
3936 goto error;
3937 }
3938 if (addr_type != ADDR_LINES)
3939 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003940 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003941 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003942 goto error;
3943 }
3944 if (skip)
3945 ++cmd;
3946 else
3947 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003948 // Only accept a mark in another file when it is
3949 // used by itself: ":'M".
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003950 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
3951 ++cmd;
3952 if (fp == (pos_T *)-1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003953 // Jumped to another file.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003954 lnum = curwin->w_cursor.lnum;
3955 else
3956 {
3957 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003958 {
3959 cmd = NULL;
3960 goto error;
3961 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003962 lnum = fp->lnum;
3963 }
3964 }
3965 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003966
3967 case '/':
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003968 case '?': // '/' or '?' - search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003969 c = *cmd++;
3970 if (addr_type != ADDR_LINES)
3971 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003972 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003973 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003974 goto error;
3975 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003976 if (skip) // skip "/pat/"
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003977 {
Bram Moolenaarf4e20992020-12-21 19:59:08 +01003978 cmd = skip_regexp(cmd, c, magic_isset());
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003979 if (*cmd == c)
3980 ++cmd;
3981 }
3982 else
3983 {
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003984 int flags;
3985
3986 pos = curwin->w_cursor; // save curwin->w_cursor
3987
3988 // When '/' or '?' follows another address, start from
3989 // there.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003990 if (lnum != MAXLNUM)
3991 curwin->w_cursor.lnum = lnum;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003992
3993 // Start a forward search at the end of the line (unless
3994 // before the first line).
3995 // Start a backward search at the start of the line.
3996 // This makes sure we never match in the current
3997 // line, and can match anywhere in the
3998 // next/previous line.
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01003999 if (c == '/' && curwin->w_cursor.lnum > 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004000 curwin->w_cursor.col = MAXCOL;
4001 else
4002 curwin->w_cursor.col = 0;
4003 searchcmdlen = 0;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004004 flags = silent ? 0 : SEARCH_HIS | SEARCH_MSG;
Bram Moolenaarc036e872020-02-21 21:30:52 +01004005 if (!do_search(NULL, c, c, cmd, 1L, flags, NULL))
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004006 {
4007 curwin->w_cursor = pos;
4008 cmd = NULL;
4009 goto error;
4010 }
4011 lnum = curwin->w_cursor.lnum;
4012 curwin->w_cursor = pos;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004013 // adjust command string pointer
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004014 cmd += searchcmdlen;
4015 }
4016 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004017
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004018 case '\\': // "\?", "\/" or "\&", repeat search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004019 ++cmd;
4020 if (addr_type != ADDR_LINES)
4021 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004022 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004023 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004024 goto error;
4025 }
4026 if (*cmd == '&')
4027 i = RE_SUBST;
4028 else if (*cmd == '?' || *cmd == '/')
4029 i = RE_SEARCH;
4030 else
4031 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004032 emsg(_(e_backslash));
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004033 cmd = NULL;
4034 goto error;
4035 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004036
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004037 if (!skip)
4038 {
4039 /*
4040 * When search follows another address, start from
4041 * there.
4042 */
4043 if (lnum != MAXLNUM)
4044 pos.lnum = lnum;
4045 else
4046 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004047
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004048 /*
4049 * Start the search just like for the above
4050 * do_search().
4051 */
4052 if (*cmd != '?')
4053 pos.col = MAXCOL;
4054 else
4055 pos.col = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02004056 pos.coladd = 0;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01004057 if (searchit(curwin, curbuf, &pos, NULL,
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004058 *cmd == '?' ? BACKWARD : FORWARD,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004059 (char_u *)"", 1L, SEARCH_MSG, i, NULL) != FAIL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004060 lnum = pos.lnum;
4061 else
4062 {
4063 cmd = NULL;
4064 goto error;
4065 }
4066 }
4067 ++cmd;
4068 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069
4070 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004071 if (VIM_ISDIGIT(*cmd)) // absolute line number
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004072 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004073 }
4074
4075 for (;;)
4076 {
4077 cmd = skipwhite(cmd);
4078 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4079 break;
4080
4081 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004082 {
4083 switch (addr_type)
4084 {
4085 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004086 case ADDR_OTHER:
4087 // "+1" is same as ".+1"
Bram Moolenaarf240e182014-11-27 18:33:02 +01004088 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004089 break;
4090 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004091 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004092 break;
4093 case ADDR_ARGUMENTS:
4094 lnum = curwin->w_arg_idx + 1;
4095 break;
4096 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004097 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004098 lnum = curbuf->b_fnum;
4099 break;
4100 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004101 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004102 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004103 case ADDR_TABS_RELATIVE:
4104 lnum = 1;
4105 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004106 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004107#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004108 lnum = qf_get_cur_idx(eap);
4109#endif
4110 break;
4111 case ADDR_QUICKFIX_VALID:
4112#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004113 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02004114#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004115 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004116 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004117 case ADDR_UNSIGNED:
4118 lnum = 0;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004119 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004120 }
4121 }
4122
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123 if (VIM_ISDIGIT(*cmd))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004124 i = '+'; // "number" is same as "+number"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004125 else
4126 i = *cmd++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004127 if (!VIM_ISDIGIT(*cmd)) // '+' is '+1', but '+0' is not '+1'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128 n = 1;
4129 else
4130 n = getdigits(&cmd);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004131
4132 if (addr_type == ADDR_TABS_RELATIVE)
4133 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004134 emsg(_(e_invrange));
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004135 cmd = NULL;
4136 goto error;
4137 }
4138 else if (addr_type == ADDR_LOADED_BUFFERS
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004139 || addr_type == ADDR_BUFFERS)
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004140 lnum = compute_buffer_local_count(
4141 addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142 else
Bram Moolenaarded27822017-01-02 14:27:34 +01004143 {
4144#ifdef FEAT_FOLDING
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004145 // Relative line addressing, need to adjust for folded lines
4146 // now, but only do it after the first address.
Bram Moolenaarded27822017-01-02 14:27:34 +01004147 if (addr_type == ADDR_LINES && (i == '-' || i == '+')
4148 && address_count >= 2)
4149 (void)hasFolding(lnum, NULL, &lnum);
4150#endif
4151 if (i == '-')
4152 lnum -= n;
4153 else
4154 lnum += n;
4155 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156 }
4157 } while (*cmd == '/' || *cmd == '?');
4158
4159error:
4160 *ptr = cmd;
4161 return lnum;
4162}
4163
4164/*
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02004165 * Set eap->line1 and eap->line2 to the whole range.
4166 * Used for commands with the EX_DFLALL flag and no range given.
4167 */
4168 static void
4169address_default_all(exarg_T *eap)
4170{
4171 eap->line1 = 1;
4172 switch (eap->addr_type)
4173 {
4174 case ADDR_LINES:
4175 case ADDR_OTHER:
4176 eap->line2 = curbuf->b_ml.ml_line_count;
4177 break;
4178 case ADDR_LOADED_BUFFERS:
4179 {
4180 buf_T *buf = firstbuf;
4181
4182 while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL)
4183 buf = buf->b_next;
4184 eap->line1 = buf->b_fnum;
4185 buf = lastbuf;
4186 while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL)
4187 buf = buf->b_prev;
4188 eap->line2 = buf->b_fnum;
4189 }
4190 break;
4191 case ADDR_BUFFERS:
4192 eap->line1 = firstbuf->b_fnum;
4193 eap->line2 = lastbuf->b_fnum;
4194 break;
4195 case ADDR_WINDOWS:
4196 eap->line2 = LAST_WIN_NR;
4197 break;
4198 case ADDR_TABS:
4199 eap->line2 = LAST_TAB_NR;
4200 break;
4201 case ADDR_TABS_RELATIVE:
4202 eap->line2 = 1;
4203 break;
4204 case ADDR_ARGUMENTS:
4205 if (ARGCOUNT == 0)
4206 eap->line1 = eap->line2 = 0;
4207 else
4208 eap->line2 = ARGCOUNT;
4209 break;
4210 case ADDR_QUICKFIX_VALID:
4211#ifdef FEAT_QUICKFIX
4212 eap->line2 = qf_get_valid_size(eap);
4213 if (eap->line2 == 0)
4214 eap->line2 = 1;
4215#endif
4216 break;
4217 case ADDR_NONE:
4218 case ADDR_UNSIGNED:
4219 case ADDR_QUICKFIX:
4220 iemsg(_("INTERNAL: Cannot use EX_DFLALL with ADDR_NONE, ADDR_UNSIGNED or ADDR_QUICKFIX"));
4221 break;
4222 }
4223}
4224
4225
4226/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004227 * Get flags from an Ex command argument.
4228 */
4229 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004230get_flags(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004231{
4232 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4233 {
4234 if (*eap->arg == 'l')
4235 eap->flags |= EXFLAG_LIST;
4236 else if (*eap->arg == 'p')
4237 eap->flags |= EXFLAG_PRINT;
4238 else
4239 eap->flags |= EXFLAG_NR;
4240 eap->arg = skipwhite(eap->arg + 1);
4241 }
4242}
4243
4244/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245 * Function called for command which is Not Implemented. NI!
4246 */
4247 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004248ex_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249{
4250 if (!eap->skip)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02004251 eap->errmsg =
4252 _("E319: Sorry, the command is not available in this version");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253}
4254
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004255#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256/*
4257 * Function called for script command which is Not Implemented. NI!
4258 * Skips over ":perl <<EOF" constructs.
4259 */
4260 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004261ex_script_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262{
4263 if (!eap->skip)
4264 ex_ni(eap);
4265 else
4266 vim_free(script_get(eap, eap->arg));
4267}
4268#endif
4269
4270/*
4271 * Check range in Ex command for validity.
4272 * Return NULL when valid, error message when invalid.
4273 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004274 static char *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004275invalid_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004276{
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004277 buf_T *buf;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004278
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279 if ( eap->line1 < 0
4280 || eap->line2 < 0
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004281 || eap->line1 > eap->line2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004282 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004283
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004284 if (eap->argt & EX_RANGE)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004285 {
Bram Moolenaarb7316892019-05-01 18:08:42 +02004286 switch (eap->addr_type)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004287 {
4288 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004289 if (eap->line2 > curbuf->b_ml.ml_line_count
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004290#ifdef FEAT_DIFF
4291 + (eap->cmdidx == CMD_diffget)
4292#endif
4293 )
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004294 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004295 break;
4296 case ADDR_ARGUMENTS:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004297 // add 1 if ARGCOUNT is 0
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004298 if (eap->line2 > ARGCOUNT + (!ARGCOUNT))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004299 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004300 break;
4301 case ADDR_BUFFERS:
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02004302 // Only a boundary check, not whether the buffers actually
4303 // exist.
4304 if (eap->line1 < 1 || eap->line2 > get_highest_fnum())
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004305 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004306 break;
4307 case ADDR_LOADED_BUFFERS:
4308 buf = firstbuf;
4309 while (buf->b_ml.ml_mfp == NULL)
4310 {
4311 if (buf->b_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004312 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004313 buf = buf->b_next;
4314 }
4315 if (eap->line1 < buf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004316 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004317 buf = lastbuf;
4318 while (buf->b_ml.ml_mfp == NULL)
4319 {
4320 if (buf->b_prev == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004321 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004322 buf = buf->b_prev;
4323 }
4324 if (eap->line2 > buf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004325 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004326 break;
4327 case ADDR_WINDOWS:
Bram Moolenaar8be63882015-01-14 11:25:05 +01004328 if (eap->line2 > LAST_WIN_NR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004329 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004330 break;
4331 case ADDR_TABS:
4332 if (eap->line2 > LAST_TAB_NR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004333 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004334 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004335 case ADDR_TABS_RELATIVE:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004336 case ADDR_OTHER:
4337 // Any range is OK.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004338 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004339 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004340#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004341 // No error for value that is too big, will use the last entry.
4342 if (eap->line2 <= 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004343 return _(e_invrange);
Bram Moolenaare906c502015-09-09 21:10:39 +02004344#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004345 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004346 case ADDR_QUICKFIX_VALID:
4347#ifdef FEAT_QUICKFIX
4348 if ((eap->line2 != 1 && eap->line2 > qf_get_valid_size(eap))
4349 || eap->line2 < 0)
4350 return _(e_invrange);
4351#endif
4352 break;
4353 case ADDR_UNSIGNED:
4354 if (eap->line2 < 0)
4355 return _(e_invrange);
4356 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004357 case ADDR_NONE:
4358 // Will give an error elsewhere.
4359 break;
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004360 }
4361 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362 return NULL;
4363}
4364
4365/*
4366 * Correct the range for zero line number, if required.
4367 */
4368 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004369correct_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004370{
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004371 if (!(eap->argt & EX_ZEROR)) // zero in range not allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004372 {
4373 if (eap->line1 == 0)
4374 eap->line1 = 1;
4375 if (eap->line2 == 0)
4376 eap->line2 = 1;
4377 }
4378}
4379
Bram Moolenaar748bf032005-02-02 23:04:36 +00004380#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004381/*
4382 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4383 * pattern. Otherwise return eap->arg.
4384 */
4385 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004386skip_grep_pat(exarg_T *eap)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004387{
4388 char_u *p = eap->arg;
4389
Bram Moolenaara37420f2006-02-04 22:37:47 +00004390 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4391 || eap->cmdidx == CMD_vimgrepadd
4392 || eap->cmdidx == CMD_lvimgrepadd
4393 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004394 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004395 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004396 if (p == NULL)
4397 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004398 }
4399 return p;
4400}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004401
4402/*
4403 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4404 * in the command line, so that things like % get expanded.
4405 */
4406 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004407replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004408{
4409 char_u *new_cmdline;
4410 char_u *program;
4411 char_u *pos;
4412 char_u *ptr;
4413 int len;
4414 int i;
4415
4416 /*
4417 * Don't do it when ":vimgrep" is used for ":grep".
4418 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004419 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4420 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4421 || eap->cmdidx == CMD_grepadd
4422 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004423 && !grep_internal(eap->cmdidx))
4424 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004425 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4426 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004427 {
4428 if (*curbuf->b_p_gp == NUL)
4429 program = p_gp;
4430 else
4431 program = curbuf->b_p_gp;
4432 }
4433 else
4434 {
4435 if (*curbuf->b_p_mp == NUL)
4436 program = p_mp;
4437 else
4438 program = curbuf->b_p_mp;
4439 }
4440
4441 p = skipwhite(p);
4442
4443 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4444 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004445 // replace $* by given arguments
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004446 i = 1;
4447 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4448 ++i;
4449 len = (int)STRLEN(p);
Bram Moolenaar51e14382019-05-25 20:21:28 +02004450 new_cmdline = alloc(STRLEN(program) + i * (len - 2) + 1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004451 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004452 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004453 ptr = new_cmdline;
4454 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4455 {
4456 i = (int)(pos - program);
4457 STRNCPY(ptr, program, i);
4458 STRCPY(ptr += i, p);
4459 ptr += len;
4460 program = pos + 2;
4461 }
4462 STRCPY(ptr, program);
4463 }
4464 else
4465 {
Bram Moolenaar51e14382019-05-25 20:21:28 +02004466 new_cmdline = alloc(STRLEN(program) + STRLEN(p) + 2);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004467 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004468 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004469 STRCPY(new_cmdline, program);
4470 STRCAT(new_cmdline, " ");
4471 STRCAT(new_cmdline, p);
4472 }
4473 msg_make(p);
4474
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004475 // 'eap->cmd' is not set here, because it is not used at CMD_make
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004476 vim_free(*cmdlinep);
4477 *cmdlinep = new_cmdline;
4478 p = new_cmdline;
4479 }
4480 return p;
4481}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004482#endif
4483
Bram Moolenaar071d4272004-06-13 20:20:40 +00004484/*
4485 * Expand file name in Ex command argument.
Bram Moolenaar0abb4272019-06-15 16:06:00 +02004486 * When an error is detected, "errormsgp" is set to a non-NULL pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004487 * Return FAIL for failure, OK otherwise.
4488 */
4489 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004490expand_filename(
4491 exarg_T *eap,
4492 char_u **cmdlinep,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004493 char **errormsgp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004494{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004495 int has_wildcards; // need to expand wildcards
Bram Moolenaar071d4272004-06-13 20:20:40 +00004496 char_u *repl;
4497 int srclen;
4498 char_u *p;
4499 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004500 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004501
Bram Moolenaar748bf032005-02-02 23:04:36 +00004502#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004503 // Skip a regexp pattern for ":vimgrep[add] pat file..."
Bram Moolenaar748bf032005-02-02 23:04:36 +00004504 p = skip_grep_pat(eap);
4505#else
4506 p = eap->arg;
4507#endif
4508
Bram Moolenaar071d4272004-06-13 20:20:40 +00004509 /*
4510 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4511 * the file name contains a wildcard it should not cause expanding.
4512 * (it will be expanded anyway if there is a wildcard before replacing).
4513 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004514 has_wildcards = mch_has_wildcard(p);
4515 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004516 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004517#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004518 // Skip over `=expr`, wildcards in it are not expanded.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004519 if (p[0] == '`' && p[1] == '=')
4520 {
4521 p += 2;
Bram Moolenaar683581e2020-10-22 21:22:58 +02004522 (void)skip_expr(&p, NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004523 if (*p == '`')
4524 ++p;
4525 continue;
4526 }
4527#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004528 /*
4529 * Quick check if this cannot be the start of a special string.
4530 * Also removes backslash before '%', '#' and '<'.
4531 */
4532 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4533 {
4534 ++p;
4535 continue;
4536 }
4537
4538 /*
4539 * Try to find a match at this position.
4540 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00004541 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
4542 errormsgp, &escaped);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004543 if (*errormsgp != NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00004544 return FAIL;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004545 if (repl == NULL) // no match found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004546 {
4547 p += srclen;
4548 continue;
4549 }
4550
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004551 // Wildcards won't be expanded below, the replacement is taken
4552 // literally. But do expand "~/file", "~user/file" and "$HOME/file".
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004553 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4554 {
4555 char_u *l = repl;
4556
4557 repl = expand_env_save(repl);
4558 vim_free(l);
4559 }
4560
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004561 // Need to escape white space et al. with a backslash.
4562 // Don't do this for:
4563 // - replacement that already has been escaped: "##"
4564 // - shell commands (may have to use quotes instead).
4565 // - non-unix systems when there is a single argument (spaces don't
4566 // separate arguments then).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004567 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00004568 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00004569 && eap->cmdidx != CMD_bang
Bram Moolenaar071d4272004-06-13 20:20:40 +00004570 && eap->cmdidx != CMD_grep
4571 && eap->cmdidx != CMD_grepadd
Bram Moolenaarbf15b8d2017-06-04 20:43:48 +02004572 && eap->cmdidx != CMD_hardcopy
Bram Moolenaar67883b42017-07-27 22:57:00 +02004573 && eap->cmdidx != CMD_lgrep
4574 && eap->cmdidx != CMD_lgrepadd
4575 && eap->cmdidx != CMD_lmake
4576 && eap->cmdidx != CMD_make
4577 && eap->cmdidx != CMD_terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00004578#ifndef UNIX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004579 && !(eap->argt & EX_NOSPC)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004580#endif
4581 )
4582 {
4583 char_u *l;
4584#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004585 // Don't escape a backslash here, because rem_backslash() doesn't
4586 // remove it later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004587 static char_u *nobslash = (char_u *)" \t\"|";
4588# define ESCAPE_CHARS nobslash
4589#else
4590# define ESCAPE_CHARS escape_chars
4591#endif
4592
4593 for (l = repl; *l; ++l)
4594 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
4595 {
4596 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
4597 if (l != NULL)
4598 {
4599 vim_free(repl);
4600 repl = l;
4601 }
4602 break;
4603 }
4604 }
4605
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004606 // For a shell command a '!' must be escaped.
Bram Moolenaar67883b42017-07-27 22:57:00 +02004607 if ((eap->usefilter || eap->cmdidx == CMD_bang
4608 || eap->cmdidx == CMD_terminal)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004609 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004610 {
4611 char_u *l;
4612
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004613 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614 if (l != NULL)
4615 {
4616 vim_free(repl);
4617 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004618 }
4619 }
4620
4621 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
4622 vim_free(repl);
4623 if (p == NULL)
4624 return FAIL;
4625 }
4626
4627 /*
4628 * One file argument: Expand wildcards.
4629 * Don't do this with ":r !command" or ":w !command".
4630 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004631 if ((eap->argt & EX_NOSPC) && !eap->usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004632 {
4633 /*
4634 * May do this twice:
4635 * 1. Replace environment variables.
4636 * 2. Replace any other wildcards, remove backslashes.
4637 */
4638 for (n = 1; n <= 2; ++n)
4639 {
4640 if (n == 2)
4641 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004642 /*
4643 * Halve the number of backslashes (this is Vi compatible).
4644 * For Unix and OS/2, when wildcards are expanded, this is
4645 * done by ExpandOne() below.
4646 */
Bram Moolenaare7fedb62015-12-31 19:07:19 +01004647#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004648 if (!has_wildcards)
4649#endif
4650 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004651 }
4652
4653 if (has_wildcards)
4654 {
4655 if (n == 1)
4656 {
4657 /*
4658 * First loop: May expand environment variables. This
4659 * can be done much faster with expand_env() than with
4660 * something else (e.g., calling a shell).
4661 * After expanding environment variables, check again
4662 * if there are still wildcards present.
4663 */
4664 if (vim_strchr(eap->arg, '$') != NULL
4665 || vim_strchr(eap->arg, '~') != NULL)
4666 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00004667 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004668 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004669 has_wildcards = mch_has_wildcard(NameBuff);
4670 p = NameBuff;
4671 }
4672 else
4673 p = NULL;
4674 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004675 else // n == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004676 {
4677 expand_T xpc;
Bram Moolenaarb40c2572019-10-19 21:01:05 +02004678 int options = WILD_LIST_NOTFOUND
4679 | WILD_NOERROR | WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004680
4681 ExpandInit(&xpc);
4682 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004683 if (p_wic)
4684 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004685 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01004686 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004687 if (p == NULL)
4688 return FAIL;
4689 }
4690 if (p != NULL)
4691 {
4692 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
4693 p, cmdlinep);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004694 if (n == 2) // p came from ExpandOne()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004695 vim_free(p);
4696 }
4697 }
4698 }
4699 }
4700 return OK;
4701}
4702
4703/*
4704 * Replace part of the command line, keeping eap->cmd, eap->arg and
4705 * eap->nextcmd correct.
4706 * "src" points to the part that is to be replaced, of length "srclen".
4707 * "repl" is the replacement string.
4708 * Returns a pointer to the character after the replaced string.
4709 * Returns NULL for failure.
4710 */
4711 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004712repl_cmdline(
4713 exarg_T *eap,
4714 char_u *src,
4715 int srclen,
4716 char_u *repl,
4717 char_u **cmdlinep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004718{
4719 int len;
4720 int i;
4721 char_u *new_cmdline;
4722
4723 /*
4724 * The new command line is build in new_cmdline[].
4725 * First allocate it.
4726 * Careful: a "+cmd" argument may have been NUL terminated.
4727 */
4728 len = (int)STRLEN(repl);
4729 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004730 if (eap->nextcmd != NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004731 i += (int)STRLEN(eap->nextcmd);// add space for next command
Bram Moolenaar964b3742019-05-24 18:54:09 +02004732 if ((new_cmdline = alloc(i)) == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004733 return NULL; // out of memory!
Bram Moolenaar071d4272004-06-13 20:20:40 +00004734
4735 /*
4736 * Copy the stuff before the expanded part.
4737 * Copy the expanded stuff.
4738 * Copy what came after the expanded part.
4739 * Copy the next commands, if there are any.
4740 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004741 i = (int)(src - *cmdlinep); // length of part before match
Bram Moolenaar071d4272004-06-13 20:20:40 +00004742 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00004743
Bram Moolenaar071d4272004-06-13 20:20:40 +00004744 mch_memmove(new_cmdline + i, repl, (size_t)len);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004745 i += len; // remember the end of the string
Bram Moolenaar071d4272004-06-13 20:20:40 +00004746 STRCPY(new_cmdline + i, src + srclen);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004747 src = new_cmdline + i; // remember where to continue
Bram Moolenaar071d4272004-06-13 20:20:40 +00004748
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004749 if (eap->nextcmd != NULL) // append next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00004750 {
4751 i = (int)STRLEN(new_cmdline) + 1;
4752 STRCPY(new_cmdline + i, eap->nextcmd);
4753 eap->nextcmd = new_cmdline + i;
4754 }
4755 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
4756 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
4757 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
4758 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
4759 vim_free(*cmdlinep);
4760 *cmdlinep = new_cmdline;
4761
4762 return src;
4763}
4764
4765/*
4766 * Check for '|' to separate commands and '"' to start comments.
4767 */
4768 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004769separate_nextcmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770{
4771 char_u *p;
4772
Bram Moolenaar86b68352004-12-27 21:59:20 +00004773#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004774 p = skip_grep_pat(eap);
4775#else
4776 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004777#endif
4778
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004779 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004780 {
4781 if (*p == Ctrl_V)
4782 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004783 if (eap->argt & (EX_CTRLV | EX_XFILE))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004784 ++p; // skip CTRL-V and next char
Bram Moolenaar071d4272004-06-13 20:20:40 +00004785 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004786 // remove CTRL-V and skip next char
Bram Moolenaara7241f52008-06-24 20:39:31 +00004787 STRMOVE(p, p + 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004788 if (*p == NUL) // stop at NUL after CTRL-V
Bram Moolenaar071d4272004-06-13 20:20:40 +00004789 break;
4790 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004791
4792#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004793 // Skip over `=expr` when wildcards are expanded.
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004794 else if (p[0] == '`' && p[1] == '=' && (eap->argt & EX_XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004795 {
4796 p += 2;
Bram Moolenaar683581e2020-10-22 21:22:58 +02004797 (void)skip_expr(&p, NULL);
Bram Moolenaarcaf73dc2020-10-26 21:39:13 +01004798 if (*p == NUL) // stop at NUL after CTRL-V
4799 break;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004800 }
4801#endif
4802
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004803 // Check for '"': start of comment or '|': next command
4804 // :@" and :*" do not start a comment!
4805 // :redir @" doesn't either.
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004806 else if ((*p == '"'
Bram Moolenaara26b9702020-04-18 19:53:28 +02004807#ifdef FEAT_EVAL
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004808 && !in_vim9script()
4809#endif
4810 && !(eap->argt & EX_NOTRLCOM)
4811 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
4812 || p != eap->arg)
4813 && (eap->cmdidx != CMD_redir
4814 || p != eap->arg + 1 || p[-1] != '@'))
4815#ifdef FEAT_EVAL
4816 || (*p == '#'
4817 && in_vim9script()
Bram Moolenaarb8a92962020-08-20 18:02:47 +02004818 && !(eap->argt & EX_NOTRLCOM)
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004819 && p > eap->cmd && VIM_ISWHITE(p[-1]))
Bram Moolenaara26b9702020-04-18 19:53:28 +02004820#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004821 || *p == '|' || *p == '\n')
4822 {
4823 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004824 * We remove the '\' before the '|', unless EX_CTRLV is used
Bram Moolenaar071d4272004-06-13 20:20:40 +00004825 * AND 'b' is present in 'cpoptions'.
4826 */
4827 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004828 || !(eap->argt & EX_CTRLV)) && *(p - 1) == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004829 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004830 STRMOVE(p - 1, p); // remove the '\'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004831 --p;
4832 }
4833 else
4834 {
4835 eap->nextcmd = check_nextcmd(p);
4836 *p = NUL;
4837 break;
4838 }
4839 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004840 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004841
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004842 if (!(eap->argt & EX_NOTRLCOM)) // remove trailing spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00004843 del_trailing_spaces(eap->arg);
4844}
4845
4846/*
4847 * get + command from ex argument
4848 */
4849 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004850getargcmd(char_u **argp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004851{
4852 char_u *arg = *argp;
4853 char_u *command = NULL;
4854
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004855 if (*arg == '+') // +[command]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004856 {
4857 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02004858 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004859 command = dollar_command;
4860 else
4861 {
4862 command = arg;
4863 arg = skip_cmd_arg(command, TRUE);
4864 if (*arg != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004865 *arg++ = NUL; // terminate command with NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004866 }
4867
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004868 arg = skipwhite(arg); // skip over spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00004869 *argp = arg;
4870 }
4871 return command;
4872}
4873
4874/*
4875 * Find end of "+command" argument. Skip over "\ " and "\\".
4876 */
Bram Moolenaard0190392019-08-23 21:17:35 +02004877 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004878skip_cmd_arg(
4879 char_u *p,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004880 int rembs) // TRUE to halve the number of backslashes
Bram Moolenaar071d4272004-06-13 20:20:40 +00004881{
4882 while (*p && !vim_isspace(*p))
4883 {
4884 if (*p == '\\' && p[1] != NUL)
4885 {
4886 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00004887 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004888 else
4889 ++p;
4890 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004891 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004892 }
4893 return p;
4894}
4895
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004896 int
4897get_bad_opt(char_u *p, exarg_T *eap)
4898{
4899 if (STRICMP(p, "keep") == 0)
4900 eap->bad_char = BAD_KEEP;
4901 else if (STRICMP(p, "drop") == 0)
4902 eap->bad_char = BAD_DROP;
4903 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
4904 eap->bad_char = *p;
Bram Moolenaar75808492018-06-12 12:39:41 +02004905 else
4906 return FAIL;
4907 return OK;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004908}
4909
Bram Moolenaar071d4272004-06-13 20:20:40 +00004910/*
4911 * Get "++opt=arg" argument.
4912 * Return FAIL or OK.
4913 */
4914 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004915getargopt(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916{
4917 char_u *arg = eap->arg + 2;
4918 int *pp = NULL;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004919 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004920 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004921
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004922 // ":edit ++[no]bin[ary] file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004923 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
4924 {
4925 if (*arg == 'n')
4926 {
4927 arg += 2;
4928 eap->force_bin = FORCE_NOBIN;
4929 }
4930 else
4931 eap->force_bin = FORCE_BIN;
4932 if (!checkforcmd(&arg, "binary", 3))
4933 return FAIL;
4934 eap->arg = skipwhite(arg);
4935 return OK;
4936 }
4937
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004938 // ":read ++edit file"
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004939 if (STRNCMP(arg, "edit", 4) == 0)
4940 {
4941 eap->read_edit = TRUE;
4942 eap->arg = skipwhite(arg + 4);
4943 return OK;
4944 }
4945
Bram Moolenaar071d4272004-06-13 20:20:40 +00004946 if (STRNCMP(arg, "ff", 2) == 0)
4947 {
4948 arg += 2;
4949 pp = &eap->force_ff;
4950 }
4951 else if (STRNCMP(arg, "fileformat", 10) == 0)
4952 {
4953 arg += 10;
4954 pp = &eap->force_ff;
4955 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004956 else if (STRNCMP(arg, "enc", 3) == 0)
4957 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01004958 if (STRNCMP(arg, "encoding", 8) == 0)
4959 arg += 8;
4960 else
4961 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962 pp = &eap->force_enc;
4963 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004964 else if (STRNCMP(arg, "bad", 3) == 0)
4965 {
4966 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004967 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004968 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004969
4970 if (pp == NULL || *arg != '=')
4971 return FAIL;
4972
4973 ++arg;
4974 *pp = (int)(arg - eap->cmd);
4975 arg = skip_cmd_arg(arg, FALSE);
4976 eap->arg = skipwhite(arg);
4977 *arg = NUL;
4978
Bram Moolenaar071d4272004-06-13 20:20:40 +00004979 if (pp == &eap->force_ff)
4980 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
4982 return FAIL;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004983 eap->force_ff = eap->cmd[eap->force_ff];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004984 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004985 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004986 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004987 // Make 'fileencoding' lower case.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004988 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
4989 *p = TOLOWER_ASC(*p);
4990 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004991 else
4992 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004993 // Check ++bad= argument. Must be a single-byte character, "keep" or
4994 // "drop".
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004995 if (get_bad_opt(eap->cmd + bad_char_idx, eap) == FAIL)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004996 return FAIL;
4997 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004998
4999 return OK;
5000}
5001
Bram Moolenaar071d4272004-06-13 20:20:40 +00005002 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005003ex_autocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005004{
5005 /*
Bram Moolenaar26d4b892018-07-04 22:26:28 +02005006 * Disallow autocommands from .exrc and .vimrc in current
Bram Moolenaar071d4272004-06-13 20:20:40 +00005007 * directory for security reasons.
5008 */
5009 if (secure)
5010 {
5011 secure = 2;
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005012 eap->errmsg = _(e_curdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005013 }
5014 else if (eap->cmdidx == CMD_autocmd)
5015 do_autocmd(eap->arg, eap->forceit);
5016 else
5017 do_augroup(eap->arg, eap->forceit);
5018}
5019
5020/*
5021 * ":doautocmd": Apply the automatic commands to the current buffer.
5022 */
5023 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005024ex_doautocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005025{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005026 char_u *arg = eap->arg;
5027 int call_do_modelines = check_nomodeline(&arg);
Bram Moolenaar1610d052016-06-09 22:53:01 +02005028 int did_aucmd;
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005029
Bram Moolenaar1610d052016-06-09 22:53:01 +02005030 (void)do_doautocmd(arg, TRUE, &did_aucmd);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005031 // Only when there is no <nomodeline>.
Bram Moolenaar1610d052016-06-09 22:53:01 +02005032 if (call_do_modelines && did_aucmd)
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005033 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005034}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005035
Bram Moolenaar071d4272004-06-13 20:20:40 +00005036/*
5037 * :[N]bunload[!] [N] [bufname] unload buffer
5038 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
5039 * :[N]bwipeout[!] [N] [bufname] delete buffer really
5040 */
5041 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005042ex_bunload(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005043{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005044 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005045 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005046 eap->errmsg = do_bufdel(
5047 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
5048 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
5049 : DOBUF_UNLOAD, eap->arg,
5050 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
5051}
5052
5053/*
5054 * :[N]buffer [N] to buffer N
5055 * :[N]sbuffer [N] to buffer N
5056 */
5057 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005058ex_buffer(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005060 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005061 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005062 if (*eap->arg)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005063 eap->errmsg = ex_errmsg(e_trailing_arg, eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005064 else
5065 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005066 if (eap->addr_count == 0) // default is current buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00005067 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
5068 else
5069 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005070 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005071 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005072 }
5073}
5074
5075/*
5076 * :[N]bmodified [N] to next mod. buffer
5077 * :[N]sbmodified [N] to next mod. buffer
5078 */
5079 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005080ex_bmodified(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005081{
5082 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005083 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005084 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005085}
5086
5087/*
5088 * :[N]bnext [N] to next buffer
5089 * :[N]sbnext [N] split and to next buffer
5090 */
5091 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005092ex_bnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005093{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005094 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005095 return;
5096
Bram Moolenaar071d4272004-06-13 20:20:40 +00005097 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005098 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005099 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005100}
5101
5102/*
5103 * :[N]bNext [N] to previous buffer
5104 * :[N]bprevious [N] to previous buffer
5105 * :[N]sbNext [N] split and to previous buffer
5106 * :[N]sbprevious [N] split and to previous buffer
5107 */
5108 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005109ex_bprevious(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005110{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005111 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005112 return;
5113
Bram Moolenaar071d4272004-06-13 20:20:40 +00005114 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005115 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005116 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005117}
5118
5119/*
5120 * :brewind to first buffer
5121 * :bfirst to first buffer
5122 * :sbrewind split and to first buffer
5123 * :sbfirst split and to first buffer
5124 */
5125 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005126ex_brewind(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 Moolenaar3029bcc2020-01-18 15:06:19 +01005129 return;
5130
Bram Moolenaar071d4272004-06-13 20:20:40 +00005131 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005132 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005133 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005134}
5135
5136/*
5137 * :blast to last buffer
5138 * :sblast split and to last buffer
5139 */
5140 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005141ex_blast(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005142{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005143 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005144 return;
5145
Bram Moolenaar071d4272004-06-13 20:20:40 +00005146 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005147 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005148 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005149}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005150
Bram Moolenaar7a092242020-04-16 22:10:49 +02005151/*
5152 * Check if "c" ends an Ex command.
Bram Moolenaarb5b94802020-12-13 17:50:20 +01005153 * In Vim9 script does not check for white space before #.
Bram Moolenaar7a092242020-04-16 22:10:49 +02005154 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005155 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005156ends_excmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005157{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005158 int comment_char = '"';
5159
Bram Moolenaarfbda69b2020-04-13 15:06:53 +02005160#ifdef FEAT_EVAL
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005161 if (in_vim9script())
5162 comment_char = '#';
Bram Moolenaar7a092242020-04-16 22:10:49 +02005163#endif
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005164 return (c == NUL || c == '|' || c == comment_char || c == '\n');
Bram Moolenaar7a092242020-04-16 22:10:49 +02005165}
5166
5167/*
5168 * Like ends_excmd() but checks that a # in Vim9 script either has "cmd" equal
5169 * to "cmd_start" or has a white space character before it.
5170 */
5171 int
Bram Moolenaar21cfe502020-04-16 23:01:50 +02005172ends_excmd2(char_u *cmd_start UNUSED, char_u *cmd)
Bram Moolenaar7a092242020-04-16 22:10:49 +02005173{
5174 int c = *cmd;
5175
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005176 if (c == NUL || c == '|' || c == '\n')
5177 return TRUE;
Bram Moolenaar7a092242020-04-16 22:10:49 +02005178#ifdef FEAT_EVAL
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005179 if (in_vim9script())
5180 return c == '#' && cmd[1] != '{'
5181 && (cmd == cmd_start || VIM_ISWHITE(cmd[-1]));
Bram Moolenaarfbda69b2020-04-13 15:06:53 +02005182#endif
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005183 return c == '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005184}
5185
5186#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5187 || defined(PROTO)
5188/*
5189 * Return the next command, after the first '|' or '\n'.
5190 * Return NULL if not found.
5191 */
5192 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005193find_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194{
5195 while (*p != '|' && *p != '\n')
5196 {
5197 if (*p == NUL)
5198 return NULL;
5199 ++p;
5200 }
5201 return (p + 1);
5202}
5203#endif
5204
5205/*
Bram Moolenaar2256c992016-11-15 21:17:07 +01005206 * Check if *p is a separator between Ex commands, skipping over white space.
5207 * Return NULL if it isn't, the following character if it is.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005208 */
5209 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005210check_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005211{
Bram Moolenaar2256c992016-11-15 21:17:07 +01005212 char_u *s = skipwhite(p);
5213
5214 if (*s == '|' || *s == '\n')
5215 return (s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005216 else
5217 return NULL;
5218}
5219
5220/*
5221 * - if there are more files to edit
5222 * - and this is the last window
5223 * - and forceit not used
5224 * - and not repeated twice on a row
5225 * return FAIL and give error message if 'message' TRUE
5226 * return OK otherwise
5227 */
5228 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005229check_more(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005230 int message, // when FALSE check only, no messages
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005231 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005232{
5233 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5234
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005235 if (!forceit && only_one_window()
Bram Moolenaar7b221172020-08-17 19:34:10 +02005236 && ARGCOUNT > 1 && !arg_had_last && n > 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005237 {
5238 if (message)
5239 {
5240#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02005241 if ((p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM))
5242 && curbuf->b_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005244 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005245
Bram Moolenaarda6e8912018-08-21 15:12:14 +02005246 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
5247 NGETTEXT("%d more file to edit. Quit anyway?",
5248 "%d more files to edit. Quit anyway?", n), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005249 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5250 return OK;
5251 return FAIL;
5252 }
5253#endif
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01005254 semsg(NGETTEXT("E173: %d more file to edit",
5255 "E173: %d more files to edit", n), n);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005256 quitmore = 2; // next try to quit is allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00005257 }
5258 return FAIL;
5259 }
5260 return OK;
5261}
5262
Bram Moolenaar071d4272004-06-13 20:20:40 +00005263/*
5264 * Function given to ExpandGeneric() to obtain the list of command names.
5265 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005266 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005267get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005268{
5269 if (idx >= (int)CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005270 return get_user_command_name(idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005271 return cmdnames[idx].cmd_name;
5272}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005273
Bram Moolenaar071d4272004-06-13 20:20:40 +00005274 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005275ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005276{
Bram Moolenaare6850792010-05-14 15:28:44 +02005277 if (*eap->arg == NUL)
5278 {
5279#ifdef FEAT_EVAL
5280 char_u *expr = vim_strsave((char_u *)"g:colors_name");
5281 char_u *p = NULL;
5282
5283 if (expr != NULL)
5284 {
5285 ++emsg_off;
Bram Moolenaarb171fb12020-06-24 20:34:03 +02005286 p = eval_to_string(expr, FALSE);
Bram Moolenaare6850792010-05-14 15:28:44 +02005287 --emsg_off;
5288 vim_free(expr);
5289 }
5290 if (p != NULL)
5291 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005292 msg((char *)p);
Bram Moolenaare6850792010-05-14 15:28:44 +02005293 vim_free(p);
5294 }
5295 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005296 msg("default");
Bram Moolenaare6850792010-05-14 15:28:44 +02005297#else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005298 msg(_("unknown"));
Bram Moolenaare6850792010-05-14 15:28:44 +02005299#endif
5300 }
5301 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005302 semsg(_("E185: Cannot find color scheme '%s'"), eap->arg);
Bram Moolenaarf58d81a2019-01-28 20:19:05 +01005303
5304#ifdef FEAT_VTP
5305 else if (has_vtp_working())
5306 {
5307 // background color change requires clear + redraw
5308 update_screen(CLEAR);
5309 redrawcmd();
5310 }
5311#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005312}
5313
5314 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005315ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005316{
5317 if (*eap->arg == NUL && eap->cmd[2] == '!')
Bram Moolenaar32526b32019-01-19 17:43:09 +01005318 msg(_("Greetings, Vim user!"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005319 do_highlight(eap->arg, eap->forceit, FALSE);
5320}
5321
5322
5323/*
5324 * Call this function if we thought we were going to exit, but we won't
5325 * (because of an error). May need to restore the terminal mode.
5326 */
5327 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005328not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005329{
5330 exiting = FALSE;
5331 settmode(TMODE_RAW);
5332}
5333
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005334 static int
5335before_quit_autocmds(win_T *wp, int quit_all, int forceit)
5336{
5337 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, wp->w_buffer);
5338
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02005339 // Bail out when autocommands closed the window.
5340 // Refuse to quit when the buffer in the last window is being closed (can
5341 // only happen in autocommands).
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005342 if (!win_valid(wp)
5343 || curbuf_locked()
5344 || (wp->w_buffer->b_nwindows == 1 && wp->w_buffer->b_locked > 0))
5345 return TRUE;
5346
5347 if (quit_all || (check_more(FALSE, forceit) == OK && only_one_window()))
5348 {
5349 apply_autocmds(EVENT_EXITPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02005350 // Refuse to quit when locked or when the window was closed or the
5351 // buffer in the last window is being closed (can only happen in
5352 // autocommands).
5353 if (!win_valid(wp) || curbuf_locked()
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005354 || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
5355 return TRUE;
5356 }
5357
5358 return FALSE;
5359}
5360
Bram Moolenaar071d4272004-06-13 20:20:40 +00005361/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01005362 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005363 * ":{nr}quit": quit window {nr}
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02005364 * Also used when closing a terminal window that's the last one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005365 */
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02005366 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005367ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005368{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005369 win_T *wp;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005370
Bram Moolenaar071d4272004-06-13 20:20:40 +00005371#ifdef FEAT_CMDWIN
5372 if (cmdwin_type != 0)
5373 {
5374 cmdwin_result = Ctrl_C;
5375 return;
5376 }
5377#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005378 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005379 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005380 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005381 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005382 return;
5383 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005384 if (eap->addr_count > 0)
5385 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01005386 int wnr = eap->line2;
5387
5388 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
5389 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005390 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005391 }
5392 else
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005393 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01005394
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005395 // Refuse to quit when locked.
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02005396 if (curbuf_locked())
5397 return;
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005398
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005399 // Trigger QuitPre and maybe ExitPre
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005400 if (before_quit_autocmds(wp, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005401 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005402
5403#ifdef FEAT_NETBEANS_INTG
5404 netbeansForcedQuit = eap->forceit;
5405#endif
5406
5407 /*
5408 * If there are more files or windows we won't exit.
5409 */
5410 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
5411 exiting = TRUE;
Bram Moolenaarff930ca2017-10-19 17:12:10 +02005412 if ((!buf_hide(wp->w_buffer)
5413 && check_changed(wp->w_buffer, (p_awa ? CCGD_AW : 0)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01005414 | (eap->forceit ? CCGD_FORCEIT : 0)
5415 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005416 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01005417 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005418 {
5419 not_exiting();
5420 }
5421 else
5422 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005423 // quit last window
5424 // Note: only_one_window() returns true, even so a help window is
5425 // still open. In that case only quit, if no address has been
5426 // specified. Example:
5427 // :h|wincmd w|1q - don't quit
5428 // :h|wincmd w|q - quit
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01005429 if (only_one_window() && (ONE_WINDOW || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005430 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02005431 not_exiting();
Bram Moolenaar4033c552017-09-16 20:54:51 +02005432#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005433 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005434#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005435 // close window; may free buffer
Bram Moolenaareb44a682017-08-03 22:44:55 +02005436 win_close(wp, !buf_hide(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005437 }
5438}
5439
5440/*
5441 * ":cquit".
5442 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005443 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005444ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005445{
Bram Moolenaar1860bde2020-01-06 21:47:21 +01005446 // this does not always pass on the exit code to the Manx compiler. why?
5447 getout(eap->addr_count > 0 ? (int)eap->line2 : EXIT_FAILURE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005448}
5449
5450/*
5451 * ":qall": try to quit all windows
5452 */
5453 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005454ex_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005455{
5456# ifdef FEAT_CMDWIN
5457 if (cmdwin_type != 0)
5458 {
5459 if (eap->forceit)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005460 cmdwin_result = K_XF1; // ex_window() takes care of this
Bram Moolenaar071d4272004-06-13 20:20:40 +00005461 else
5462 cmdwin_result = K_XF2;
5463 return;
5464 }
5465# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005466
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005467 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005468 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005469 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005470 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005471 return;
5472 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005473
5474 if (before_quit_autocmds(curwin, TRUE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005475 return;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005476
Bram Moolenaar071d4272004-06-13 20:20:40 +00005477 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01005478 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005479 getout(0);
5480 not_exiting();
5481}
5482
Bram Moolenaar071d4272004-06-13 20:20:40 +00005483/*
5484 * ":close": close current window, unless it is the last one
5485 */
5486 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005487ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005488{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005489 win_T *win;
5490 int winnr = 0;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005491#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00005492 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02005493 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005494 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005495#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005496 if (!text_locked() && !curbuf_locked())
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005497 {
5498 if (eap->addr_count == 0)
5499 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar6d41c782018-06-06 09:11:12 +02005500 else
5501 {
Bram Moolenaar29323592016-07-24 22:04:11 +02005502 FOR_ALL_WINDOWS(win)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005503 {
5504 winnr++;
5505 if (winnr == eap->line2)
5506 break;
5507 }
5508 if (win == NULL)
5509 win = lastwin;
5510 ex_win_close(eap->forceit, win, NULL);
5511 }
5512 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005513}
5514
Bram Moolenaar4033c552017-09-16 20:54:51 +02005515#ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005516/*
5517 * ":pclose": Close any preview window.
5518 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005519 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005520ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005521{
5522 win_T *win;
5523
Bram Moolenaar79648732019-07-18 21:43:07 +02005524 // First close any normal window.
Bram Moolenaar29323592016-07-24 22:04:11 +02005525 FOR_ALL_WINDOWS(win)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005526 if (win->w_p_pvw)
5527 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005528 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar79648732019-07-18 21:43:07 +02005529 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005530 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005531# ifdef FEAT_PROP_POPUP
Bram Moolenaar79648732019-07-18 21:43:07 +02005532 // Also when 'previewpopup' is empty, it might have been cleared.
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02005533 popup_close_preview();
Bram Moolenaar79648732019-07-18 21:43:07 +02005534# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005535}
Bram Moolenaar4033c552017-09-16 20:54:51 +02005536#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005537
Bram Moolenaarf740b292006-02-16 22:11:02 +00005538/*
5539 * Close window "win" and take care of handling closing the last window for a
5540 * modified buffer.
5541 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005542 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005543ex_win_close(
5544 int forceit,
5545 win_T *win,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005546 tabpage_T *tp) // NULL or the tab page "win" is in
Bram Moolenaar071d4272004-06-13 20:20:40 +00005547{
5548 int need_hide;
5549 buf_T *buf = win->w_buffer;
5550
Bram Moolenaarcf844172020-06-26 19:44:06 +02005551 // Never close the autocommand window.
5552 if (win == aucmd_win)
5553 {
5554 emsg(_(e_autocmd_close));
5555 return;
5556 }
5557
Bram Moolenaar071d4272004-06-13 20:20:40 +00005558 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02005559 if (need_hide && !buf_hide(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005560 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02005561#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02005562 if ((p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM)) && p_write)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005563 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005564 bufref_T bufref;
5565
5566 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005567 dialog_changed(buf, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005568 if (bufref_valid(&bufref) && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005569 return;
5570 need_hide = FALSE;
5571 }
5572 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005573#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005574 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02005575 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005576 return;
5577 }
5578 }
5579
Bram Moolenaar4033c552017-09-16 20:54:51 +02005580#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005581 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005582#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005583
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005584 // free buffer when not hiding it or when it's a scratch buffer
Bram Moolenaarf740b292006-02-16 22:11:02 +00005585 if (tp == NULL)
Bram Moolenaareb44a682017-08-03 22:44:55 +02005586 win_close(win, !need_hide && !buf_hide(buf));
Bram Moolenaarf740b292006-02-16 22:11:02 +00005587 else
Bram Moolenaareb44a682017-08-03 22:44:55 +02005588 win_close_othertab(win, !need_hide && !buf_hide(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005589}
5590
Bram Moolenaar071d4272004-06-13 20:20:40 +00005591/*
Bram Moolenaardea25702017-01-29 15:18:10 +01005592 * Handle the argument for a tabpage related ex command.
5593 * Returns a tabpage number.
5594 * When an error is encountered then eap->errmsg is set.
5595 */
5596 static int
5597get_tabpage_arg(exarg_T *eap)
5598{
5599 int tab_number;
5600 int unaccept_arg0 = (eap->cmdidx == CMD_tabmove) ? 0 : 1;
5601
5602 if (eap->arg && *eap->arg != NUL)
5603 {
5604 char_u *p = eap->arg;
5605 char_u *p_save;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005606 int relative = 0; // argument +N/-N means: go to N places to the
5607 // right/left relative to the current position.
Bram Moolenaardea25702017-01-29 15:18:10 +01005608
5609 if (*p == '-')
5610 {
5611 relative = -1;
5612 p++;
5613 }
5614 else if (*p == '+')
5615 {
5616 relative = 1;
5617 p++;
5618 }
5619
5620 p_save = p;
5621 tab_number = getdigits(&p);
5622
5623 if (relative == 0)
5624 {
5625 if (STRCMP(p, "$") == 0)
5626 tab_number = LAST_TAB_NR;
Bram Moolenaar94f4ffa2020-08-10 19:21:15 +02005627 else if (STRCMP(p, "#") == 0)
5628 if (valid_tabpage(lastused_tabpage))
5629 tab_number = tabpage_index(lastused_tabpage);
5630 else
5631 {
5632 eap->errmsg = ex_errmsg(e_invargval, eap->arg);
5633 tab_number = 0;
5634 goto theend;
5635 }
Bram Moolenaardea25702017-01-29 15:18:10 +01005636 else if (p == p_save || *p_save == '-' || *p != NUL
5637 || tab_number > LAST_TAB_NR)
5638 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005639 // No numbers as argument.
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005640 eap->errmsg = ex_errmsg(e_invarg2, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01005641 goto theend;
5642 }
5643 }
5644 else
5645 {
5646 if (*p_save == NUL)
5647 tab_number = 1;
5648 else if (p == p_save || *p_save == '-' || *p != NUL
5649 || tab_number == 0)
5650 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005651 // No numbers as argument.
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005652 eap->errmsg = ex_errmsg(e_invarg2, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01005653 goto theend;
5654 }
5655 tab_number = tab_number * relative + tabpage_index(curtab);
5656 if (!unaccept_arg0 && relative == -1)
5657 --tab_number;
5658 }
5659 if (tab_number < unaccept_arg0 || tab_number > LAST_TAB_NR)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005660 eap->errmsg = ex_errmsg(e_invarg2, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01005661 }
5662 else if (eap->addr_count > 0)
5663 {
5664 if (unaccept_arg0 && eap->line2 == 0)
Bram Moolenaarc6251552017-01-29 21:42:20 +01005665 {
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005666 eap->errmsg = _(e_invrange);
Bram Moolenaarc6251552017-01-29 21:42:20 +01005667 tab_number = 0;
5668 }
Bram Moolenaardea25702017-01-29 15:18:10 +01005669 else
5670 {
5671 tab_number = eap->line2;
Bram Moolenaar82a12462019-01-22 22:41:42 +01005672 if (!unaccept_arg0 && *skipwhite(*eap->cmdlinep) == '-')
Bram Moolenaardea25702017-01-29 15:18:10 +01005673 {
5674 --tab_number;
5675 if (tab_number < unaccept_arg0)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005676 eap->errmsg = _(e_invrange);
Bram Moolenaardea25702017-01-29 15:18:10 +01005677 }
5678 }
5679 }
5680 else
5681 {
5682 switch (eap->cmdidx)
5683 {
5684 case CMD_tabnext:
5685 tab_number = tabpage_index(curtab) + 1;
5686 if (tab_number > LAST_TAB_NR)
5687 tab_number = 1;
5688 break;
5689 case CMD_tabmove:
5690 tab_number = LAST_TAB_NR;
5691 break;
5692 default:
5693 tab_number = tabpage_index(curtab);
5694 }
5695 }
5696
5697theend:
5698 return tab_number;
5699}
5700
5701/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00005702 * ":tabclose": close current tab page, unless it is the last one.
5703 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005704 */
5705 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005706ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005707{
Bram Moolenaarf740b292006-02-16 22:11:02 +00005708 tabpage_T *tp;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005709 int tab_number;
Bram Moolenaarf740b292006-02-16 22:11:02 +00005710
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005711# ifdef FEAT_CMDWIN
5712 if (cmdwin_type != 0)
5713 cmdwin_result = K_IGNORE;
5714 else
5715# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005716 if (first_tabpage->tp_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005717 emsg(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00005718 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005719 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005720 tab_number = get_tabpage_arg(eap);
5721 if (eap->errmsg == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005722 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005723 tp = find_tabpage(tab_number);
Bram Moolenaarf740b292006-02-16 22:11:02 +00005724 if (tp == NULL)
5725 {
5726 beep_flush();
5727 return;
5728 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005729 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005730 {
5731 tabpage_close_other(tp, eap->forceit);
5732 return;
5733 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005734 else if (!text_locked() && !curbuf_locked())
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005735 tabpage_close(eap->forceit);
5736 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005737 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005738}
5739
5740/*
5741 * ":tabonly": close all tab pages except the current one
5742 */
5743 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005744ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005745{
5746 tabpage_T *tp;
5747 int done;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005748 int tab_number;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005749
5750# ifdef FEAT_CMDWIN
5751 if (cmdwin_type != 0)
5752 cmdwin_result = K_IGNORE;
5753 else
5754# endif
5755 if (first_tabpage->tp_next == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005756 msg(_("Already only one tab page"));
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005757 else
5758 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005759 tab_number = get_tabpage_arg(eap);
5760 if (eap->errmsg == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005761 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005762 goto_tabpage(tab_number);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005763 // Repeat this up to a 1000 times, because autocommands may
5764 // mess up the lists.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005765 for (done = 0; done < 1000; ++done)
5766 {
5767 FOR_ALL_TABPAGES(tp)
5768 if (tp->tp_topframe != topframe)
5769 {
5770 tabpage_close_other(tp, eap->forceit);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005771 // if we failed to close it quit
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005772 if (valid_tabpage(tp))
5773 done = 1000;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005774 // start over, "tp" is now invalid
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005775 break;
5776 }
5777 if (first_tabpage->tp_next == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005778 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005779 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005780 }
5781 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005782}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005783
5784/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00005785 * Close the current tab page.
5786 */
5787 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005788tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005789{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005790 // First close all the windows but the current one. If that worked then
5791 // close the last window in this tab, that will close it.
Bram Moolenaar459ca562016-11-10 18:16:33 +01005792 if (!ONE_WINDOW)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005793 close_others(TRUE, forceit);
Bram Moolenaar459ca562016-11-10 18:16:33 +01005794 if (ONE_WINDOW)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005795 ex_win_close(forceit, curwin, NULL);
5796# ifdef FEAT_GUI
5797 need_mouse_correct = TRUE;
5798# endif
5799}
5800
5801/*
5802 * Close tab page "tp", which is not the current tab page.
5803 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005804 * Also takes care of the tab pages line disappearing when closing the
5805 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00005806 */
5807 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005808tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005809{
5810 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005811 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005812 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00005813
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005814 // Limit to 1000 windows, autocommands may add a window while we close
5815 // one. OK, so I'm paranoid...
Bram Moolenaarf740b292006-02-16 22:11:02 +00005816 while (++done < 1000)
5817 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005818 wp = tp->tp_firstwin;
5819 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00005820
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005821 // Autocommands may delete the tab page under our fingers and we may
5822 // fail to close a window with a modified buffer.
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005823 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005824 break;
5825 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005826
Bram Moolenaar29323592016-07-24 22:04:11 +02005827 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar29323592016-07-24 22:04:11 +02005828
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005829 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005830 if (h != tabline_height())
5831 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00005832}
5833
5834/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005835 * ":only".
5836 */
5837 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005838ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005839{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005840 win_T *wp;
5841 int wnr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005842# ifdef FEAT_GUI
5843 need_mouse_correct = TRUE;
5844# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005845 if (eap->addr_count > 0)
5846 {
5847 wnr = eap->line2;
5848 for (wp = firstwin; --wnr > 0; )
5849 {
5850 if (wp->w_next == NULL)
5851 break;
5852 else
5853 wp = wp->w_next;
5854 }
5855 win_goto(wp);
5856 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005857 close_others(TRUE, eap->forceit);
5858}
5859
Bram Moolenaar071d4272004-06-13 20:20:40 +00005860 static void
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01005861ex_hide(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005862{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005863 // ":hide" or ":hide | cmd": hide current window
Bram Moolenaar2256c992016-11-15 21:17:07 +01005864 if (!eap->skip)
5865 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02005866#ifdef FEAT_GUI
Bram Moolenaar2256c992016-11-15 21:17:07 +01005867 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005868#endif
Bram Moolenaar2256c992016-11-15 21:17:07 +01005869 if (eap->addr_count == 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005870 win_close(curwin, FALSE); // don't free buffer
Bram Moolenaar2256c992016-11-15 21:17:07 +01005871 else
5872 {
5873 int winnr = 0;
5874 win_T *win;
Bram Moolenaarf240e182014-11-27 18:33:02 +01005875
Bram Moolenaar2256c992016-11-15 21:17:07 +01005876 FOR_ALL_WINDOWS(win)
5877 {
5878 winnr++;
5879 if (winnr == eap->line2)
5880 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005881 }
Bram Moolenaar2256c992016-11-15 21:17:07 +01005882 if (win == NULL)
5883 win = lastwin;
5884 win_close(win, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005885 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005886 }
5887}
5888
5889/*
5890 * ":stop" and ":suspend": Suspend Vim.
5891 */
5892 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005893ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005894{
5895 /*
5896 * Disallow suspending for "rvim".
5897 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005898 if (!check_restricted())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005899 {
5900 if (!eap->forceit)
5901 autowrite_all();
Bram Moolenaar100118c2020-12-11 19:30:34 +01005902 apply_autocmds(EVENT_VIMSUSPEND, NULL, NULL, FALSE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005903 windgoto((int)Rows - 1, 0);
5904 out_char('\n');
5905 out_flush();
5906 stoptermcap();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005907 out_flush(); // needed for SUN to restore xterm buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00005908#ifdef FEAT_TITLE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005909 mch_restore_title(SAVE_RESTORE_BOTH); // restore window titles
Bram Moolenaar071d4272004-06-13 20:20:40 +00005910#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005911 ui_suspend(); // call machine specific function
Bram Moolenaar071d4272004-06-13 20:20:40 +00005912#ifdef FEAT_TITLE
5913 maketitle();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005914 resettitle(); // force updating the title
Bram Moolenaar071d4272004-06-13 20:20:40 +00005915#endif
5916 starttermcap();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005917 scroll_start(); // scroll screen before redrawing
Bram Moolenaar071d4272004-06-13 20:20:40 +00005918 redraw_later_clear();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005919 shell_resized(); // may have resized window
Bram Moolenaar100118c2020-12-11 19:30:34 +01005920 apply_autocmds(EVENT_VIMRESUME, NULL, NULL, FALSE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005921 }
5922}
5923
5924/*
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005925 * ":exit", ":xit" and ":wq": Write file and quite the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005926 */
5927 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005928ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005929{
Bram Moolenaar461f2122020-07-28 20:25:47 +02005930#ifdef FEAT_EVAL
Bram Moolenaarae616492020-07-28 20:07:27 +02005931 if (not_in_vim9(eap) == FAIL)
5932 return;
Bram Moolenaar461f2122020-07-28 20:25:47 +02005933#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005934#ifdef FEAT_CMDWIN
5935 if (cmdwin_type != 0)
5936 {
5937 cmdwin_result = Ctrl_C;
5938 return;
5939 }
5940#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005941 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005942 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005943 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005944 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005945 return;
5946 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005947
5948 if (before_quit_autocmds(curwin, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005949 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005950
5951 /*
5952 * if more files or windows we won't exit
5953 */
5954 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
5955 exiting = TRUE;
5956 if ( ((eap->cmdidx == CMD_wq
5957 || curbufIsChanged())
5958 && do_write(eap) == FAIL)
5959 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01005960 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005961 {
5962 not_exiting();
5963 }
5964 else
5965 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005966 if (only_one_window()) // quit last window, exit Vim
Bram Moolenaar071d4272004-06-13 20:20:40 +00005967 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02005968 not_exiting();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005969# ifdef FEAT_GUI
5970 need_mouse_correct = TRUE;
5971# endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005972 // Quit current window, may free the buffer.
Bram Moolenaareb44a682017-08-03 22:44:55 +02005973 win_close(curwin, !buf_hide(curwin->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005974 }
5975}
5976
5977/*
5978 * ":print", ":list", ":number".
5979 */
5980 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005981ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005982{
Bram Moolenaardf177f62005-02-22 08:39:57 +00005983 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005984 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +00005985 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005986 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00005987 for ( ;!got_int; ui_breakcheck())
5988 {
5989 print_line(eap->line1,
5990 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
5991 || (eap->flags & EXFLAG_NR)),
5992 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
5993 if (++eap->line1 > eap->line2)
5994 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005995 out_flush(); // show one line at a time
Bram Moolenaardf177f62005-02-22 08:39:57 +00005996 }
5997 setpcmark();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005998 // put cursor at last line
Bram Moolenaardf177f62005-02-22 08:39:57 +00005999 curwin->w_cursor.lnum = eap->line2;
6000 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006001 }
6002
Bram Moolenaar071d4272004-06-13 20:20:40 +00006003 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006004}
6005
6006#ifdef FEAT_BYTEOFF
6007 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006008ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006009{
6010 goto_byte(eap->line2);
6011}
6012#endif
6013
6014/*
6015 * ":shell".
6016 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006017 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006018ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006019{
6020 do_shell(NULL, 0);
6021}
6022
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006023#if defined(HAVE_DROP_FILE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006024
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006025static int drop_busy = FALSE;
6026static int drop_filec;
6027static char_u **drop_filev = NULL;
6028static int drop_split;
6029static void (*drop_callback)(void *);
6030static void *drop_cookie;
6031
6032 static void
6033handle_drop_internal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006034{
6035 exarg_T ea;
6036 int save_msg_scroll = msg_scroll;
6037
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006038 // Setting the argument list may cause screen updates and being called
6039 // recursively. Avoid that by setting drop_busy.
6040 drop_busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006041
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006042 // Check whether the current buffer is changed. If so, we will need
6043 // to split the current window or data could be lost.
6044 // We don't need to check if the 'hidden' option is set, as in this
6045 // case the buffer won't be lost.
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006046 if (!buf_hide(curbuf) && !drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006047 {
6048 ++emsg_off;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006049 drop_split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006050 --emsg_off;
6051 }
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006052 if (drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006053 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006054 if (win_split(0, 0) == FAIL)
6055 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006056 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006057
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006058 // When splitting the window, create a new alist. Otherwise the
6059 // existing one is overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006060 alist_unlink(curwin->w_alist);
6061 alist_new();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006062 }
6063
6064 /*
6065 * Set up the new argument list.
6066 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006067 alist_set(ALIST(curwin), drop_filec, drop_filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006068
6069 /*
6070 * Move to the first file.
6071 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006072 // Fake up a minimal "next" command for do_argfile()
Bram Moolenaara80faa82020-04-12 19:37:17 +02006073 CLEAR_FIELD(ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006074 ea.cmd = (char_u *)"next";
6075 do_argfile(&ea, 0);
6076
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006077 // do_ecmd() may set need_start_insertmode, but since we never left Insert
6078 // mode that is not needed here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006079 need_start_insertmode = FALSE;
6080
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006081 // Restore msg_scroll, otherwise a following command may cause scrolling
6082 // unexpectedly. The screen will be redrawn by the caller, thus
6083 // msg_scroll being set by displaying a message is irrelevant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006084 msg_scroll = save_msg_scroll;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006085
6086 if (drop_callback != NULL)
6087 drop_callback(drop_cookie);
6088
6089 drop_filev = NULL;
6090 drop_busy = FALSE;
6091}
6092
6093/*
6094 * Handle a file drop. The code is here because a drop is *nearly* like an
6095 * :args command, but not quite (we have a list of exact filenames, so we
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01006096 * don't want to (a) parse a command line, or (b) expand wildcards). So the
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006097 * code is very similar to :args and hence needs access to a lot of the static
6098 * functions in this file.
6099 *
6100 * The "filev" list must have been allocated using alloc(), as should each item
6101 * in the list. This function takes over responsibility for freeing the "filev"
6102 * list.
6103 */
6104 void
6105handle_drop(
6106 int filec, // the number of files dropped
6107 char_u **filev, // the list of files dropped
6108 int split, // force splitting the window
6109 void (*callback)(void *), // to be called after setting the argument
6110 // list
6111 void *cookie) // argument for "callback" (allocated)
6112{
6113 // Cannot handle recursive drops, finish the pending one.
6114 if (drop_busy)
6115 {
6116 FreeWild(filec, filev);
6117 vim_free(cookie);
6118 return;
6119 }
6120
6121 // When calling handle_drop() more than once in a row we only use the last
6122 // one.
6123 if (drop_filev != NULL)
6124 {
6125 FreeWild(drop_filec, drop_filev);
6126 vim_free(drop_cookie);
6127 }
6128
6129 drop_filec = filec;
6130 drop_filev = filev;
6131 drop_split = split;
6132 drop_callback = callback;
6133 drop_cookie = cookie;
6134
6135 // Postpone this when:
6136 // - editing the command line
6137 // - not possible to change the current buffer
6138 // - updating the screen
6139 // As it may change buffers and window structures that are in use and cause
6140 // freed memory to be used.
6141 if (text_locked() || curbuf_locked() || updating_screen)
6142 return;
6143
6144 handle_drop_internal();
6145}
6146
6147/*
6148 * To be called when text is unlocked, curbuf is unlocked or updating_screen is
6149 * reset: Handle a postponed drop.
6150 */
6151 void
6152handle_any_postponed_drop(void)
6153{
6154 if (!drop_busy && drop_filev != NULL
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02006155 && !text_locked() && !curbuf_locked() && !updating_screen)
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006156 handle_drop_internal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006157}
6158#endif
6159
Bram Moolenaar071d4272004-06-13 20:20:40 +00006160/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006161 * ":preserve".
6162 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006163 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006164ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006165{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00006166 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006167 ml_preserve(curbuf, TRUE);
6168}
6169
6170/*
6171 * ":recover".
6172 */
6173 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006174ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006175{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006176 // Set recoverymode right away to avoid the ATTENTION prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006177 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01006178 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
6179 | CCGD_MULTWIN
6180 | (eap->forceit ? CCGD_FORCEIT : 0)
6181 | CCGD_EXCMD)
6182
Bram Moolenaar071d4272004-06-13 20:20:40 +00006183 && (*eap->arg == NUL
6184 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
Bram Moolenaar99499b12019-05-23 21:35:48 +02006185 ml_recover(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006186 recoverymode = FALSE;
6187}
6188
6189/*
6190 * Command modifier used in a wrong way.
6191 */
6192 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006193ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006194{
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006195 eap->errmsg = _(e_invalid_command);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006196}
6197
Bram Moolenaar071d4272004-06-13 20:20:40 +00006198/*
6199 * :sview [+command] file split window with new file, read-only
6200 * :split [[+command] file] split window with current or new file
6201 * :vsplit [[+command] file] split window vertically with current or new file
6202 * :new [[+command] file] split window with no or new file
6203 * :vnew [[+command] file] split vertically window with no or new file
6204 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006205 *
6206 * :tabedit open new Tab page with empty window
6207 * :tabedit [+command] file open new Tab page and edit "file"
6208 * :tabnew [[+command] file] just like :tabedit
6209 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006210 */
6211 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006212ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006213{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006214 win_T *old_curwin = curwin;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006215#if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006216 char_u *fname = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006217#endif
6218#ifdef FEAT_BROWSE
Bram Moolenaar21cbe172020-10-13 19:08:24 +02006219 char_u dot_path[] = ".";
Bram Moolenaare1004402020-10-24 20:49:43 +02006220 int save_cmod_flags = cmdmod.cmod_flags;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006221#endif
Bram Moolenaar39902a02018-06-21 22:10:08 +02006222 int use_tab = eap->cmdidx == CMD_tabedit
6223 || eap->cmdidx == CMD_tabfind
6224 || eap->cmdidx == CMD_tabnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006225
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01006226 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006227 return;
6228
Bram Moolenaar4033c552017-09-16 20:54:51 +02006229#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006230 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006231#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006232
Bram Moolenaar4033c552017-09-16 20:54:51 +02006233#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006234 // A ":split" in the quickfix window works like ":new". Don't want two
6235 // quickfix windows. But it's OK when doing ":tab split".
Bram Moolenaare1004402020-10-24 20:49:43 +02006236 if (bt_quickfix(curbuf) && cmdmod.cmod_tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006237 {
6238 if (eap->cmdidx == CMD_split)
6239 eap->cmdidx = CMD_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006240 if (eap->cmdidx == CMD_vsplit)
6241 eap->cmdidx = CMD_vnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006242 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006243#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006244
Bram Moolenaar4033c552017-09-16 20:54:51 +02006245#ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006246 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006247 {
6248 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
6249 FNAME_MESS, TRUE, curbuf->b_ffname);
6250 if (fname == NULL)
6251 goto theend;
6252 eap->arg = fname;
6253 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006254# ifdef FEAT_BROWSE
Bram Moolenaar4033c552017-09-16 20:54:51 +02006255 else
6256# endif
6257#endif
6258#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02006259 if ((cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006260 && eap->cmdidx != CMD_vnew
Bram Moolenaar071d4272004-06-13 20:20:40 +00006261 && eap->cmdidx != CMD_new)
6262 {
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006263 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006264# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006265 !gui.in_use &&
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006266# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006267 au_has_group((char_u *)"FileExplorer"))
6268 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006269 // No browsing supported but we do have the file explorer:
6270 // Edit the directory.
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006271 if (*eap->arg == NUL || !mch_isdir(eap->arg))
Bram Moolenaar21cbe172020-10-13 19:08:24 +02006272 eap->arg = dot_path;
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006273 }
6274 else
6275 {
Bram Moolenaar39902a02018-06-21 22:10:08 +02006276 fname = do_browse(0, (char_u *)(use_tab
6277 ? _("Edit File in new tab page")
6278 : _("Edit File in new window")),
Bram Moolenaar071d4272004-06-13 20:20:40 +00006279 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006280 if (fname == NULL)
6281 goto theend;
6282 eap->arg = fname;
6283 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006284 }
Bram Moolenaare1004402020-10-24 20:49:43 +02006285 cmdmod.cmod_flags &= ~CMOD_BROWSE; // Don't browse again in do_ecmd().
Bram Moolenaar4033c552017-09-16 20:54:51 +02006286#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006287
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006288 /*
6289 * Either open new tab page or split the window.
6290 */
Bram Moolenaar39902a02018-06-21 22:10:08 +02006291 if (use_tab)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006292 {
Bram Moolenaare1004402020-10-24 20:49:43 +02006293 if (win_new_tabpage(cmdmod.cmod_tab != 0 ? cmdmod.cmod_tab
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006294 : eap->addr_count == 0 ? 0
6295 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006296 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00006297 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006298
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006299 // set the alternate buffer for the window we came from
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006300 if (curwin != old_curwin
6301 && win_valid(old_curwin)
6302 && old_curwin->w_buffer != curbuf
Bram Moolenaare1004402020-10-24 20:49:43 +02006303 && (cmdmod.cmod_flags & CMOD_KEEPALT) == 0)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006304 old_curwin->w_alt_fnum = curbuf->b_fnum;
6305 }
6306 }
6307 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006308 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
6309 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006310 // Reset 'scrollbind' when editing another file, but keep it when
6311 // doing ":split" without arguments.
Bram Moolenaare1004402020-10-24 20:49:43 +02006312 if (*eap->arg != NUL)
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006313 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006314 else
6315 do_check_scrollbind(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006316 do_exedit(eap, old_curwin);
6317 }
6318
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006319# ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02006320 cmdmod.cmod_flags = save_cmod_flags;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006321# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006322
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006323# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006324theend:
6325 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006326# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006327}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006328
6329/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006330 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006331 */
6332 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006333tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006334{
6335 exarg_T ea;
6336
Bram Moolenaara80faa82020-04-12 19:37:17 +02006337 CLEAR_FIELD(ea);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006338 ea.cmdidx = CMD_tabnew;
6339 ea.cmd = (char_u *)"tabn";
6340 ea.arg = (char_u *)"";
6341 ex_splitview(&ea);
6342}
6343
6344/*
6345 * :tabnext command
6346 */
6347 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006348ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006349{
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006350 int tab_number;
6351
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02006352 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006353 return;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006354 switch (eap->cmdidx)
6355 {
6356 case CMD_tabfirst:
6357 case CMD_tabrewind:
6358 goto_tabpage(1);
6359 break;
6360 case CMD_tablast:
6361 goto_tabpage(9999);
6362 break;
6363 case CMD_tabprevious:
6364 case CMD_tabNext:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006365 if (eap->arg && *eap->arg != NUL)
6366 {
6367 char_u *p = eap->arg;
6368 char_u *p_save = p;
6369
6370 tab_number = getdigits(&p);
6371 if (p == p_save || *p_save == '-' || *p != NUL
6372 || tab_number == 0)
6373 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006374 // No numbers as argument.
Bram Moolenaar8930caa2020-07-23 16:37:03 +02006375 eap->errmsg = ex_errmsg(e_invarg2, eap->arg);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006376 return;
6377 }
6378 }
6379 else
6380 {
6381 if (eap->addr_count == 0)
6382 tab_number = 1;
6383 else
6384 {
6385 tab_number = eap->line2;
6386 if (tab_number < 1)
6387 {
Bram Moolenaar8930caa2020-07-23 16:37:03 +02006388 eap->errmsg = _(e_invrange);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006389 return;
6390 }
6391 }
6392 }
6393 goto_tabpage(-tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006394 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006395 default: // CMD_tabnext
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006396 tab_number = get_tabpage_arg(eap);
6397 if (eap->errmsg == NULL)
6398 goto_tabpage(tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006399 break;
6400 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006401}
6402
6403/*
6404 * :tabmove command
6405 */
6406 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006407ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006408{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02006409 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02006410
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006411 tab_number = get_tabpage_arg(eap);
6412 if (eap->errmsg == NULL)
6413 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006414}
6415
6416/*
6417 * :tabs command: List tabs and their contents.
6418 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006419 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006420ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006421{
6422 tabpage_T *tp;
6423 win_T *wp;
6424 int tabcount = 1;
6425
6426 msg_start();
6427 msg_scroll = TRUE;
6428 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
6429 {
6430 msg_putchar('\n');
6431 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
Bram Moolenaar8820b482017-03-16 17:23:31 +01006432 msg_outtrans_attr(IObuff, HL_ATTR(HLF_T));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006433 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006434 ui_breakcheck();
6435
Bram Moolenaar030f0df2006-02-21 22:02:53 +00006436 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006437 wp = firstwin;
6438 else
6439 wp = tp->tp_firstwin;
6440 for ( ; wp != NULL && !got_int; wp = wp->w_next)
6441 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006442 msg_putchar('\n');
6443 msg_putchar(wp == curwin ? '>' : ' ');
6444 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006445 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
6446 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006447 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02006448 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006449 else
6450 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
6451 IObuff, IOSIZE, TRUE);
6452 msg_outtrans(IObuff);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006453 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006454 ui_breakcheck();
6455 }
6456 }
6457}
6458
Bram Moolenaar071d4272004-06-13 20:20:40 +00006459/*
6460 * ":mode": Set screen mode.
6461 * If no argument given, just get the screen size and redraw.
6462 */
6463 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006464ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006465{
6466 if (*eap->arg == NUL)
6467 shell_resized();
6468 else
Bram Moolenaar3c71aec2020-01-17 19:32:20 +01006469 emsg(_(e_screenmode));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006470}
6471
Bram Moolenaar071d4272004-06-13 20:20:40 +00006472/*
6473 * ":resize".
6474 * set, increment or decrement current window height
6475 */
6476 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006477ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006478{
6479 int n;
6480 win_T *wp = curwin;
6481
6482 if (eap->addr_count > 0)
6483 {
6484 n = eap->line2;
6485 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
6486 ;
6487 }
6488
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006489# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006490 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006491# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006492 n = atol((char *)eap->arg);
Bram Moolenaare1004402020-10-24 20:49:43 +02006493 if (cmdmod.cmod_split & WSP_VERT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006494 {
6495 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar9668cc52020-10-17 17:39:55 +02006496 n += wp->w_width;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006497 else if (n == 0 && eap->arg[0] == NUL) // default is very wide
Bram Moolenaar071d4272004-06-13 20:20:40 +00006498 n = 9999;
6499 win_setwidth_win((int)n, wp);
6500 }
6501 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006502 {
6503 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar9668cc52020-10-17 17:39:55 +02006504 n += wp->w_height;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006505 else if (n == 0 && eap->arg[0] == NUL) // default is very high
Bram Moolenaar071d4272004-06-13 20:20:40 +00006506 n = 9999;
6507 win_setheight_win((int)n, wp);
6508 }
6509}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006510
6511/*
6512 * ":find [+command] <file>" command.
6513 */
6514 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006515ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006516{
6517#ifdef FEAT_SEARCHPATH
6518 char_u *fname;
6519 int count;
6520
6521 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
6522 TRUE, curbuf->b_ffname);
6523 if (eap->addr_count > 0)
6524 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006525 // Repeat finding the file "count" times. This matters when it
6526 // appears several times in the path.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006527 count = eap->line2;
6528 while (fname != NULL && --count > 0)
6529 {
6530 vim_free(fname);
6531 fname = find_file_in_path(NULL, 0, FNAME_MESS,
6532 FALSE, curbuf->b_ffname);
6533 }
6534 }
6535
6536 if (fname != NULL)
6537 {
6538 eap->arg = fname;
6539#endif
6540 do_exedit(eap, NULL);
6541#ifdef FEAT_SEARCHPATH
6542 vim_free(fname);
6543 }
6544#endif
6545}
6546
6547/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00006548 * ":open" simulation: for now just work like ":visual".
6549 */
6550 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006551ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006552{
6553 regmatch_T regmatch;
6554 char_u *p;
6555
6556 curwin->w_cursor.lnum = eap->line2;
6557 beginline(BL_SOL | BL_FIX);
6558 if (*eap->arg == '/')
6559 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006560 // ":open /pattern/": put cursor in column found with pattern
Bram Moolenaardf177f62005-02-22 08:39:57 +00006561 ++eap->arg;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01006562 p = skip_regexp(eap->arg, '/', magic_isset());
Bram Moolenaardf177f62005-02-22 08:39:57 +00006563 *p = NUL;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01006564 regmatch.regprog = vim_regcomp(eap->arg, magic_isset() ? RE_MAGIC : 0);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006565 if (regmatch.regprog != NULL)
6566 {
6567 regmatch.rm_ic = p_ic;
6568 p = ml_get_curline();
6569 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006570 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006571 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006572 emsg(_(e_nomatch));
Bram Moolenaar473de612013-06-08 18:19:48 +02006573 vim_regfree(regmatch.regprog);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006574 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006575 // Move to the NUL, ignore any other arguments.
Bram Moolenaardf177f62005-02-22 08:39:57 +00006576 eap->arg += STRLEN(eap->arg);
6577 }
6578 check_cursor();
6579
6580 eap->cmdidx = CMD_visual;
6581 do_exedit(eap, NULL);
6582}
6583
6584/*
Bram Moolenaar59d8e562020-11-07 18:41:10 +01006585 * ":edit", ":badd", ":balt", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006586 */
6587 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006588ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006589{
6590 do_exedit(eap, NULL);
6591}
6592
6593/*
Bram Moolenaar4b96df52020-01-26 22:00:26 +01006594 * ":edit <file>" command and alike.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006595 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006596 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006597do_exedit(
6598 exarg_T *eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006599 win_T *old_curwin) // curwin before doing a split or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006600{
6601 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006602 int need_hide;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006603 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006604
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01006605 if ((eap->cmdidx != CMD_pedit && ERROR_IF_POPUP_WINDOW)
6606 || ERROR_IF_TERM_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006607 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006608 /*
6609 * ":vi" command ends Ex mode.
6610 */
6611 if (exmode_active && (eap->cmdidx == CMD_visual
6612 || eap->cmdidx == CMD_view))
6613 {
6614 exmode_active = FALSE;
Bram Moolenaar158ea172020-06-18 17:28:39 +02006615 ex_pressedreturn = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006616 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006617 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006618 // Special case: ":global/pat/visual\NLvi-commands"
Bram Moolenaardf177f62005-02-22 08:39:57 +00006619 if (global_busy)
6620 {
6621 int rd = RedrawingDisabled;
6622 int nwr = no_wait_return;
6623 int ms = msg_scroll;
6624#ifdef FEAT_GUI
6625 int he = hold_gui_events;
6626#endif
6627
6628 if (eap->nextcmd != NULL)
6629 {
6630 stuffReadbuff(eap->nextcmd);
6631 eap->nextcmd = NULL;
6632 }
6633
6634 if (exmode_was != EXMODE_VIM)
6635 settmode(TMODE_RAW);
6636 RedrawingDisabled = 0;
6637 no_wait_return = 0;
6638 need_wait_return = FALSE;
6639 msg_scroll = 0;
6640#ifdef FEAT_GUI
6641 hold_gui_events = 0;
6642#endif
6643 must_redraw = CLEAR;
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01006644 pending_exmode_active = TRUE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006645
6646 main_loop(FALSE, TRUE);
6647
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01006648 pending_exmode_active = FALSE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006649 RedrawingDisabled = rd;
6650 no_wait_return = nwr;
6651 msg_scroll = ms;
6652#ifdef FEAT_GUI
6653 hold_gui_events = he;
6654#endif
6655 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006656 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006657 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006658 }
6659
6660 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006661 || eap->cmdidx == CMD_tabnew
6662 || eap->cmdidx == CMD_tabedit
Bram Moolenaar4033c552017-09-16 20:54:51 +02006663 || eap->cmdidx == CMD_vnew) && *eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006664 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006665 // ":new" or ":tabnew" without argument: edit an new empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006666 setpcmark();
6667 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006668 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
6669 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006670 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006671 else if ((eap->cmdidx != CMD_split && eap->cmdidx != CMD_vsplit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006672 || *eap->arg != NUL
6673#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02006674 || (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006675#endif
6676 )
6677 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006678 // Can't edit another file when "curbuf_lock" is set. Only ":edit"
6679 // can bring us here, others are stopped earlier.
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006680 if (*eap->arg != NUL && curbuf_locked())
6681 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006682
Bram Moolenaar071d4272004-06-13 20:20:40 +00006683 n = readonlymode;
6684 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
6685 readonlymode = TRUE;
6686 else if (eap->cmdidx == CMD_enew)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006687 readonlymode = FALSE; // 'readonly' doesn't make sense in an
6688 // empty buffer
Bram Moolenaar3482be62020-11-27 11:00:38 +01006689 if (eap->cmdidx != CMD_balt && eap->cmdidx != CMD_badd)
6690 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006691 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
6692 NULL, eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006693 // ":edit" goes to first line if Vi compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00006694 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
6695 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
6696 ? ECMD_ONE : eap->do_ecmd_lnum,
Bram Moolenaareb44a682017-08-03 22:44:55 +02006697 (buf_hide(curbuf) ? ECMD_HIDE : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006698 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006699 // after a split we can use an existing buffer
Bram Moolenaar7b449342014-03-25 13:03:48 +01006700 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar59d8e562020-11-07 18:41:10 +01006701 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0)
6702 + (eap->cmdidx == CMD_balt ? ECMD_ALTBUF : 0)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006703 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006704 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006705 // Editing the file failed. If the window was split, close it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006706 if (old_curwin != NULL)
6707 {
6708 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02006709 if (!need_hide || buf_hide(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006710 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006711#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006712 cleanup_T cs;
6713
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006714 // Reset the error/interrupt/exception state here so that
6715 // aborting() returns FALSE when closing a window.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006716 enter_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02006717#endif
6718#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006719 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006720#endif
Bram Moolenaareb44a682017-08-03 22:44:55 +02006721 win_close(curwin, !need_hide && !buf_hide(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006722
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006723#if defined(FEAT_EVAL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006724 // Restore the error/interrupt/exception state if not
6725 // discarded by a new aborting error, interrupt, or
6726 // uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006727 leave_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02006728#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006729 }
6730 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006731 }
6732 else if (readonlymode && curbuf->b_nwindows == 1)
6733 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006734 // When editing an already visited buffer, 'readonly' won't be set
6735 // but the previous value is kept. With ":view" and ":sview" we
6736 // want the file to be readonly, except when another window is
6737 // editing the same buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006738 curbuf->b_p_ro = TRUE;
6739 }
6740 readonlymode = n;
6741 }
6742 else
6743 {
6744 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01006745 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006746#ifdef FEAT_TITLE
6747 n = curwin->w_arg_idx_invalid;
6748#endif
6749 check_arg_idx(curwin);
6750#ifdef FEAT_TITLE
6751 if (n != curwin->w_arg_idx_invalid)
6752 maketitle();
6753#endif
6754 }
6755
Bram Moolenaar071d4272004-06-13 20:20:40 +00006756 /*
6757 * if ":split file" worked, set alternate file name in old window to new
6758 * file
6759 */
6760 if (old_curwin != NULL
6761 && *eap->arg != NUL
6762 && curwin != old_curwin
6763 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00006764 && old_curwin->w_buffer != curbuf
Bram Moolenaare1004402020-10-24 20:49:43 +02006765 && (cmdmod.cmod_flags & CMOD_KEEPALT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006766 old_curwin->w_alt_fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006767
6768 ex_no_reprint = TRUE;
6769}
6770
6771#ifndef FEAT_GUI
6772/*
6773 * ":gui" and ":gvim" when there is no GUI.
6774 */
6775 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006776ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006777{
Bram Moolenaar8930caa2020-07-23 16:37:03 +02006778 eap->errmsg = _(e_nogvim);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006779}
6780#endif
6781
Bram Moolenaar4f974752019-02-17 17:44:42 +01006782#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006783 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006784ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006785{
6786 gui_make_tearoff(eap->arg);
6787}
6788#endif
6789
Bram Moolenaar29a2c082018-03-05 21:06:23 +01006790#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
6791 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006792 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006793ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006794{
Bram Moolenaar29a2c082018-03-05 21:06:23 +01006795# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
6796 if (gui.in_use)
6797 gui_make_popup(eap->arg, eap->forceit);
6798# ifdef FEAT_TERM_POPUP_MENU
6799 else
6800# endif
6801# endif
6802# ifdef FEAT_TERM_POPUP_MENU
6803 pum_make_popup(eap->arg, eap->forceit);
6804# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006805}
6806#endif
6807
Bram Moolenaar071d4272004-06-13 20:20:40 +00006808 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006809ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006810{
6811 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006812 msg(_("No swap file"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006813 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01006814 msg((char *)curbuf->b_ml.ml_mfp->mf_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006815}
6816
6817/*
6818 * ":syncbind" forces all 'scrollbind' windows to have the same relative
6819 * offset.
6820 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
6821 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006822 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006823ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006824{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006825 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006826 win_T *save_curwin = curwin;
6827 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006828 long topline;
6829 long y;
6830 linenr_T old_linenr = curwin->w_cursor.lnum;
6831
6832 setpcmark();
6833
6834 /*
6835 * determine max topline
6836 */
6837 if (curwin->w_p_scb)
6838 {
6839 topline = curwin->w_topline;
Bram Moolenaar29323592016-07-24 22:04:11 +02006840 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006841 {
6842 if (wp->w_p_scb && wp->w_buffer)
6843 {
Bram Moolenaar375e3392019-01-31 18:26:10 +01006844 y = wp->w_buffer->b_ml.ml_line_count - get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006845 if (topline > y)
6846 topline = y;
6847 }
6848 }
6849 if (topline < 1)
6850 topline = 1;
6851 }
6852 else
6853 {
6854 topline = 1;
6855 }
6856
6857
6858 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006859 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006860 */
Bram Moolenaar29323592016-07-24 22:04:11 +02006861 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006862 {
6863 if (curwin->w_p_scb)
6864 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006865 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006866 y = topline - curwin->w_topline;
6867 if (y > 0)
6868 scrollup(y, TRUE);
6869 else
6870 scrolldown(-y, TRUE);
6871 curwin->w_scbind_pos = topline;
6872 redraw_later(VALID);
6873 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006874 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006875 }
6876 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006877 curwin = save_curwin;
6878 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006879 if (curwin->w_p_scb)
6880 {
6881 did_syncbind = TRUE;
6882 checkpcmark();
6883 if (old_linenr != curwin->w_cursor.lnum)
6884 {
6885 char_u ctrl_o[2];
6886
6887 ctrl_o[0] = Ctrl_O;
6888 ctrl_o[1] = 0;
6889 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
6890 }
6891 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006892}
6893
6894
6895 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006896ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006897{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006898 int i;
6899 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
6900 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006901
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006902 if (eap->usefilter) // :r!cmd
Bram Moolenaar071d4272004-06-13 20:20:40 +00006903 do_bang(1, eap, FALSE, FALSE, TRUE);
6904 else
6905 {
6906 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
6907 return;
6908
6909#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02006910 if (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006911 {
6912 char_u *browseFile;
6913
Bram Moolenaar7171abe2004-10-11 10:06:20 +00006914 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006915 NULL, NULL, NULL, curbuf);
6916 if (browseFile != NULL)
6917 {
6918 i = readfile(browseFile, NULL,
6919 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
6920 vim_free(browseFile);
6921 }
6922 else
6923 i = OK;
6924 }
6925 else
6926#endif
6927 if (*eap->arg == NUL)
6928 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006929 if (check_fname() == FAIL) // check for no file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00006930 return;
6931 i = readfile(curbuf->b_ffname, curbuf->b_fname,
6932 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
6933 }
6934 else
6935 {
6936 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
6937 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
6938 i = readfile(eap->arg, NULL,
6939 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
6940
6941 }
Bram Moolenaare13b9af2017-01-13 22:01:02 +01006942 if (i != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006943 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006944#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006945 if (!aborting())
6946#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006947 semsg(_(e_notopen), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006948 }
6949 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00006950 {
6951 if (empty && exmode_active)
6952 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006953 // Delete the empty line that remains. Historically ex does
6954 // this but vi doesn't.
Bram Moolenaardf177f62005-02-22 08:39:57 +00006955 if (eap->line2 == 0)
6956 lnum = curbuf->b_ml.ml_line_count;
6957 else
6958 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00006959 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006960 {
Bram Moolenaarca70c072020-05-30 20:30:46 +02006961 ml_delete(lnum);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00006962 if (curwin->w_cursor.lnum > 1
6963 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006964 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00006965 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006966 }
6967 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006968 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006969 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006970 }
6971}
6972
Bram Moolenaarea408852005-06-25 22:49:46 +00006973static char_u *prev_dir = NULL;
6974
6975#if defined(EXITFREE) || defined(PROTO)
6976 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006977free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00006978{
Bram Moolenaard23a8232018-02-10 18:45:26 +01006979 VIM_CLEAR(prev_dir);
6980 VIM_CLEAR(globaldir);
Bram Moolenaarea408852005-06-25 22:49:46 +00006981}
6982#endif
6983
Bram Moolenaarf4258302013-06-02 18:20:17 +02006984/*
Bram Moolenaar002bc792020-06-05 22:33:42 +02006985 * Get the previous directory for the given chdir scope.
6986 */
6987 static char_u *
6988get_prevdir(cdscope_T scope)
6989{
6990 if (scope == CDSCOPE_WINDOW)
6991 return curwin->w_prevdir;
6992 else if (scope == CDSCOPE_TABPAGE)
6993 return curtab->tp_prevdir;
6994 return prev_dir;
6995}
6996
6997/*
Bram Moolenaarf4258302013-06-02 18:20:17 +02006998 * Deal with the side effects of changing the current directory.
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006999 * When 'scope' is CDSCOPE_TABPAGE then this was after an ":tcd" command.
7000 * When 'scope' is CDSCOPE_WINDOW then this was after an ":lcd" command.
Bram Moolenaarf4258302013-06-02 18:20:17 +02007001 */
7002 void
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007003post_chdir(cdscope_T scope)
Bram Moolenaarf4258302013-06-02 18:20:17 +02007004{
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007005 if (scope != CDSCOPE_WINDOW)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007006 // Clear tab local directory for both :cd and :tcd
7007 VIM_CLEAR(curtab->tp_localdir);
Bram Moolenaard23a8232018-02-10 18:45:26 +01007008 VIM_CLEAR(curwin->w_localdir);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007009 if (scope != CDSCOPE_GLOBAL)
Bram Moolenaarf4258302013-06-02 18:20:17 +02007010 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02007011 char_u *pdir = get_prevdir(scope);
7012
7013 // If still in the global directory, need to remember current
7014 // directory as the global directory.
7015 if (globaldir == NULL && pdir != NULL)
7016 globaldir = vim_strsave(pdir);
7017
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007018 // Remember this local directory for the window.
Bram Moolenaarf4258302013-06-02 18:20:17 +02007019 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007020 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007021 if (scope == CDSCOPE_TABPAGE)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007022 curtab->tp_localdir = vim_strsave(NameBuff);
7023 else
7024 curwin->w_localdir = vim_strsave(NameBuff);
7025 }
Bram Moolenaarf4258302013-06-02 18:20:17 +02007026 }
7027 else
7028 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02007029 // We are now in the global directory, no need to remember its name.
Bram Moolenaard23a8232018-02-10 18:45:26 +01007030 VIM_CLEAR(globaldir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02007031 }
7032
7033 shorten_fnames(TRUE);
7034}
7035
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007036/*
7037 * Change directory function used by :cd/:tcd/:lcd Ex commands and the
Bram Moolenaar95058722020-06-01 16:26:19 +02007038 * chdir() function.
7039 * scope == CDSCOPE_WINDOW: changes the window-local directory
7040 * scope == CDSCOPE_TABPAGE: changes the tab-local directory
7041 * Otherwise: changes the global directory
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007042 * Returns TRUE if the directory is successfully changed.
7043 */
7044 int
7045changedir_func(
7046 char_u *new_dir,
7047 int forceit,
7048 cdscope_T scope)
7049{
7050 char_u *tofree;
Bram Moolenaar002bc792020-06-05 22:33:42 +02007051 char_u *pdir = NULL;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007052 int dir_differs;
7053 int retval = FALSE;
7054
Bram Moolenaar7cc96922020-01-31 22:41:38 +01007055 if (new_dir == NULL || allbuf_locked())
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007056 return FALSE;
7057
7058 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged() && !forceit)
7059 {
7060 emsg(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
7061 return FALSE;
7062 }
7063
7064 // ":cd -": Change to previous directory
7065 if (STRCMP(new_dir, "-") == 0)
7066 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02007067 pdir = get_prevdir(scope);
7068 if (pdir == NULL)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007069 {
7070 emsg(_("E186: No previous directory"));
7071 return FALSE;
7072 }
Bram Moolenaar002bc792020-06-05 22:33:42 +02007073 new_dir = pdir;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007074 }
7075
Bram Moolenaar002bc792020-06-05 22:33:42 +02007076 // Free the previous directory
7077 tofree = get_prevdir(scope);
7078
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007079 // Save current directory for next ":cd -"
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007080 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar002bc792020-06-05 22:33:42 +02007081 pdir = vim_strsave(NameBuff);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007082 else
Bram Moolenaar002bc792020-06-05 22:33:42 +02007083 pdir = NULL;
7084 if (scope == CDSCOPE_WINDOW)
7085 curwin->w_prevdir = pdir;
7086 else if (scope == CDSCOPE_TABPAGE)
7087 curtab->tp_prevdir = pdir;
7088 else
7089 prev_dir = pdir;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007090
7091#if defined(UNIX) || defined(VMS)
7092 // for UNIX ":cd" means: go to home directory
7093 if (*new_dir == NUL)
7094 {
7095 // use NameBuff for home directory name
7096# ifdef VMS
7097 char_u *p;
7098
7099 p = mch_getenv((char_u *)"SYS$LOGIN");
7100 if (p == NULL || *p == NUL) // empty is the same as not set
7101 NameBuff[0] = NUL;
7102 else
7103 vim_strncpy(NameBuff, p, MAXPATHL - 1);
7104# else
7105 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
7106# endif
7107 new_dir = NameBuff;
7108 }
7109#endif
Bram Moolenaar002bc792020-06-05 22:33:42 +02007110 dir_differs = new_dir == NULL || pdir == NULL
7111 || pathcmp((char *)pdir, (char *)new_dir, -1) != 0;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007112 if (new_dir == NULL || (dir_differs && vim_chdir(new_dir)))
7113 emsg(_(e_failed));
7114 else
7115 {
7116 char_u *acmd_fname;
7117
7118 post_chdir(scope);
7119
7120 if (dir_differs)
7121 {
7122 if (scope == CDSCOPE_WINDOW)
7123 acmd_fname = (char_u *)"window";
7124 else if (scope == CDSCOPE_TABPAGE)
7125 acmd_fname = (char_u *)"tabpage";
7126 else
7127 acmd_fname = (char_u *)"global";
7128 apply_autocmds(EVENT_DIRCHANGED, acmd_fname, new_dir, FALSE,
7129 curbuf);
7130 }
7131 retval = TRUE;
7132 }
7133 vim_free(tofree);
7134
7135 return retval;
7136}
Bram Moolenaarea408852005-06-25 22:49:46 +00007137
Bram Moolenaar071d4272004-06-13 20:20:40 +00007138/*
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007139 * ":cd", ":tcd", ":lcd", ":chdir" ":tchdir" and ":lchdir".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007140 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00007141 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007142ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007143{
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01007144 char_u *new_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007145
7146 new_dir = eap->arg;
7147#if !defined(UNIX) && !defined(VMS)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007148 // for non-UNIX ":cd" means: print current directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007149 if (*new_dir == NUL)
7150 ex_pwd(NULL);
7151 else
7152#endif
7153 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007154 cdscope_T scope = CDSCOPE_GLOBAL;
7155
7156 if (eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir)
7157 scope = CDSCOPE_WINDOW;
7158 else if (eap->cmdidx == CMD_tcd || eap->cmdidx == CMD_tchdir)
7159 scope = CDSCOPE_TABPAGE;
7160
7161 if (changedir_func(new_dir, eap->forceit, scope))
Bram Moolenaardf177f62005-02-22 08:39:57 +00007162 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007163 // Echo the new current directory if the command was typed.
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00007164 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007165 ex_pwd(eap);
7166 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007167 }
7168}
7169
7170/*
7171 * ":pwd".
7172 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007173 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007174ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007175{
7176 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7177 {
7178#ifdef BACKSLASH_IN_FILENAME
7179 slash_adjust(NameBuff);
7180#endif
Bram Moolenaar95058722020-06-01 16:26:19 +02007181 if (p_verbose > 0)
7182 {
7183 char *context = "global";
7184
7185 if (curwin->w_localdir != NULL)
7186 context = "window";
7187 else if (curtab->tp_localdir != NULL)
7188 context = "tabpage";
7189 smsg("[%s] %s", context, (char *)NameBuff);
7190 }
7191 else
7192 msg((char *)NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007193 }
7194 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007195 emsg(_("E187: Unknown"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007196}
7197
7198/*
7199 * ":=".
7200 */
7201 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007202ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007203{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007204 smsg("%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007205 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007206}
7207
7208 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007209ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007210{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007211 int n;
7212 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007213
7214 if (cursor_valid())
7215 {
7216 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
7217 if (n >= 0)
Bram Moolenaar53f81742017-09-22 14:35:51 +02007218 windgoto((int)n, curwin->w_wincol + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007219 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007220
7221 len = eap->line2;
7222 switch (*eap->arg)
7223 {
7224 case 'm': break;
7225 case NUL: len *= 1000L; break;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007226 default: semsg(_(e_invarg2), eap->arg); return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007227 }
7228 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007229}
7230
7231/*
7232 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
7233 */
7234 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007235do_sleep(long msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007236{
Bram Moolenaar52953192019-08-16 10:27:13 +02007237 long done = 0;
Bram Moolenaar975b5272016-03-15 23:10:59 +01007238 long wait_now;
Bram Moolenaar52953192019-08-16 10:27:13 +02007239# ifdef ELAPSED_FUNC
7240 elapsed_T start_tv;
7241
7242 // Remember at what time we started, so that we know how much longer we
7243 // should wait after waiting for a bit.
7244 ELAPSED_INIT(start_tv);
7245# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007246
7247 cursor_on();
Bram Moolenaarf45d7472018-09-30 18:22:26 +02007248 out_flush_cursor(FALSE, FALSE);
Bram Moolenaar52953192019-08-16 10:27:13 +02007249 while (!got_int && done < msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007250 {
Bram Moolenaar975b5272016-03-15 23:10:59 +01007251 wait_now = msec - done > 1000L ? 1000L : msec - done;
7252#ifdef FEAT_TIMERS
7253 {
7254 long due_time = check_due_timer();
7255
7256 if (due_time > 0 && due_time < wait_now)
7257 wait_now = due_time;
7258 }
7259#endif
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007260#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar28e67e02019-08-15 23:05:49 +02007261 if (has_any_channel() && wait_now > 20L)
7262 wait_now = 20L;
7263#endif
7264#ifdef FEAT_SOUND
7265 if (has_any_sound_callback() && wait_now > 20L)
7266 wait_now = 20L;
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007267#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +01007268 ui_delay(wait_now, TRUE);
Bram Moolenaar52953192019-08-16 10:27:13 +02007269
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007270#ifdef FEAT_JOB_CHANNEL
7271 if (has_any_channel())
7272 ui_breakcheck_force(TRUE);
7273 else
7274#endif
7275 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007276#ifdef MESSAGE_QUEUE
Bram Moolenaar52953192019-08-16 10:27:13 +02007277 // Process the netbeans and clientserver messages that may have been
7278 // received in the call to ui_breakcheck() when the GUI is in use. This
7279 // may occur when running a test case.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007280 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02007281#endif
Bram Moolenaar52953192019-08-16 10:27:13 +02007282
7283# ifdef ELAPSED_FUNC
7284 // actual time passed
7285 done = ELAPSED_FUNC(start_tv);
7286# else
7287 // guestimate time passed (will actually be more)
7288 done += wait_now;
7289# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007290 }
Bram Moolenaar1ebff3d2018-07-07 16:18:13 +02007291
7292 // If CTRL-C was typed to interrupt the sleep, drop the CTRL-C from the
7293 // input buffer, otherwise a following call to input() fails.
7294 if (got_int)
7295 (void)vpeekc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007296}
7297
Bram Moolenaar071d4272004-06-13 20:20:40 +00007298/*
7299 * ":winsize" command (obsolete).
7300 */
7301 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007302ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007303{
7304 int w, h;
7305 char_u *arg = eap->arg;
7306 char_u *p;
7307
7308 w = getdigits(&arg);
7309 arg = skipwhite(arg);
7310 p = arg;
7311 h = getdigits(&arg);
7312 if (*p != NUL && *arg == NUL)
7313 set_shellsize(w, h, TRUE);
7314 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007315 emsg(_("E465: :winsize requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007316}
7317
Bram Moolenaar071d4272004-06-13 20:20:40 +00007318 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007319ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007320{
7321 int xchar = NUL;
7322 char_u *p;
7323
7324 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
7325 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007326 // CTRL-W g and CTRL-W CTRL-G have an extra command character
Bram Moolenaar071d4272004-06-13 20:20:40 +00007327 if (eap->arg[1] == NUL)
7328 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007329 emsg(_(e_invarg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007330 return;
7331 }
7332 xchar = eap->arg[1];
7333 p = eap->arg + 2;
7334 }
7335 else
7336 p = eap->arg + 1;
7337
7338 eap->nextcmd = check_nextcmd(p);
7339 p = skipwhite(p);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02007340 if (*p != NUL && *p != (
7341#ifdef FEAT_EVAL
7342 in_vim9script() ? '#' :
7343#endif
7344 '"')
7345 && eap->nextcmd == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007346 emsg(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02007347 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007348 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007349 // Pass flags on for ":vertical wincmd ]".
Bram Moolenaare1004402020-10-24 20:49:43 +02007350 postponed_split_flags = cmdmod.cmod_split;
7351 postponed_split_tab = cmdmod.cmod_tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007352 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
7353 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00007354 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007355 }
7356}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007357
Bram Moolenaar843ee412004-06-30 16:16:41 +00007358#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007359/*
7360 * ":winpos".
7361 */
7362 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007363ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007364{
7365 int x, y;
7366 char_u *arg = eap->arg;
7367 char_u *p;
7368
7369 if (*arg == NUL)
7370 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00007371# if defined(FEAT_GUI) || defined(MSWIN)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007372# ifdef VIMDLL
7373 if (gui.in_use ? gui_mch_get_winpos(&x, &y) != FAIL :
7374 mch_get_winpos(&x, &y) != FAIL)
7375# elif defined(FEAT_GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007376 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00007377# else
7378 if (mch_get_winpos(&x, &y) != FAIL)
7379# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007380 {
7381 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
Bram Moolenaar32526b32019-01-19 17:43:09 +01007382 msg((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007383 }
7384 else
7385# endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007386 emsg(_("E188: Obtaining window position not implemented for this platform"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007387 }
7388 else
7389 {
7390 x = getdigits(&arg);
7391 arg = skipwhite(arg);
7392 p = arg;
7393 y = getdigits(&arg);
7394 if (*p == NUL || *arg != NUL)
7395 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007396 emsg(_("E466: :winpos requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007397 return;
7398 }
7399# ifdef FEAT_GUI
7400 if (gui.in_use)
7401 gui_mch_set_winpos(x, y);
7402 else if (gui.starting)
7403 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007404 // Remember the coordinates for when the window is opened.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007405 gui_win_x = x;
7406 gui_win_y = y;
7407 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007408# if defined(HAVE_TGETENT) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007409 else
7410# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007411# endif
7412# if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar843ee412004-06-30 16:16:41 +00007413 mch_set_winpos(x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007414# endif
7415# ifdef HAVE_TGETENT
7416 if (*T_CWP)
7417 term_set_winpos(x, y);
7418# endif
7419 }
7420}
7421#endif
7422
7423/*
7424 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
7425 */
7426 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007427ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007428{
7429 oparg_T oa;
7430
7431 clear_oparg(&oa);
7432 oa.regname = eap->regname;
7433 oa.start.lnum = eap->line1;
7434 oa.end.lnum = eap->line2;
7435 oa.line_count = eap->line2 - eap->line1 + 1;
7436 oa.motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007437 virtual_op = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007438 if (eap->cmdidx != CMD_yank) // position cursor for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00007439 {
7440 setpcmark();
7441 curwin->w_cursor.lnum = eap->line1;
7442 beginline(BL_SOL | BL_FIX);
7443 }
7444
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007445 if (VIsual_active)
7446 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007447
Bram Moolenaar071d4272004-06-13 20:20:40 +00007448 switch (eap->cmdidx)
7449 {
7450 case CMD_delete:
7451 oa.op_type = OP_DELETE;
7452 op_delete(&oa);
7453 break;
7454
7455 case CMD_yank:
7456 oa.op_type = OP_YANK;
7457 (void)op_yank(&oa, FALSE, TRUE);
7458 break;
7459
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007460 default: // CMD_rshift or CMD_lshift
Bram Moolenaar6e707362013-06-14 19:15:58 +02007461 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007462#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02007463 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
7464#else
7465 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00007466#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02007467 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007468 oa.op_type = OP_RSHIFT;
7469 else
7470 oa.op_type = OP_LSHIFT;
7471 op_shift(&oa, FALSE, eap->amount);
7472 break;
7473 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007474 virtual_op = MAYBE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007475 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007476}
7477
7478/*
7479 * ":put".
7480 */
7481 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007482ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007483{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007484 // ":0put" works like ":1put!".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007485 if (eap->line2 == 0)
7486 {
7487 eap->line2 = 1;
7488 eap->forceit = TRUE;
7489 }
7490 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007491 do_put(eap->regname, NULL, eap->forceit ? BACKWARD : FORWARD, 1L,
Bram Moolenaardf177f62005-02-22 08:39:57 +00007492 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007493}
7494
7495/*
7496 * Handle ":copy" and ":move".
7497 */
7498 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007499ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007500{
7501 long n;
7502
Bram Moolenaar491799b2020-08-01 19:23:43 +02007503#ifdef FEAT_EVAL
Bram Moolenaarf5a48012020-08-01 17:00:03 +02007504 if (not_in_vim9(eap) == FAIL)
7505 return;
Bram Moolenaar491799b2020-08-01 19:23:43 +02007506#endif
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02007507 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE, FALSE, 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007508 if (eap->arg == NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00007509 {
7510 eap->nextcmd = NULL;
7511 return;
7512 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00007513 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007514
7515 /*
7516 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
7517 */
7518 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
7519 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02007520 emsg(_(e_invrange));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007521 return;
7522 }
7523
7524 if (eap->cmdidx == CMD_move)
7525 {
7526 if (do_move(eap->line1, eap->line2, n) == FAIL)
7527 return;
7528 }
7529 else
7530 ex_copy(eap->line1, eap->line2, n);
7531 u_clearline();
7532 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007533 ex_may_print(eap);
7534}
7535
7536/*
7537 * Print the current line if flags were given to the Ex command.
7538 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02007539 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007540ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007541{
7542 if (eap->flags != 0)
7543 {
7544 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
7545 (eap->flags & EXFLAG_LIST));
7546 ex_no_reprint = TRUE;
7547 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007548}
7549
7550/*
7551 * ":smagic" and ":snomagic".
7552 */
7553 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007554ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007555{
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01007556 optmagic_T saved = magic_overruled;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007557
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01007558 magic_overruled = eap->cmdidx == CMD_smagic
7559 ? OPTION_MAGIC_ON : OPTION_MAGIC_OFF;
Bram Moolenaar66e00142020-08-12 21:58:12 +02007560 ex_substitute(eap);
Bram Moolenaarf4e20992020-12-21 19:59:08 +01007561 magic_overruled = saved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007562}
7563
7564/*
7565 * ":join".
7566 */
7567 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007568ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007569{
7570 curwin->w_cursor.lnum = eap->line1;
7571 if (eap->line1 == eap->line2)
7572 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007573 if (eap->addr_count >= 2) // :2,2join does nothing
Bram Moolenaar071d4272004-06-13 20:20:40 +00007574 return;
7575 if (eap->line2 == curbuf->b_ml.ml_line_count)
7576 {
7577 beep_flush();
7578 return;
7579 }
7580 ++eap->line2;
7581 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02007582 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007583 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007584 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007585}
7586
7587/*
7588 * ":[addr]@r" or ":[addr]*r": execute register
7589 */
7590 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007591ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007592{
7593 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00007594 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007595
7596 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4930a762016-09-11 14:39:53 +02007597 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007598
7599#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007600 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00007601#endif
7602
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007603 // get the register name. No name means to use the previous one
Bram Moolenaar071d4272004-06-13 20:20:40 +00007604 c = *eap->arg;
7605 if (c == NUL || (c == '*' && *eap->cmd == '*'))
7606 c = '@';
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007607 // Put the register in the typeahead buffer with the "silent" flag.
Bram Moolenaard333d1e2006-11-07 17:43:47 +00007608 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
7609 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007610 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007611 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00007612 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007613 else
7614 {
7615 int save_efr = exec_from_reg;
7616
7617 exec_from_reg = TRUE;
7618
7619 /*
7620 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00007621 * Continue until the stuff buffer is empty and all added characters
7622 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007623 */
Bram Moolenaar60462872009-11-03 11:40:19 +00007624 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007625 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
7626
7627 exec_from_reg = save_efr;
7628 }
7629}
7630
7631/*
7632 * ":!".
7633 */
7634 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007635ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007636{
7637 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
7638}
7639
7640/*
7641 * ":undo".
7642 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007643 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01007644ex_undo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007645{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007646 if (eap->addr_count == 1) // :undo 123
Bram Moolenaar730cde92010-06-27 05:18:54 +02007647 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00007648 else
7649 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007650}
7651
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007652#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02007653 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007654ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007655{
7656 char_u hash[UNDO_HASH_SIZE];
7657
7658 u_compute_hash(hash);
7659 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
7660}
7661
Bram Moolenaar6df6f472010-07-18 18:04:50 +02007662 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007663ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007664{
7665 char_u hash[UNDO_HASH_SIZE];
7666
7667 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02007668 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007669}
7670#endif
7671
Bram Moolenaar071d4272004-06-13 20:20:40 +00007672/*
7673 * ":redo".
7674 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007675 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007676ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007677{
7678 u_redo(1);
7679}
7680
7681/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007682 * ":earlier" and ":later".
7683 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007684 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007685ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007686{
7687 long count = 0;
7688 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02007689 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007690 char_u *p = eap->arg;
7691
7692 if (*p == NUL)
7693 count = 1;
7694 else if (isdigit(*p))
7695 {
7696 count = getdigits(&p);
7697 switch (*p)
7698 {
7699 case 's': ++p; sec = TRUE; break;
7700 case 'm': ++p; sec = TRUE; count *= 60; break;
7701 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02007702 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
7703 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007704 }
7705 }
7706
7707 if (*p != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007708 semsg(_(e_invarg2), eap->arg);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007709 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02007710 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
7711 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007712}
7713
7714/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007715 * ":redir": start/stop redirection.
7716 */
7717 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007718ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007719{
7720 char *mode;
7721 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00007722 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007723
Bram Moolenaarba768492016-07-08 15:32:54 +02007724#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02007725 if (redir_execute)
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007726 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007727 emsg(_("E930: Cannot use :redir inside execute()"));
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007728 return;
7729 }
Bram Moolenaarba768492016-07-08 15:32:54 +02007730#endif
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007731
Bram Moolenaar071d4272004-06-13 20:20:40 +00007732 if (STRICMP(eap->arg, "END") == 0)
7733 close_redir();
7734 else
7735 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007736 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007737 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007738 ++arg;
7739 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007740 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007741 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007742 mode = "a";
7743 }
7744 else
7745 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00007746 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007747
7748 close_redir();
7749
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007750 // Expand environment variables and "~/".
Bram Moolenaarca472992005-01-21 11:46:23 +00007751 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007752 if (fname == NULL)
7753 return;
7754#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02007755 if (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007756 {
7757 char_u *browseFile;
7758
Bram Moolenaar7171abe2004-10-11 10:06:20 +00007759 browseFile = do_browse(BROWSE_SAVE,
7760 (char_u *)_("Save Redirection"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +02007761 fname, NULL, NULL,
7762 (char_u *)_(BROWSE_FILTER_ALL_FILES), curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007763 if (browseFile == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007764 return; // operation cancelled
Bram Moolenaar071d4272004-06-13 20:20:40 +00007765 vim_free(fname);
7766 fname = browseFile;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007767 eap->forceit = TRUE; // since dialog already asked
Bram Moolenaar071d4272004-06-13 20:20:40 +00007768 }
7769#endif
7770
7771 redir_fd = open_exfile(fname, eap->forceit, mode);
7772 vim_free(fname);
7773 }
7774#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00007775 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007776 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007777 // redirect to a register a-z (resp. A-Z for appending)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007778 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00007779 ++arg;
7780 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007781# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00007782 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00007783 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00007784# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00007785 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007786 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007787 redir_reg = *arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007788 if (*arg == '>' && arg[1] == '>') // append
Bram Moolenaard9d30582005-05-18 22:10:28 +00007789 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00007790 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007791 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007792 // Can use both "@a" and "@a>".
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00007793 if (*arg == '>')
7794 arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007795 // Make register empty when not using @A-@Z and the
7796 // command is valid.
Bram Moolenaarc188b882007-10-19 14:20:54 +00007797 if (*arg == NUL && !isupper(redir_reg))
7798 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007799 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00007800 }
7801 if (*arg != NUL)
7802 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007803 redir_reg = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007804 semsg(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007805 }
7806 }
7807 else if (*arg == '=' && arg[1] == '>')
7808 {
7809 int append;
7810
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007811 // redirect to a variable
Bram Moolenaardf177f62005-02-22 08:39:57 +00007812 close_redir();
7813 arg += 2;
7814
7815 if (*arg == '>')
7816 {
7817 ++arg;
7818 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007819 }
7820 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00007821 append = FALSE;
7822
7823 if (var_redir_start(skipwhite(arg), append) == OK)
7824 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007825 }
7826#endif
7827
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007828 // TODO: redirect to a buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00007829
Bram Moolenaar071d4272004-06-13 20:20:40 +00007830 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007831 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007832 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00007833
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007834 // Make sure redirection is not off. Can happen for cmdline completion
7835 // that indirectly invokes a command to catch its output.
Bram Moolenaar29b2d262006-09-10 19:07:28 +00007836 if (redir_fd != NULL
7837#ifdef FEAT_EVAL
7838 || redir_reg || redir_vname
7839#endif
7840 )
7841 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007842}
7843
7844/*
7845 * ":redraw": force redraw
7846 */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01007847 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007848ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007849{
7850 int r = RedrawingDisabled;
7851 int p = p_lz;
7852
7853 RedrawingDisabled = 0;
7854 p_lz = FALSE;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01007855 validate_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007856 update_topline();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007857 update_screen(eap->forceit ? CLEAR : VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007858#ifdef FEAT_TITLE
7859 if (need_maketitle)
7860 maketitle();
7861#endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007862#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
7863# ifdef VIMDLL
7864 if (!gui.in_use)
7865# endif
7866 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01007867#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007868 RedrawingDisabled = r;
7869 p_lz = p;
7870
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007871 // Reset msg_didout, so that a message that's there is overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007872 msg_didout = FALSE;
7873 msg_col = 0;
7874
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007875 // No need to wait after an intentional redraw.
Bram Moolenaar56667a52013-07-17 11:54:28 +02007876 need_wait_return = FALSE;
7877
Bram Moolenaar071d4272004-06-13 20:20:40 +00007878 out_flush();
7879}
7880
7881/*
7882 * ":redrawstatus": force redraw of status line(s)
7883 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007884 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007885ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007886{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007887 int r = RedrawingDisabled;
7888 int p = p_lz;
7889
7890 RedrawingDisabled = 0;
7891 p_lz = FALSE;
7892 if (eap->forceit)
7893 status_redraw_all();
7894 else
7895 status_redraw_curbuf();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007896 update_screen(VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007897 RedrawingDisabled = r;
7898 p_lz = p;
7899 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007900}
7901
Bram Moolenaare12bab32019-01-08 22:02:56 +01007902/*
7903 * ":redrawtabline": force redraw of the tabline
7904 */
7905 static void
7906ex_redrawtabline(exarg_T *eap UNUSED)
7907{
7908 int r = RedrawingDisabled;
7909 int p = p_lz;
7910
7911 RedrawingDisabled = 0;
7912 p_lz = FALSE;
7913
7914 draw_tabline();
7915
7916 RedrawingDisabled = r;
7917 p_lz = p;
7918 out_flush();
7919}
7920
Bram Moolenaar071d4272004-06-13 20:20:40 +00007921 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007922close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007923{
7924 if (redir_fd != NULL)
7925 {
7926 fclose(redir_fd);
7927 redir_fd = NULL;
7928 }
7929#ifdef FEAT_EVAL
7930 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007931 if (redir_vname)
7932 {
7933 var_redir_stop();
7934 redir_vname = 0;
7935 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007936#endif
7937}
7938
Bram Moolenaarf96ae0b2019-07-28 15:21:55 +02007939#if (defined(FEAT_SESSION) || defined(FEAT_EVAL)) || defined(PROTO)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007940 int
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02007941vim_mkdir_emsg(char_u *name, int prot UNUSED)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007942{
7943 if (vim_mkdir(name, prot) != 0)
7944 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007945 semsg(_("E739: Cannot create directory: %s"), name);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007946 return FAIL;
7947 }
7948 return OK;
7949}
7950#endif
7951
Bram Moolenaar071d4272004-06-13 20:20:40 +00007952/*
7953 * Open a file for writing for an Ex command, with some checks.
7954 * Return file descriptor, or NULL on failure.
7955 */
7956 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007957open_exfile(
7958 char_u *fname,
7959 int forceit,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007960 char *mode) // "w" for create new file or "a" for append
Bram Moolenaar071d4272004-06-13 20:20:40 +00007961{
7962 FILE *fd;
7963
7964#ifdef UNIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007965 // with Unix it is possible to open a directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007966 if (mch_isdir(fname))
7967 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007968 semsg(_(e_isadir2), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007969 return NULL;
7970 }
7971#endif
7972 if (!forceit && *mode != 'a' && vim_fexists(fname))
7973 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007974 semsg(_("E189: \"%s\" exists (add ! to override)"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007975 return NULL;
7976 }
7977
7978 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007979 semsg(_("E190: Cannot open \"%s\" for writing"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007980
7981 return fd;
7982}
7983
7984/*
7985 * ":mark" and ":k".
7986 */
7987 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007988ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007989{
7990 pos_T pos;
7991
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007992 if (*eap->arg == NUL) // No argument?
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007993 emsg(_(e_argreq));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007994 else if (eap->arg[1] != NUL) // more than one character?
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02007995 semsg(_(e_trailing_arg), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007996 else
7997 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007998 pos = curwin->w_cursor; // save curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00007999 curwin->w_cursor.lnum = eap->line2;
8000 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008001 if (setmark(*eap->arg) == FAIL) // set mark
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008002 emsg(_("E191: Argument must be a letter or forward/backward quote"));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008003 curwin->w_cursor = pos; // restore curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00008004 }
8005}
8006
8007/*
8008 * Update w_topline, w_leftcol and the cursor position.
8009 */
8010 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008011update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008012{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008013 check_cursor(); // put cursor on valid line
Bram Moolenaar071d4272004-06-13 20:20:40 +00008014 update_topline();
8015 if (!curwin->w_p_wrap)
8016 validate_cursor();
8017 update_curswant();
8018}
8019
Bram Moolenaar071d4272004-06-13 20:20:40 +00008020/*
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008021 * Save the current State and go to Normal mode.
8022 * Return TRUE if the typeahead could be saved.
8023 */
8024 int
8025save_current_state(save_state_T *sst)
8026{
8027 sst->save_msg_scroll = msg_scroll;
8028 sst->save_restart_edit = restart_edit;
8029 sst->save_msg_didout = msg_didout;
8030 sst->save_State = State;
8031 sst->save_insertmode = p_im;
8032 sst->save_finish_op = finish_op;
8033 sst->save_opcount = opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01008034 sst->save_reg_executing = reg_executing;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008035
Bram Moolenaar4324d872020-12-01 20:12:24 +01008036 msg_scroll = FALSE; // no msg scrolling in Normal mode
8037 restart_edit = 0; // don't go to Insert mode
8038 p_im = FALSE; // don't use 'insertmode'
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01008039
Bram Moolenaara452b802020-12-01 21:47:59 +01008040 sst->save_script_version = current_sctx.sc_version;
Bram Moolenaar4324d872020-12-01 20:12:24 +01008041 current_sctx.sc_version = 1; // not in Vim9 script
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008042
8043 /*
8044 * Save the current typeahead. This is required to allow using ":normal"
8045 * from an event handler and makes sure we don't hang when the argument
8046 * ends with half a command.
8047 */
8048 save_typeahead(&sst->tabuf);
8049 return sst->tabuf.typebuf_valid;
8050}
8051
8052 void
8053restore_current_state(save_state_T *sst)
8054{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008055 // Restore the previous typeahead.
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008056 restore_typeahead(&sst->tabuf);
8057
8058 msg_scroll = sst->save_msg_scroll;
8059 restart_edit = sst->save_restart_edit;
8060 p_im = sst->save_insertmode;
8061 finish_op = sst->save_finish_op;
8062 opcount = sst->save_opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01008063 reg_executing = sst->save_reg_executing;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008064 msg_didout |= sst->save_msg_didout; // don't reset msg_didout now
Bram Moolenaar4324d872020-12-01 20:12:24 +01008065 current_sctx.sc_version = sst->save_script_version;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008066
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008067 // Restore the state (needed when called from a function executed for
8068 // 'indentexpr'). Update the mouse and cursor, they may have changed.
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008069 State = sst->save_State;
8070#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008071 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008072#endif
8073}
8074
8075/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008076 * ":normal[!] {commands}": Execute normal mode commands.
8077 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01008078 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008079ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008080{
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008081 save_state_T save_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008082 char_u *arg = NULL;
8083 int l;
8084 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008085
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008086 if (ex_normal_lock > 0)
8087 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008088 emsg(_(e_secure));
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008089 return;
8090 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008091 if (ex_normal_busy >= p_mmd)
8092 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008093 emsg(_("E192: Recursive use of :normal too deep"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008094 return;
8095 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008096
Bram Moolenaar071d4272004-06-13 20:20:40 +00008097 /*
8098 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
8099 * this for the K_SPECIAL leading byte, otherwise special keys will not
8100 * work.
8101 */
8102 if (has_mbyte)
8103 {
8104 int len = 0;
8105
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008106 // Count the number of characters to be escaped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008107 for (p = eap->arg; *p != NUL; ++p)
8108 {
Bram Moolenaar13505972019-01-24 15:04:48 +01008109#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008110 if (*p == CSI) // leadbyte CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008111 len += 2;
Bram Moolenaar13505972019-01-24 15:04:48 +01008112#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008113 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008114 if (*++p == K_SPECIAL // trailbyte K_SPECIAL or CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01008115#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008116 || *p == CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01008117#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008118 )
8119 len += 2;
8120 }
8121 if (len > 0)
8122 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02008123 arg = alloc(STRLEN(eap->arg) + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008124 if (arg != NULL)
8125 {
8126 len = 0;
8127 for (p = eap->arg; *p != NUL; ++p)
8128 {
8129 arg[len++] = *p;
Bram Moolenaar13505972019-01-24 15:04:48 +01008130#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008131 if (*p == CSI)
8132 {
8133 arg[len++] = KS_EXTRA;
8134 arg[len++] = (int)KE_CSI;
8135 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008136#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008137 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008138 {
8139 arg[len++] = *++p;
8140 if (*p == K_SPECIAL)
8141 {
8142 arg[len++] = KS_SPECIAL;
8143 arg[len++] = KE_FILLER;
8144 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008145#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008146 else if (*p == CSI)
8147 {
8148 arg[len++] = KS_EXTRA;
8149 arg[len++] = (int)KE_CSI;
8150 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008151#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008152 }
8153 arg[len] = NUL;
8154 }
8155 }
8156 }
8157 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008158
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008159 ++ex_normal_busy;
8160 if (save_current_state(&save_state))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008161 {
8162 /*
8163 * Repeat the :normal command for each line in the range. When no
8164 * range given, execute it just once, without positioning the cursor
8165 * first.
8166 */
8167 do
8168 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008169 if (eap->addr_count != 0)
8170 {
8171 curwin->w_cursor.lnum = eap->line1++;
8172 curwin->w_cursor.col = 0;
Bram Moolenaard5d37532017-03-27 23:02:07 +02008173 check_cursor_moved(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008174 }
8175
Bram Moolenaar13505972019-01-24 15:04:48 +01008176 exec_normal_cmd(arg != NULL
8177 ? arg
8178 : eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008179 }
8180 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
8181 }
8182
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008183 // Might not return to the main loop when in an event handler.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008184 update_topline_cursor();
8185
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008186 restore_current_state(&save_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008187 --ex_normal_busy;
Bram Moolenaareda73602014-11-05 09:53:23 +01008188 setmouse();
Bram Moolenaareda73602014-11-05 09:53:23 +01008189#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008190 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaareda73602014-11-05 09:53:23 +01008191#endif
8192
Bram Moolenaar071d4272004-06-13 20:20:40 +00008193 vim_free(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008194}
8195
8196/*
Bram Moolenaar64969662005-12-14 21:59:55 +00008197 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008198 */
8199 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008200ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008201{
Bram Moolenaarfd371682005-01-14 21:42:54 +00008202 if (eap->forceit)
8203 {
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008204 // cursor line can be zero on startup
Bram Moolenaar09ca9322017-09-26 17:40:45 +02008205 if (!curwin->w_cursor.lnum)
8206 curwin->w_cursor.lnum = 1;
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008207 set_cursor_for_append_to_line();
Bram Moolenaarfd371682005-01-14 21:42:54 +00008208 }
Bram Moolenaareb58a242020-04-19 18:13:19 +02008209#ifdef FEAT_TERMINAL
8210 // Ignore this when running in an active terminal.
8211 if (term_job_running(curbuf->b_term))
8212 return;
8213#endif
Bram Moolenaarfd371682005-01-14 21:42:54 +00008214
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008215 // Ignore the command when already in Insert mode. Inserting an
8216 // expression register that invokes a function can do this.
Bram Moolenaara40c5002005-01-09 21:16:21 +00008217 if (State & INSERT)
8218 return;
8219
Bram Moolenaar64969662005-12-14 21:59:55 +00008220 if (eap->cmdidx == CMD_startinsert)
8221 restart_edit = 'a';
8222 else if (eap->cmdidx == CMD_startreplace)
8223 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008224 else
Bram Moolenaar64969662005-12-14 21:59:55 +00008225 restart_edit = 'V';
8226
8227 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008228 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00008229 if (eap->cmdidx == CMD_startinsert)
8230 restart_edit = 'i';
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008231 curwin->w_curswant = 0; // avoid MAXCOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008232 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01008233
8234 if (VIsual_active)
8235 showmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008236}
8237
8238/*
8239 * ":stopinsert"
8240 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008241 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008242ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008243{
8244 restart_edit = 0;
8245 stop_insert_mode = TRUE;
Bram Moolenaarfd773e92016-04-02 19:39:16 +02008246 clearmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008247}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008248
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008249/*
8250 * Execute normal mode command "cmd".
8251 * "remap" can be REMAP_NONE or REMAP_YES.
8252 */
8253 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008254exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008255{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008256 // Stuff the argument into the typeahead buffer.
Bram Moolenaar25281632016-01-21 23:32:32 +01008257 ins_typebuf(cmd, remap, 0, TRUE, silent);
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008258 exec_normal(FALSE, FALSE, FALSE);
Bram Moolenaar25281632016-01-21 23:32:32 +01008259}
Bram Moolenaar25281632016-01-21 23:32:32 +01008260
Bram Moolenaar25281632016-01-21 23:32:32 +01008261/*
8262 * Execute normal_cmd() until there is no typeahead left.
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008263 * When "use_vpeekc" is TRUE use vpeekc() to check for available chars.
Bram Moolenaar25281632016-01-21 23:32:32 +01008264 */
8265 void
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008266exec_normal(int was_typed, int use_vpeekc, int may_use_terminal_loop UNUSED)
Bram Moolenaar25281632016-01-21 23:32:32 +01008267{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008268 oparg_T oa;
Bram Moolenaar905dd902019-04-07 14:21:47 +02008269 int c;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008270
Bram Moolenaar905dd902019-04-07 14:21:47 +02008271 // When calling vpeekc() from feedkeys() it will return Ctrl_C when there
8272 // is nothing to get, so also check for Ctrl_C.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008273 clear_oparg(&oa);
8274 finish_op = FALSE;
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008275 while ((!stuff_empty()
8276 || ((was_typed || !typebuf_typed()) && typebuf.tb_len > 0)
Bram Moolenaar905dd902019-04-07 14:21:47 +02008277 || (use_vpeekc && (c = vpeekc()) != NUL && c != Ctrl_C))
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008278 && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008279 {
8280 update_topline_cursor();
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008281#ifdef FEAT_TERMINAL
Bram Moolenaar655a82a2018-05-08 22:01:07 +02008282 if (may_use_terminal_loop && term_use_loop()
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008283 && oa.op_type == OP_NOP && oa.regname == NUL
8284 && !VIsual_active)
8285 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008286 // If terminal_loop() returns OK we got a key that is handled
8287 // in Normal model. With FAIL we first need to position the
8288 // cursor and the screen needs to be redrawn.
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008289 if (terminal_loop(TRUE) == OK)
8290 normal_cmd(&oa, TRUE);
8291 }
8292 else
8293#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008294 // execute a Normal mode cmd
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008295 normal_cmd(&oa, TRUE);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008296 }
8297}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008298
Bram Moolenaar071d4272004-06-13 20:20:40 +00008299#ifdef FEAT_FIND_ID
8300 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008301ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008302{
8303 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
8304 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
8305 (linenr_T)1, (linenr_T)MAXLNUM);
8306}
8307
Bram Moolenaar4033c552017-09-16 20:54:51 +02008308#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008309/*
8310 * ":psearch"
8311 */
8312 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008313ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008314{
8315 g_do_tagpreview = p_pvh;
8316 ex_findpat(eap);
8317 g_do_tagpreview = 0;
8318}
8319#endif
8320
8321 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008322ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008323{
8324 int whole = TRUE;
8325 long n;
8326 char_u *p;
8327 int action;
8328
8329 switch (cmdnames[eap->cmdidx].cmd_name[2])
8330 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008331 case 'e': // ":psearch", ":isearch" and ":dsearch"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008332 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
8333 action = ACTION_GOTO;
8334 else
8335 action = ACTION_SHOW;
8336 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008337 case 'i': // ":ilist" and ":dlist"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008338 action = ACTION_SHOW_ALL;
8339 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008340 case 'u': // ":ijump" and ":djump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008341 action = ACTION_GOTO;
8342 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008343 default: // ":isplit" and ":dsplit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008344 action = ACTION_SPLIT;
8345 break;
8346 }
8347
8348 n = 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008349 if (vim_isdigit(*eap->arg)) // get count
Bram Moolenaar071d4272004-06-13 20:20:40 +00008350 {
8351 n = getdigits(&eap->arg);
8352 eap->arg = skipwhite(eap->arg);
8353 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008354 if (*eap->arg == '/') // Match regexp, not just whole words
Bram Moolenaar071d4272004-06-13 20:20:40 +00008355 {
8356 whole = FALSE;
8357 ++eap->arg;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01008358 p = skip_regexp(eap->arg, '/', magic_isset());
Bram Moolenaar071d4272004-06-13 20:20:40 +00008359 if (*p)
8360 {
8361 *p++ = NUL;
8362 p = skipwhite(p);
8363
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008364 // Check for trailing illegal characters
Bram Moolenaarfaac4102020-04-20 17:46:14 +02008365 if (!ends_excmd2(eap->arg, p))
Bram Moolenaar8930caa2020-07-23 16:37:03 +02008366 eap->errmsg = ex_errmsg(e_trailing_arg, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008367 else
8368 eap->nextcmd = check_nextcmd(p);
8369 }
8370 }
8371 if (!eap->skip)
8372 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
8373 whole, !eap->forceit,
8374 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
8375 n, action, eap->line1, eap->line2);
8376}
8377#endif
8378
Bram Moolenaar071d4272004-06-13 20:20:40 +00008379
Bram Moolenaar4033c552017-09-16 20:54:51 +02008380#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00008381/*
8382 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
8383 */
8384 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008385ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008386{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008387 g_do_tagpreview = p_pvh; // will be reset to 0 in ex_tag_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00008388 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
8389}
8390
8391/*
8392 * ":pedit"
8393 */
8394 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008395ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008396{
8397 win_T *curwin_save = curwin;
8398
Bram Moolenaar3a2505c2020-03-09 16:40:41 +01008399 if (ERROR_IF_ANY_POPUP_WINDOW)
8400 return;
8401
Bram Moolenaar026587b2019-08-17 15:08:00 +02008402 // Open the preview window or popup and make it the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008403 g_do_tagpreview = p_pvh;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02008404 prepare_tagpreview(TRUE, TRUE, FALSE);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008405
Bram Moolenaar026587b2019-08-17 15:08:00 +02008406 // Edit the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008407 do_exedit(eap, NULL);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008408
Bram Moolenaar071d4272004-06-13 20:20:40 +00008409 if (curwin != curwin_save && win_valid(curwin_save))
8410 {
Bram Moolenaar026587b2019-08-17 15:08:00 +02008411 // Return cursor to where we were
Bram Moolenaar071d4272004-06-13 20:20:40 +00008412 validate_cursor();
8413 redraw_later(VALID);
8414 win_enter(curwin_save, TRUE);
8415 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01008416# ifdef FEAT_PROP_POPUP
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008417 else if (WIN_IS_POPUP(curwin))
8418 {
8419 // can't keep focus in popup window
8420 win_enter(firstwin, TRUE);
8421 }
8422# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008423 g_do_tagpreview = 0;
8424}
Bram Moolenaar4033c552017-09-16 20:54:51 +02008425#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008426
8427/*
8428 * ":stag", ":stselect" and ":stjump".
8429 */
8430 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008431ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008432{
8433 postponed_split = -1;
Bram Moolenaare1004402020-10-24 20:49:43 +02008434 postponed_split_flags = cmdmod.cmod_split;
8435 postponed_split_tab = cmdmod.cmod_tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008436 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
8437 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008438 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008439}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008440
8441/*
8442 * ":tag", ":tselect", ":tjump", ":tnext", etc.
8443 */
8444 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008445ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008446{
8447 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
8448}
8449
8450 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008451ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008452{
8453 int cmd;
8454
8455 switch (name[1])
8456 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008457 case 'j': cmd = DT_JUMP; // ":tjump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008458 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008459 case 's': cmd = DT_SELECT; // ":tselect"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008460 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008461 case 'p': cmd = DT_PREV; // ":tprevious"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008462 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008463 case 'N': cmd = DT_PREV; // ":tNext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008464 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008465 case 'n': cmd = DT_NEXT; // ":tnext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008466 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008467 case 'o': cmd = DT_POP; // ":pop"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008468 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008469 case 'f': // ":tfirst"
8470 case 'r': cmd = DT_FIRST; // ":trewind"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008471 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008472 case 'l': cmd = DT_LAST; // ":tlast"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008473 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008474 default: // ":tag"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008475#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +00008476 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008477 {
Bram Moolenaard4db7712016-11-12 19:16:46 +01008478 ex_cstag(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008479 return;
8480 }
8481#endif
8482 cmd = DT_TAG;
8483 break;
8484 }
8485
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00008486 if (name[0] == 'l')
8487 {
8488#ifndef FEAT_QUICKFIX
8489 ex_ni(eap);
8490 return;
8491#else
8492 cmd = DT_LTAG;
8493#endif
8494 }
8495
Bram Moolenaar071d4272004-06-13 20:20:40 +00008496 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
8497 eap->forceit, TRUE);
8498}
8499
8500/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008501 * Check "str" for starting with a special cmdline variable.
8502 * If found return one of the SPEC_ values and set "*usedlen" to the length of
8503 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
8504 */
8505 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008506find_cmdline_var(char_u *src, int *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008507{
8508 int len;
8509 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +00008510 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008511 "%",
8512#define SPEC_PERC 0
8513 "#",
Bram Moolenaar65f08472017-09-10 18:16:20 +02008514#define SPEC_HASH (SPEC_PERC + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008515 "<cword>", // cursor word
Bram Moolenaar65f08472017-09-10 18:16:20 +02008516#define SPEC_CWORD (SPEC_HASH + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008517 "<cWORD>", // cursor WORD
Bram Moolenaar65f08472017-09-10 18:16:20 +02008518#define SPEC_CCWORD (SPEC_CWORD + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008519 "<cexpr>", // expr under cursor
Bram Moolenaar65f08472017-09-10 18:16:20 +02008520#define SPEC_CEXPR (SPEC_CCWORD + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008521 "<cfile>", // cursor path name
Bram Moolenaar65f08472017-09-10 18:16:20 +02008522#define SPEC_CFILE (SPEC_CEXPR + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008523 "<sfile>", // ":so" file name
Bram Moolenaar65f08472017-09-10 18:16:20 +02008524#define SPEC_SFILE (SPEC_CFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008525 "<slnum>", // ":so" file line number
Bram Moolenaar65f08472017-09-10 18:16:20 +02008526#define SPEC_SLNUM (SPEC_SFILE + 1)
Bram Moolenaara5d04232020-07-26 15:37:02 +02008527 "<stack>", // call stack
8528#define SPEC_STACK (SPEC_SLNUM + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008529 "<afile>", // autocommand file name
Bram Moolenaara5d04232020-07-26 15:37:02 +02008530#define SPEC_AFILE (SPEC_STACK + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008531 "<abuf>", // autocommand buffer number
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008532#define SPEC_ABUF (SPEC_AFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008533 "<amatch>", // autocommand match name
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008534#define SPEC_AMATCH (SPEC_ABUF + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008535 "<sflnum>", // script file line number
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008536#define SPEC_SFLNUM (SPEC_AMATCH + 1)
Bram Moolenaar90944302020-08-01 20:45:11 +02008537 "<SID>", // script ID: <SNR>123_
8538#define SPEC_SID (SPEC_SFLNUM + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008539#ifdef FEAT_CLIENTSERVER
8540 "<client>"
Bram Moolenaar90944302020-08-01 20:45:11 +02008541# define SPEC_CLIENT (SPEC_SID + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008542#endif
8543 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008544
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008545 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008546 {
8547 len = (int)STRLEN(spec_str[i]);
8548 if (STRNCMP(src, spec_str[i], len) == 0)
8549 {
8550 *usedlen = len;
8551 return i;
8552 }
8553 }
8554 return -1;
8555}
8556
8557/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008558 * Evaluate cmdline variables.
8559 *
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008560 * change "%" to curbuf->b_ffname
8561 * "#" to curwin->w_alt_fnum
8562 * "%%" to curwin->w_alt_fnum in Vim9 script
8563 * "<cword>" to word under the cursor
8564 * "<cWORD>" to WORD under the cursor
8565 * "<cexpr>" to C-expression under the cursor
8566 * "<cfile>" to path name under the cursor
8567 * "<sfile>" to sourced file name
8568 * "<stack>" to call stack
8569 * "<slnum>" to sourced file line number
8570 * "<afile>" to file name for autocommand
8571 * "<abuf>" to buffer number for autocommand
8572 * "<amatch>" to matching name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008573 *
8574 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
8575 * "" for error without a message) and NULL is returned.
8576 * Returns an allocated string if a valid match was found.
8577 * Returns NULL if no match was found. "usedlen" then still contains the
8578 * number of characters to skip.
8579 */
8580 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008581eval_vars(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008582 char_u *src, // pointer into commandline
8583 char_u *srcstart, // beginning of valid memory for src
8584 int *usedlen, // characters after src that are used
8585 linenr_T *lnump, // line number for :e command, or NULL
8586 char **errormsg, // pointer to error message
8587 int *escaped) // return value has escaped white space (can
8588 // be NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008589{
8590 int i;
8591 char_u *s;
8592 char_u *result;
8593 char_u *resultbuf = NULL;
8594 int resultlen;
8595 buf_T *buf;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008596 int valid = VALID_HEAD + VALID_PATH; // assume valid result
Bram Moolenaar071d4272004-06-13 20:20:40 +00008597 int spec_idx;
Bram Moolenaarfd249462018-07-28 16:14:30 +02008598 int tilde_file = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008599 int skip_mod = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008600 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008601
8602 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +00008603 if (escaped != NULL)
8604 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008605
8606 /*
8607 * Check if there is something to do.
8608 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008609 spec_idx = find_cmdline_var(src, usedlen);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008610 if (spec_idx < 0) // no match
Bram Moolenaar071d4272004-06-13 20:20:40 +00008611 {
8612 *usedlen = 1;
8613 return NULL;
8614 }
8615
8616 /*
8617 * Skip when preceded with a backslash "\%" and "\#".
8618 * Note: In "\\%" the % is also not recognized!
8619 */
8620 if (src > srcstart && src[-1] == '\\')
8621 {
8622 *usedlen = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008623 STRMOVE(src - 1, src); // remove backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00008624 return NULL;
8625 }
8626
8627 /*
8628 * word or WORD under cursor
8629 */
Bram Moolenaar65f08472017-09-10 18:16:20 +02008630 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD
8631 || spec_idx == SPEC_CEXPR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008632 {
Bram Moolenaar65f08472017-09-10 18:16:20 +02008633 resultlen = find_ident_under_cursor(&result,
8634 spec_idx == SPEC_CWORD ? (FIND_IDENT | FIND_STRING)
8635 : spec_idx == SPEC_CEXPR ? (FIND_IDENT | FIND_STRING | FIND_EVAL)
8636 : FIND_STRING);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008637 if (resultlen == 0)
8638 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008639 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008640 return NULL;
8641 }
8642 }
8643
8644 /*
8645 * '#': Alternate file name
8646 * '%': Current file name
8647 * File name under the cursor
8648 * File name for autocommand
8649 * and following modifiers
8650 */
8651 else
8652 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008653 int off = 0;
8654
Bram Moolenaar071d4272004-06-13 20:20:40 +00008655 switch (spec_idx)
8656 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008657 case SPEC_PERC:
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01008658#ifdef FEAT_EVAL
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008659 if (!in_vim9script() || src[1] != '%')
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01008660#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008661 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008662 // '%': current file
8663 if (curbuf->b_fname == NULL)
8664 {
8665 result = (char_u *)"";
8666 valid = 0; // Must have ":p:h" to be valid
8667 }
8668 else
8669 {
8670 result = curbuf->b_fname;
8671 tilde_file = STRCMP(result, "~") == 0;
8672 }
8673 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008674 }
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01008675#ifdef FEAT_EVAL
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008676 // "%%" alternate file
8677 off = 1;
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01008678#endif
Bram Moolenaar9618a252020-12-27 19:18:03 +01008679 // FALLTHROUGH
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008680 case SPEC_HASH: // '#' or "#99": alternate file
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008681 if (off == 0 ? src[1] == '#' : src[2] == '%')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008682 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008683 // "##" or "%%%": the argument list
Bram Moolenaar071d4272004-06-13 20:20:40 +00008684 result = arg_all();
8685 resultbuf = result;
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008686 *usedlen = off + 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +00008687 if (escaped != NULL)
8688 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008689 skip_mod = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008690 break;
8691 }
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008692 s = src + off + 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008693 if (*s == '<') // "#<99" uses v:oldfiles
Bram Moolenaard812df62008-11-09 12:46:09 +00008694 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008695 i = (int)getdigits(&s);
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008696 if (s == src + off + 2 && src[off + 1] == '-')
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008697 // just a minus sign, don't skip over it
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008698 s--;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008699 *usedlen = (int)(s - src); // length of what we expand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008700
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008701 if (src[off + 1] == '<' && i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008702 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008703 if (*usedlen < off + 2)
Bram Moolenaard812df62008-11-09 12:46:09 +00008704 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008705 // Should we give an error message for #<text?
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008706 *usedlen = off + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00008707 return NULL;
8708 }
8709#ifdef FEAT_EVAL
8710 result = list_find_str(get_vim_var_list(VV_OLDFILES),
8711 (long)i);
8712 if (result == NULL)
8713 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008714 *errormsg = "";
Bram Moolenaard812df62008-11-09 12:46:09 +00008715 return NULL;
8716 }
8717#else
Bram Moolenaar8e481e82019-01-15 20:07:48 +01008718 *errormsg = _("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008719 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00008720#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008721 }
8722 else
Bram Moolenaard812df62008-11-09 12:46:09 +00008723 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008724 if (i == 0 && src[off + 1] == '<' && *usedlen > off + 1)
8725 *usedlen = off + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00008726 buf = buflist_findnr(i);
8727 if (buf == NULL)
8728 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008729 *errormsg = _("E194: No alternate file name to substitute for '#'");
Bram Moolenaard812df62008-11-09 12:46:09 +00008730 return NULL;
8731 }
8732 if (lnump != NULL)
8733 *lnump = ECMD_LAST;
8734 if (buf->b_fname == NULL)
8735 {
8736 result = (char_u *)"";
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008737 valid = 0; // Must have ":p:h" to be valid
Bram Moolenaard812df62008-11-09 12:46:09 +00008738 }
8739 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008740 {
Bram Moolenaard812df62008-11-09 12:46:09 +00008741 result = buf->b_fname;
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008742 tilde_file = STRCMP(result, "~") == 0;
8743 }
Bram Moolenaard812df62008-11-09 12:46:09 +00008744 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008745 break;
8746
8747#ifdef FEAT_SEARCHPATH
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008748 case SPEC_CFILE: // file name under cursor
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008749 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008750 if (result == NULL)
8751 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008752 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008753 return NULL;
8754 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008755 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00008756 break;
8757#endif
8758
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008759 case SPEC_AFILE: // file name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008760 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +00008761 if (result != NULL && !autocmd_fname_full)
8762 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008763 // Still need to turn the fname into a full path. It is
8764 // postponed to avoid a delay when <afile> is not used.
Bram Moolenaarf6dad432008-09-18 19:29:58 +00008765 autocmd_fname_full = TRUE;
8766 result = FullName_save(autocmd_fname, FALSE);
8767 vim_free(autocmd_fname);
8768 autocmd_fname = result;
8769 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008770 if (result == NULL)
8771 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008772 *errormsg = _("E495: no autocommand file name to substitute for \"<afile>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008773 return NULL;
8774 }
Bram Moolenaara0174af2008-01-02 20:08:25 +00008775 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008776 break;
8777
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008778 case SPEC_ABUF: // buffer number for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008779 if (autocmd_bufnr <= 0)
8780 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008781 *errormsg = _("E496: no autocommand buffer number to substitute for \"<abuf>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008782 return NULL;
8783 }
8784 sprintf((char *)strbuf, "%d", autocmd_bufnr);
8785 result = strbuf;
8786 break;
8787
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008788 case SPEC_AMATCH: // match name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008789 result = autocmd_match;
8790 if (result == NULL)
8791 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008792 *errormsg = _("E497: no autocommand match name to substitute for \"<amatch>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008793 return NULL;
8794 }
8795 break;
8796
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008797 case SPEC_SFILE: // file name for ":so" command
Bram Moolenaara5d04232020-07-26 15:37:02 +02008798 case SPEC_STACK: // call stack
Bram Moolenaar4f25b1a2020-09-10 19:25:05 +02008799 result = estack_sfile(spec_idx == SPEC_SFILE
8800 ? ESTACK_SFILE : ESTACK_STACK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008801 if (result == NULL)
8802 {
Bram Moolenaara5d04232020-07-26 15:37:02 +02008803 *errormsg = spec_idx == SPEC_SFILE
8804 ? _("E498: no :source file name to substitute for \"<sfile>\"")
8805 : _("E489: no call stack to substitute for \"<stack>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008806 return NULL;
8807 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008808 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00008809 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008810
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008811 case SPEC_SLNUM: // line in file for ":so" command
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008812 if (SOURCING_NAME == NULL || SOURCING_LNUM == 0)
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008813 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008814 *errormsg = _("E842: no line number to use for \"<slnum>\"");
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008815 return NULL;
8816 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008817 sprintf((char *)strbuf, "%ld", SOURCING_LNUM);
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008818 result = strbuf;
8819 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008820
8821#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008822 case SPEC_SFLNUM: // line in script file
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008823 if (current_sctx.sc_lnum + SOURCING_LNUM == 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008824 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008825 *errormsg = _("E961: no line number to use for \"<sflnum>\"");
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008826 return NULL;
8827 }
8828 sprintf((char *)strbuf, "%ld",
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008829 (long)(current_sctx.sc_lnum + SOURCING_LNUM));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008830 result = strbuf;
8831 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008832
Bram Moolenaar90944302020-08-01 20:45:11 +02008833 case SPEC_SID:
8834 if (current_sctx.sc_sid <= 0)
8835 {
8836 *errormsg = _(e_usingsid);
8837 return NULL;
8838 }
8839 sprintf((char *)strbuf, "<SNR>%d_", current_sctx.sc_sid);
8840 result = strbuf;
8841 break;
Bram Moolenaare4218b92020-08-01 21:11:38 +02008842#endif
Bram Moolenaar90944302020-08-01 20:45:11 +02008843
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008844#ifdef FEAT_CLIENTSERVER
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008845 case SPEC_CLIENT: // Source of last submitted input
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008846 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
8847 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008848 result = strbuf;
8849 break;
8850#endif
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008851
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02008852 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008853 result = (char_u *)""; // avoid gcc warning
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02008854 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008855 }
8856
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008857 resultlen = (int)STRLEN(result); // length of new string
8858 if (src[*usedlen] == '<') // remove the file name extension
Bram Moolenaar071d4272004-06-13 20:20:40 +00008859 {
8860 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008861 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008862 resultlen = (int)(s - result);
8863 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008864 else if (!skip_mod)
8865 {
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008866 valid |= modify_fname(src, tilde_file, usedlen, &result, &resultbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008867 &resultlen);
8868 if (result == NULL)
8869 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008870 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008871 return NULL;
8872 }
8873 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008874 }
8875
8876 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
8877 {
8878 if (valid != VALID_HEAD + VALID_PATH)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008879 // xgettext:no-c-format
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008880 *errormsg = _("E499: Empty file name for '%' or '#', only works with \":p:h\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008881 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008882 *errormsg = _("E500: Evaluates to an empty string");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008883 result = NULL;
8884 }
8885 else
8886 result = vim_strnsave(result, resultlen);
8887 vim_free(resultbuf);
8888 return result;
8889}
8890
8891/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008892 * Expand the <sfile> string in "arg".
8893 *
8894 * Returns an allocated string, or NULL for any error.
8895 */
8896 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008897expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008898{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008899 char *errormsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008900 int len;
8901 char_u *result;
8902 char_u *newres;
8903 char_u *repl;
8904 int srclen;
8905 char_u *p;
8906
8907 result = vim_strsave(arg);
8908 if (result == NULL)
8909 return NULL;
8910
8911 for (p = result; *p; )
8912 {
8913 if (STRNCMP(p, "<sfile>", 7) != 0)
8914 ++p;
8915 else
8916 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008917 // replace "<sfile>" with the sourced file name, and do ":" stuff
Bram Moolenaar63b92542007-03-27 14:57:09 +00008918 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008919 if (errormsg != NULL)
8920 {
8921 if (*errormsg)
8922 emsg(errormsg);
8923 vim_free(result);
8924 return NULL;
8925 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008926 if (repl == NULL) // no match (cannot happen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008927 {
8928 p += srclen;
8929 continue;
8930 }
8931 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
8932 newres = alloc(len);
8933 if (newres == NULL)
8934 {
8935 vim_free(repl);
8936 vim_free(result);
8937 return NULL;
8938 }
8939 mch_memmove(newres, result, (size_t)(p - result));
8940 STRCPY(newres + (p - result), repl);
8941 len = (int)STRLEN(newres);
8942 STRCAT(newres, p + srclen);
8943 vim_free(repl);
8944 vim_free(result);
8945 result = newres;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008946 p = newres + len; // continue after the match
Bram Moolenaar071d4272004-06-13 20:20:40 +00008947 }
8948 }
8949
8950 return result;
8951}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008952
Bram Moolenaar071d4272004-06-13 20:20:40 +00008953#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00008954/*
Bram Moolenaard9462e32011-04-11 21:35:11 +02008955 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +00008956 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +00008957 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008958 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008959dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008960{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008961 if (fname == NULL)
8962 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +02008963 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008964}
8965#endif
8966
8967/*
8968 * ":behave {mswin,xterm}"
8969 */
8970 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008971ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008972{
8973 if (STRCMP(eap->arg, "mswin") == 0)
8974 {
8975 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
8976 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
8977 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
8978 set_option_value((char_u *)"keymodel", 0L,
8979 (char_u *)"startsel,stopsel", 0);
8980 }
8981 else if (STRCMP(eap->arg, "xterm") == 0)
8982 {
8983 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
8984 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
8985 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
8986 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
8987 }
8988 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008989 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008990}
8991
Bram Moolenaar071d4272004-06-13 20:20:40 +00008992static int filetype_detect = FALSE;
8993static int filetype_plugin = FALSE;
8994static int filetype_indent = FALSE;
8995
8996/*
8997 * ":filetype [plugin] [indent] {on,off,detect}"
8998 * on: Load the filetype.vim file to install autocommands for file types.
8999 * off: Load the ftoff.vim file to remove all autocommands for file types.
9000 * plugin on: load filetype.vim and ftplugin.vim
9001 * plugin off: load ftplugof.vim
9002 * indent on: load filetype.vim and indent.vim
9003 * indent off: load indoff.vim
9004 */
9005 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009006ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009007{
9008 char_u *arg = eap->arg;
9009 int plugin = FALSE;
9010 int indent = FALSE;
9011
9012 if (*eap->arg == NUL)
9013 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009014 // Print current status.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009015 smsg("filetype detection:%s plugin:%s indent:%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00009016 filetype_detect ? "ON" : "OFF",
9017 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
9018 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
9019 return;
9020 }
9021
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009022 // Accept "plugin" and "indent" in any order.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009023 for (;;)
9024 {
9025 if (STRNCMP(arg, "plugin", 6) == 0)
9026 {
9027 plugin = TRUE;
9028 arg = skipwhite(arg + 6);
9029 continue;
9030 }
9031 if (STRNCMP(arg, "indent", 6) == 0)
9032 {
9033 indent = TRUE;
9034 arg = skipwhite(arg + 6);
9035 continue;
9036 }
9037 break;
9038 }
9039 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
9040 {
9041 if (*arg == 'o' || !filetype_detect)
9042 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009043 source_runtime((char_u *)FILETYPE_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009044 filetype_detect = TRUE;
9045 if (plugin)
9046 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009047 source_runtime((char_u *)FTPLUGIN_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009048 filetype_plugin = TRUE;
9049 }
9050 if (indent)
9051 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009052 source_runtime((char_u *)INDENT_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009053 filetype_indent = TRUE;
9054 }
9055 }
9056 if (*arg == 'd')
9057 {
Bram Moolenaar1610d052016-06-09 22:53:01 +02009058 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +00009059 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009060 }
9061 }
9062 else if (STRCMP(arg, "off") == 0)
9063 {
9064 if (plugin || indent)
9065 {
9066 if (plugin)
9067 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009068 source_runtime((char_u *)FTPLUGOF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009069 filetype_plugin = FALSE;
9070 }
9071 if (indent)
9072 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009073 source_runtime((char_u *)INDOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009074 filetype_indent = FALSE;
9075 }
9076 }
9077 else
9078 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009079 source_runtime((char_u *)FTOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009080 filetype_detect = FALSE;
9081 }
9082 }
9083 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009084 semsg(_(e_invarg2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009085}
9086
9087/*
Bram Moolenaar3e545692017-06-04 19:00:32 +02009088 * ":setfiletype [FALLBACK] {name}"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009089 */
9090 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009091ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009092{
9093 if (!did_filetype)
Bram Moolenaar3e545692017-06-04 19:00:32 +02009094 {
9095 char_u *arg = eap->arg;
9096
9097 if (STRNCMP(arg, "FALLBACK ", 9) == 0)
9098 arg += 9;
9099
9100 set_option_value((char_u *)"filetype", 0L, arg, OPT_LOCAL);
9101 if (arg != eap->arg)
9102 did_filetype = FALSE;
9103 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009104}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009105
Bram Moolenaar071d4272004-06-13 20:20:40 +00009106 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009107ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009108{
9109#ifdef FEAT_DIGRAPHS
9110 if (*eap->arg != NUL)
9111 putdigraph(eap->arg);
9112 else
Bram Moolenaareae8ae12018-12-14 18:53:02 +01009113 listdigraphs(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009114#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009115 emsg(_("E196: No digraphs in this version"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009116#endif
9117}
9118
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02009119#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
9120 void
9121set_no_hlsearch(int flag)
9122{
9123 no_hlsearch = flag;
9124# ifdef FEAT_EVAL
9125 set_vim_var_nr(VV_HLSEARCH, !no_hlsearch && p_hls);
9126# endif
9127}
9128
Bram Moolenaar071d4272004-06-13 20:20:40 +00009129/*
9130 * ":nohlsearch"
9131 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009132 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009133ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009134{
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02009135 set_no_hlsearch(TRUE);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00009136 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009137}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009138#endif
9139
9140#ifdef FEAT_CRYPT
9141/*
9142 * ":X": Get crypt key
9143 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009144 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009145ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009146{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +01009147 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02009148 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009149}
9150#endif
9151
9152#ifdef FEAT_FOLDING
9153 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009154ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009155{
9156 if (foldManualAllowed(TRUE))
9157 foldCreate(eap->line1, eap->line2);
9158}
9159
9160 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009161ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009162{
9163 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
9164 eap->forceit, FALSE);
9165}
9166
9167 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009168ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009169{
9170 linenr_T lnum;
9171
Bram Moolenaar4facea32019-10-12 20:17:40 +02009172# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02009173 start_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +02009174# endif
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02009175
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009176 // First set the marks for all lines closed/open.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009177 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
9178 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
9179 ml_setmarked(lnum);
9180
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009181 // Execute the command on the marked lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009182 global_exe(eap->arg);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009183 ml_clearmarked(); // clear rest of the marks
Bram Moolenaar4facea32019-10-12 20:17:40 +02009184# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02009185 end_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +02009186# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009187}
9188#endif
Bram Moolenaarf5291f32017-09-14 22:55:37 +02009189
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009190#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar39665952018-08-15 20:59:48 +02009191/*
9192 * Returns TRUE if the supplied Ex cmdidx is for a location list command
9193 * instead of a quickfix command.
9194 */
9195 int
9196is_loclist_cmd(int cmdidx)
9197{
Bram Moolenaar74c8be22018-08-23 22:51:40 +02009198 if (cmdidx < 0 || cmdidx >= CMD_SIZE)
Bram Moolenaar39665952018-08-15 20:59:48 +02009199 return FALSE;
9200 return cmdnames[cmdidx].cmd_name[0] == 'l';
9201}
9202#endif
9203
Bram Moolenaar4facea32019-10-12 20:17:40 +02009204#if defined(FEAT_TIMERS) || defined(PROTO)
Bram Moolenaarf5291f32017-09-14 22:55:37 +02009205 int
9206get_pressedreturn(void)
9207{
9208 return ex_pressedreturn;
9209}
9210
9211 void
9212set_pressedreturn(int val)
9213{
9214 ex_pressedreturn = val;
9215}
9216#endif