blob: 047e368e573352f52f30642f9f306b762d20c245 [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 Moolenaarddef1292019-12-16 17:10:33 +010023static char_u *do_one_cmd(char_u **, int, cstack_T *, char_u *(*fgetline)(int, void *, int, int), void *cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024#else
Bram Moolenaare96a2492019-06-25 04:12:16 +020025static char_u *do_one_cmd(char_u **, int, char_u *(*fgetline)(int, void *, int, int), 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 Moolenaar33c4dbb2018-08-14 16:06:16 +020028static void free_cmdmod(void);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010029static void append_command(char_u *cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000030
Bram Moolenaar071d4272004-06-13 20:20:40 +000031#ifndef FEAT_MENU
32# define ex_emenu ex_ni
33# define ex_menu ex_ni
34# define ex_menutranslate ex_ni
35#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010036static void ex_autocmd(exarg_T *eap);
37static void ex_doautocmd(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010038static void ex_bunload(exarg_T *eap);
39static void ex_buffer(exarg_T *eap);
40static void ex_bmodified(exarg_T *eap);
41static void ex_bnext(exarg_T *eap);
42static void ex_bprevious(exarg_T *eap);
43static void ex_brewind(exarg_T *eap);
44static void ex_blast(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010045static char_u *getargcmd(char_u **);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010046static int getargopt(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000047#ifndef FEAT_QUICKFIX
48# define ex_make ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +000049# define ex_cbuffer ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000050# define ex_cc ex_ni
51# define ex_cnext ex_ni
Bram Moolenaar3ff33112019-05-03 21:56:35 +020052# define ex_cbelow ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000053# define ex_cfile ex_ni
54# define qf_list ex_ni
55# define qf_age ex_ni
Bram Moolenaarf6acffb2016-07-16 16:54:24 +020056# define qf_history ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000057# define ex_helpgrep ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +000058# define ex_vimgrep ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000059#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +020060#if !defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +000061# define ex_cclose ex_ni
62# define ex_copen ex_ni
63# define ex_cwindow ex_ni
Bram Moolenaardcb17002016-07-07 18:58:59 +020064# define ex_cbottom ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000065#endif
Bram Moolenaar1e015462005-09-25 22:16:38 +000066#if !defined(FEAT_QUICKFIX) || !defined(FEAT_EVAL)
67# define ex_cexpr ex_ni
68#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000069
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 Moolenaarf28dbce2016-01-29 22:03:47 +010071static void get_flags(exarg_T *eap);
Bram Moolenaar85363ab2010-07-18 13:58:26 +020072#if !defined(FEAT_PERL) \
73 || !defined(FEAT_PYTHON) || !defined(FEAT_PYTHON3) \
74 || !defined(FEAT_TCL) \
75 || !defined(FEAT_RUBY) \
76 || !defined(FEAT_LUA) \
77 || !defined(FEAT_MZSCHEME)
Bram Moolenaar7bb75552007-07-16 18:39:49 +000078# define HAVE_EX_SCRIPT_NI
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010079static void ex_script_ni(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000080#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010081static char *invalid_range(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010082static void correct_range(exarg_T *eap);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000083#ifdef FEAT_QUICKFIX
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010084static char_u *replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000085#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010086static char_u *repl_cmdline(exarg_T *eap, char_u *src, int srclen, char_u *repl, char_u **cmdlinep);
87static void ex_highlight(exarg_T *eap);
88static void ex_colorscheme(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010089static void ex_cquit(exarg_T *eap);
90static void ex_quit_all(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010091static void ex_close(exarg_T *eap);
92static void ex_win_close(int forceit, win_T *win, tabpage_T *tp);
93static void ex_only(exarg_T *eap);
94static void ex_resize(exarg_T *eap);
95static void ex_stag(exarg_T *eap);
96static void ex_tabclose(exarg_T *eap);
97static void ex_tabonly(exarg_T *eap);
98static void ex_tabnext(exarg_T *eap);
99static void ex_tabmove(exarg_T *eap);
100static void ex_tabs(exarg_T *eap);
Bram Moolenaar4033c552017-09-16 20:54:51 +0200101#if defined(FEAT_QUICKFIX)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100102static void ex_pclose(exarg_T *eap);
103static void ex_ptag(exarg_T *eap);
104static void ex_pedit(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000105#else
106# define ex_pclose ex_ni
107# define ex_ptag ex_ni
108# define ex_pedit ex_ni
109#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100110static void ex_hide(exarg_T *eap);
111static void ex_stop(exarg_T *eap);
112static void ex_exit(exarg_T *eap);
113static void ex_print(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000114#ifdef FEAT_BYTEOFF
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100115static void ex_goto(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000116#else
117# define ex_goto ex_ni
118#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100119static void ex_shell(exarg_T *eap);
120static void ex_preserve(exarg_T *eap);
121static void ex_recover(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100122static void ex_mode(exarg_T *eap);
123static void ex_wrongmodifier(exarg_T *eap);
124static void ex_find(exarg_T *eap);
125static void ex_open(exarg_T *eap);
126static void ex_edit(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000127#ifndef FEAT_GUI
128# define ex_gui ex_nogui
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100129static void ex_nogui(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000130#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +0100131#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100132static void ex_tearoff(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000133#else
134# define ex_tearoff ex_ni
135#endif
Bram Moolenaar29a2c082018-03-05 21:06:23 +0100136#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
137 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100138static void ex_popup(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000139#else
140# define ex_popup ex_ni
141#endif
142#ifndef FEAT_GUI_MSWIN
143# define ex_simalt ex_ni
144#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000145#if !defined(FEAT_GUI_MSWIN) && !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000146# define gui_mch_find_dialog ex_ni
147# define gui_mch_replace_dialog ex_ni
148#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000149#if !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000150# define ex_helpfind ex_ni
151#endif
152#ifndef FEAT_CSCOPE
Bram Moolenaard4db7712016-11-12 19:16:46 +0100153# define ex_cscope ex_ni
154# define ex_scscope ex_ni
155# define ex_cstag ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000156#endif
157#ifndef FEAT_SYN_HL
158# define ex_syntax ex_ni
Bram Moolenaar860cae12010-06-05 23:22:07 +0200159# define ex_ownsyntax ex_ni
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000160#endif
Bram Moolenaarf7512552013-06-06 14:55:19 +0200161#if !defined(FEAT_SYN_HL) || !defined(FEAT_PROFILE)
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +0200162# define ex_syntime ex_ni
163#endif
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000164#ifndef FEAT_SPELL
Bram Moolenaarb765d632005-06-07 21:00:02 +0000165# define ex_spell ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000166# define ex_mkspell ex_ni
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000167# define ex_spelldump ex_ni
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000168# define ex_spellinfo ex_ni
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000169# define ex_spellrepall ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000170#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200171#ifndef FEAT_PERSISTENT_UNDO
172# define ex_rundo ex_ni
173# define ex_wundo ex_ni
174#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200175#ifndef FEAT_LUA
176# define ex_lua ex_script_ni
177# define ex_luado ex_ni
178# define ex_luafile ex_ni
179#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000180#ifndef FEAT_MZSCHEME
181# define ex_mzscheme ex_script_ni
182# define ex_mzfile ex_ni
183#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000184#ifndef FEAT_PERL
185# define ex_perl ex_script_ni
186# define ex_perldo ex_ni
187#endif
188#ifndef FEAT_PYTHON
189# define ex_python ex_script_ni
Bram Moolenaard620aa92013-05-17 16:40:06 +0200190# define ex_pydo ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000191# define ex_pyfile ex_ni
192#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200193#ifndef FEAT_PYTHON3
Bram Moolenaar368373e2010-07-19 20:46:22 +0200194# define ex_py3 ex_script_ni
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200195# define ex_py3do ex_ni
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200196# define ex_py3file ex_ni
197#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100198#if !defined(FEAT_PYTHON) && !defined(FEAT_PYTHON3)
199# define ex_pyx ex_script_ni
200# define ex_pyxdo ex_ni
201# define ex_pyxfile ex_ni
202#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000203#ifndef FEAT_TCL
204# define ex_tcl ex_script_ni
205# define ex_tcldo ex_ni
206# define ex_tclfile ex_ni
207#endif
208#ifndef FEAT_RUBY
209# define ex_ruby ex_script_ni
210# define ex_rubydo ex_ni
211# define ex_rubyfile ex_ni
212#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000213#ifndef FEAT_KEYMAP
214# define ex_loadkeymap ex_ni
215#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100216static void ex_swapname(exarg_T *eap);
217static void ex_syncbind(exarg_T *eap);
218static void ex_read(exarg_T *eap);
219static void ex_pwd(exarg_T *eap);
220static void ex_equal(exarg_T *eap);
221static void ex_sleep(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100222static void ex_winsize(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100223static void ex_wincmd(exarg_T *eap);
Bram Moolenaar843ee412004-06-30 16:16:41 +0000224#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100225static void ex_winpos(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000226#else
227# define ex_winpos ex_ni
228#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100229static void ex_operators(exarg_T *eap);
230static void ex_put(exarg_T *eap);
231static void ex_copymove(exarg_T *eap);
232static void ex_submagic(exarg_T *eap);
233static void ex_join(exarg_T *eap);
234static void ex_at(exarg_T *eap);
235static void ex_bang(exarg_T *eap);
236static void ex_undo(exarg_T *eap);
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200237#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100238static void ex_wundo(exarg_T *eap);
239static void ex_rundo(exarg_T *eap);
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200240#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100241static void ex_redo(exarg_T *eap);
242static void ex_later(exarg_T *eap);
243static void ex_redir(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100244static void ex_redrawstatus(exarg_T *eap);
Bram Moolenaare12bab32019-01-08 22:02:56 +0100245static void ex_redrawtabline(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100246static void close_redir(void);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100247static void ex_mark(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100248static void ex_startinsert(exarg_T *eap);
249static void ex_stopinsert(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000250#ifdef FEAT_FIND_ID
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100251static void ex_checkpath(exarg_T *eap);
252static void ex_findpat(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000253#else
254# define ex_findpat ex_ni
255# define ex_checkpath ex_ni
256#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +0200257#if defined(FEAT_FIND_ID) && defined(FEAT_QUICKFIX)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100258static void ex_psearch(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000259#else
260# define ex_psearch ex_ni
261#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100262static void ex_tag(exarg_T *eap);
263static void ex_tag_cmd(exarg_T *eap, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000264#ifndef FEAT_EVAL
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200265# define ex_break ex_ni
266# define ex_breakadd ex_ni
267# define ex_breakdel ex_ni
268# define ex_breaklist ex_ni
269# define ex_call ex_ni
270# define ex_catch ex_ni
271# define ex_compiler ex_ni
272# define ex_const ex_ni
273# define ex_continue ex_ni
274# define ex_debug ex_ni
275# define ex_debuggreedy ex_ni
276# define ex_delfunction ex_ni
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100277# define ex_disassemble ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000278# define ex_echo ex_ni
279# define ex_echohl ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000280# define ex_else ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200281# define ex_endfunction ex_ni
282# define ex_endif ex_ni
283# define ex_endtry ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000284# define ex_endwhile ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200285# define ex_eval ex_ni
286# define ex_execute ex_ni
287# define ex_finally ex_ni
288# define ex_finish ex_ni
289# define ex_function ex_ni
290# define ex_if ex_ni
291# define ex_let ex_ni
292# define ex_lockvar ex_ni
293# define ex_oldfiles ex_ni
294# define ex_options ex_ni
295# define ex_packadd ex_ni
296# define ex_packloadall ex_ni
297# define ex_return ex_ni
298# define ex_scriptnames ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000299# define ex_throw ex_ni
300# define ex_try ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000301# define ex_unlet ex_ni
Bram Moolenaar65c1b012005-01-31 19:02:28 +0000302# define ex_unlockvar ex_ni
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100303# define ex_vim9script ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200304# define ex_while ex_ni
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100305# define ex_import ex_ni
306# define ex_export ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000307#endif
Bram Moolenaar84538072019-07-28 14:15:42 +0200308#ifndef FEAT_SESSION
Bram Moolenaar071d4272004-06-13 20:20:40 +0000309# define ex_loadview ex_ni
310#endif
Bram Moolenaardefa0672019-07-21 19:25:37 +0200311#ifndef FEAT_VIMINFO
Bram Moolenaar071d4272004-06-13 20:20:40 +0000312# define ex_viminfo ex_ni
313#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100314static void ex_behave(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100315static void ex_filetype(exarg_T *eap);
316static void ex_setfiletype(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000317#ifndef FEAT_DIFF
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000318# define ex_diffoff ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000319# define ex_diffpatch ex_ni
320# define ex_diffgetput ex_ni
321# define ex_diffsplit ex_ni
322# define ex_diffthis ex_ni
323# define ex_diffupdate ex_ni
324#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100325static void ex_digraphs(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000326#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100327static void ex_nohlsearch(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000328#else
329# define ex_nohlsearch ex_ni
330# define ex_match ex_ni
331#endif
332#ifdef FEAT_CRYPT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100333static void ex_X(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000334#else
335# define ex_X ex_ni
336#endif
337#ifdef FEAT_FOLDING
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100338static void ex_fold(exarg_T *eap);
339static void ex_foldopen(exarg_T *eap);
340static void ex_folddo(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000341#else
342# define ex_fold ex_ni
343# define ex_foldopen ex_ni
344# define ex_folddo ex_ni
345#endif
Bram Moolenaar13505972019-01-24 15:04:48 +0100346#if !(defined(HAVE_LOCALE_H) || defined(X_LOCALE))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000347# define ex_language ex_ni
348#endif
349#ifndef FEAT_SIGNS
350# define ex_sign ex_ni
351#endif
Bram Moolenaar009b2592004-10-24 19:18:58 +0000352#ifndef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200353# define ex_nbclose ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000354# define ex_nbkey ex_ni
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200355# define ex_nbstart ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000356#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000357
Bram Moolenaar071d4272004-06-13 20:20:40 +0000358#ifndef FEAT_JUMPLIST
359# define ex_jumps ex_ni
Bram Moolenaar2d358992016-06-12 21:20:54 +0200360# define ex_clearjumps ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000361# define ex_changes ex_ni
362#endif
363
Bram Moolenaar05159a02005-02-26 23:04:13 +0000364#ifndef FEAT_PROFILE
365# define ex_profile ex_ni
366#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200367#ifndef FEAT_TERMINAL
368# define ex_terminal ex_ni
369#endif
Bram Moolenaard4aa83a2019-05-09 18:59:31 +0200370#if !defined(FEAT_X11) || !defined(FEAT_XCLIPBOARD)
371# define ex_xrestore ex_ni
372#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100373#if !defined(FEAT_PROP_POPUP)
Bram Moolenaar682725c2019-05-25 20:10:37 +0200374# define ex_popupclear ex_ni
375#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +0000376
Bram Moolenaar071d4272004-06-13 20:20:40 +0000377/*
378 * Declare cmdnames[].
379 */
380#define DO_DECLARE_EXCMD
381#include "ex_cmds.h"
Bram Moolenaar6de5e122017-04-20 21:55:44 +0200382#include "ex_cmdidxs.h"
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +0100383
Bram Moolenaar071d4272004-06-13 20:20:40 +0000384static char_u dollar_command[2] = {'$', 0};
385
386
387#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100388// Struct for storing a line inside a while/for loop
Bram Moolenaar071d4272004-06-13 20:20:40 +0000389typedef struct
390{
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100391 char_u *line; // command line
392 linenr_T lnum; // sourcing_lnum of the line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000393} wcmd_T;
394
395/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000396 * Structure used to store info for line position in a while or for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000397 * This is required, because do_one_cmd() may invoke ex_function(), which
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000398 * reads more lines that may come from the while/for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000399 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000400struct loop_cookie
Bram Moolenaar071d4272004-06-13 20:20:40 +0000401{
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100402 garray_T *lines_gap; // growarray with line info
403 int current_line; // last read line from growarray
404 int repeating; // TRUE when looping a second time
405 // When "repeating" is FALSE use "getline" and "cookie" to get lines
Bram Moolenaare96a2492019-06-25 04:12:16 +0200406 char_u *(*getline)(int, void *, int, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000407 void *cookie;
408};
409
Bram Moolenaare96a2492019-06-25 04:12:16 +0200410static char_u *get_loop_line(int c, void *cookie, int indent, int do_concat);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100411static int store_loop_line(garray_T *gap, char_u *line);
412static void free_cmdlines(garray_T *gap);
Bram Moolenaared203462004-06-16 11:19:22 +0000413
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100414// Struct to save a few things while debugging. Used in do_cmdline() only.
Bram Moolenaared203462004-06-16 11:19:22 +0000415struct dbg_stuff
416{
417 int trylevel;
418 int force_abort;
419 except_T *caught_stack;
420 char_u *vv_exception;
421 char_u *vv_throwpoint;
422 int did_emsg;
423 int got_int;
424 int did_throw;
425 int need_rethrow;
426 int check_cstack;
427 except_T *current_exception;
428};
429
Bram Moolenaared203462004-06-16 11:19:22 +0000430 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100431save_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000432{
433 dsp->trylevel = trylevel; trylevel = 0;
434 dsp->force_abort = force_abort; force_abort = FALSE;
435 dsp->caught_stack = caught_stack; caught_stack = NULL;
436 dsp->vv_exception = v_exception(NULL);
437 dsp->vv_throwpoint = v_throwpoint(NULL);
438
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100439 // Necessary for debugging an inactive ":catch", ":finally", ":endtry"
Bram Moolenaared203462004-06-16 11:19:22 +0000440 dsp->did_emsg = did_emsg; did_emsg = FALSE;
441 dsp->got_int = got_int; got_int = FALSE;
442 dsp->did_throw = did_throw; did_throw = FALSE;
443 dsp->need_rethrow = need_rethrow; need_rethrow = FALSE;
444 dsp->check_cstack = check_cstack; check_cstack = FALSE;
445 dsp->current_exception = current_exception; current_exception = NULL;
446}
447
448 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100449restore_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000450{
451 suppress_errthrow = FALSE;
452 trylevel = dsp->trylevel;
453 force_abort = dsp->force_abort;
454 caught_stack = dsp->caught_stack;
455 (void)v_exception(dsp->vv_exception);
456 (void)v_throwpoint(dsp->vv_throwpoint);
457 did_emsg = dsp->did_emsg;
458 got_int = dsp->got_int;
459 did_throw = dsp->did_throw;
460 need_rethrow = dsp->need_rethrow;
461 check_cstack = dsp->check_cstack;
462 current_exception = dsp->current_exception;
463}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000464#endif
465
Bram Moolenaar071d4272004-06-13 20:20:40 +0000466/*
467 * do_exmode(): Repeatedly get commands for the "Ex" mode, until the ":vi"
468 * command is given.
469 */
470 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100471do_exmode(
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100472 int improved) // TRUE for "improved Ex" mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000473{
474 int save_msg_scroll;
475 int prev_msg_row;
476 linenr_T prev_line;
Bram Moolenaar79518e22017-02-17 16:31:35 +0100477 varnumber_T changedtick;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000478
479 if (improved)
480 exmode_active = EXMODE_VIM;
481 else
482 exmode_active = EXMODE_NORMAL;
483 State = NORMAL;
484
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100485 // When using ":global /pat/ visual" and then "Q" we return to continue
486 // the :global command.
Bram Moolenaardf177f62005-02-22 08:39:57 +0000487 if (global_busy)
488 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000489
490 save_msg_scroll = msg_scroll;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100491 ++RedrawingDisabled; // don't redisplay the window
492 ++no_wait_return; // don't wait for return
Bram Moolenaar071d4272004-06-13 20:20:40 +0000493#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100494 // Ignore scrollbar and mouse events in Ex mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000495 ++hold_gui_events;
496#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000497
Bram Moolenaar32526b32019-01-19 17:43:09 +0100498 msg(_("Entering Ex mode. Type \"visual\" to go to Normal mode."));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000499 while (exmode_active)
500 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100501 // Check for a ":normal" command and no more characters left.
Bram Moolenaar7c626922005-02-07 22:01:03 +0000502 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
503 {
504 exmode_active = FALSE;
505 break;
506 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000507 msg_scroll = TRUE;
508 need_wait_return = FALSE;
509 ex_pressedreturn = FALSE;
510 ex_no_reprint = FALSE;
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100511 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000512 prev_msg_row = msg_row;
513 prev_line = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000514 if (improved)
515 {
516 cmdline_row = msg_row;
517 do_cmdline(NULL, getexline, NULL, 0);
518 }
519 else
520 do_cmdline(NULL, getexmodeline, NULL, DOCMD_NOWAIT);
521 lines_left = Rows - 1;
522
Bram Moolenaardf177f62005-02-22 08:39:57 +0000523 if ((prev_line != curwin->w_cursor.lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100524 || changedtick != CHANGEDTICK(curbuf)) && !ex_no_reprint)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000525 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000526 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100527 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000528 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000529 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000530 if (ex_pressedreturn)
531 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100532 // go up one line, to overwrite the ":<CR>" line, so the
533 // output doesn't contain empty lines.
Bram Moolenaardf177f62005-02-22 08:39:57 +0000534 msg_row = prev_msg_row;
535 if (prev_msg_row == Rows - 1)
536 msg_row--;
537 }
538 msg_col = 0;
539 print_line_no_prefix(curwin->w_cursor.lnum, FALSE, FALSE);
540 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000541 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000542 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100543 else if (ex_pressedreturn && !ex_no_reprint) // must be at EOF
Bram Moolenaardf177f62005-02-22 08:39:57 +0000544 {
545 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100546 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000547 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100548 emsg(_("E501: At end-of-file"));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000549 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000550 }
551
552#ifdef FEAT_GUI
553 --hold_gui_events;
554#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000555 --RedrawingDisabled;
556 --no_wait_return;
557 update_screen(CLEAR);
558 need_wait_return = FALSE;
559 msg_scroll = save_msg_scroll;
560}
561
562/*
Bram Moolenaar4facea32019-10-12 20:17:40 +0200563 * Print the executed command for when 'verbose' is set.
564 * When "lnum" is 0 only print the command.
565 */
566 static void
567msg_verbose_cmd(linenr_T lnum, char_u *cmd)
568{
569 ++no_wait_return;
570 verbose_enter_scroll();
571
572 if (lnum == 0)
573 smsg(_("Executing: %s"), cmd);
574 else
575 smsg(_("line %ld: %s"), (long)lnum, cmd);
576 if (msg_silent == 0)
577 msg_puts("\n"); // don't overwrite this
578
579 verbose_leave_scroll();
580 --no_wait_return;
581}
582
583/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000584 * Execute a simple command line. Used for translated commands like "*".
585 */
586 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100587do_cmdline_cmd(char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000588{
589 return do_cmdline(cmd, NULL, NULL,
590 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
591}
592
593/*
594 * do_cmdline(): execute one Ex command line
595 *
596 * 1. Execute "cmdline" when it is not NULL.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100597 * If "cmdline" is NULL, or more lines are needed, fgetline() is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000598 * 2. Split up in parts separated with '|'.
599 *
600 * This function can be called recursively!
601 *
602 * flags:
603 * DOCMD_VERBOSE - The command will be included in the error message.
604 * DOCMD_NOWAIT - Don't call wait_return() and friends.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100605 * DOCMD_REPEAT - Repeat execution until fgetline() returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000606 * DOCMD_KEYTYPED - Don't reset KeyTyped.
607 * DOCMD_EXCRESET - Reset the exception environment (used for debugging).
608 * DOCMD_KEEPLINE - Store first typed line (for repeating with ".").
609 *
610 * return FAIL if cmdline could not be executed, OK otherwise
611 */
612 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100613do_cmdline(
614 char_u *cmdline,
Bram Moolenaare96a2492019-06-25 04:12:16 +0200615 char_u *(*fgetline)(int, void *, int, int),
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100616 void *cookie, // argument for fgetline()
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100617 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000618{
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100619 char_u *next_cmdline; // next cmd to execute
620 char_u *cmdline_copy = NULL; // copy of cmd line
621 int used_getline = FALSE; // used "fgetline" to obtain command
622 static int recursive = 0; // recursive depth
Bram Moolenaar071d4272004-06-13 20:20:40 +0000623 int msg_didout_before_start = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100624 int count = 0; // line number count
625 int did_inc = FALSE; // incremented RedrawingDisabled
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626 int retval = OK;
627#ifdef FEAT_EVAL
Bram Moolenaarddef1292019-12-16 17:10:33 +0100628 cstack_T cstack; // conditional stack
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100629 garray_T lines_ga; // keep lines for ":while"/":for"
630 int current_line = 0; // active line in lines_ga
631 char_u *fname = NULL; // function or script name
632 linenr_T *breakpoint = NULL; // ptr to breakpoint field in cookie
633 int *dbg_tick = NULL; // ptr to dbg_tick field in cookie
634 struct dbg_stuff debug_saved; // saved things for debug mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000635 int initial_trylevel;
636 struct msglist **saved_msg_list = NULL;
637 struct msglist *private_msg_list;
638
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100639 // "fgetline" and "cookie" passed to do_one_cmd()
Bram Moolenaare96a2492019-06-25 04:12:16 +0200640 char_u *(*cmd_getline)(int, void *, int, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641 void *cmd_cookie;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000642 struct loop_cookie cmd_loop_cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000643 void *real_cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000644 int getline_is_func;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000645#else
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100646# define cmd_getline fgetline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000647# define cmd_cookie cookie
648#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100649 static int call_depth = 0; // recursiveness
Bram Moolenaar2196bce2020-04-12 20:01:11 +0200650#ifdef FEAT_EVAL
Bram Moolenaare31ee862020-01-07 20:59:34 +0100651 ESTACK_CHECK_DECLARATION
Bram Moolenaar071d4272004-06-13 20:20:40 +0000652
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100653 // For every pair of do_cmdline()/do_one_cmd() calls, use an extra memory
654 // location for storing error messages to be converted to an exception.
655 // This ensures that the do_errthrow() call in do_one_cmd() does not
656 // combine the messages stored by an earlier invocation of do_one_cmd()
657 // with the command name of the later one. This would happen when
658 // BufWritePost autocommands are executed after a write error.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659 saved_msg_list = msg_list;
660 msg_list = &private_msg_list;
661 private_msg_list = NULL;
662#endif
663
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100664 // It's possible to create an endless loop with ":execute", catch that
665 // here. The value of 200 allows nested function calls, ":source", etc.
666 // Allow 200 or 'maxfuncdepth', whatever is larger.
Bram Moolenaarb094ff42017-01-02 16:16:39 +0100667 if (call_depth >= 200
668#ifdef FEAT_EVAL
669 && call_depth >= p_mfd
670#endif
671 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000672 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100673 emsg(_("E169: Command too recursive"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000674#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100675 // When converting to an exception, we do not include the command name
676 // since this is not an error of the specific command.
Bram Moolenaarddef1292019-12-16 17:10:33 +0100677 do_errthrow((cstack_T *)NULL, (char_u *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678 msg_list = saved_msg_list;
679#endif
680 return FAIL;
681 }
682 ++call_depth;
683
684#ifdef FEAT_EVAL
685 cstack.cs_idx = -1;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000686 cstack.cs_looplevel = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000687 cstack.cs_trylevel = 0;
688 cstack.cs_emsg_silent_list = NULL;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000689 cstack.cs_lflags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690 ga_init2(&lines_ga, (int)sizeof(wcmd_T), 10);
691
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100692 real_cookie = getline_cookie(fgetline, cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100694 // Inside a function use a higher nesting level.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100695 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000696 if (getline_is_func && ex_nesting_level == func_level(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697 ++ex_nesting_level;
698
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100699 // Get the function or script name and the address where the next breakpoint
700 // line and the debug tick for a function or script are stored.
Bram Moolenaar05159a02005-02-26 23:04:13 +0000701 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000702 {
703 fname = func_name(real_cookie);
704 breakpoint = func_breakpoint(real_cookie);
705 dbg_tick = func_dbg_tick(real_cookie);
706 }
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100707 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100709 fname = SOURCING_NAME;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000710 breakpoint = source_breakpoint(real_cookie);
711 dbg_tick = source_dbg_tick(real_cookie);
712 }
713
714 /*
715 * Initialize "force_abort" and "suppress_errthrow" at the top level.
716 */
717 if (!recursive)
718 {
719 force_abort = FALSE;
720 suppress_errthrow = FALSE;
721 }
722
723 /*
724 * If requested, store and reset the global values controlling the
Bram Moolenaar89d40322006-08-29 15:30:07 +0000725 * exception handling (used when debugging). Otherwise clear it to avoid
726 * a bogus compiler warning when the optimizer uses inline functions...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000727 */
Bram Moolenaarb4872942006-05-13 10:32:52 +0000728 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +0000729 save_dbg_stuff(&debug_saved);
Bram Moolenaar89d40322006-08-29 15:30:07 +0000730 else
Bram Moolenaara80faa82020-04-12 19:37:17 +0200731 CLEAR_FIELD(debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732
733 initial_trylevel = trylevel;
734
735 /*
736 * "did_throw" will be set to TRUE when an exception is being thrown.
737 */
738 did_throw = FALSE;
739#endif
740 /*
741 * "did_emsg" will be set to TRUE when emsg() is used, in which case we
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000742 * cancel the whole command line, and any if/endif or loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000743 * If force_abort is set, we cancel everything.
744 */
745 did_emsg = FALSE;
746
747 /*
748 * KeyTyped is only set when calling vgetc(). Reset it here when not
749 * calling vgetc() (sourced command lines).
750 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100751 if (!(flags & DOCMD_KEYTYPED)
752 && !getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753 KeyTyped = FALSE;
754
755 /*
756 * Continue executing command lines:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000757 * - when inside an ":if", ":while" or ":for"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000758 * - for multiple commands on one line, separated with '|'
759 * - when repeating until there are no more lines (for ":source")
760 */
761 next_cmdline = cmdline;
762 do
763 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000764#ifdef FEAT_EVAL
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100765 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000766#endif
767
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100768 // stop skipping cmds for an error msg after all endif/while/for
Bram Moolenaar071d4272004-06-13 20:20:40 +0000769 if (next_cmdline == NULL
770#ifdef FEAT_EVAL
771 && !force_abort
772 && cstack.cs_idx < 0
Bram Moolenaar05159a02005-02-26 23:04:13 +0000773 && !(getline_is_func && func_has_abort(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000774#endif
775 )
776 did_emsg = FALSE;
777
778 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000779 * 1. If repeating a line in a loop, get a line from lines_ga.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100780 * 2. If no line given: Get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000781 * 3. If a line is given: Make a copy, so we can mess with it.
782 */
783
784#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100785 // 1. If repeating, get a previous line from lines_ga.
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000786 if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100788 // Each '|' separated command is stored separately in lines_ga, to
789 // be able to jump to it. Don't use next_cmdline now.
Bram Moolenaard23a8232018-02-10 18:45:26 +0100790 VIM_CLEAR(cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100792 // Check if a function has returned or, unless it has an unclosed
793 // try conditional, aborted.
Bram Moolenaar05159a02005-02-26 23:04:13 +0000794 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000795 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000796# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000797 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000798 func_line_end(real_cookie);
799# endif
800 if (func_has_ended(real_cookie))
801 {
802 retval = FAIL;
803 break;
804 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000806#ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000807 else if (do_profiling == PROF_YES
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100808 && getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000809 script_line_end();
810#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100812 // Check if a sourced file hit a ":finish" command.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100813 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814 {
815 retval = FAIL;
816 break;
817 }
818
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100819 // If breakpoints have been added/deleted need to check for it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820 if (breakpoint != NULL && dbg_tick != NULL
821 && *dbg_tick != debug_tick)
822 {
823 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100824 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100825 fname, SOURCING_LNUM);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826 *dbg_tick = debug_tick;
827 }
828
829 next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100830 SOURCING_LNUM = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000831
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100832 // Did we encounter a breakpoint?
Bram Moolenaar071d4272004-06-13 20:20:40 +0000833 if (breakpoint != NULL && *breakpoint != 0
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100834 && *breakpoint <= SOURCING_LNUM)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000835 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100836 dbg_breakpoint(fname, SOURCING_LNUM);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100837 // Find next breakpoint.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000838 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100839 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100840 fname, SOURCING_LNUM);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000841 *dbg_tick = debug_tick;
842 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000843# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000844 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000845 {
846 if (getline_is_func)
847 func_line_start(real_cookie);
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100848 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000849 script_line_start();
850 }
851# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852 }
853
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000854 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100856 // Inside a while/for loop we need to store the lines and use them
857 // again. Pass a different "fgetline" function to do_one_cmd()
858 // below, so that it stores lines in or reads them from
859 // "lines_ga". Makes it possible to define a function inside a
860 // while/for loop.
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000861 cmd_getline = get_loop_line;
862 cmd_cookie = (void *)&cmd_loop_cookie;
863 cmd_loop_cookie.lines_gap = &lines_ga;
864 cmd_loop_cookie.current_line = current_line;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100865 cmd_loop_cookie.getline = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000866 cmd_loop_cookie.cookie = cookie;
867 cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000868 }
869 else
870 {
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100871 cmd_getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000872 cmd_cookie = cookie;
873 }
874#endif
875
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100876 // 2. If no line given, get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000877 if (next_cmdline == NULL)
878 {
879 /*
880 * Need to set msg_didout for the first line after an ":if",
881 * otherwise the ":if" will be overwritten.
882 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100883 if (count == 1 && getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000884 msg_didout = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100885 if (fgetline == NULL || (next_cmdline = fgetline(':', cookie,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000886#ifdef FEAT_EVAL
887 cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2
888#else
889 0
890#endif
Bram Moolenaare96a2492019-06-25 04:12:16 +0200891 , TRUE)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000892 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100893 // Don't call wait_return for aborted command line. The NULL
894 // returned for the end of a sourced file or executed function
895 // doesn't do this.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896 if (KeyTyped && !(flags & DOCMD_REPEAT))
897 need_wait_return = FALSE;
898 retval = FAIL;
899 break;
900 }
901 used_getline = TRUE;
902
903 /*
904 * Keep the first typed line. Clear it when more lines are typed.
905 */
906 if (flags & DOCMD_KEEPLINE)
907 {
908 vim_free(repeat_cmdline);
909 if (count == 0)
910 repeat_cmdline = vim_strsave(next_cmdline);
911 else
912 repeat_cmdline = NULL;
913 }
914 }
915
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100916 // 3. Make a copy of the command so we can mess with it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000917 else if (cmdline_copy == NULL)
918 {
919 next_cmdline = vim_strsave(next_cmdline);
920 if (next_cmdline == NULL)
921 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100922 emsg(_(e_outofmem));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000923 retval = FAIL;
924 break;
925 }
926 }
927 cmdline_copy = next_cmdline;
928
929#ifdef FEAT_EVAL
930 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000931 * Save the current line when inside a ":while" or ":for", and when
932 * the command looks like a ":while" or ":for", because we may need it
933 * later. When there is a '|' and another command, it is stored
934 * separately, because we need to be able to jump back to it from an
935 * :endwhile/:endfor.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000936 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000937 if (current_line == lines_ga.ga_len
938 && (cstack.cs_looplevel || has_loop_cmd(next_cmdline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000940 if (store_loop_line(&lines_ga, next_cmdline) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000941 {
942 retval = FAIL;
943 break;
944 }
945 }
946 did_endif = FALSE;
947#endif
948
949 if (count++ == 0)
950 {
951 /*
952 * All output from the commands is put below each other, without
953 * waiting for a return. Don't do this when executing commands
954 * from a script or when being called recursive (e.g. for ":e
955 * +command file").
956 */
957 if (!(flags & DOCMD_NOWAIT) && !recursive)
958 {
959 msg_didout_before_start = msg_didout;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100960 msg_didany = FALSE; // no output yet
Bram Moolenaar071d4272004-06-13 20:20:40 +0000961 msg_start();
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100962 msg_scroll = TRUE; // put messages below each other
963 ++no_wait_return; // don't wait for return until finished
Bram Moolenaar071d4272004-06-13 20:20:40 +0000964 ++RedrawingDisabled;
965 did_inc = TRUE;
966 }
967 }
968
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100969 if (p_verbose >= 15 && SOURCING_NAME != NULL)
970 msg_verbose_cmd(SOURCING_LNUM, cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971
972 /*
973 * 2. Execute one '|' separated command.
974 * do_one_cmd() will return NULL if there is no trailing '|'.
975 * "cmdline_copy" can change, e.g. for '%' and '#' expansion.
976 */
977 ++recursive;
978 next_cmdline = do_one_cmd(&cmdline_copy, flags & DOCMD_VERBOSE,
979#ifdef FEAT_EVAL
980 &cstack,
981#endif
982 cmd_getline, cmd_cookie);
983 --recursive;
984
985#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000986 if (cmd_cookie == (void *)&cmd_loop_cookie)
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100987 // Use "current_line" from "cmd_loop_cookie", it may have been
988 // incremented when defining a function.
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000989 current_line = cmd_loop_cookie.current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990#endif
991
992 if (next_cmdline == NULL)
993 {
Bram Moolenaard23a8232018-02-10 18:45:26 +0100994 VIM_CLEAR(cmdline_copy);
Bram Moolenaard7663c22019-08-06 21:59:57 +0200995
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996 /*
997 * If the command was typed, remember it for the ':' register.
998 * Do this AFTER executing the command to make :@: work.
999 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001000 if (getline_equal(fgetline, cookie, getexline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001 && new_last_cmdline != NULL)
1002 {
1003 vim_free(last_cmdline);
1004 last_cmdline = new_last_cmdline;
1005 new_last_cmdline = NULL;
1006 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007 }
1008 else
1009 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001010 // need to copy the command after the '|' to cmdline_copy, for the
1011 // next do_one_cmd()
Bram Moolenaara7241f52008-06-24 20:39:31 +00001012 STRMOVE(cmdline_copy, next_cmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013 next_cmdline = cmdline_copy;
1014 }
1015
1016
1017#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001018 // reset did_emsg for a function that is not aborted by an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019 if (did_emsg && !force_abort
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001020 && getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021 && !func_has_abort(real_cookie))
1022 did_emsg = FALSE;
1023
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001024 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025 {
1026 ++current_line;
1027
1028 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001029 * An ":endwhile", ":endfor" and ":continue" is handled here.
1030 * If we were executing commands, jump back to the ":while" or
1031 * ":for".
1032 * If we were not executing commands, decrement cs_looplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001033 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001034 if (cstack.cs_lflags & (CSL_HAD_CONT | CSL_HAD_ENDLOOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001036 cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001037
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001038 // Jump back to the matching ":while" or ":for". Be careful
1039 // not to use a cs_line[] from an entry that isn't a ":while"
1040 // or ":for": It would make "current_line" invalid and can
1041 // cause a crash.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042 if (!did_emsg && !got_int && !did_throw
1043 && cstack.cs_idx >= 0
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001044 && (cstack.cs_flags[cstack.cs_idx]
1045 & (CSF_WHILE | CSF_FOR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001046 && cstack.cs_line[cstack.cs_idx] >= 0
1047 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE))
1048 {
1049 current_line = cstack.cs_line[cstack.cs_idx];
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001050 // remember we jumped there
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001051 cstack.cs_lflags |= CSL_HAD_LOOP;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001052 line_breakcheck(); // check if CTRL-C typed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001053
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001054 // Check for the next breakpoint at or after the ":while"
1055 // or ":for".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056 if (breakpoint != NULL)
1057 {
1058 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001059 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060 fname,
1061 ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1);
1062 *dbg_tick = debug_tick;
1063 }
1064 }
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001065 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001066 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001067 // can only get here with ":endwhile" or ":endfor"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068 if (cstack.cs_idx >= 0)
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001069 rewind_conditionals(&cstack, cstack.cs_idx - 1,
1070 CSF_WHILE | CSF_FOR, &cstack.cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071 }
1072 }
1073
1074 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001075 * For a ":while" or ":for" we need to remember the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001076 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001077 else if (cstack.cs_lflags & CSL_HAD_LOOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001079 cstack.cs_lflags &= ~CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080 cstack.cs_line[cstack.cs_idx] = current_line - 1;
1081 }
1082 }
1083
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001084 // Check for the next breakpoint after a watchexpression
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01001085 if (breakpoint != NULL && has_watchexpr())
1086 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001087 *breakpoint = dbg_find_breakpoint(FALSE, fname, SOURCING_LNUM);
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01001088 *dbg_tick = debug_tick;
1089 }
1090
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091 /*
1092 * When not inside any ":while" loop, clear remembered lines.
1093 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001094 if (cstack.cs_looplevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095 {
1096 if (lines_ga.ga_len > 0)
1097 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001098 SOURCING_LNUM =
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099 ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum;
1100 free_cmdlines(&lines_ga);
1101 }
1102 current_line = 0;
1103 }
1104
1105 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001106 * A ":finally" makes did_emsg, got_int, and did_throw pending for
1107 * being restored at the ":endtry". Reset them here and set the
1108 * ACTIVE and FINALLY flags, so that the finally clause gets executed.
1109 * This includes the case where a missing ":endif", ":endwhile" or
1110 * ":endfor" was detected by the ":finally" itself.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001111 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001112 if (cstack.cs_lflags & CSL_HAD_FINA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001114 cstack.cs_lflags &= ~CSL_HAD_FINA;
1115 report_make_pending(cstack.cs_pending[cstack.cs_idx]
1116 & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001117 did_throw ? (void *)current_exception : NULL);
1118 did_emsg = got_int = did_throw = FALSE;
1119 cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY;
1120 }
1121
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001122 // Update global "trylevel" for recursive calls to do_cmdline() from
1123 // within this loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124 trylevel = initial_trylevel + cstack.cs_trylevel;
1125
1126 /*
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001127 * If the outermost try conditional (across function calls and sourced
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128 * files) is aborted because of an error, an interrupt, or an uncaught
1129 * exception, cancel everything. If it is left normally, reset
1130 * force_abort to get the non-EH compatible abortion behavior for
1131 * the rest of the script.
1132 */
1133 if (trylevel == 0 && !did_emsg && !got_int && !did_throw)
1134 force_abort = FALSE;
1135
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001136 // Convert an interrupt to an exception if appropriate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137 (void)do_intthrow(&cstack);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001138#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139
1140 }
1141 /*
1142 * Continue executing command lines when:
1143 * - no CTRL-C typed, no aborting error, no exception thrown or try
1144 * conditionals need to be checked for executing finally clauses or
1145 * catching an interrupt exception
1146 * - didn't get an error message or lines are not typed
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001147 * - there is a command after '|', inside a :if, :while, :for or :try, or
Bram Moolenaar071d4272004-06-13 20:20:40 +00001148 * looping for ":source" command or function call.
1149 */
1150 while (!((got_int
1151#ifdef FEAT_EVAL
1152 || (did_emsg && force_abort) || did_throw
1153#endif
1154 )
1155#ifdef FEAT_EVAL
1156 && cstack.cs_trylevel == 0
1157#endif
1158 )
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001159 && !(did_emsg
1160#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001161 // Keep going when inside try/catch, so that the error can be
1162 // deal with, except when it is a syntax error, it may cause
1163 // the :endtry to be missed.
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001164 && (cstack.cs_trylevel == 0 || did_emsg_syntax)
1165#endif
1166 && used_getline
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001167 && (getline_equal(fgetline, cookie, getexmodeline)
1168 || getline_equal(fgetline, cookie, getexline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169 && (next_cmdline != NULL
1170#ifdef FEAT_EVAL
1171 || cstack.cs_idx >= 0
1172#endif
1173 || (flags & DOCMD_REPEAT)));
1174
1175 vim_free(cmdline_copy);
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001176 did_emsg_syntax = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001177#ifdef FEAT_EVAL
1178 free_cmdlines(&lines_ga);
1179 ga_clear(&lines_ga);
1180
1181 if (cstack.cs_idx >= 0)
1182 {
1183 /*
1184 * If a sourced file or executed function ran to its end, report the
1185 * unclosed conditional.
1186 */
1187 if (!got_int && !did_throw
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001188 && ((getline_equal(fgetline, cookie, getsourceline)
1189 && !source_finished(fgetline, cookie))
1190 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191 && !func_has_ended(real_cookie))))
1192 {
1193 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001194 emsg(_(e_endtry));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001196 emsg(_(e_endwhile));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001197 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001198 emsg(_(e_endfor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001200 emsg(_(e_endif));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201 }
1202
1203 /*
1204 * Reset "trylevel" in case of a ":finish" or ":return" or a missing
1205 * ":endtry" in a sourced file or executed function. If the try
1206 * conditional is in its finally clause, ignore anything pending.
1207 * If it is in a catch clause, finish the caught exception.
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001208 * Also cleanup any "cs_forinfo" structures.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209 */
1210 do
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001211 {
1212 int idx = cleanup_conditionals(&cstack, 0, TRUE);
1213
Bram Moolenaar89e5d682005-01-17 22:06:23 +00001214 if (idx >= 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001215 --idx; // remove try block not in its finally clause
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001216 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
1217 &cstack.cs_looplevel);
1218 }
1219 while (cstack.cs_idx >= 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220 trylevel = initial_trylevel;
1221 }
1222
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001223 // If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
1224 // lack was reported above and the error message is to be converted to an
1225 // exception, do this now after rewinding the cstack.
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001226 do_errthrow(&cstack, getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001227 ? (char_u *)"endfunction" : (char_u *)NULL);
1228
1229 if (trylevel == 0)
1230 {
1231 /*
1232 * When an exception is being thrown out of the outermost try
1233 * conditional, discard the uncaught exception, disable the conversion
1234 * of interrupts or errors to exceptions, and ensure that no more
1235 * commands are executed.
1236 */
1237 if (did_throw)
1238 {
1239 void *p = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001240 struct msglist *messages = NULL, *next;
1241
1242 /*
1243 * If the uncaught exception is a user exception, report it as an
1244 * error. If it is an error exception, display the saved error
1245 * message now. For an interrupt exception, do nothing; the
1246 * interrupt message is given elsewhere.
1247 */
1248 switch (current_exception->type)
1249 {
1250 case ET_USER:
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001251 vim_snprintf((char *)IObuff, IOSIZE,
1252 _("E605: Exception not caught: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001253 current_exception->value);
1254 p = vim_strsave(IObuff);
1255 break;
1256 case ET_ERROR:
1257 messages = current_exception->messages;
1258 current_exception->messages = NULL;
1259 break;
1260 case ET_INTERRUPT:
1261 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001262 }
1263
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001264 estack_push(ETYPE_EXCEPT, current_exception->throw_name,
1265 current_exception->throw_lnum);
Bram Moolenaare31ee862020-01-07 20:59:34 +01001266 ESTACK_CHECK_SETUP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267 current_exception->throw_name = NULL;
1268
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001269 discard_current_exception(); // uses IObuff if 'verbose'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270 suppress_errthrow = TRUE;
1271 force_abort = TRUE;
1272
1273 if (messages != NULL)
1274 {
1275 do
1276 {
1277 next = messages->next;
1278 emsg(messages->msg);
1279 vim_free(messages->msg);
1280 vim_free(messages);
1281 messages = next;
1282 }
1283 while (messages != NULL);
1284 }
1285 else if (p != NULL)
1286 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01001287 emsg(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288 vim_free(p);
1289 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001290 vim_free(SOURCING_NAME);
Bram Moolenaare31ee862020-01-07 20:59:34 +01001291 ESTACK_CHECK_NOW
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001292 estack_pop();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001293 }
1294
1295 /*
1296 * On an interrupt or an aborting error not converted to an exception,
1297 * disable the conversion of errors to exceptions. (Interrupts are not
Bram Moolenaar2196bce2020-04-12 20:01:11 +02001298 * converted anymore, here.) This enables also the interrupt message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001299 * when force_abort is set and did_emsg unset in case of an interrupt
1300 * from a finally clause after an error.
1301 */
1302 else if (got_int || (did_emsg && force_abort))
1303 suppress_errthrow = TRUE;
1304 }
1305
1306 /*
1307 * The current cstack will be freed when do_cmdline() returns. An uncaught
1308 * exception will have to be rethrown in the previous cstack. If a function
1309 * has just returned or a script file was just finished and the previous
1310 * cstack belongs to the same function or, respectively, script file, it
1311 * will have to be checked for finally clauses to be executed due to the
1312 * ":return" or ":finish". This is done in do_one_cmd().
1313 */
1314 if (did_throw)
1315 need_rethrow = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001316 if ((getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001317 && ex_nesting_level > source_level(real_cookie))
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001318 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001319 && ex_nesting_level > func_level(real_cookie) + 1))
1320 {
1321 if (!did_throw)
1322 check_cstack = TRUE;
1323 }
1324 else
1325 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001326 // When leaving a function, reduce nesting level.
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001327 if (getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328 --ex_nesting_level;
1329 /*
1330 * Go to debug mode when returning from a function in which we are
1331 * single-stepping.
1332 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001333 if ((getline_equal(fgetline, cookie, getsourceline)
1334 || getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335 && ex_nesting_level + 1 <= debug_break_level)
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001336 do_debug(getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337 ? (char_u *)_("End of sourced file")
1338 : (char_u *)_("End of function"));
1339 }
1340
1341 /*
1342 * Restore the exception environment (done after returning from the
1343 * debugger).
1344 */
1345 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001346 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347
1348 msg_list = saved_msg_list;
Bram Moolenaar292b90d2020-03-18 15:23:16 +01001349
1350 // Cleanup if "cs_emsg_silent_list" remains.
1351 if (cstack.cs_emsg_silent_list != NULL)
1352 {
1353 eslist_T *elem, *temp;
1354
1355 for (elem = cstack.cs_emsg_silent_list; elem != NULL; elem = temp)
1356 {
1357 temp = elem->next;
1358 vim_free(elem);
1359 }
1360 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001361#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001362
1363 /*
1364 * If there was too much output to fit on the command line, ask the user to
1365 * hit return before redrawing the screen. With the ":global" command we do
1366 * this only once after the command is finished.
1367 */
1368 if (did_inc)
1369 {
1370 --RedrawingDisabled;
1371 --no_wait_return;
1372 msg_scroll = FALSE;
1373
1374 /*
1375 * When just finished an ":if"-":else" which was typed, no need to
1376 * wait for hit-return. Also for an error situation.
1377 */
1378 if (retval == FAIL
1379#ifdef FEAT_EVAL
1380 || (did_endif && KeyTyped && !did_emsg)
1381#endif
1382 )
1383 {
1384 need_wait_return = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001385 msg_didany = FALSE; // don't wait when restarting edit
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386 }
1387 else if (need_wait_return)
1388 {
1389 /*
1390 * The msg_start() above clears msg_didout. The wait_return we do
1391 * here should not overwrite the command that may be shown before
1392 * doing that.
1393 */
1394 msg_didout |= msg_didout_before_start;
1395 wait_return(FALSE);
1396 }
1397 }
1398
Bram Moolenaar2a942252012-11-28 23:03:07 +01001399#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001400 did_endif = FALSE; // in case do_cmdline used recursively
Bram Moolenaar2a942252012-11-28 23:03:07 +01001401#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402 /*
1403 * Reset if_level, in case a sourced script file contains more ":if" than
1404 * ":endif" (could be ":if x | foo | endif").
1405 */
1406 if_level = 0;
Bram Moolenaar2e18a122012-11-28 19:10:54 +01001407#endif
Bram Moolenaard4ad0d42012-11-28 17:34:48 +01001408
Bram Moolenaar071d4272004-06-13 20:20:40 +00001409 --call_depth;
1410 return retval;
1411}
1412
1413#ifdef FEAT_EVAL
1414/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001415 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416 */
1417 static char_u *
Bram Moolenaare96a2492019-06-25 04:12:16 +02001418get_loop_line(int c, void *cookie, int indent, int do_concat)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001420 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421 wcmd_T *wp;
1422 char_u *line;
1423
1424 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1425 {
1426 if (cp->repeating)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001427 return NULL; // trying to read past ":endwhile"/":endfor"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001428
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001429 // First time inside the ":while"/":for": get line normally.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001430 if (cp->getline == NULL)
Bram Moolenaare96a2492019-06-25 04:12:16 +02001431 line = getcmdline(c, 0L, indent, do_concat);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432 else
Bram Moolenaare96a2492019-06-25 04:12:16 +02001433 line = cp->getline(c, cp->cookie, indent, do_concat);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001434 if (line != NULL && store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001435 ++cp->current_line;
1436
1437 return line;
1438 }
1439
1440 KeyTyped = FALSE;
1441 ++cp->current_line;
1442 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001443 SOURCING_LNUM = wp->lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001444 return vim_strsave(wp->line);
1445}
1446
1447/*
1448 * Store a line in "gap" so that a ":while" loop can execute it again.
1449 */
1450 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001451store_loop_line(garray_T *gap, char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001452{
1453 if (ga_grow(gap, 1) == FAIL)
1454 return FAIL;
1455 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001456 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = SOURCING_LNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001457 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001458 return OK;
1459}
1460
1461/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001462 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001463 */
1464 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001465free_cmdlines(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466{
1467 while (gap->ga_len > 0)
1468 {
1469 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1470 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001471 }
1472}
1473#endif
1474
1475/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001476 * If "fgetline" is get_loop_line(), return TRUE if the getline it uses equals
1477 * "func". * Otherwise return TRUE when "fgetline" equals "func".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001478 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001479 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001480getline_equal(
Bram Moolenaare96a2492019-06-25 04:12:16 +02001481 char_u *(*fgetline)(int, void *, int, int),
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001482 void *cookie UNUSED, // argument for fgetline()
Bram Moolenaare96a2492019-06-25 04:12:16 +02001483 char_u *(*func)(int, void *, int, int))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484{
1485#ifdef FEAT_EVAL
Bram Moolenaare96a2492019-06-25 04:12:16 +02001486 char_u *(*gp)(int, void *, int, int);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001487 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001489 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1490 // function that's originally used to obtain the lines. This may be
1491 // nested several levels.
Bram Moolenaar89d40322006-08-29 15:30:07 +00001492 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001493 cp = (struct loop_cookie *)cookie;
1494 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001495 {
1496 gp = cp->getline;
1497 cp = cp->cookie;
1498 }
1499 return gp == func;
1500#else
Bram Moolenaar89d40322006-08-29 15:30:07 +00001501 return fgetline == func;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001502#endif
1503}
1504
Bram Moolenaar071d4272004-06-13 20:20:40 +00001505/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001506 * If "fgetline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001507 * getline function. Otherwise return "cookie".
1508 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001509 void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001510getline_cookie(
Bram Moolenaare96a2492019-06-25 04:12:16 +02001511 char_u *(*fgetline)(int, void *, int, int) UNUSED,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001512 void *cookie) // argument for fgetline()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513{
Bram Moolenaar13505972019-01-24 15:04:48 +01001514#ifdef FEAT_EVAL
Bram Moolenaare96a2492019-06-25 04:12:16 +02001515 char_u *(*gp)(int, void *, int, int);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001516 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001518 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1519 // cookie that's originally used to obtain the lines. This may be nested
1520 // several levels.
Bram Moolenaar89d40322006-08-29 15:30:07 +00001521 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001522 cp = (struct loop_cookie *)cookie;
1523 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524 {
1525 gp = cp->getline;
1526 cp = cp->cookie;
1527 }
1528 return cp;
Bram Moolenaar13505972019-01-24 15:04:48 +01001529#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001530 return cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001531#endif
Bram Moolenaar13505972019-01-24 15:04:48 +01001532}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001533
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001534
1535/*
1536 * Helper function to apply an offset for buffer commands, i.e. ":bdelete",
1537 * ":bwipeout", etc.
1538 * Returns the buffer number.
1539 */
1540 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001541compute_buffer_local_count(int addr_type, int lnum, int offset)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001542{
1543 buf_T *buf;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001544 buf_T *nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001545 int count = offset;
1546
1547 buf = firstbuf;
1548 while (buf->b_next != NULL && buf->b_fnum < lnum)
1549 buf = buf->b_next;
1550 while (count != 0)
1551 {
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001552 count += (offset < 0) ? 1 : -1;
1553 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1554 if (nextbuf == NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001555 break;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001556 buf = nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001557 if (addr_type == ADDR_LOADED_BUFFERS)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001558 // skip over unloaded buffers
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001559 while (buf->b_ml.ml_mfp == NULL)
1560 {
1561 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1562 if (nextbuf == NULL)
1563 break;
1564 buf = nextbuf;
1565 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001566 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001567 // we might have gone too far, last buffer is not loadedd
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001568 if (addr_type == ADDR_LOADED_BUFFERS)
1569 while (buf->b_ml.ml_mfp == NULL)
1570 {
1571 nextbuf = (offset >= 0) ? buf->b_prev : buf->b_next;
1572 if (nextbuf == NULL)
1573 break;
1574 buf = nextbuf;
1575 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001576 return buf->b_fnum;
1577}
1578
Bram Moolenaarb7316892019-05-01 18:08:42 +02001579/*
1580 * Return the window number of "win".
1581 * When "win" is NULL return the number of windows.
1582 */
Bram Moolenaarf240e182014-11-27 18:33:02 +01001583 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001584current_win_nr(win_T *win)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001585{
1586 win_T *wp;
1587 int nr = 0;
1588
Bram Moolenaar29323592016-07-24 22:04:11 +02001589 FOR_ALL_WINDOWS(wp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001590 {
1591 ++nr;
1592 if (wp == win)
1593 break;
1594 }
1595 return nr;
1596}
1597
1598 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001599current_tab_nr(tabpage_T *tab)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001600{
1601 tabpage_T *tp;
1602 int nr = 0;
1603
Bram Moolenaar29323592016-07-24 22:04:11 +02001604 FOR_ALL_TABPAGES(tp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001605 {
1606 ++nr;
1607 if (tp == tab)
1608 break;
1609 }
1610 return nr;
1611}
1612
1613# define CURRENT_WIN_NR current_win_nr(curwin)
1614# define LAST_WIN_NR current_win_nr(NULL)
1615# define CURRENT_TAB_NR current_tab_nr(curtab)
1616# define LAST_TAB_NR current_tab_nr(NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001617
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618/*
1619 * Execute one Ex command.
1620 *
1621 * If 'sourcing' is TRUE, the command will be included in the error message.
1622 *
1623 * 1. skip comment lines and leading space
1624 * 2. handle command modifiers
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001625 * 3. find the command
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001626 * 4. parse range
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001627 * 5. Parse the command.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001628 * 6. parse arguments
1629 * 7. switch on command name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001630 *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001631 * Note: "fgetline" can be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001632 *
1633 * This function may be called recursively!
1634 */
1635#if (_MSC_VER == 1200)
1636/*
Bram Moolenaared203462004-06-16 11:19:22 +00001637 * Avoid optimisation bug in VC++ version 6.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638 */
Bram Moolenaar281bdce2005-01-25 21:53:18 +00001639 #pragma optimize( "g", off )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001640#endif
1641 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001642do_one_cmd(
Bram Moolenaarddef1292019-12-16 17:10:33 +01001643 char_u **cmdlinep,
1644 int sourcing,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001645#ifdef FEAT_EVAL
Bram Moolenaarddef1292019-12-16 17:10:33 +01001646 cstack_T *cstack,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001647#endif
Bram Moolenaarddef1292019-12-16 17:10:33 +01001648 char_u *(*fgetline)(int, void *, int, int),
1649 void *cookie) // argument for fgetline()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001650{
Bram Moolenaarddef1292019-12-16 17:10:33 +01001651 char_u *p;
1652 linenr_T lnum;
1653 long n;
1654 char *errormsg = NULL; // error message
1655 char_u *after_modifier = NULL;
1656 exarg_T ea; // Ex command arguments
1657 int save_msg_scroll = msg_scroll;
1658 cmdmod_T save_cmdmod;
1659 int save_reg_executing = reg_executing;
1660 int ni; // set when Not Implemented
1661 char_u *cmd;
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001662#ifdef FEAT_EVAL
1663 int starts_with_colon;
1664#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001665
Bram Moolenaara80faa82020-04-12 19:37:17 +02001666 CLEAR_FIELD(ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667 ea.line1 = 1;
1668 ea.line2 = 1;
1669#ifdef FEAT_EVAL
1670 ++ex_nesting_level;
1671#endif
1672
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001673 // When the last file has not been edited :q has to be typed twice.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001674 if (quitmore
1675#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001676 // avoid that a function call in 'statusline' does this
Bram Moolenaar89d40322006-08-29 15:30:07 +00001677 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001678#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001679 // avoid that an autocommand, e.g. QuitPre, does this
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001680 && !getline_equal(fgetline, cookie, getnextac))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681 --quitmore;
1682
1683 /*
1684 * Reset browse, confirm, etc.. They are restored when returning, for
1685 * recursive calls.
1686 */
1687 save_cmdmod = cmdmod;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001688
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001689 // "#!anything" is handled like a comment.
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001690 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1691 goto doend;
1692
Bram Moolenaar4facea32019-10-12 20:17:40 +02001693 if (p_verbose >= 16)
1694 msg_verbose_cmd(0, *cmdlinep);
1695
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02001696/*
1697 * 1. Skip comment lines and leading white space and colons.
1698 * 2. Handle command modifiers.
1699 */
1700 // The "ea" structure holds the arguments that can be used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701 ea.cmd = *cmdlinep;
Bram Moolenaareffed932018-08-14 13:38:17 +02001702 ea.cmdlinep = cmdlinep;
1703 ea.getline = fgetline;
1704 ea.cookie = cookie;
1705#ifdef FEAT_EVAL
1706 ea.cstack = cstack;
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001707 starts_with_colon = *skipwhite(ea.cmd) == ':';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001708#endif
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02001709 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaareffed932018-08-14 13:38:17 +02001710 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001711
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001712 after_modifier = ea.cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713
1714#ifdef FEAT_EVAL
1715 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
1716 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
1717#else
1718 ea.skip = (if_level > 0);
1719#endif
1720
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001722 * 3. Skip over the range to find the command. Let "p" point to after it.
1723 *
1724 * We need the command to know what kind of range it uses.
1725 */
1726 cmd = ea.cmd;
1727 ea.cmd = skip_range(ea.cmd, NULL);
1728 if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
1729 ea.cmd = skipwhite(ea.cmd + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001730
1731#ifdef FEAT_EVAL
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001732 if (current_sctx.sc_version == SCRIPT_VERSION_VIM9 && !starts_with_colon)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001733 p = find_ex_command(&ea, NULL, lookup_scriptvar, NULL);
1734 else
1735#endif
1736 p = find_ex_command(&ea, NULL, NULL, NULL);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001737
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001738#ifdef FEAT_EVAL
1739# ifdef FEAT_PROFILE
1740 // Count this line for profiling if skip is TRUE.
1741 if (do_profiling == PROF_YES
1742 && (!ea.skip || cstack->cs_idx == 0 || (cstack->cs_idx > 0
1743 && (cstack->cs_flags[cstack->cs_idx - 1] & CSF_ACTIVE))))
1744 {
1745 int skip = did_emsg || got_int || did_throw;
1746
1747 if (ea.cmdidx == CMD_catch)
1748 skip = !skip && !(cstack->cs_idx >= 0
1749 && (cstack->cs_flags[cstack->cs_idx] & CSF_THROWN)
1750 && !(cstack->cs_flags[cstack->cs_idx] & CSF_CAUGHT));
1751 else if (ea.cmdidx == CMD_else || ea.cmdidx == CMD_elseif)
1752 skip = skip || !(cstack->cs_idx >= 0
1753 && !(cstack->cs_flags[cstack->cs_idx]
1754 & (CSF_ACTIVE | CSF_TRUE)));
1755 else if (ea.cmdidx == CMD_finally)
1756 skip = FALSE;
1757 else if (ea.cmdidx != CMD_endif
1758 && ea.cmdidx != CMD_endfor
1759 && ea.cmdidx != CMD_endtry
1760 && ea.cmdidx != CMD_endwhile)
1761 skip = ea.skip;
1762
1763 if (!skip)
1764 {
1765 if (getline_equal(fgetline, cookie, get_func_line))
1766 func_line_exec(getline_cookie(fgetline, cookie));
1767 else if (getline_equal(fgetline, cookie, getsourceline))
1768 script_line_exec();
1769 }
1770 }
1771# endif
1772
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001773 // May go to debug mode. If this happens and the ">quit" debug command is
1774 // used, throw an interrupt exception and skip the next command.
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001775 dbg_check_breakpoint(&ea);
1776 if (!ea.skip && got_int)
1777 {
1778 ea.skip = TRUE;
1779 (void)do_intthrow(cstack);
1780 }
1781#endif
1782
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001783/*
1784 * 4. parse a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00001785 *
1786 * where 'addr' is:
1787 *
1788 * % (entire file)
1789 * $ [+-NUM]
1790 * 'x [+-NUM] (where x denotes a currently defined mark)
1791 * . [+-NUM]
1792 * [+-NUM]..
1793 * NUM
1794 *
1795 * The ea.cmd pointer is updated to point to the first character following the
1796 * range spec. If an initial address is found, but no second, the upper bound
1797 * is equal to the lower.
1798 */
1799
Bram Moolenaar25190db2019-05-04 15:05:28 +02001800 // ea.addr_type for user commands is set by find_ucmd
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001801 if (!IS_USER_CMDIDX(ea.cmdidx))
1802 {
1803 if (ea.cmdidx != CMD_SIZE)
1804 ea.addr_type = cmdnames[(int)ea.cmdidx].cmd_addr_type;
1805 else
1806 ea.addr_type = ADDR_LINES;
1807
Bram Moolenaar25190db2019-05-04 15:05:28 +02001808 // :wincmd range depends on the argument.
Bram Moolenaar164f3262015-01-14 21:22:01 +01001809 if (ea.cmdidx == CMD_wincmd && p != NULL)
1810 get_wincmd_addr_type(skipwhite(p), &ea);
Bram Moolenaar25190db2019-05-04 15:05:28 +02001811#ifdef FEAT_QUICKFIX
1812 // :.cc in quickfix window uses line number
1813 if ((ea.cmdidx == CMD_cc || ea.cmdidx == CMD_ll) && bt_quickfix(curbuf))
1814 ea.addr_type = ADDR_OTHER;
1815#endif
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001816 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001817
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001818 ea.cmd = cmd;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02001819 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaaree8415b2018-08-10 23:13:12 +02001820 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001823 * 5. Parse the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001824 */
1825
1826 /*
1827 * Skip ':' and any white space
1828 */
1829 ea.cmd = skipwhite(ea.cmd);
1830 while (*ea.cmd == ':')
1831 ea.cmd = skipwhite(ea.cmd + 1);
1832
1833 /*
1834 * If we got a line, but no command, then go to the line.
1835 * If we find a '|' or '\n' we set ea.nextcmd.
1836 */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001837 if (*ea.cmd == NUL || *ea.cmd == '"'
1838 || (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001839 {
1840 /*
1841 * strange vi behaviour:
1842 * ":3" jumps to line 3
1843 * ":3|..." prints line 3
1844 * ":|" prints current line
1845 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001846 if (ea.skip) // skip this if inside :if
Bram Moolenaar071d4272004-06-13 20:20:40 +00001847 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001848 if (*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849 {
1850 ea.cmdidx = CMD_print;
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001851 ea.argt = EX_RANGE+EX_COUNT+EX_TRLBAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852 if ((errormsg = invalid_range(&ea)) == NULL)
1853 {
1854 correct_range(&ea);
1855 ex_print(&ea);
1856 }
1857 }
1858 else if (ea.addr_count != 0)
1859 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001860 if (ea.line2 > curbuf->b_ml.ml_line_count)
1861 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001862 // With '-' in 'cpoptions' a line number past the file is an
1863 // error, otherwise put it at the end of the file.
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001864 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
1865 ea.line2 = -1;
1866 else
1867 ea.line2 = curbuf->b_ml.ml_line_count;
1868 }
1869
1870 if (ea.line2 < 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001871 errormsg = _(e_invrange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872 else
1873 {
1874 if (ea.line2 == 0)
1875 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001876 else
1877 curwin->w_cursor.lnum = ea.line2;
1878 beginline(BL_SOL | BL_FIX);
1879 }
1880 }
1881 goto doend;
1882 }
1883
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001884 // If this looks like an undefined user command and there are CmdUndefined
1885 // autocommands defined, trigger the matching autocommands.
Bram Moolenaard5005162014-08-22 23:05:54 +02001886 if (p != NULL && ea.cmdidx == CMD_SIZE && !ea.skip
1887 && ASCII_ISUPPER(*ea.cmd)
1888 && has_cmdundefined())
1889 {
Bram Moolenaard5005162014-08-22 23:05:54 +02001890 int ret;
1891
Bram Moolenaar5a31b462014-08-23 14:16:20 +02001892 p = ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02001893 while (ASCII_ISALNUM(*p))
1894 ++p;
Bram Moolenaar120f4a82014-09-09 12:22:06 +02001895 p = vim_strnsave(ea.cmd, (int)(p - ea.cmd));
Bram Moolenaard5005162014-08-22 23:05:54 +02001896 ret = apply_autocmds(EVENT_CMDUNDEFINED, p, p, TRUE, NULL);
1897 vim_free(p);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001898 // If the autocommands did something and didn't cause an error, try
1899 // finding the command again.
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001900 p = (ret
1901#ifdef FEAT_EVAL
1902 && !aborting()
Bram Moolenaard5005162014-08-22 23:05:54 +02001903#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001904 ) ? find_ex_command(&ea, NULL, NULL, NULL) : ea.cmd;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001905 }
Bram Moolenaard5005162014-08-22 23:05:54 +02001906
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907 if (p == NULL)
1908 {
1909 if (!ea.skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001910 errormsg = _("E464: Ambiguous use of user-defined command");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001911 goto doend;
1912 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001913 // Check for wrong commands.
Bram Moolenaar6f9a4762017-06-22 20:39:17 +02001914 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78
1915 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001916 {
1917 errormsg = uc_fun_cmd();
1918 goto doend;
1919 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001920
Bram Moolenaar071d4272004-06-13 20:20:40 +00001921 if (ea.cmdidx == CMD_SIZE)
1922 {
1923 if (!ea.skip)
1924 {
1925 STRCPY(IObuff, _("E492: Not an editor command"));
1926 if (!sourcing)
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001927 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001928 // If the modifier was parsed OK the error must be in the
1929 // following command
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001930 if (after_modifier != NULL)
1931 append_command(after_modifier);
1932 else
1933 append_command(*cmdlinep);
1934 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001935 errormsg = (char *)IObuff;
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001936 did_emsg_syntax = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001937 }
1938 goto doend;
1939 }
1940
Bram Moolenaar958636c2014-10-21 20:01:58 +02001941 ni = (!IS_USER_CMDIDX(ea.cmdidx)
1942 && (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00001943#ifdef HAVE_EX_SCRIPT_NI
1944 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
1945#endif
1946 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001947
1948#ifndef FEAT_EVAL
1949 /*
1950 * When the expression evaluation is disabled, recognize the ":if" and
1951 * ":endif" commands and ignore everything in between it.
1952 */
1953 if (ea.cmdidx == CMD_if)
1954 ++if_level;
1955 if (if_level)
1956 {
1957 if (ea.cmdidx == CMD_endif)
1958 --if_level;
1959 goto doend;
1960 }
1961
1962#endif
1963
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001964 // forced commands
Bram Moolenaar196b3b02008-06-20 16:51:41 +00001965 if (*p == '!' && ea.cmdidx != CMD_substitute
1966 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001967 {
1968 ++p;
1969 ea.forceit = TRUE;
1970 }
1971 else
1972 ea.forceit = FALSE;
1973
1974/*
Bram Moolenaarb7316892019-05-01 18:08:42 +02001975 * 6. Parse arguments. Then check for errors.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001976 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02001977 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001978 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001979
1980 if (!ea.skip)
1981 {
1982#ifdef HAVE_SANDBOX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001983 if (sandbox != 0 && !(ea.argt & EX_SBOXOK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001984 {
Bram Moolenaar8c62a082019-02-08 14:34:10 +01001985 // Command not allowed in sandbox.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001986 errormsg = _(e_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001987 goto doend;
1988 }
1989#endif
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001990 if (restricted != 0 && (ea.argt & EX_RESTRICT))
Bram Moolenaar8c62a082019-02-08 14:34:10 +01001991 {
1992 errormsg = _("E981: Command not allowed in rvim");
1993 goto doend;
1994 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001995 if (!curbuf->b_p_ma && (ea.argt & EX_MODIFY))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001996 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001997 // Command not allowed in non-'modifiable' buffer
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001998 errormsg = _(e_modifiable);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999 goto doend;
2000 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002001
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002002 if (text_locked() && !(ea.argt & EX_CMDWIN)
Bram Moolenaar958636c2014-10-21 20:01:58 +02002003 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002004 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002005 // Command not allowed when editing the command line.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002006 errormsg = _(get_text_locked_msg());
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007 goto doend;
2008 }
Bram Moolenaar379fb762018-08-30 15:58:28 +02002009
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002010 // Disallow editing another buffer when "curbuf_lock" is set.
2011 // Do allow ":checktime" (it is postponed).
2012 // Do allow ":edit" (check for an argument later).
2013 // Do allow ":file" with no arguments (check for an argument later).
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002014 if (!(ea.argt & EX_CMDWIN)
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002015 && ea.cmdidx != CMD_checktime
Bram Moolenaar379fb762018-08-30 15:58:28 +02002016 && ea.cmdidx != CMD_edit
2017 && ea.cmdidx != CMD_file
Bram Moolenaar958636c2014-10-21 20:01:58 +02002018 && !IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002019 && curbuf_locked())
2020 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002021
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002022 if (!ni && !(ea.argt & EX_RANGE) && ea.addr_count > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002023 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002024 // no range allowed
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002025 errormsg = _(e_norange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002026 goto doend;
2027 }
2028 }
2029
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002030 if (!ni && !(ea.argt & EX_BANG) && ea.forceit) // no <!> allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002031 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002032 errormsg = _(e_nobang);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002033 goto doend;
2034 }
2035
2036 /*
2037 * Don't complain about the range if it is not used
2038 * (could happen if line_count is accidentally set to 0).
2039 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002040 if (!ea.skip && !ni && (ea.argt & EX_RANGE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002041 {
2042 /*
2043 * If the range is backwards, ask for confirmation and, if given, swap
2044 * ea.line1 & ea.line2 so it's forwards again.
2045 * When global command is busy, don't ask, will fail below.
2046 */
2047 if (!global_busy && ea.line1 > ea.line2)
2048 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002049 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002051 if (sourcing || exmode_active)
2052 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002053 errormsg = _("E493: Backwards range given");
Bram Moolenaara5792f52005-11-23 21:25:05 +00002054 goto doend;
2055 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002056 if (ask_yesno((char_u *)
2057 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002058 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002059 }
2060 lnum = ea.line1;
2061 ea.line1 = ea.line2;
2062 ea.line2 = lnum;
2063 }
2064 if ((errormsg = invalid_range(&ea)) != NULL)
2065 goto doend;
2066 }
2067
Bram Moolenaarb7316892019-05-01 18:08:42 +02002068 if ((ea.addr_type == ADDR_OTHER) && ea.addr_count == 0)
2069 // default is 1, not cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002070 ea.line2 = 1;
2071
2072 correct_range(&ea);
2073
2074#ifdef FEAT_FOLDING
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002075 if (((ea.argt & EX_WHOLEFOLD) || ea.addr_count >= 2) && !global_busy
Bram Moolenaara3306952016-01-02 21:41:06 +01002076 && ea.addr_type == ADDR_LINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002077 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002078 // Put the first line at the start of a closed fold, put the last line
2079 // at the end of a closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002080 (void)hasFolding(ea.line1, &ea.line1, NULL);
2081 (void)hasFolding(ea.line2, NULL, &ea.line2);
2082 }
2083#endif
2084
2085#ifdef FEAT_QUICKFIX
2086 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002087 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002088 * option here, so things like % get expanded.
2089 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002090 p = replace_makeprg(&ea, p, cmdlinep);
2091 if (p == NULL)
2092 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002093#endif
2094
2095 /*
2096 * Skip to start of argument.
2097 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2098 */
2099 if (ea.cmdidx == CMD_bang)
2100 ea.arg = p;
2101 else
2102 ea.arg = skipwhite(p);
2103
Bram Moolenaar379fb762018-08-30 15:58:28 +02002104 // ":file" cannot be run with an argument when "curbuf_lock" is set
2105 if (ea.cmdidx == CMD_file && *ea.arg != NUL && curbuf_locked())
2106 goto doend;
2107
Bram Moolenaar071d4272004-06-13 20:20:40 +00002108 /*
2109 * Check for "++opt=val" argument.
2110 * Must be first, allow ":w ++enc=utf8 !cmd"
2111 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002112 if (ea.argt & EX_ARGOPT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002113 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2114 if (getargopt(&ea) == FAIL && !ni)
2115 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002116 errormsg = _(e_invarg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002117 goto doend;
2118 }
2119
2120 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2121 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002122 if (*ea.arg == '>') // append
Bram Moolenaar071d4272004-06-13 20:20:40 +00002123 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002124 if (*++ea.arg != '>') // typed wrong
Bram Moolenaar071d4272004-06-13 20:20:40 +00002125 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002126 errormsg = _("E494: Use w or w>>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002127 goto doend;
2128 }
2129 ea.arg = skipwhite(ea.arg + 1);
2130 ea.append = TRUE;
2131 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002132 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) // :w !filter
Bram Moolenaar071d4272004-06-13 20:20:40 +00002133 {
2134 ++ea.arg;
2135 ea.usefilter = TRUE;
2136 }
2137 }
2138
2139 if (ea.cmdidx == CMD_read)
2140 {
2141 if (ea.forceit)
2142 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002143 ea.usefilter = TRUE; // :r! filter if ea.forceit
Bram Moolenaar071d4272004-06-13 20:20:40 +00002144 ea.forceit = FALSE;
2145 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002146 else if (*ea.arg == '!') // :r !filter
Bram Moolenaar071d4272004-06-13 20:20:40 +00002147 {
2148 ++ea.arg;
2149 ea.usefilter = TRUE;
2150 }
2151 }
2152
2153 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2154 {
2155 ea.amount = 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002156 while (*ea.arg == *ea.cmd) // count number of '>' or '<'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002157 {
2158 ++ea.arg;
2159 ++ea.amount;
2160 }
2161 ea.arg = skipwhite(ea.arg);
2162 }
2163
2164 /*
2165 * Check for "+command" argument, before checking for next command.
2166 * Don't do this for ":read !cmd" and ":write !cmd".
2167 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002168 if ((ea.argt & EX_CMDARG) && !ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002169 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2170
2171 /*
2172 * Check for '|' to separate commands and '"' to start comments.
2173 * Don't do this for ":read !cmd" and ":write !cmd".
2174 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002175 if ((ea.argt & EX_TRLBAR) && !ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176 separate_nextcmd(&ea);
2177
2178 /*
2179 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002180 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2181 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002182 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002183 else if (ea.cmdidx == CMD_bang
Bram Moolenaar67883b42017-07-27 22:57:00 +02002184 || ea.cmdidx == CMD_terminal
Bram Moolenaardf177f62005-02-22 08:39:57 +00002185 || ea.cmdidx == CMD_global
2186 || ea.cmdidx == CMD_vglobal
2187 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002188 {
2189 for (p = ea.arg; *p; ++p)
2190 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002191 // Remove one backslash before a newline, so that it's possible to
2192 // pass a newline to the shell and also a newline that is preceded
2193 // with a backslash. This makes it impossible to end a shell
2194 // command in a backslash, but that doesn't appear useful.
2195 // Halving the number of backslashes is incompatible with previous
2196 // versions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002197 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002198 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002199 else if (*p == '\n')
2200 {
2201 ea.nextcmd = p + 1;
2202 *p = NUL;
2203 break;
2204 }
2205 }
2206 }
2207
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002208 if ((ea.argt & EX_DFLALL) && ea.addr_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002209 {
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002210 buf_T *buf;
2211
Bram Moolenaar071d4272004-06-13 20:20:40 +00002212 ea.line1 = 1;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002213 switch (ea.addr_type)
2214 {
2215 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02002216 case ADDR_OTHER:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002217 ea.line2 = curbuf->b_ml.ml_line_count;
2218 break;
2219 case ADDR_LOADED_BUFFERS:
2220 buf = firstbuf;
2221 while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL)
2222 buf = buf->b_next;
2223 ea.line1 = buf->b_fnum;
2224 buf = lastbuf;
2225 while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL)
2226 buf = buf->b_prev;
2227 ea.line2 = buf->b_fnum;
2228 break;
2229 case ADDR_BUFFERS:
2230 ea.line1 = firstbuf->b_fnum;
2231 ea.line2 = lastbuf->b_fnum;
2232 break;
2233 case ADDR_WINDOWS:
2234 ea.line2 = LAST_WIN_NR;
2235 break;
2236 case ADDR_TABS:
2237 ea.line2 = LAST_TAB_NR;
2238 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01002239 case ADDR_TABS_RELATIVE:
2240 ea.line2 = 1;
2241 break;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002242 case ADDR_ARGUMENTS:
2243 if (ARGCOUNT == 0)
2244 ea.line1 = ea.line2 = 0;
2245 else
2246 ea.line2 = ARGCOUNT;
2247 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02002248 case ADDR_QUICKFIX_VALID:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002249#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02002250 ea.line2 = qf_get_valid_size(&ea);
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002251 if (ea.line2 == 0)
2252 ea.line2 = 1;
Bram Moolenaare906c502015-09-09 21:10:39 +02002253#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002254 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02002255 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02002256 case ADDR_UNSIGNED:
2257 case ADDR_QUICKFIX:
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002258 iemsg(_("INTERNAL: Cannot use EX_DFLALL with ADDR_NONE, ADDR_UNSIGNED or ADDR_QUICKFIX"));
Bram Moolenaarb7316892019-05-01 18:08:42 +02002259 break;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002260 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002261 }
2262
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002263 // accept numbered register only when no count allowed (:put)
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002264 if ( (ea.argt & EX_REGSTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002265 && *ea.arg != NUL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002266 // Do not allow register = for user commands
Bram Moolenaar958636c2014-10-21 20:01:58 +02002267 && (!IS_USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002268 && !((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002269 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002270#ifndef FEAT_CLIPBOARD
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002271 // check these explicitly for a more specific error message
Bram Moolenaar85de2062011-05-05 14:26:41 +02002272 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002273 {
Bram Moolenaar99b12722019-01-14 20:16:40 +01002274 errormsg = _(e_invalidreg);
Bram Moolenaar85de2062011-05-05 14:26:41 +02002275 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276 }
2277#endif
Bram Moolenaar958636c2014-10-21 20:01:58 +02002278 if (valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2279 && !IS_USER_CMDIDX(ea.cmdidx))))
Bram Moolenaar85de2062011-05-05 14:26:41 +02002280 {
2281 ea.regname = *ea.arg++;
2282#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002283 // for '=' register: accept the rest of the line as an expression
Bram Moolenaar85de2062011-05-05 14:26:41 +02002284 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2285 {
2286 set_expr_line(vim_strsave(ea.arg));
2287 ea.arg += STRLEN(ea.arg);
2288 }
2289#endif
2290 ea.arg = skipwhite(ea.arg);
2291 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292 }
2293
2294 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002295 * Check for a count. When accepting a EX_BUFNAME, don't use "123foo" as a
Bram Moolenaar071d4272004-06-13 20:20:40 +00002296 * count, it's a buffer name.
2297 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002298 if ((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)
2299 && (!(ea.argt & EX_BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01002300 || VIM_ISWHITE(*p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301 {
2302 n = getdigits(&ea.arg);
2303 ea.arg = skipwhite(ea.arg);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002304 if (n <= 0 && !ni && (ea.argt & EX_ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002306 errormsg = _(e_zerocount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307 goto doend;
2308 }
Bram Moolenaarb7316892019-05-01 18:08:42 +02002309 if (ea.addr_type != ADDR_LINES) // e.g. :buffer 2, :sleep 3
Bram Moolenaar071d4272004-06-13 20:20:40 +00002310 {
2311 ea.line2 = n;
2312 if (ea.addr_count == 0)
2313 ea.addr_count = 1;
2314 }
2315 else
2316 {
2317 ea.line1 = ea.line2;
2318 ea.line2 += n - 1;
2319 ++ea.addr_count;
2320 /*
2321 * Be vi compatible: no error message for out of range.
2322 */
Bram Moolenaarb7316892019-05-01 18:08:42 +02002323 if (ea.line2 > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324 ea.line2 = curbuf->b_ml.ml_line_count;
2325 }
2326 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002327
2328 /*
2329 * Check for flags: 'l', 'p' and '#'.
2330 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002331 if (ea.argt & EX_FLAGS)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002332 get_flags(&ea);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002333 if (!ni && !(ea.argt & EX_EXTRA) && *ea.arg != NUL
2334 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & EX_TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002335 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002336 // no arguments allowed but there is something
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002337 errormsg = _(e_trailing);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002338 goto doend;
2339 }
2340
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002341 if (!ni && (ea.argt & EX_NEEDARG) && *ea.arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002342 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002343 errormsg = _(e_argreq);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344 goto doend;
2345 }
2346
2347#ifdef FEAT_EVAL
2348 /*
2349 * Skip the command when it's not going to be executed.
2350 * The commands like :if, :endif, etc. always need to be executed.
2351 * Also make an exception for commands that handle a trailing command
2352 * themselves.
2353 */
2354 if (ea.skip)
2355 {
2356 switch (ea.cmdidx)
2357 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002358 // commands that need evaluation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002359 case CMD_while:
2360 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002361 case CMD_for:
2362 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363 case CMD_if:
2364 case CMD_elseif:
2365 case CMD_else:
2366 case CMD_endif:
2367 case CMD_try:
2368 case CMD_catch:
2369 case CMD_finally:
2370 case CMD_endtry:
2371 case CMD_function:
Bram Moolenaarab55c682020-03-01 19:41:43 +01002372 case CMD_def:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002373 break;
2374
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002375 // Commands that handle '|' themselves. Check: A command should
2376 // either have the EX_TRLBAR flag, appear in this list or appear in
2377 // the list at ":help :bar".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002378 case CMD_aboveleft:
2379 case CMD_and:
2380 case CMD_belowright:
2381 case CMD_botright:
2382 case CMD_browse:
2383 case CMD_call:
2384 case CMD_confirm:
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01002385 case CMD_const:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002386 case CMD_delfunction:
2387 case CMD_djump:
2388 case CMD_dlist:
2389 case CMD_dsearch:
2390 case CMD_dsplit:
2391 case CMD_echo:
2392 case CMD_echoerr:
2393 case CMD_echomsg:
2394 case CMD_echon:
Bram Moolenaara76b3152020-02-16 16:20:21 +01002395 case CMD_eval:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002396 case CMD_execute:
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002397 case CMD_filter:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398 case CMD_help:
2399 case CMD_hide:
2400 case CMD_ijump:
2401 case CMD_ilist:
2402 case CMD_isearch:
2403 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002404 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405 case CMD_keepjumps:
2406 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01002407 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408 case CMD_leftabove:
2409 case CMD_let:
2410 case CMD_lockmarks:
Bram Moolenaarcc4423a2019-11-26 17:05:00 +01002411 case CMD_lockvar:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002412 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002414 case CMD_mzscheme:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01002415 case CMD_noautocmd:
2416 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002417 case CMD_perl:
2418 case CMD_psearch:
2419 case CMD_python:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002420 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002421 case CMD_python3:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422 case CMD_return:
2423 case CMD_rightbelow:
2424 case CMD_ruby:
2425 case CMD_silent:
2426 case CMD_smagic:
2427 case CMD_snomagic:
2428 case CMD_substitute:
2429 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002430 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002431 case CMD_tcl:
2432 case CMD_throw:
2433 case CMD_tilde:
2434 case CMD_topleft:
2435 case CMD_unlet:
Bram Moolenaarcc4423a2019-11-26 17:05:00 +01002436 case CMD_unlockvar:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437 case CMD_verbose:
2438 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002439 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002440 break;
2441
2442 default: goto doend;
2443 }
2444 }
2445#endif
2446
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002447 if (ea.argt & EX_XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002448 {
2449 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2450 goto doend;
2451 }
2452
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453 /*
2454 * Accept buffer name. Cannot be used at the same time with a buffer
2455 * number. Don't do this for a user command.
2456 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002457 if ((ea.argt & EX_BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
Bram Moolenaar958636c2014-10-21 20:01:58 +02002458 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459 {
2460 /*
2461 * :bdelete, :bwipeout and :bunload take several arguments, separated
2462 * by spaces: find next space (skipping over escaped characters).
2463 * The others take one argument: ignore trailing spaces.
2464 */
2465 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2466 || ea.cmdidx == CMD_bunload)
2467 p = skiptowhite_esc(ea.arg);
2468 else
2469 {
2470 p = ea.arg + STRLEN(ea.arg);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002471 while (p > ea.arg && VIM_ISWHITE(p[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472 --p;
2473 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002474 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & EX_BUFUNL) != 0,
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002475 FALSE, FALSE);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002476 if (ea.line2 < 0) // failed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477 goto doend;
2478 ea.addr_count = 1;
2479 ea.arg = skipwhite(p);
2480 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002482 // The :try command saves the emsg_silent flag, reset it here when
2483 // ":silent! try" was used, it should only apply to :try itself.
Bram Moolenaareffed932018-08-14 13:38:17 +02002484 if (ea.cmdidx == CMD_try && ea.did_esilent > 0)
Bram Moolenaar2be57332018-02-13 18:05:18 +01002485 {
Bram Moolenaareffed932018-08-14 13:38:17 +02002486 emsg_silent -= ea.did_esilent;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002487 if (emsg_silent < 0)
2488 emsg_silent = 0;
Bram Moolenaareffed932018-08-14 13:38:17 +02002489 ea.did_esilent = 0;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002490 }
2491
Bram Moolenaar071d4272004-06-13 20:20:40 +00002492/*
Bram Moolenaar2be57332018-02-13 18:05:18 +01002493 * 7. Execute the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495
Bram Moolenaar958636c2014-10-21 20:01:58 +02002496 if (IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497 {
2498 /*
2499 * Execute a user-defined command.
2500 */
2501 do_ucmd(&ea);
2502 }
2503 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002504 {
2505 /*
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002506 * Call the function to execute the builtin command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002507 */
2508 ea.errmsg = NULL;
2509 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2510 if (ea.errmsg != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002511 errormsg = _(ea.errmsg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512 }
2513
2514#ifdef FEAT_EVAL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002515 // Set flag that any command was executed, used by ex_vim9script().
Bram Moolenaar16531552020-02-08 16:00:46 +01002516 if (getline_equal(ea.getline, ea.cookie, getsourceline)
2517 && current_sctx.sc_sid > 0)
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002518 SCRIPT_ITEM(current_sctx.sc_sid)->sn_had_command = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002519
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520 /*
2521 * If the command just executed called do_cmdline(), any throw or ":return"
2522 * or ":finish" encountered there must also check the cstack of the still
2523 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2524 * exception, or reanimate a returned function or finished script file and
2525 * return or finish it again.
2526 */
2527 if (need_rethrow)
2528 do_throw(cstack);
2529 else if (check_cstack)
2530 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002531 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002533 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002534 && current_func_returned())
2535 do_return(&ea, TRUE, FALSE, NULL);
2536 }
2537 need_rethrow = check_cstack = FALSE;
2538#endif
2539
2540doend:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002541 if (curwin->w_cursor.lnum == 0) // can happen with zero line number
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002542 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002543 curwin->w_cursor.lnum = 1;
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002544 curwin->w_cursor.col = 0;
2545 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546
2547 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2548 {
2549 if (sourcing)
2550 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002551 if (errormsg != (char *)IObuff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002552 {
2553 STRCPY(IObuff, errormsg);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002554 errormsg = (char *)IObuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002555 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002556 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002557 }
2558 emsg(errormsg);
2559 }
2560#ifdef FEAT_EVAL
2561 do_errthrow(cstack,
Bram Moolenaar958636c2014-10-21 20:01:58 +02002562 (ea.cmdidx != CMD_SIZE && !IS_USER_CMDIDX(ea.cmdidx))
2563 ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002564#endif
2565
Bram Moolenaareffed932018-08-14 13:38:17 +02002566 if (ea.verbose_save >= 0)
2567 p_verbose = ea.verbose_save;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002568
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002569 free_cmdmod();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002570 cmdmod = save_cmdmod;
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01002571 reg_executing = save_reg_executing;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002572
Bram Moolenaareffed932018-08-14 13:38:17 +02002573 if (ea.save_msg_silent != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002574 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002575 // messages could be enabled for a serious error, need to check if the
2576 // counters don't become negative
Bram Moolenaareffed932018-08-14 13:38:17 +02002577 if (!did_emsg || msg_silent > ea.save_msg_silent)
2578 msg_silent = ea.save_msg_silent;
2579 emsg_silent -= ea.did_esilent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580 if (emsg_silent < 0)
2581 emsg_silent = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002582 // Restore msg_scroll, it's set by file I/O commands, even when no
2583 // message is actually displayed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002584 msg_scroll = save_msg_scroll;
Bram Moolenaar77ab2802009-04-22 12:44:48 +00002585
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002586 // "silent reg" or "silent echo x" inside "redir" leaves msg_col
2587 // somewhere in the line. Put it back in the first column.
Bram Moolenaar77ab2802009-04-22 12:44:48 +00002588 if (redirecting())
2589 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002590 }
2591
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002592#ifdef HAVE_SANDBOX
Bram Moolenaareffed932018-08-14 13:38:17 +02002593 if (ea.did_sandbox)
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002594 --sandbox;
2595#endif
2596
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002597 if (ea.nextcmd && *ea.nextcmd == NUL) // not really a next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00002598 ea.nextcmd = NULL;
2599
2600#ifdef FEAT_EVAL
2601 --ex_nesting_level;
2602#endif
2603
2604 return ea.nextcmd;
2605}
2606#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00002607 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002608#endif
2609
2610/*
Bram Moolenaareffed932018-08-14 13:38:17 +02002611 * Parse and skip over command modifiers:
2612 * - update eap->cmd
2613 * - store flags in "cmdmod".
2614 * - Set ex_pressedreturn for an empty command line.
2615 * - set msg_silent for ":silent"
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002616 * - set 'eventignore' to "all" for ":noautocmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002617 * - set p_verbose for ":verbose"
2618 * - Increment "sandbox" for ":sandbox"
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002619 * When "skip_only" is TRUE the global variables are not changed, except for
2620 * "cmdmod".
Bram Moolenaareffed932018-08-14 13:38:17 +02002621 * Return FAIL when the command is not to be executed.
2622 * May set "errormsg" to an error message.
2623 */
2624 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002625parse_command_modifiers(exarg_T *eap, char **errormsg, int skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002626{
2627 char_u *p;
2628
Bram Moolenaara80faa82020-04-12 19:37:17 +02002629 CLEAR_FIELD(cmdmod);
Bram Moolenaareffed932018-08-14 13:38:17 +02002630 eap->verbose_save = -1;
2631 eap->save_msg_silent = -1;
2632
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002633 // Repeat until no more command modifiers are found.
Bram Moolenaareffed932018-08-14 13:38:17 +02002634 for (;;)
2635 {
Bram Moolenaareffed932018-08-14 13:38:17 +02002636 while (*eap->cmd == ' ' || *eap->cmd == '\t' || *eap->cmd == ':')
2637 ++eap->cmd;
2638
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002639 // in ex mode, an empty line works like :+
Bram Moolenaareffed932018-08-14 13:38:17 +02002640 if (*eap->cmd == NUL && exmode_active
2641 && (getline_equal(eap->getline, eap->cookie, getexmodeline)
2642 || getline_equal(eap->getline, eap->cookie, getexline))
2643 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2644 {
2645 eap->cmd = (char_u *)"+";
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002646 if (!skip_only)
2647 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002648 }
2649
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002650 // ignore comment and empty lines
Bram Moolenaareffed932018-08-14 13:38:17 +02002651 if (*eap->cmd == '"')
2652 return FAIL;
2653 if (*eap->cmd == NUL)
2654 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002655 if (!skip_only)
2656 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002657 return FAIL;
2658 }
2659
Bram Moolenaareffed932018-08-14 13:38:17 +02002660 p = skip_range(eap->cmd, NULL);
2661 switch (*p)
2662 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002663 // When adding an entry, also modify cmd_exists().
Bram Moolenaareffed932018-08-14 13:38:17 +02002664 case 'a': if (!checkforcmd(&eap->cmd, "aboveleft", 3))
2665 break;
2666 cmdmod.split |= WSP_ABOVE;
2667 continue;
2668
2669 case 'b': if (checkforcmd(&eap->cmd, "belowright", 3))
2670 {
2671 cmdmod.split |= WSP_BELOW;
2672 continue;
2673 }
2674 if (checkforcmd(&eap->cmd, "browse", 3))
2675 {
2676#ifdef FEAT_BROWSE_CMD
2677 cmdmod.browse = TRUE;
2678#endif
2679 continue;
2680 }
2681 if (!checkforcmd(&eap->cmd, "botright", 2))
2682 break;
2683 cmdmod.split |= WSP_BOT;
2684 continue;
2685
2686 case 'c': if (!checkforcmd(&eap->cmd, "confirm", 4))
2687 break;
2688#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2689 cmdmod.confirm = TRUE;
2690#endif
2691 continue;
2692
2693 case 'k': if (checkforcmd(&eap->cmd, "keepmarks", 3))
2694 {
2695 cmdmod.keepmarks = TRUE;
2696 continue;
2697 }
2698 if (checkforcmd(&eap->cmd, "keepalt", 5))
2699 {
2700 cmdmod.keepalt = TRUE;
2701 continue;
2702 }
2703 if (checkforcmd(&eap->cmd, "keeppatterns", 5))
2704 {
2705 cmdmod.keeppatterns = TRUE;
2706 continue;
2707 }
2708 if (!checkforcmd(&eap->cmd, "keepjumps", 5))
2709 break;
2710 cmdmod.keepjumps = TRUE;
2711 continue;
2712
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002713 case 'f': // only accept ":filter {pat} cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002714 {
2715 char_u *reg_pat;
2716
2717 if (!checkforcmd(&p, "filter", 4)
2718 || *p == NUL || ends_excmd(*p))
2719 break;
2720 if (*p == '!')
2721 {
2722 cmdmod.filter_force = TRUE;
2723 p = skipwhite(p + 1);
2724 if (*p == NUL || ends_excmd(*p))
2725 break;
2726 }
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002727 if (skip_only)
2728 p = skip_vimgrep_pat(p, NULL, NULL);
2729 else
2730 // NOTE: This puts a NUL after the pattern.
2731 p = skip_vimgrep_pat(p, &reg_pat, NULL);
Bram Moolenaareffed932018-08-14 13:38:17 +02002732 if (p == NULL || *p == NUL)
2733 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002734 if (!skip_only)
2735 {
2736 cmdmod.filter_regmatch.regprog =
Bram Moolenaareffed932018-08-14 13:38:17 +02002737 vim_regcomp(reg_pat, RE_MAGIC);
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002738 if (cmdmod.filter_regmatch.regprog == NULL)
2739 break;
2740 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002741 eap->cmd = p;
2742 continue;
2743 }
2744
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002745 // ":hide" and ":hide | cmd" are not modifiers
Bram Moolenaareffed932018-08-14 13:38:17 +02002746 case 'h': if (p != eap->cmd || !checkforcmd(&p, "hide", 3)
2747 || *p == NUL || ends_excmd(*p))
2748 break;
2749 eap->cmd = p;
2750 cmdmod.hide = TRUE;
2751 continue;
2752
2753 case 'l': if (checkforcmd(&eap->cmd, "lockmarks", 3))
2754 {
2755 cmdmod.lockmarks = TRUE;
2756 continue;
2757 }
2758
2759 if (!checkforcmd(&eap->cmd, "leftabove", 5))
2760 break;
2761 cmdmod.split |= WSP_ABOVE;
2762 continue;
2763
2764 case 'n': if (checkforcmd(&eap->cmd, "noautocmd", 3))
2765 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002766 if (cmdmod.save_ei == NULL && !skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002767 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002768 // Set 'eventignore' to "all". Restore the
2769 // existing option value later.
Bram Moolenaareffed932018-08-14 13:38:17 +02002770 cmdmod.save_ei = vim_strsave(p_ei);
2771 set_string_option_direct((char_u *)"ei", -1,
2772 (char_u *)"all", OPT_FREE, SID_NONE);
2773 }
2774 continue;
2775 }
2776 if (!checkforcmd(&eap->cmd, "noswapfile", 3))
2777 break;
2778 cmdmod.noswapfile = TRUE;
2779 continue;
2780
2781 case 'r': if (!checkforcmd(&eap->cmd, "rightbelow", 6))
2782 break;
2783 cmdmod.split |= WSP_BELOW;
2784 continue;
2785
2786 case 's': if (checkforcmd(&eap->cmd, "sandbox", 3))
2787 {
2788#ifdef HAVE_SANDBOX
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002789 if (!skip_only)
2790 {
2791 if (!eap->did_sandbox)
2792 ++sandbox;
2793 eap->did_sandbox = TRUE;
2794 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002795#endif
2796 continue;
2797 }
2798 if (!checkforcmd(&eap->cmd, "silent", 3))
2799 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002800 if (!skip_only)
2801 {
2802 if (eap->save_msg_silent == -1)
2803 eap->save_msg_silent = msg_silent;
2804 ++msg_silent;
2805 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002806 if (*eap->cmd == '!' && !VIM_ISWHITE(eap->cmd[-1]))
2807 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002808 // ":silent!", but not "silent !cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002809 eap->cmd = skipwhite(eap->cmd + 1);
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002810 if (!skip_only)
2811 {
2812 ++emsg_silent;
2813 ++eap->did_esilent;
2814 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002815 }
2816 continue;
2817
2818 case 't': if (checkforcmd(&p, "tab", 3))
2819 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002820 if (!skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002821 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002822 long tabnr = get_address(eap, &eap->cmd,
2823 ADDR_TABS, eap->skip,
2824 skip_only, FALSE, 1);
2825 if (tabnr == MAXLNUM)
2826 cmdmod.tab = tabpage_index(curtab) + 1;
2827 else
Bram Moolenaareffed932018-08-14 13:38:17 +02002828 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002829 if (tabnr < 0 || tabnr > LAST_TAB_NR)
2830 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002831 *errormsg = _(e_invrange);
Bram Moolenaar548e5982018-12-26 21:45:00 +01002832 return FAIL;
2833 }
2834 cmdmod.tab = tabnr + 1;
Bram Moolenaareffed932018-08-14 13:38:17 +02002835 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002836 }
2837 eap->cmd = p;
2838 continue;
2839 }
2840 if (!checkforcmd(&eap->cmd, "topleft", 2))
2841 break;
2842 cmdmod.split |= WSP_TOP;
2843 continue;
2844
2845 case 'u': if (!checkforcmd(&eap->cmd, "unsilent", 3))
2846 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002847 if (!skip_only)
2848 {
2849 if (eap->save_msg_silent == -1)
2850 eap->save_msg_silent = msg_silent;
2851 msg_silent = 0;
2852 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002853 continue;
2854
2855 case 'v': if (checkforcmd(&eap->cmd, "vertical", 4))
2856 {
2857 cmdmod.split |= WSP_VERT;
2858 continue;
2859 }
2860 if (!checkforcmd(&p, "verbose", 4))
2861 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002862 if (!skip_only)
2863 {
2864 if (eap->verbose_save < 0)
2865 eap->verbose_save = p_verbose;
2866 if (vim_isdigit(*eap->cmd))
2867 p_verbose = atoi((char *)eap->cmd);
2868 else
2869 p_verbose = 1;
2870 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002871 eap->cmd = p;
2872 continue;
2873 }
2874 break;
2875 }
2876
2877 return OK;
2878}
2879
2880/*
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002881 * Free contents of "cmdmod".
2882 */
2883 static void
2884free_cmdmod(void)
2885{
2886 if (cmdmod.save_ei != NULL)
2887 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002888 // Restore 'eventignore' to the value before ":noautocmd".
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002889 set_string_option_direct((char_u *)"ei", -1, cmdmod.save_ei,
2890 OPT_FREE, SID_NONE);
2891 free_string_option(cmdmod.save_ei);
2892 }
2893
2894 if (cmdmod.filter_regmatch.regprog != NULL)
2895 vim_regfree(cmdmod.filter_regmatch.regprog);
2896}
2897
2898/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002899 * Parse the address range, if any, in "eap".
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02002900 * May set the last search pattern, unless "silent" is TRUE.
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002901 * Return FAIL and set "errormsg" or return OK.
2902 */
2903 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002904parse_cmd_address(exarg_T *eap, char **errormsg, int silent)
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002905{
2906 int address_count = 1;
2907 linenr_T lnum;
2908
2909 // Repeat for all ',' or ';' separated addresses.
2910 for (;;)
2911 {
2912 eap->line1 = eap->line2;
2913 switch (eap->addr_type)
2914 {
2915 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02002916 case ADDR_OTHER:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002917 // default is current line number
2918 eap->line2 = curwin->w_cursor.lnum;
2919 break;
2920 case ADDR_WINDOWS:
2921 eap->line2 = CURRENT_WIN_NR;
2922 break;
2923 case ADDR_ARGUMENTS:
2924 eap->line2 = curwin->w_arg_idx + 1;
2925 if (eap->line2 > ARGCOUNT)
2926 eap->line2 = ARGCOUNT;
2927 break;
2928 case ADDR_LOADED_BUFFERS:
2929 case ADDR_BUFFERS:
2930 eap->line2 = curbuf->b_fnum;
2931 break;
2932 case ADDR_TABS:
2933 eap->line2 = CURRENT_TAB_NR;
2934 break;
2935 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02002936 case ADDR_UNSIGNED:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002937 eap->line2 = 1;
2938 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002939 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002940#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02002941 eap->line2 = qf_get_cur_idx(eap);
2942#endif
2943 break;
2944 case ADDR_QUICKFIX_VALID:
2945#ifdef FEAT_QUICKFIX
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002946 eap->line2 = qf_get_cur_valid_idx(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002947#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002948 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02002949 case ADDR_NONE:
2950 // Will give an error later if a range is found.
2951 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002952 }
2953 eap->cmd = skipwhite(eap->cmd);
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02002954 lnum = get_address(eap, &eap->cmd, eap->addr_type, eap->skip, silent,
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002955 eap->addr_count == 0, address_count++);
2956 if (eap->cmd == NULL) // error detected
2957 return FAIL;
2958 if (lnum == MAXLNUM)
2959 {
2960 if (*eap->cmd == '%') // '%' - all lines
2961 {
2962 ++eap->cmd;
2963 switch (eap->addr_type)
2964 {
2965 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02002966 case ADDR_OTHER:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002967 eap->line1 = 1;
2968 eap->line2 = curbuf->b_ml.ml_line_count;
2969 break;
2970 case ADDR_LOADED_BUFFERS:
2971 {
2972 buf_T *buf = firstbuf;
2973
2974 while (buf->b_next != NULL
2975 && buf->b_ml.ml_mfp == NULL)
2976 buf = buf->b_next;
2977 eap->line1 = buf->b_fnum;
2978 buf = lastbuf;
2979 while (buf->b_prev != NULL
2980 && buf->b_ml.ml_mfp == NULL)
2981 buf = buf->b_prev;
2982 eap->line2 = buf->b_fnum;
2983 break;
2984 }
2985 case ADDR_BUFFERS:
2986 eap->line1 = firstbuf->b_fnum;
2987 eap->line2 = lastbuf->b_fnum;
2988 break;
2989 case ADDR_WINDOWS:
2990 case ADDR_TABS:
2991 if (IS_USER_CMDIDX(eap->cmdidx))
2992 {
2993 eap->line1 = 1;
2994 eap->line2 = eap->addr_type == ADDR_WINDOWS
2995 ? LAST_WIN_NR : LAST_TAB_NR;
2996 }
2997 else
2998 {
2999 // there is no Vim command which uses '%' and
3000 // ADDR_WINDOWS or ADDR_TABS
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003001 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003002 return FAIL;
3003 }
3004 break;
3005 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003006 case ADDR_UNSIGNED:
3007 case ADDR_QUICKFIX:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003008 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003009 return FAIL;
3010 case ADDR_ARGUMENTS:
3011 if (ARGCOUNT == 0)
3012 eap->line1 = eap->line2 = 0;
3013 else
3014 {
3015 eap->line1 = 1;
3016 eap->line2 = ARGCOUNT;
3017 }
3018 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003019 case ADDR_QUICKFIX_VALID:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003020#ifdef FEAT_QUICKFIX
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003021 eap->line1 = 1;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003022 eap->line2 = qf_get_valid_size(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003023 if (eap->line2 == 0)
3024 eap->line2 = 1;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003025#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003026 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003027 case ADDR_NONE:
3028 // Will give an error later if a range is found.
3029 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003030 }
3031 ++eap->addr_count;
3032 }
3033 else if (*eap->cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
3034 {
3035 pos_T *fp;
3036
3037 // '*' - visual area
3038 if (eap->addr_type != ADDR_LINES)
3039 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003040 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003041 return FAIL;
3042 }
3043
3044 ++eap->cmd;
3045 if (!eap->skip)
3046 {
3047 fp = getmark('<', FALSE);
3048 if (check_mark(fp) == FAIL)
3049 return FAIL;
3050 eap->line1 = fp->lnum;
3051 fp = getmark('>', FALSE);
3052 if (check_mark(fp) == FAIL)
3053 return FAIL;
3054 eap->line2 = fp->lnum;
3055 ++eap->addr_count;
3056 }
3057 }
3058 }
3059 else
3060 eap->line2 = lnum;
3061 eap->addr_count++;
3062
3063 if (*eap->cmd == ';')
3064 {
3065 if (!eap->skip)
3066 {
3067 curwin->w_cursor.lnum = eap->line2;
3068 // don't leave the cursor on an illegal line or column
3069 check_cursor();
3070 }
3071 }
3072 else if (*eap->cmd != ',')
3073 break;
3074 ++eap->cmd;
3075 }
3076
3077 // One address given: set start and end lines.
3078 if (eap->addr_count == 1)
3079 {
3080 eap->line1 = eap->line2;
3081 // ... but only implicit: really no address given
3082 if (lnum == MAXLNUM)
3083 eap->addr_count = 0;
3084 }
3085 return OK;
3086}
3087
3088/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003089 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003090 * If there is a match advance "pp" to the argument and return TRUE.
3091 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003092 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003093checkforcmd(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003094 char_u **pp, // start of command
3095 char *cmd, // name of command
3096 int len) // required length
Bram Moolenaar071d4272004-06-13 20:20:40 +00003097{
3098 int i;
3099
3100 for (i = 0; cmd[i] != NUL; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003101 if (((char_u *)cmd)[i] != (*pp)[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003102 break;
3103 if (i >= len && !isalpha((*pp)[i]))
3104 {
3105 *pp = skipwhite(*pp + i);
3106 return TRUE;
3107 }
3108 return FALSE;
3109}
3110
3111/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003112 * Append "cmd" to the error message in IObuff.
3113 * Takes care of limiting the length and handling 0xa0, which would be
3114 * invisible otherwise.
3115 */
3116 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003117append_command(char_u *cmd)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003118{
3119 char_u *s = cmd;
3120 char_u *d;
3121
3122 STRCAT(IObuff, ": ");
3123 d = IObuff + STRLEN(IObuff);
3124 while (*s != NUL && d - IObuff < IOSIZE - 7)
3125 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003126 if (enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) : *s == 0xa0)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003127 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003128 s += enc_utf8 ? 2 : 1;
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003129 STRCPY(d, "<a0>");
3130 d += 4;
3131 }
3132 else
3133 MB_COPY_CHAR(s, d);
3134 }
3135 *d = NUL;
3136}
3137
3138/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003139 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003140 * Start of the name can be found at eap->cmd.
Bram Moolenaar25190db2019-05-04 15:05:28 +02003141 * Sets eap->cmdidx and returns a pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003142 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003143 *
3144 * If "lookup" is not NULL recognize expression without "eval" or "call" and
3145 * assignment without "let". Sets eap->cmdidx to the command while returning
3146 * "eap->cmd".
3147 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003148 * Returns NULL for an ambiguous user command.
3149 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003150 char_u *
3151find_ex_command(
3152 exarg_T *eap,
3153 int *full UNUSED,
3154 int (*lookup)(char_u *, size_t, cctx_T *) UNUSED,
3155 cctx_T *cctx UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156{
3157 int len;
3158 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003159 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003160
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003161#ifdef FEAT_EVAL
3162 /*
3163 * Recognize a Vim9 script function/method call and assignment:
3164 * "lvar = value", "lvar(arg)", "[1, 2 3]->Func()"
3165 */
Bram Moolenaar0c6ceaf2020-02-22 18:36:32 +01003166 p = eap->cmd;
3167 if (lookup != NULL && (*p == '('
3168 || ((p = to_name_const_end(eap->cmd)) > eap->cmd && *p != NUL)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003169 {
3170 int oplen;
3171 int heredoc;
3172
3173 // "funcname(" is always a function call.
3174 // "varname[]" is an expression.
3175 // "g:varname" is an expression.
3176 // "varname->expr" is an expression.
Bram Moolenaar0c6ceaf2020-02-22 18:36:32 +01003177 // "(..." is an expression.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003178 if (*p == '('
3179 || *p == '['
3180 || p[1] == ':'
3181 || (*p == '-' && p[1] == '>'))
3182 {
3183 eap->cmdidx = CMD_eval;
3184 return eap->cmd;
3185 }
3186
3187 oplen = assignment_len(skipwhite(p), &heredoc);
3188 if (oplen > 0)
3189 {
3190 // Recognize an assignment if we recognize the variable name:
3191 // "g:var = expr"
3192 // "var = expr" where "var" is a local var name.
3193 if (((p - eap->cmd) > 2 && eap->cmd[1] == ':')
3194 || lookup(eap->cmd, p - eap->cmd, cctx) >= 0)
3195 {
3196 eap->cmdidx = CMD_let;
3197 return eap->cmd;
3198 }
3199 }
3200 }
3201#endif
3202
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203 /*
3204 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00003205 * Exceptions:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003206 * - the 'k' command can directly be followed by any character.
3207 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003208 * but :sre[wind] is another command, as are :scr[iptnames],
Bram Moolenaar071d4272004-06-13 20:20:40 +00003209 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00003210 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003211 */
3212 p = eap->cmd;
3213 if (*p == 'k')
3214 {
3215 eap->cmdidx = CMD_k;
3216 ++p;
3217 }
3218 else if (p[0] == 's'
Bram Moolenaar204b93f2015-08-04 22:02:51 +02003219 && ((p[1] == 'c' && (p[2] == NUL || (p[2] != 's' && p[2] != 'r'
3220 && (p[3] == NUL || (p[3] != 'i' && p[4] != 'p')))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003221 || p[1] == 'g'
3222 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
3223 || p[1] == 'I'
3224 || (p[1] == 'r' && p[2] != 'e')))
3225 {
3226 eap->cmdidx = CMD_substitute;
3227 ++p;
3228 }
3229 else
3230 {
3231 while (ASCII_ISALPHA(*p))
3232 ++p;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003233 // for python 3.x support ":py3", ":python3", ":py3file", etc.
Bram Moolenaar55d5c032010-07-17 23:52:29 +02003234 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003235 {
Bram Moolenaarb6590522010-07-21 16:00:43 +02003236 while (ASCII_ISALNUM(*p))
3237 ++p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003238 }
3239 else if (*p == '9' && STRNCMP("vim9", eap->cmd, 4) == 0)
3240 {
3241 // include "9" for "vim9script"
3242 ++p;
3243 while (ASCII_ISALPHA(*p))
3244 ++p;
3245 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02003246
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003247 // check for non-alpha command
Bram Moolenaar071d4272004-06-13 20:20:40 +00003248 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3249 ++p;
3250 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003251 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
3252 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003253 // Check for ":dl", ":dell", etc. to ":deletel": that's
3254 // :delete with the 'l' flag. Same for 'p'.
Bram Moolenaardf177f62005-02-22 08:39:57 +00003255 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003256 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00003257 break;
3258 if (i == len - 1)
3259 {
3260 --len;
3261 if (p[-1] == 'l')
3262 eap->flags |= EXFLAG_LIST;
3263 else
3264 eap->flags |= EXFLAG_PRINT;
3265 }
3266 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003267
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003268 if (ASCII_ISLOWER(eap->cmd[0]))
3269 {
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003270 int c1 = eap->cmd[0];
Bram Moolenaar94f82cb2019-07-24 22:30:27 +02003271 int c2 = len == 1 ? NUL : eap->cmd[1];
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003272
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003273 if (command_count != (int)CMD_SIZE)
3274 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003275 iemsg(_("E943: Command table needs to be updated, run 'make cmdidxs'"));
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003276 getout(1);
3277 }
3278
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003279 // Use a precomputed index for fast look-up in cmdnames[]
3280 // taking into account the first 2 letters of eap->cmd.
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003281 eap->cmdidx = cmdidxs1[CharOrdLow(c1)];
3282 if (ASCII_ISLOWER(c2))
3283 eap->cmdidx += cmdidxs2[CharOrdLow(c1)][CharOrdLow(c2)];
3284 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003285 else
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003286 eap->cmdidx = CMD_bang;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003287
3288 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
3289 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
3290 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
3291 (size_t)len) == 0)
3292 {
3293#ifdef FEAT_EVAL
3294 if (full != NULL
3295 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
3296 *full = TRUE;
3297#endif
3298 break;
3299 }
3300
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003301 // Look for a user defined command as a last resort. Let ":Print" be
3302 // overruled by a user defined command.
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01003303 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
3304 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305 {
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003306 // User defined commands may contain digits.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003307 while (ASCII_ISALNUM(*p))
3308 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003309 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003310 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003311 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312 eap->cmdidx = CMD_SIZE;
3313 }
3314
3315 return p;
3316}
3317
3318#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003319static struct cmdmod
3320{
3321 char *name;
3322 int minlen;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003323 int has_count; // :123verbose :3tab
Bram Moolenaared53fb92007-11-24 20:50:24 +00003324} cmdmods[] = {
3325 {"aboveleft", 3, FALSE},
3326 {"belowright", 3, FALSE},
3327 {"botright", 2, FALSE},
3328 {"browse", 3, FALSE},
3329 {"confirm", 4, FALSE},
Bram Moolenaar7b668e82016-08-23 23:51:21 +02003330 {"filter", 4, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003331 {"hide", 3, FALSE},
3332 {"keepalt", 5, FALSE},
3333 {"keepjumps", 5, FALSE},
3334 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01003335 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003336 {"leftabove", 5, FALSE},
3337 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003338 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003339 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003340 {"rightbelow", 6, FALSE},
3341 {"sandbox", 3, FALSE},
3342 {"silent", 3, FALSE},
3343 {"tab", 3, TRUE},
3344 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003345 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003346 {"verbose", 4, TRUE},
3347 {"vertical", 4, FALSE},
3348};
3349
3350/*
3351 * Return length of a command modifier (including optional count).
3352 * Return zero when it's not a modifier.
3353 */
3354 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003355modifier_len(char_u *cmd)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003356{
3357 int i, j;
3358 char_u *p = cmd;
3359
3360 if (VIM_ISDIGIT(*cmd))
3361 p = skipwhite(skipdigits(cmd));
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003362 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003363 {
3364 for (j = 0; p[j] != NUL; ++j)
3365 if (p[j] != cmdmods[i].name[j])
3366 break;
Bram Moolenaar2d473ab2013-06-12 17:12:24 +02003367 if (!ASCII_ISALPHA(p[j]) && j >= cmdmods[i].minlen
Bram Moolenaared53fb92007-11-24 20:50:24 +00003368 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003369 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003370 }
3371 return 0;
3372}
3373
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374/*
3375 * Return > 0 if an Ex command "name" exists.
3376 * Return 2 if there is an exact match.
3377 * Return 3 if there is an ambiguous match.
3378 */
3379 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003380cmd_exists(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381{
3382 exarg_T ea;
3383 int full = FALSE;
3384 int i;
3385 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003386 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003388 // Check command modifiers.
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003389 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390 {
3391 for (j = 0; name[j] != NUL; ++j)
3392 if (name[j] != cmdmods[i].name[j])
3393 break;
3394 if (name[j] == NUL && j >= cmdmods[i].minlen)
3395 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3396 }
3397
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003398 // Check built-in commands and user defined commands.
3399 // For ":2match" and ":3match" we need to skip the number.
Bram Moolenaara9587612006-05-04 21:47:50 +00003400 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003402 p = find_ex_command(&ea, &full, NULL, NULL);
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003403 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003405 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3406 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003407 if (*skipwhite(p) != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003408 return 0; // trailing garbage
Bram Moolenaar071d4272004-06-13 20:20:40 +00003409 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3410}
3411#endif
3412
Bram Moolenaard0190392019-08-23 21:17:35 +02003413 cmdidx_T
3414excmd_get_cmdidx(char_u *cmd, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415{
Bram Moolenaard0190392019-08-23 21:17:35 +02003416 cmdidx_T idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003417
Bram Moolenaard0190392019-08-23 21:17:35 +02003418 for (idx = (cmdidx_T)0; (int)idx < (int)CMD_SIZE;
3419 idx = (cmdidx_T)((int)idx + 1))
3420 if (STRNCMP(cmdnames[(int)idx].cmd_name, cmd, (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421 break;
3422
Bram Moolenaard0190392019-08-23 21:17:35 +02003423 return idx;
3424}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003425
Bram Moolenaard0190392019-08-23 21:17:35 +02003426 long
3427excmd_get_argt(cmdidx_T idx)
3428{
3429 return (long)cmdnames[(int)idx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430}
3431
3432/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003433 * Skip a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434 *
3435 * Backslashed delimiters after / or ? will be skipped, and commands will
3436 * not be expanded between /'s and ?'s or after "'".
3437 *
Bram Moolenaardf177f62005-02-22 08:39:57 +00003438 * Also skip white space and ":" characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439 * Returns the "cmd" pointer advanced to beyond the range.
3440 */
3441 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003442skip_range(
3443 char_u *cmd,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003444 int *ctx) // pointer to xp_context or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003446 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01003448 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;\\", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449 {
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01003450 if (*cmd == '\\')
3451 {
3452 if (cmd[1] == '?' || cmd[1] == '/' || cmd[1] == '&')
3453 ++cmd;
3454 else
3455 break;
3456 }
3457 else if (*cmd == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458 {
3459 if (*++cmd == NUL && ctx != NULL)
3460 *ctx = EXPAND_NOTHING;
3461 }
3462 else if (*cmd == '/' || *cmd == '?')
3463 {
3464 delim = *cmd++;
3465 while (*cmd != NUL && *cmd != delim)
3466 if (*cmd++ == '\\' && *cmd != NUL)
3467 ++cmd;
3468 if (*cmd == NUL && ctx != NULL)
3469 *ctx = EXPAND_NOTHING;
3470 }
3471 if (*cmd != NUL)
3472 ++cmd;
3473 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00003474
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003475 // Skip ":" and white space.
Bram Moolenaardf177f62005-02-22 08:39:57 +00003476 while (*cmd == ':')
3477 cmd = skipwhite(cmd + 1);
3478
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479 return cmd;
3480}
3481
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003482 static void
3483addr_error(cmd_addr_T addr_type)
3484{
3485 if (addr_type == ADDR_NONE)
3486 emsg(_(e_norange));
3487 else
3488 emsg(_(e_invrange));
3489}
3490
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491/*
Bram Moolenaar198cb662018-09-06 21:44:17 +02003492 * Get a single EX address.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493 *
3494 * Set ptr to the next character after the part that was interpreted.
3495 * Set ptr to NULL when an error is encountered.
Bram Moolenaar198cb662018-09-06 21:44:17 +02003496 * This may set the last used search pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497 *
3498 * Return MAXLNUM when no Ex address was found.
3499 */
3500 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003501get_address(
3502 exarg_T *eap UNUSED,
3503 char_u **ptr,
Bram Moolenaar1b03a192019-12-08 17:08:29 +01003504 cmd_addr_T addr_type,
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003505 int skip, // only skip the address, don't use it
3506 int silent, // no errors or side effects
3507 int to_other_file, // flag: may jump to other file
3508 int address_count UNUSED) // 1 for first address, >1 after comma
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509{
3510 int c;
3511 int i;
3512 long n;
3513 char_u *cmd;
3514 pos_T pos;
3515 pos_T *fp;
3516 linenr_T lnum;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003517 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518
3519 cmd = skipwhite(*ptr);
3520 lnum = MAXLNUM;
3521 do
3522 {
3523 switch (*cmd)
3524 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003525 case '.': // '.' - Cursor position
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003526 ++cmd;
3527 switch (addr_type)
3528 {
3529 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003530 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531 lnum = curwin->w_cursor.lnum;
3532 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003533 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003534 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003535 break;
3536 case ADDR_ARGUMENTS:
3537 lnum = curwin->w_arg_idx + 1;
3538 break;
3539 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003540 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003541 lnum = curbuf->b_fnum;
3542 break;
3543 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003544 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003545 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003546 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003547 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003548 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003549 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003550 cmd = NULL;
3551 goto error;
3552 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003553 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003554#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02003555 lnum = qf_get_cur_idx(eap);
3556#endif
3557 break;
3558 case ADDR_QUICKFIX_VALID:
3559#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003560 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02003561#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003562 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003563 }
3564 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003566 case '$': // '$' - last line
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003567 ++cmd;
3568 switch (addr_type)
3569 {
3570 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003571 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003572 lnum = curbuf->b_ml.ml_line_count;
3573 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003574 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003575 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003576 break;
3577 case ADDR_ARGUMENTS:
3578 lnum = ARGCOUNT;
3579 break;
3580 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003581 buf = lastbuf;
3582 while (buf->b_ml.ml_mfp == NULL)
3583 {
3584 if (buf->b_prev == NULL)
3585 break;
3586 buf = buf->b_prev;
3587 }
3588 lnum = buf->b_fnum;
3589 break;
3590 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003591 lnum = lastbuf->b_fnum;
3592 break;
3593 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003594 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003595 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003596 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003597 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003598 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003599 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003600 cmd = NULL;
3601 goto error;
3602 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003603 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003604#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003605 lnum = qf_get_size(eap);
3606 if (lnum == 0)
3607 lnum = 1;
Bram Moolenaare906c502015-09-09 21:10:39 +02003608#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003609 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003610 case ADDR_QUICKFIX_VALID:
3611#ifdef FEAT_QUICKFIX
3612 lnum = qf_get_valid_size(eap);
3613 if (lnum == 0)
3614 lnum = 1;
3615#endif
3616 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003617 }
3618 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003620 case '\'': // ''' - mark
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003621 if (*++cmd == NUL)
3622 {
3623 cmd = NULL;
3624 goto error;
3625 }
3626 if (addr_type != ADDR_LINES)
3627 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003628 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003629 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003630 goto error;
3631 }
3632 if (skip)
3633 ++cmd;
3634 else
3635 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003636 // Only accept a mark in another file when it is
3637 // used by itself: ":'M".
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003638 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
3639 ++cmd;
3640 if (fp == (pos_T *)-1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003641 // Jumped to another file.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003642 lnum = curwin->w_cursor.lnum;
3643 else
3644 {
3645 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646 {
3647 cmd = NULL;
3648 goto error;
3649 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003650 lnum = fp->lnum;
3651 }
3652 }
3653 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003654
3655 case '/':
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003656 case '?': // '/' or '?' - search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003657 c = *cmd++;
3658 if (addr_type != ADDR_LINES)
3659 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003660 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003661 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003662 goto error;
3663 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003664 if (skip) // skip "/pat/"
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003665 {
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02003666 cmd = skip_regexp(cmd, c, (int)p_magic);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003667 if (*cmd == c)
3668 ++cmd;
3669 }
3670 else
3671 {
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003672 int flags;
3673
3674 pos = curwin->w_cursor; // save curwin->w_cursor
3675
3676 // When '/' or '?' follows another address, start from
3677 // there.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003678 if (lnum != MAXLNUM)
3679 curwin->w_cursor.lnum = lnum;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003680
3681 // Start a forward search at the end of the line (unless
3682 // before the first line).
3683 // Start a backward search at the start of the line.
3684 // This makes sure we never match in the current
3685 // line, and can match anywhere in the
3686 // next/previous line.
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01003687 if (c == '/' && curwin->w_cursor.lnum > 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003688 curwin->w_cursor.col = MAXCOL;
3689 else
3690 curwin->w_cursor.col = 0;
3691 searchcmdlen = 0;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003692 flags = silent ? 0 : SEARCH_HIS | SEARCH_MSG;
Bram Moolenaarc036e872020-02-21 21:30:52 +01003693 if (!do_search(NULL, c, c, cmd, 1L, flags, NULL))
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003694 {
3695 curwin->w_cursor = pos;
3696 cmd = NULL;
3697 goto error;
3698 }
3699 lnum = curwin->w_cursor.lnum;
3700 curwin->w_cursor = pos;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003701 // adjust command string pointer
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003702 cmd += searchcmdlen;
3703 }
3704 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003705
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003706 case '\\': // "\?", "\/" or "\&", repeat search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003707 ++cmd;
3708 if (addr_type != ADDR_LINES)
3709 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003710 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003711 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003712 goto error;
3713 }
3714 if (*cmd == '&')
3715 i = RE_SUBST;
3716 else if (*cmd == '?' || *cmd == '/')
3717 i = RE_SEARCH;
3718 else
3719 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003720 emsg(_(e_backslash));
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003721 cmd = NULL;
3722 goto error;
3723 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003725 if (!skip)
3726 {
3727 /*
3728 * When search follows another address, start from
3729 * there.
3730 */
3731 if (lnum != MAXLNUM)
3732 pos.lnum = lnum;
3733 else
3734 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003735
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003736 /*
3737 * Start the search just like for the above
3738 * do_search().
3739 */
3740 if (*cmd != '?')
3741 pos.col = MAXCOL;
3742 else
3743 pos.col = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02003744 pos.coladd = 0;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01003745 if (searchit(curwin, curbuf, &pos, NULL,
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003746 *cmd == '?' ? BACKWARD : FORWARD,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02003747 (char_u *)"", 1L, SEARCH_MSG, i, NULL) != FAIL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003748 lnum = pos.lnum;
3749 else
3750 {
3751 cmd = NULL;
3752 goto error;
3753 }
3754 }
3755 ++cmd;
3756 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003757
3758 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003759 if (VIM_ISDIGIT(*cmd)) // absolute line number
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003760 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003761 }
3762
3763 for (;;)
3764 {
3765 cmd = skipwhite(cmd);
3766 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
3767 break;
3768
3769 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003770 {
3771 switch (addr_type)
3772 {
3773 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003774 case ADDR_OTHER:
3775 // "+1" is same as ".+1"
Bram Moolenaarf240e182014-11-27 18:33:02 +01003776 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003777 break;
3778 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003779 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003780 break;
3781 case ADDR_ARGUMENTS:
3782 lnum = curwin->w_arg_idx + 1;
3783 break;
3784 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003785 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003786 lnum = curbuf->b_fnum;
3787 break;
3788 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003789 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003790 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003791 case ADDR_TABS_RELATIVE:
3792 lnum = 1;
3793 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003794 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003795#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02003796 lnum = qf_get_cur_idx(eap);
3797#endif
3798 break;
3799 case ADDR_QUICKFIX_VALID:
3800#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003801 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02003802#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003803 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003804 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003805 case ADDR_UNSIGNED:
3806 lnum = 0;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003807 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003808 }
3809 }
3810
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811 if (VIM_ISDIGIT(*cmd))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003812 i = '+'; // "number" is same as "+number"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003813 else
3814 i = *cmd++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003815 if (!VIM_ISDIGIT(*cmd)) // '+' is '+1', but '+0' is not '+1'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003816 n = 1;
3817 else
3818 n = getdigits(&cmd);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003819
3820 if (addr_type == ADDR_TABS_RELATIVE)
3821 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003822 emsg(_(e_invrange));
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003823 cmd = NULL;
3824 goto error;
3825 }
3826 else if (addr_type == ADDR_LOADED_BUFFERS
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003827 || addr_type == ADDR_BUFFERS)
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003828 lnum = compute_buffer_local_count(
3829 addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830 else
Bram Moolenaarded27822017-01-02 14:27:34 +01003831 {
3832#ifdef FEAT_FOLDING
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003833 // Relative line addressing, need to adjust for folded lines
3834 // now, but only do it after the first address.
Bram Moolenaarded27822017-01-02 14:27:34 +01003835 if (addr_type == ADDR_LINES && (i == '-' || i == '+')
3836 && address_count >= 2)
3837 (void)hasFolding(lnum, NULL, &lnum);
3838#endif
3839 if (i == '-')
3840 lnum -= n;
3841 else
3842 lnum += n;
3843 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003844 }
3845 } while (*cmd == '/' || *cmd == '?');
3846
3847error:
3848 *ptr = cmd;
3849 return lnum;
3850}
3851
3852/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00003853 * Get flags from an Ex command argument.
3854 */
3855 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003856get_flags(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00003857{
3858 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
3859 {
3860 if (*eap->arg == 'l')
3861 eap->flags |= EXFLAG_LIST;
3862 else if (*eap->arg == 'p')
3863 eap->flags |= EXFLAG_PRINT;
3864 else
3865 eap->flags |= EXFLAG_NR;
3866 eap->arg = skipwhite(eap->arg + 1);
3867 }
3868}
3869
3870/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003871 * Function called for command which is Not Implemented. NI!
3872 */
3873 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003874ex_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003875{
3876 if (!eap->skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003877 eap->errmsg = N_("E319: Sorry, the command is not available in this version");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003878}
3879
Bram Moolenaar7bb75552007-07-16 18:39:49 +00003880#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003881/*
3882 * Function called for script command which is Not Implemented. NI!
3883 * Skips over ":perl <<EOF" constructs.
3884 */
3885 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003886ex_script_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003887{
3888 if (!eap->skip)
3889 ex_ni(eap);
3890 else
3891 vim_free(script_get(eap, eap->arg));
3892}
3893#endif
3894
3895/*
3896 * Check range in Ex command for validity.
3897 * Return NULL when valid, error message when invalid.
3898 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003899 static char *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003900invalid_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003901{
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003902 buf_T *buf;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003903
Bram Moolenaar071d4272004-06-13 20:20:40 +00003904 if ( eap->line1 < 0
3905 || eap->line2 < 0
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003906 || eap->line1 > eap->line2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003907 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003908
Bram Moolenaar8071cb22019-07-12 17:58:01 +02003909 if (eap->argt & EX_RANGE)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003910 {
Bram Moolenaarb7316892019-05-01 18:08:42 +02003911 switch (eap->addr_type)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003912 {
3913 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003914 if (eap->line2 > curbuf->b_ml.ml_line_count
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003915#ifdef FEAT_DIFF
3916 + (eap->cmdidx == CMD_diffget)
3917#endif
3918 )
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003919 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003920 break;
3921 case ADDR_ARGUMENTS:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003922 // add 1 if ARGCOUNT is 0
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003923 if (eap->line2 > ARGCOUNT + (!ARGCOUNT))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003924 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003925 break;
3926 case ADDR_BUFFERS:
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02003927 // Only a boundary check, not whether the buffers actually
3928 // exist.
3929 if (eap->line1 < 1 || eap->line2 > get_highest_fnum())
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003930 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003931 break;
3932 case ADDR_LOADED_BUFFERS:
3933 buf = firstbuf;
3934 while (buf->b_ml.ml_mfp == NULL)
3935 {
3936 if (buf->b_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003937 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003938 buf = buf->b_next;
3939 }
3940 if (eap->line1 < buf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003941 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003942 buf = lastbuf;
3943 while (buf->b_ml.ml_mfp == NULL)
3944 {
3945 if (buf->b_prev == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003946 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003947 buf = buf->b_prev;
3948 }
3949 if (eap->line2 > buf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003950 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003951 break;
3952 case ADDR_WINDOWS:
Bram Moolenaar8be63882015-01-14 11:25:05 +01003953 if (eap->line2 > LAST_WIN_NR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003954 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003955 break;
3956 case ADDR_TABS:
3957 if (eap->line2 > LAST_TAB_NR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003958 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003959 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003960 case ADDR_TABS_RELATIVE:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003961 case ADDR_OTHER:
3962 // Any range is OK.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003963 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003964 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003965#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02003966 // No error for value that is too big, will use the last entry.
3967 if (eap->line2 <= 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003968 return _(e_invrange);
Bram Moolenaare906c502015-09-09 21:10:39 +02003969#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003970 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003971 case ADDR_QUICKFIX_VALID:
3972#ifdef FEAT_QUICKFIX
3973 if ((eap->line2 != 1 && eap->line2 > qf_get_valid_size(eap))
3974 || eap->line2 < 0)
3975 return _(e_invrange);
3976#endif
3977 break;
3978 case ADDR_UNSIGNED:
3979 if (eap->line2 < 0)
3980 return _(e_invrange);
3981 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003982 case ADDR_NONE:
3983 // Will give an error elsewhere.
3984 break;
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003985 }
3986 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003987 return NULL;
3988}
3989
3990/*
3991 * Correct the range for zero line number, if required.
3992 */
3993 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003994correct_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995{
Bram Moolenaar8071cb22019-07-12 17:58:01 +02003996 if (!(eap->argt & EX_ZEROR)) // zero in range not allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00003997 {
3998 if (eap->line1 == 0)
3999 eap->line1 = 1;
4000 if (eap->line2 == 0)
4001 eap->line2 = 1;
4002 }
4003}
4004
Bram Moolenaar748bf032005-02-02 23:04:36 +00004005#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004006/*
4007 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4008 * pattern. Otherwise return eap->arg.
4009 */
4010 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004011skip_grep_pat(exarg_T *eap)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004012{
4013 char_u *p = eap->arg;
4014
Bram Moolenaara37420f2006-02-04 22:37:47 +00004015 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4016 || eap->cmdidx == CMD_vimgrepadd
4017 || eap->cmdidx == CMD_lvimgrepadd
4018 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004019 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004020 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004021 if (p == NULL)
4022 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004023 }
4024 return p;
4025}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004026
4027/*
4028 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4029 * in the command line, so that things like % get expanded.
4030 */
4031 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004032replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004033{
4034 char_u *new_cmdline;
4035 char_u *program;
4036 char_u *pos;
4037 char_u *ptr;
4038 int len;
4039 int i;
4040
4041 /*
4042 * Don't do it when ":vimgrep" is used for ":grep".
4043 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004044 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4045 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4046 || eap->cmdidx == CMD_grepadd
4047 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004048 && !grep_internal(eap->cmdidx))
4049 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004050 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4051 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004052 {
4053 if (*curbuf->b_p_gp == NUL)
4054 program = p_gp;
4055 else
4056 program = curbuf->b_p_gp;
4057 }
4058 else
4059 {
4060 if (*curbuf->b_p_mp == NUL)
4061 program = p_mp;
4062 else
4063 program = curbuf->b_p_mp;
4064 }
4065
4066 p = skipwhite(p);
4067
4068 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4069 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004070 // replace $* by given arguments
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004071 i = 1;
4072 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4073 ++i;
4074 len = (int)STRLEN(p);
Bram Moolenaar51e14382019-05-25 20:21:28 +02004075 new_cmdline = alloc(STRLEN(program) + i * (len - 2) + 1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004076 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004077 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004078 ptr = new_cmdline;
4079 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4080 {
4081 i = (int)(pos - program);
4082 STRNCPY(ptr, program, i);
4083 STRCPY(ptr += i, p);
4084 ptr += len;
4085 program = pos + 2;
4086 }
4087 STRCPY(ptr, program);
4088 }
4089 else
4090 {
Bram Moolenaar51e14382019-05-25 20:21:28 +02004091 new_cmdline = alloc(STRLEN(program) + STRLEN(p) + 2);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004092 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004093 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004094 STRCPY(new_cmdline, program);
4095 STRCAT(new_cmdline, " ");
4096 STRCAT(new_cmdline, p);
4097 }
4098 msg_make(p);
4099
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004100 // 'eap->cmd' is not set here, because it is not used at CMD_make
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004101 vim_free(*cmdlinep);
4102 *cmdlinep = new_cmdline;
4103 p = new_cmdline;
4104 }
4105 return p;
4106}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004107#endif
4108
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109/*
4110 * Expand file name in Ex command argument.
Bram Moolenaar0abb4272019-06-15 16:06:00 +02004111 * When an error is detected, "errormsgp" is set to a non-NULL pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112 * Return FAIL for failure, OK otherwise.
4113 */
4114 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004115expand_filename(
4116 exarg_T *eap,
4117 char_u **cmdlinep,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004118 char **errormsgp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004120 int has_wildcards; // need to expand wildcards
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121 char_u *repl;
4122 int srclen;
4123 char_u *p;
4124 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004125 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126
Bram Moolenaar748bf032005-02-02 23:04:36 +00004127#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004128 // Skip a regexp pattern for ":vimgrep[add] pat file..."
Bram Moolenaar748bf032005-02-02 23:04:36 +00004129 p = skip_grep_pat(eap);
4130#else
4131 p = eap->arg;
4132#endif
4133
Bram Moolenaar071d4272004-06-13 20:20:40 +00004134 /*
4135 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4136 * the file name contains a wildcard it should not cause expanding.
4137 * (it will be expanded anyway if there is a wildcard before replacing).
4138 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004139 has_wildcards = mch_has_wildcard(p);
4140 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004142#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004143 // Skip over `=expr`, wildcards in it are not expanded.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004144 if (p[0] == '`' && p[1] == '=')
4145 {
4146 p += 2;
4147 (void)skip_expr(&p);
4148 if (*p == '`')
4149 ++p;
4150 continue;
4151 }
4152#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004153 /*
4154 * Quick check if this cannot be the start of a special string.
4155 * Also removes backslash before '%', '#' and '<'.
4156 */
4157 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4158 {
4159 ++p;
4160 continue;
4161 }
4162
4163 /*
4164 * Try to find a match at this position.
4165 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00004166 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
4167 errormsgp, &escaped);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004168 if (*errormsgp != NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169 return FAIL;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004170 if (repl == NULL) // no match found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171 {
4172 p += srclen;
4173 continue;
4174 }
4175
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004176 // Wildcards won't be expanded below, the replacement is taken
4177 // literally. But do expand "~/file", "~user/file" and "$HOME/file".
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004178 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4179 {
4180 char_u *l = repl;
4181
4182 repl = expand_env_save(repl);
4183 vim_free(l);
4184 }
4185
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004186 // Need to escape white space et al. with a backslash.
4187 // Don't do this for:
4188 // - replacement that already has been escaped: "##"
4189 // - shell commands (may have to use quotes instead).
4190 // - non-unix systems when there is a single argument (spaces don't
4191 // separate arguments then).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00004193 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00004194 && eap->cmdidx != CMD_bang
Bram Moolenaar071d4272004-06-13 20:20:40 +00004195 && eap->cmdidx != CMD_grep
4196 && eap->cmdidx != CMD_grepadd
Bram Moolenaarbf15b8d2017-06-04 20:43:48 +02004197 && eap->cmdidx != CMD_hardcopy
Bram Moolenaar67883b42017-07-27 22:57:00 +02004198 && eap->cmdidx != CMD_lgrep
4199 && eap->cmdidx != CMD_lgrepadd
4200 && eap->cmdidx != CMD_lmake
4201 && eap->cmdidx != CMD_make
4202 && eap->cmdidx != CMD_terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203#ifndef UNIX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004204 && !(eap->argt & EX_NOSPC)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205#endif
4206 )
4207 {
4208 char_u *l;
4209#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004210 // Don't escape a backslash here, because rem_backslash() doesn't
4211 // remove it later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004212 static char_u *nobslash = (char_u *)" \t\"|";
4213# define ESCAPE_CHARS nobslash
4214#else
4215# define ESCAPE_CHARS escape_chars
4216#endif
4217
4218 for (l = repl; *l; ++l)
4219 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
4220 {
4221 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
4222 if (l != NULL)
4223 {
4224 vim_free(repl);
4225 repl = l;
4226 }
4227 break;
4228 }
4229 }
4230
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004231 // For a shell command a '!' must be escaped.
Bram Moolenaar67883b42017-07-27 22:57:00 +02004232 if ((eap->usefilter || eap->cmdidx == CMD_bang
4233 || eap->cmdidx == CMD_terminal)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004234 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004235 {
4236 char_u *l;
4237
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004238 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239 if (l != NULL)
4240 {
4241 vim_free(repl);
4242 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243 }
4244 }
4245
4246 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
4247 vim_free(repl);
4248 if (p == NULL)
4249 return FAIL;
4250 }
4251
4252 /*
4253 * One file argument: Expand wildcards.
4254 * Don't do this with ":r !command" or ":w !command".
4255 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004256 if ((eap->argt & EX_NOSPC) && !eap->usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004257 {
4258 /*
4259 * May do this twice:
4260 * 1. Replace environment variables.
4261 * 2. Replace any other wildcards, remove backslashes.
4262 */
4263 for (n = 1; n <= 2; ++n)
4264 {
4265 if (n == 2)
4266 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004267 /*
4268 * Halve the number of backslashes (this is Vi compatible).
4269 * For Unix and OS/2, when wildcards are expanded, this is
4270 * done by ExpandOne() below.
4271 */
Bram Moolenaare7fedb62015-12-31 19:07:19 +01004272#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004273 if (!has_wildcards)
4274#endif
4275 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004276 }
4277
4278 if (has_wildcards)
4279 {
4280 if (n == 1)
4281 {
4282 /*
4283 * First loop: May expand environment variables. This
4284 * can be done much faster with expand_env() than with
4285 * something else (e.g., calling a shell).
4286 * After expanding environment variables, check again
4287 * if there are still wildcards present.
4288 */
4289 if (vim_strchr(eap->arg, '$') != NULL
4290 || vim_strchr(eap->arg, '~') != NULL)
4291 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00004292 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004293 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004294 has_wildcards = mch_has_wildcard(NameBuff);
4295 p = NameBuff;
4296 }
4297 else
4298 p = NULL;
4299 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004300 else // n == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004301 {
4302 expand_T xpc;
Bram Moolenaarb40c2572019-10-19 21:01:05 +02004303 int options = WILD_LIST_NOTFOUND
4304 | WILD_NOERROR | WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004305
4306 ExpandInit(&xpc);
4307 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004308 if (p_wic)
4309 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004310 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01004311 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004312 if (p == NULL)
4313 return FAIL;
4314 }
4315 if (p != NULL)
4316 {
4317 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
4318 p, cmdlinep);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004319 if (n == 2) // p came from ExpandOne()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004320 vim_free(p);
4321 }
4322 }
4323 }
4324 }
4325 return OK;
4326}
4327
4328/*
4329 * Replace part of the command line, keeping eap->cmd, eap->arg and
4330 * eap->nextcmd correct.
4331 * "src" points to the part that is to be replaced, of length "srclen".
4332 * "repl" is the replacement string.
4333 * Returns a pointer to the character after the replaced string.
4334 * Returns NULL for failure.
4335 */
4336 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004337repl_cmdline(
4338 exarg_T *eap,
4339 char_u *src,
4340 int srclen,
4341 char_u *repl,
4342 char_u **cmdlinep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004343{
4344 int len;
4345 int i;
4346 char_u *new_cmdline;
4347
4348 /*
4349 * The new command line is build in new_cmdline[].
4350 * First allocate it.
4351 * Careful: a "+cmd" argument may have been NUL terminated.
4352 */
4353 len = (int)STRLEN(repl);
4354 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004355 if (eap->nextcmd != NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004356 i += (int)STRLEN(eap->nextcmd);// add space for next command
Bram Moolenaar964b3742019-05-24 18:54:09 +02004357 if ((new_cmdline = alloc(i)) == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004358 return NULL; // out of memory!
Bram Moolenaar071d4272004-06-13 20:20:40 +00004359
4360 /*
4361 * Copy the stuff before the expanded part.
4362 * Copy the expanded stuff.
4363 * Copy what came after the expanded part.
4364 * Copy the next commands, if there are any.
4365 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004366 i = (int)(src - *cmdlinep); // length of part before match
Bram Moolenaar071d4272004-06-13 20:20:40 +00004367 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00004368
Bram Moolenaar071d4272004-06-13 20:20:40 +00004369 mch_memmove(new_cmdline + i, repl, (size_t)len);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004370 i += len; // remember the end of the string
Bram Moolenaar071d4272004-06-13 20:20:40 +00004371 STRCPY(new_cmdline + i, src + srclen);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004372 src = new_cmdline + i; // remember where to continue
Bram Moolenaar071d4272004-06-13 20:20:40 +00004373
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004374 if (eap->nextcmd != NULL) // append next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00004375 {
4376 i = (int)STRLEN(new_cmdline) + 1;
4377 STRCPY(new_cmdline + i, eap->nextcmd);
4378 eap->nextcmd = new_cmdline + i;
4379 }
4380 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
4381 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
4382 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
4383 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
4384 vim_free(*cmdlinep);
4385 *cmdlinep = new_cmdline;
4386
4387 return src;
4388}
4389
4390/*
4391 * Check for '|' to separate commands and '"' to start comments.
4392 */
4393 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004394separate_nextcmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004395{
4396 char_u *p;
4397
Bram Moolenaar86b68352004-12-27 21:59:20 +00004398#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004399 p = skip_grep_pat(eap);
4400#else
4401 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004402#endif
4403
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004404 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405 {
4406 if (*p == Ctrl_V)
4407 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004408 if (eap->argt & (EX_CTRLV | EX_XFILE))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004409 ++p; // skip CTRL-V and next char
Bram Moolenaar071d4272004-06-13 20:20:40 +00004410 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004411 // remove CTRL-V and skip next char
Bram Moolenaara7241f52008-06-24 20:39:31 +00004412 STRMOVE(p, p + 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004413 if (*p == NUL) // stop at NUL after CTRL-V
Bram Moolenaar071d4272004-06-13 20:20:40 +00004414 break;
4415 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004416
4417#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004418 // Skip over `=expr` when wildcards are expanded.
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004419 else if (p[0] == '`' && p[1] == '=' && (eap->argt & EX_XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004420 {
4421 p += 2;
4422 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004423 }
4424#endif
4425
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004426 // Check for '"': start of comment or '|': next command
4427 // :@" and :*" do not start a comment!
4428 // :redir @" doesn't either.
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004429 else if ((*p == '"' && !(eap->argt & EX_NOTRLCOM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004430 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
4431 || p != eap->arg)
4432 && (eap->cmdidx != CMD_redir
4433 || p != eap->arg + 1 || p[-1] != '@'))
4434 || *p == '|' || *p == '\n')
4435 {
4436 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004437 * We remove the '\' before the '|', unless EX_CTRLV is used
Bram Moolenaar071d4272004-06-13 20:20:40 +00004438 * AND 'b' is present in 'cpoptions'.
4439 */
4440 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004441 || !(eap->argt & EX_CTRLV)) && *(p - 1) == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004442 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004443 STRMOVE(p - 1, p); // remove the '\'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004444 --p;
4445 }
4446 else
4447 {
4448 eap->nextcmd = check_nextcmd(p);
4449 *p = NUL;
4450 break;
4451 }
4452 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004453 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004454
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004455 if (!(eap->argt & EX_NOTRLCOM)) // remove trailing spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00004456 del_trailing_spaces(eap->arg);
4457}
4458
4459/*
4460 * get + command from ex argument
4461 */
4462 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004463getargcmd(char_u **argp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004464{
4465 char_u *arg = *argp;
4466 char_u *command = NULL;
4467
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004468 if (*arg == '+') // +[command]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004469 {
4470 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02004471 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004472 command = dollar_command;
4473 else
4474 {
4475 command = arg;
4476 arg = skip_cmd_arg(command, TRUE);
4477 if (*arg != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004478 *arg++ = NUL; // terminate command with NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004479 }
4480
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004481 arg = skipwhite(arg); // skip over spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00004482 *argp = arg;
4483 }
4484 return command;
4485}
4486
4487/*
4488 * Find end of "+command" argument. Skip over "\ " and "\\".
4489 */
Bram Moolenaard0190392019-08-23 21:17:35 +02004490 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004491skip_cmd_arg(
4492 char_u *p,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004493 int rembs) // TRUE to halve the number of backslashes
Bram Moolenaar071d4272004-06-13 20:20:40 +00004494{
4495 while (*p && !vim_isspace(*p))
4496 {
4497 if (*p == '\\' && p[1] != NUL)
4498 {
4499 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00004500 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004501 else
4502 ++p;
4503 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004504 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004505 }
4506 return p;
4507}
4508
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004509 int
4510get_bad_opt(char_u *p, exarg_T *eap)
4511{
4512 if (STRICMP(p, "keep") == 0)
4513 eap->bad_char = BAD_KEEP;
4514 else if (STRICMP(p, "drop") == 0)
4515 eap->bad_char = BAD_DROP;
4516 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
4517 eap->bad_char = *p;
Bram Moolenaar75808492018-06-12 12:39:41 +02004518 else
4519 return FAIL;
4520 return OK;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004521}
4522
Bram Moolenaar071d4272004-06-13 20:20:40 +00004523/*
4524 * Get "++opt=arg" argument.
4525 * Return FAIL or OK.
4526 */
4527 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004528getargopt(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004529{
4530 char_u *arg = eap->arg + 2;
4531 int *pp = NULL;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004532 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004533 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004534
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004535 // ":edit ++[no]bin[ary] file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004536 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
4537 {
4538 if (*arg == 'n')
4539 {
4540 arg += 2;
4541 eap->force_bin = FORCE_NOBIN;
4542 }
4543 else
4544 eap->force_bin = FORCE_BIN;
4545 if (!checkforcmd(&arg, "binary", 3))
4546 return FAIL;
4547 eap->arg = skipwhite(arg);
4548 return OK;
4549 }
4550
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004551 // ":read ++edit file"
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004552 if (STRNCMP(arg, "edit", 4) == 0)
4553 {
4554 eap->read_edit = TRUE;
4555 eap->arg = skipwhite(arg + 4);
4556 return OK;
4557 }
4558
Bram Moolenaar071d4272004-06-13 20:20:40 +00004559 if (STRNCMP(arg, "ff", 2) == 0)
4560 {
4561 arg += 2;
4562 pp = &eap->force_ff;
4563 }
4564 else if (STRNCMP(arg, "fileformat", 10) == 0)
4565 {
4566 arg += 10;
4567 pp = &eap->force_ff;
4568 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004569 else if (STRNCMP(arg, "enc", 3) == 0)
4570 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01004571 if (STRNCMP(arg, "encoding", 8) == 0)
4572 arg += 8;
4573 else
4574 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004575 pp = &eap->force_enc;
4576 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004577 else if (STRNCMP(arg, "bad", 3) == 0)
4578 {
4579 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004580 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004581 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004582
4583 if (pp == NULL || *arg != '=')
4584 return FAIL;
4585
4586 ++arg;
4587 *pp = (int)(arg - eap->cmd);
4588 arg = skip_cmd_arg(arg, FALSE);
4589 eap->arg = skipwhite(arg);
4590 *arg = NUL;
4591
Bram Moolenaar071d4272004-06-13 20:20:40 +00004592 if (pp == &eap->force_ff)
4593 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004594 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
4595 return FAIL;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004596 eap->force_ff = eap->cmd[eap->force_ff];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004597 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004598 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004599 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004600 // Make 'fileencoding' lower case.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004601 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
4602 *p = TOLOWER_ASC(*p);
4603 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004604 else
4605 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004606 // Check ++bad= argument. Must be a single-byte character, "keep" or
4607 // "drop".
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004608 if (get_bad_opt(eap->cmd + bad_char_idx, eap) == FAIL)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004609 return FAIL;
4610 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004611
4612 return OK;
4613}
4614
Bram Moolenaar071d4272004-06-13 20:20:40 +00004615 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004616ex_autocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617{
4618 /*
Bram Moolenaar26d4b892018-07-04 22:26:28 +02004619 * Disallow autocommands from .exrc and .vimrc in current
Bram Moolenaar071d4272004-06-13 20:20:40 +00004620 * directory for security reasons.
4621 */
4622 if (secure)
4623 {
4624 secure = 2;
4625 eap->errmsg = e_curdir;
4626 }
4627 else if (eap->cmdidx == CMD_autocmd)
4628 do_autocmd(eap->arg, eap->forceit);
4629 else
4630 do_augroup(eap->arg, eap->forceit);
4631}
4632
4633/*
4634 * ":doautocmd": Apply the automatic commands to the current buffer.
4635 */
4636 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004637ex_doautocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004638{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01004639 char_u *arg = eap->arg;
4640 int call_do_modelines = check_nomodeline(&arg);
Bram Moolenaar1610d052016-06-09 22:53:01 +02004641 int did_aucmd;
Bram Moolenaar60542ac2012-02-12 20:14:01 +01004642
Bram Moolenaar1610d052016-06-09 22:53:01 +02004643 (void)do_doautocmd(arg, TRUE, &did_aucmd);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004644 // Only when there is no <nomodeline>.
Bram Moolenaar1610d052016-06-09 22:53:01 +02004645 if (call_do_modelines && did_aucmd)
Bram Moolenaar60542ac2012-02-12 20:14:01 +01004646 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004647}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004648
Bram Moolenaar071d4272004-06-13 20:20:40 +00004649/*
4650 * :[N]bunload[!] [N] [bufname] unload buffer
4651 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
4652 * :[N]bwipeout[!] [N] [bufname] delete buffer really
4653 */
4654 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004655ex_bunload(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004656{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004657 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02004658 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004659 eap->errmsg = do_bufdel(
4660 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
4661 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
4662 : DOBUF_UNLOAD, eap->arg,
4663 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
4664}
4665
4666/*
4667 * :[N]buffer [N] to buffer N
4668 * :[N]sbuffer [N] to buffer N
4669 */
4670 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004671ex_buffer(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004672{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004673 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02004674 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004675 if (*eap->arg)
4676 eap->errmsg = e_trailing;
4677 else
4678 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004679 if (eap->addr_count == 0) // default is current buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00004680 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
4681 else
4682 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004683 if (eap->do_ecmd_cmd != NULL)
4684 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004685 }
4686}
4687
4688/*
4689 * :[N]bmodified [N] to next mod. buffer
4690 * :[N]sbmodified [N] to next mod. buffer
4691 */
4692 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004693ex_bmodified(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694{
4695 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004696 if (eap->do_ecmd_cmd != NULL)
4697 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004698}
4699
4700/*
4701 * :[N]bnext [N] to next buffer
4702 * :[N]sbnext [N] split and to next buffer
4703 */
4704 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004705ex_bnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004706{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004707 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01004708 return;
4709
Bram Moolenaar071d4272004-06-13 20:20:40 +00004710 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004711 if (eap->do_ecmd_cmd != NULL)
4712 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004713}
4714
4715/*
4716 * :[N]bNext [N] to previous buffer
4717 * :[N]bprevious [N] to previous buffer
4718 * :[N]sbNext [N] split and to previous buffer
4719 * :[N]sbprevious [N] split and to previous buffer
4720 */
4721 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004722ex_bprevious(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004723{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004724 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01004725 return;
4726
Bram Moolenaar071d4272004-06-13 20:20:40 +00004727 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004728 if (eap->do_ecmd_cmd != NULL)
4729 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004730}
4731
4732/*
4733 * :brewind to first buffer
4734 * :bfirst to first buffer
4735 * :sbrewind split and to first buffer
4736 * :sbfirst split and to first buffer
4737 */
4738 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004739ex_brewind(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004740{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004741 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01004742 return;
4743
Bram Moolenaar071d4272004-06-13 20:20:40 +00004744 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004745 if (eap->do_ecmd_cmd != NULL)
4746 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004747}
4748
4749/*
4750 * :blast to last buffer
4751 * :sblast split and to last buffer
4752 */
4753 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004754ex_blast(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004756 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01004757 return;
4758
Bram Moolenaar071d4272004-06-13 20:20:40 +00004759 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004760 if (eap->do_ecmd_cmd != NULL)
4761 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004762}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004763
4764 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004765ends_excmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004766{
Bram Moolenaar2c330432020-04-13 14:41:35 +02004767 if (c == '#')
4768 // TODO: should check for preceding white space
4769 return in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770 return (c == NUL || c == '|' || c == '"' || c == '\n');
4771}
4772
4773#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
4774 || defined(PROTO)
4775/*
4776 * Return the next command, after the first '|' or '\n'.
4777 * Return NULL if not found.
4778 */
4779 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004780find_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004781{
4782 while (*p != '|' && *p != '\n')
4783 {
4784 if (*p == NUL)
4785 return NULL;
4786 ++p;
4787 }
4788 return (p + 1);
4789}
4790#endif
4791
4792/*
Bram Moolenaar2256c992016-11-15 21:17:07 +01004793 * Check if *p is a separator between Ex commands, skipping over white space.
4794 * Return NULL if it isn't, the following character if it is.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004795 */
4796 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004797check_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004798{
Bram Moolenaar2256c992016-11-15 21:17:07 +01004799 char_u *s = skipwhite(p);
4800
4801 if (*s == '|' || *s == '\n')
4802 return (s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004803 else
4804 return NULL;
4805}
4806
4807/*
4808 * - if there are more files to edit
4809 * - and this is the last window
4810 * - and forceit not used
4811 * - and not repeated twice on a row
4812 * return FAIL and give error message if 'message' TRUE
4813 * return OK otherwise
4814 */
4815 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004816check_more(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004817 int message, // when FALSE check only, no messages
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004818 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004819{
4820 int n = ARGCOUNT - curwin->w_arg_idx - 1;
4821
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00004822 if (!forceit && only_one_window()
4823 && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004824 {
4825 if (message)
4826 {
4827#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
4828 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
4829 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02004830 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004831
Bram Moolenaarda6e8912018-08-21 15:12:14 +02004832 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
4833 NGETTEXT("%d more file to edit. Quit anyway?",
4834 "%d more files to edit. Quit anyway?", n), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004835 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
4836 return OK;
4837 return FAIL;
4838 }
4839#endif
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01004840 semsg(NGETTEXT("E173: %d more file to edit",
4841 "E173: %d more files to edit", n), n);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004842 quitmore = 2; // next try to quit is allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004843 }
4844 return FAIL;
4845 }
4846 return OK;
4847}
4848
Bram Moolenaar071d4272004-06-13 20:20:40 +00004849/*
4850 * Function given to ExpandGeneric() to obtain the list of command names.
4851 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004852 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004853get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004854{
4855 if (idx >= (int)CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004856 return get_user_command_name(idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004857 return cmdnames[idx].cmd_name;
4858}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004859
Bram Moolenaar071d4272004-06-13 20:20:40 +00004860 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004861ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004862{
Bram Moolenaare6850792010-05-14 15:28:44 +02004863 if (*eap->arg == NUL)
4864 {
4865#ifdef FEAT_EVAL
4866 char_u *expr = vim_strsave((char_u *)"g:colors_name");
4867 char_u *p = NULL;
4868
4869 if (expr != NULL)
4870 {
4871 ++emsg_off;
4872 p = eval_to_string(expr, NULL, FALSE);
4873 --emsg_off;
4874 vim_free(expr);
4875 }
4876 if (p != NULL)
4877 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004878 msg((char *)p);
Bram Moolenaare6850792010-05-14 15:28:44 +02004879 vim_free(p);
4880 }
4881 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01004882 msg("default");
Bram Moolenaare6850792010-05-14 15:28:44 +02004883#else
Bram Moolenaar32526b32019-01-19 17:43:09 +01004884 msg(_("unknown"));
Bram Moolenaare6850792010-05-14 15:28:44 +02004885#endif
4886 }
4887 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004888 semsg(_("E185: Cannot find color scheme '%s'"), eap->arg);
Bram Moolenaarf58d81a2019-01-28 20:19:05 +01004889
4890#ifdef FEAT_VTP
4891 else if (has_vtp_working())
4892 {
4893 // background color change requires clear + redraw
4894 update_screen(CLEAR);
4895 redrawcmd();
4896 }
4897#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004898}
4899
4900 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004901ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004902{
4903 if (*eap->arg == NUL && eap->cmd[2] == '!')
Bram Moolenaar32526b32019-01-19 17:43:09 +01004904 msg(_("Greetings, Vim user!"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004905 do_highlight(eap->arg, eap->forceit, FALSE);
4906}
4907
4908
4909/*
4910 * Call this function if we thought we were going to exit, but we won't
4911 * (because of an error). May need to restore the terminal mode.
4912 */
4913 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004914not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004915{
4916 exiting = FALSE;
4917 settmode(TMODE_RAW);
4918}
4919
Bram Moolenaar12a96de2018-03-11 14:44:18 +01004920 static int
4921before_quit_autocmds(win_T *wp, int quit_all, int forceit)
4922{
4923 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, wp->w_buffer);
4924
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02004925 // Bail out when autocommands closed the window.
4926 // Refuse to quit when the buffer in the last window is being closed (can
4927 // only happen in autocommands).
Bram Moolenaar12a96de2018-03-11 14:44:18 +01004928 if (!win_valid(wp)
4929 || curbuf_locked()
4930 || (wp->w_buffer->b_nwindows == 1 && wp->w_buffer->b_locked > 0))
4931 return TRUE;
4932
4933 if (quit_all || (check_more(FALSE, forceit) == OK && only_one_window()))
4934 {
4935 apply_autocmds(EVENT_EXITPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02004936 // Refuse to quit when locked or when the window was closed or the
4937 // buffer in the last window is being closed (can only happen in
4938 // autocommands).
4939 if (!win_valid(wp) || curbuf_locked()
Bram Moolenaar12a96de2018-03-11 14:44:18 +01004940 || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
4941 return TRUE;
4942 }
4943
4944 return FALSE;
4945}
4946
Bram Moolenaar071d4272004-06-13 20:20:40 +00004947/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01004948 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar12a96de2018-03-11 14:44:18 +01004949 * ":{nr}quit": quit window {nr}
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02004950 * Also used when closing a terminal window that's the last one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004951 */
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02004952 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004953ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004955 win_T *wp;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004956
Bram Moolenaar071d4272004-06-13 20:20:40 +00004957#ifdef FEAT_CMDWIN
4958 if (cmdwin_type != 0)
4959 {
4960 cmdwin_result = Ctrl_C;
4961 return;
4962 }
4963#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004964 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004965 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004966 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004967 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004968 return;
4969 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004970 if (eap->addr_count > 0)
4971 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01004972 int wnr = eap->line2;
4973
4974 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
4975 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004976 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004977 }
4978 else
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004979 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01004980
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004981 // Refuse to quit when locked.
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02004982 if (curbuf_locked())
4983 return;
Bram Moolenaar12a96de2018-03-11 14:44:18 +01004984
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004985 // Trigger QuitPre and maybe ExitPre
Bram Moolenaar12a96de2018-03-11 14:44:18 +01004986 if (before_quit_autocmds(wp, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004987 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004988
4989#ifdef FEAT_NETBEANS_INTG
4990 netbeansForcedQuit = eap->forceit;
4991#endif
4992
4993 /*
4994 * If there are more files or windows we won't exit.
4995 */
4996 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
4997 exiting = TRUE;
Bram Moolenaarff930ca2017-10-19 17:12:10 +02004998 if ((!buf_hide(wp->w_buffer)
4999 && check_changed(wp->w_buffer, (p_awa ? CCGD_AW : 0)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01005000 | (eap->forceit ? CCGD_FORCEIT : 0)
5001 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005002 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01005003 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005004 {
5005 not_exiting();
5006 }
5007 else
5008 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005009 // quit last window
5010 // Note: only_one_window() returns true, even so a help window is
5011 // still open. In that case only quit, if no address has been
5012 // specified. Example:
5013 // :h|wincmd w|1q - don't quit
5014 // :h|wincmd w|q - quit
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01005015 if (only_one_window() && (ONE_WINDOW || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005016 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02005017 not_exiting();
Bram Moolenaar4033c552017-09-16 20:54:51 +02005018#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005020#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005021 // close window; may free buffer
Bram Moolenaareb44a682017-08-03 22:44:55 +02005022 win_close(wp, !buf_hide(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023 }
5024}
5025
5026/*
5027 * ":cquit".
5028 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005029 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005030ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005031{
Bram Moolenaar1860bde2020-01-06 21:47:21 +01005032 // this does not always pass on the exit code to the Manx compiler. why?
5033 getout(eap->addr_count > 0 ? (int)eap->line2 : EXIT_FAILURE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005034}
5035
5036/*
5037 * ":qall": try to quit all windows
5038 */
5039 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005040ex_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005041{
5042# ifdef FEAT_CMDWIN
5043 if (cmdwin_type != 0)
5044 {
5045 if (eap->forceit)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005046 cmdwin_result = K_XF1; // ex_window() takes care of this
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047 else
5048 cmdwin_result = K_XF2;
5049 return;
5050 }
5051# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005052
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005053 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005054 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005055 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005056 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005057 return;
5058 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005059
5060 if (before_quit_autocmds(curwin, TRUE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005061 return;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005062
Bram Moolenaar071d4272004-06-13 20:20:40 +00005063 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01005064 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005065 getout(0);
5066 not_exiting();
5067}
5068
Bram Moolenaar071d4272004-06-13 20:20:40 +00005069/*
5070 * ":close": close current window, unless it is the last one
5071 */
5072 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005073ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005074{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005075 win_T *win;
5076 int winnr = 0;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005077#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00005078 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02005079 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005080 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005081#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005082 if (!text_locked() && !curbuf_locked())
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005083 {
5084 if (eap->addr_count == 0)
5085 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar6d41c782018-06-06 09:11:12 +02005086 else
5087 {
Bram Moolenaar29323592016-07-24 22:04:11 +02005088 FOR_ALL_WINDOWS(win)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005089 {
5090 winnr++;
5091 if (winnr == eap->line2)
5092 break;
5093 }
5094 if (win == NULL)
5095 win = lastwin;
5096 ex_win_close(eap->forceit, win, NULL);
5097 }
5098 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005099}
5100
Bram Moolenaar4033c552017-09-16 20:54:51 +02005101#ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005102/*
5103 * ":pclose": Close any preview window.
5104 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005105 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005106ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005107{
5108 win_T *win;
5109
Bram Moolenaar79648732019-07-18 21:43:07 +02005110 // First close any normal window.
Bram Moolenaar29323592016-07-24 22:04:11 +02005111 FOR_ALL_WINDOWS(win)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005112 if (win->w_p_pvw)
5113 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005114 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar79648732019-07-18 21:43:07 +02005115 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005116 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005117# ifdef FEAT_PROP_POPUP
Bram Moolenaar79648732019-07-18 21:43:07 +02005118 // Also when 'previewpopup' is empty, it might have been cleared.
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02005119 popup_close_preview();
Bram Moolenaar79648732019-07-18 21:43:07 +02005120# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005121}
Bram Moolenaar4033c552017-09-16 20:54:51 +02005122#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005123
Bram Moolenaarf740b292006-02-16 22:11:02 +00005124/*
5125 * Close window "win" and take care of handling closing the last window for a
5126 * modified buffer.
5127 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005128 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005129ex_win_close(
5130 int forceit,
5131 win_T *win,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005132 tabpage_T *tp) // NULL or the tab page "win" is in
Bram Moolenaar071d4272004-06-13 20:20:40 +00005133{
5134 int need_hide;
5135 buf_T *buf = win->w_buffer;
5136
5137 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02005138 if (need_hide && !buf_hide(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005139 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02005140#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005141 if ((p_confirm || cmdmod.confirm) && p_write)
5142 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005143 bufref_T bufref;
5144
5145 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005146 dialog_changed(buf, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005147 if (bufref_valid(&bufref) && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005148 return;
5149 need_hide = FALSE;
5150 }
5151 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005152#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02005154 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005155 return;
5156 }
5157 }
5158
Bram Moolenaar4033c552017-09-16 20:54:51 +02005159#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005160 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005161#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005162
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005163 // free buffer when not hiding it or when it's a scratch buffer
Bram Moolenaarf740b292006-02-16 22:11:02 +00005164 if (tp == NULL)
Bram Moolenaareb44a682017-08-03 22:44:55 +02005165 win_close(win, !need_hide && !buf_hide(buf));
Bram Moolenaarf740b292006-02-16 22:11:02 +00005166 else
Bram Moolenaareb44a682017-08-03 22:44:55 +02005167 win_close_othertab(win, !need_hide && !buf_hide(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005168}
5169
Bram Moolenaar071d4272004-06-13 20:20:40 +00005170/*
Bram Moolenaardea25702017-01-29 15:18:10 +01005171 * Handle the argument for a tabpage related ex command.
5172 * Returns a tabpage number.
5173 * When an error is encountered then eap->errmsg is set.
5174 */
5175 static int
5176get_tabpage_arg(exarg_T *eap)
5177{
5178 int tab_number;
5179 int unaccept_arg0 = (eap->cmdidx == CMD_tabmove) ? 0 : 1;
5180
5181 if (eap->arg && *eap->arg != NUL)
5182 {
5183 char_u *p = eap->arg;
5184 char_u *p_save;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005185 int relative = 0; // argument +N/-N means: go to N places to the
5186 // right/left relative to the current position.
Bram Moolenaardea25702017-01-29 15:18:10 +01005187
5188 if (*p == '-')
5189 {
5190 relative = -1;
5191 p++;
5192 }
5193 else if (*p == '+')
5194 {
5195 relative = 1;
5196 p++;
5197 }
5198
5199 p_save = p;
5200 tab_number = getdigits(&p);
5201
5202 if (relative == 0)
5203 {
5204 if (STRCMP(p, "$") == 0)
5205 tab_number = LAST_TAB_NR;
5206 else if (p == p_save || *p_save == '-' || *p != NUL
5207 || tab_number > LAST_TAB_NR)
5208 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005209 // No numbers as argument.
Bram Moolenaardea25702017-01-29 15:18:10 +01005210 eap->errmsg = e_invarg;
5211 goto theend;
5212 }
5213 }
5214 else
5215 {
5216 if (*p_save == NUL)
5217 tab_number = 1;
5218 else if (p == p_save || *p_save == '-' || *p != NUL
5219 || tab_number == 0)
5220 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005221 // No numbers as argument.
Bram Moolenaardea25702017-01-29 15:18:10 +01005222 eap->errmsg = e_invarg;
5223 goto theend;
5224 }
5225 tab_number = tab_number * relative + tabpage_index(curtab);
5226 if (!unaccept_arg0 && relative == -1)
5227 --tab_number;
5228 }
5229 if (tab_number < unaccept_arg0 || tab_number > LAST_TAB_NR)
5230 eap->errmsg = e_invarg;
5231 }
5232 else if (eap->addr_count > 0)
5233 {
5234 if (unaccept_arg0 && eap->line2 == 0)
Bram Moolenaarc6251552017-01-29 21:42:20 +01005235 {
Bram Moolenaardea25702017-01-29 15:18:10 +01005236 eap->errmsg = e_invrange;
Bram Moolenaarc6251552017-01-29 21:42:20 +01005237 tab_number = 0;
5238 }
Bram Moolenaardea25702017-01-29 15:18:10 +01005239 else
5240 {
5241 tab_number = eap->line2;
Bram Moolenaar82a12462019-01-22 22:41:42 +01005242 if (!unaccept_arg0 && *skipwhite(*eap->cmdlinep) == '-')
Bram Moolenaardea25702017-01-29 15:18:10 +01005243 {
5244 --tab_number;
5245 if (tab_number < unaccept_arg0)
5246 eap->errmsg = e_invarg;
5247 }
5248 }
5249 }
5250 else
5251 {
5252 switch (eap->cmdidx)
5253 {
5254 case CMD_tabnext:
5255 tab_number = tabpage_index(curtab) + 1;
5256 if (tab_number > LAST_TAB_NR)
5257 tab_number = 1;
5258 break;
5259 case CMD_tabmove:
5260 tab_number = LAST_TAB_NR;
5261 break;
5262 default:
5263 tab_number = tabpage_index(curtab);
5264 }
5265 }
5266
5267theend:
5268 return tab_number;
5269}
5270
5271/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00005272 * ":tabclose": close current tab page, unless it is the last one.
5273 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005274 */
5275 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005276ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005277{
Bram Moolenaarf740b292006-02-16 22:11:02 +00005278 tabpage_T *tp;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005279 int tab_number;
Bram Moolenaarf740b292006-02-16 22:11:02 +00005280
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005281# ifdef FEAT_CMDWIN
5282 if (cmdwin_type != 0)
5283 cmdwin_result = K_IGNORE;
5284 else
5285# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005286 if (first_tabpage->tp_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005287 emsg(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00005288 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005289 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005290 tab_number = get_tabpage_arg(eap);
5291 if (eap->errmsg == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005292 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005293 tp = find_tabpage(tab_number);
Bram Moolenaarf740b292006-02-16 22:11:02 +00005294 if (tp == NULL)
5295 {
5296 beep_flush();
5297 return;
5298 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005299 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005300 {
5301 tabpage_close_other(tp, eap->forceit);
5302 return;
5303 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005304 else if (!text_locked() && !curbuf_locked())
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005305 tabpage_close(eap->forceit);
5306 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005307 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005308}
5309
5310/*
5311 * ":tabonly": close all tab pages except the current one
5312 */
5313 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005314ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005315{
5316 tabpage_T *tp;
5317 int done;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005318 int tab_number;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005319
5320# ifdef FEAT_CMDWIN
5321 if (cmdwin_type != 0)
5322 cmdwin_result = K_IGNORE;
5323 else
5324# endif
5325 if (first_tabpage->tp_next == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005326 msg(_("Already only one tab page"));
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005327 else
5328 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005329 tab_number = get_tabpage_arg(eap);
5330 if (eap->errmsg == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005331 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005332 goto_tabpage(tab_number);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005333 // Repeat this up to a 1000 times, because autocommands may
5334 // mess up the lists.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005335 for (done = 0; done < 1000; ++done)
5336 {
5337 FOR_ALL_TABPAGES(tp)
5338 if (tp->tp_topframe != topframe)
5339 {
5340 tabpage_close_other(tp, eap->forceit);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005341 // if we failed to close it quit
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005342 if (valid_tabpage(tp))
5343 done = 1000;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005344 // start over, "tp" is now invalid
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005345 break;
5346 }
5347 if (first_tabpage->tp_next == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005348 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005349 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005350 }
5351 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005352}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005353
5354/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00005355 * Close the current tab page.
5356 */
5357 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005358tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005359{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005360 // First close all the windows but the current one. If that worked then
5361 // close the last window in this tab, that will close it.
Bram Moolenaar459ca562016-11-10 18:16:33 +01005362 if (!ONE_WINDOW)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005363 close_others(TRUE, forceit);
Bram Moolenaar459ca562016-11-10 18:16:33 +01005364 if (ONE_WINDOW)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005365 ex_win_close(forceit, curwin, NULL);
5366# ifdef FEAT_GUI
5367 need_mouse_correct = TRUE;
5368# endif
5369}
5370
5371/*
5372 * Close tab page "tp", which is not the current tab page.
5373 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005374 * Also takes care of the tab pages line disappearing when closing the
5375 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00005376 */
5377 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005378tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005379{
5380 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005381 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005382 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00005383
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005384 // Limit to 1000 windows, autocommands may add a window while we close
5385 // one. OK, so I'm paranoid...
Bram Moolenaarf740b292006-02-16 22:11:02 +00005386 while (++done < 1000)
5387 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005388 wp = tp->tp_firstwin;
5389 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00005390
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005391 // Autocommands may delete the tab page under our fingers and we may
5392 // fail to close a window with a modified buffer.
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005393 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005394 break;
5395 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005396
Bram Moolenaar29323592016-07-24 22:04:11 +02005397 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar29323592016-07-24 22:04:11 +02005398
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005399 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005400 if (h != tabline_height())
5401 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00005402}
5403
5404/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005405 * ":only".
5406 */
5407 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005408ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005409{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005410 win_T *wp;
5411 int wnr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005412# ifdef FEAT_GUI
5413 need_mouse_correct = TRUE;
5414# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005415 if (eap->addr_count > 0)
5416 {
5417 wnr = eap->line2;
5418 for (wp = firstwin; --wnr > 0; )
5419 {
5420 if (wp->w_next == NULL)
5421 break;
5422 else
5423 wp = wp->w_next;
5424 }
5425 win_goto(wp);
5426 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005427 close_others(TRUE, eap->forceit);
5428}
5429
Bram Moolenaar071d4272004-06-13 20:20:40 +00005430 static void
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01005431ex_hide(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005432{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005433 // ":hide" or ":hide | cmd": hide current window
Bram Moolenaar2256c992016-11-15 21:17:07 +01005434 if (!eap->skip)
5435 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02005436#ifdef FEAT_GUI
Bram Moolenaar2256c992016-11-15 21:17:07 +01005437 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005438#endif
Bram Moolenaar2256c992016-11-15 21:17:07 +01005439 if (eap->addr_count == 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005440 win_close(curwin, FALSE); // don't free buffer
Bram Moolenaar2256c992016-11-15 21:17:07 +01005441 else
5442 {
5443 int winnr = 0;
5444 win_T *win;
Bram Moolenaarf240e182014-11-27 18:33:02 +01005445
Bram Moolenaar2256c992016-11-15 21:17:07 +01005446 FOR_ALL_WINDOWS(win)
5447 {
5448 winnr++;
5449 if (winnr == eap->line2)
5450 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005451 }
Bram Moolenaar2256c992016-11-15 21:17:07 +01005452 if (win == NULL)
5453 win = lastwin;
5454 win_close(win, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005455 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005456 }
5457}
5458
5459/*
5460 * ":stop" and ":suspend": Suspend Vim.
5461 */
5462 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005463ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005464{
5465 /*
5466 * Disallow suspending for "rvim".
5467 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005468 if (!check_restricted())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005469 {
5470 if (!eap->forceit)
5471 autowrite_all();
5472 windgoto((int)Rows - 1, 0);
5473 out_char('\n');
5474 out_flush();
5475 stoptermcap();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005476 out_flush(); // needed for SUN to restore xterm buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00005477#ifdef FEAT_TITLE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005478 mch_restore_title(SAVE_RESTORE_BOTH); // restore window titles
Bram Moolenaar071d4272004-06-13 20:20:40 +00005479#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005480 ui_suspend(); // call machine specific function
Bram Moolenaar071d4272004-06-13 20:20:40 +00005481#ifdef FEAT_TITLE
5482 maketitle();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005483 resettitle(); // force updating the title
Bram Moolenaar071d4272004-06-13 20:20:40 +00005484#endif
5485 starttermcap();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005486 scroll_start(); // scroll screen before redrawing
Bram Moolenaar071d4272004-06-13 20:20:40 +00005487 redraw_later_clear();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005488 shell_resized(); // may have resized window
Bram Moolenaar071d4272004-06-13 20:20:40 +00005489 }
5490}
5491
5492/*
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005493 * ":exit", ":xit" and ":wq": Write file and quite the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005494 */
5495 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005496ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005497{
5498#ifdef FEAT_CMDWIN
5499 if (cmdwin_type != 0)
5500 {
5501 cmdwin_result = Ctrl_C;
5502 return;
5503 }
5504#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005505 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005506 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005507 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005508 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005509 return;
5510 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005511
5512 if (before_quit_autocmds(curwin, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005513 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005514
5515 /*
5516 * if more files or windows we won't exit
5517 */
5518 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
5519 exiting = TRUE;
5520 if ( ((eap->cmdidx == CMD_wq
5521 || curbufIsChanged())
5522 && do_write(eap) == FAIL)
5523 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01005524 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005525 {
5526 not_exiting();
5527 }
5528 else
5529 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005530 if (only_one_window()) // quit last window, exit Vim
Bram Moolenaar071d4272004-06-13 20:20:40 +00005531 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02005532 not_exiting();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005533# ifdef FEAT_GUI
5534 need_mouse_correct = TRUE;
5535# endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005536 // Quit current window, may free the buffer.
Bram Moolenaareb44a682017-08-03 22:44:55 +02005537 win_close(curwin, !buf_hide(curwin->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005538 }
5539}
5540
5541/*
5542 * ":print", ":list", ":number".
5543 */
5544 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005545ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005546{
Bram Moolenaardf177f62005-02-22 08:39:57 +00005547 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005548 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +00005549 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005550 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00005551 for ( ;!got_int; ui_breakcheck())
5552 {
5553 print_line(eap->line1,
5554 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
5555 || (eap->flags & EXFLAG_NR)),
5556 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
5557 if (++eap->line1 > eap->line2)
5558 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005559 out_flush(); // show one line at a time
Bram Moolenaardf177f62005-02-22 08:39:57 +00005560 }
5561 setpcmark();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005562 // put cursor at last line
Bram Moolenaardf177f62005-02-22 08:39:57 +00005563 curwin->w_cursor.lnum = eap->line2;
5564 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005565 }
5566
Bram Moolenaar071d4272004-06-13 20:20:40 +00005567 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005568}
5569
5570#ifdef FEAT_BYTEOFF
5571 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005572ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005573{
5574 goto_byte(eap->line2);
5575}
5576#endif
5577
5578/*
5579 * ":shell".
5580 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005581 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005582ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005583{
5584 do_shell(NULL, 0);
5585}
5586
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005587#if defined(HAVE_DROP_FILE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005588
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005589static int drop_busy = FALSE;
5590static int drop_filec;
5591static char_u **drop_filev = NULL;
5592static int drop_split;
5593static void (*drop_callback)(void *);
5594static void *drop_cookie;
5595
5596 static void
5597handle_drop_internal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005598{
5599 exarg_T ea;
5600 int save_msg_scroll = msg_scroll;
5601
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005602 // Setting the argument list may cause screen updates and being called
5603 // recursively. Avoid that by setting drop_busy.
5604 drop_busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005605
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005606 // Check whether the current buffer is changed. If so, we will need
5607 // to split the current window or data could be lost.
5608 // We don't need to check if the 'hidden' option is set, as in this
5609 // case the buffer won't be lost.
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005610 if (!buf_hide(curbuf) && !drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005611 {
5612 ++emsg_off;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005613 drop_split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005614 --emsg_off;
5615 }
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005616 if (drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005617 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005618 if (win_split(0, 0) == FAIL)
5619 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02005620 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005621
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005622 // When splitting the window, create a new alist. Otherwise the
5623 // existing one is overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005624 alist_unlink(curwin->w_alist);
5625 alist_new();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005626 }
5627
5628 /*
5629 * Set up the new argument list.
5630 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005631 alist_set(ALIST(curwin), drop_filec, drop_filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005632
5633 /*
5634 * Move to the first file.
5635 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005636 // Fake up a minimal "next" command for do_argfile()
Bram Moolenaara80faa82020-04-12 19:37:17 +02005637 CLEAR_FIELD(ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005638 ea.cmd = (char_u *)"next";
5639 do_argfile(&ea, 0);
5640
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005641 // do_ecmd() may set need_start_insertmode, but since we never left Insert
5642 // mode that is not needed here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005643 need_start_insertmode = FALSE;
5644
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005645 // Restore msg_scroll, otherwise a following command may cause scrolling
5646 // unexpectedly. The screen will be redrawn by the caller, thus
5647 // msg_scroll being set by displaying a message is irrelevant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005648 msg_scroll = save_msg_scroll;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005649
5650 if (drop_callback != NULL)
5651 drop_callback(drop_cookie);
5652
5653 drop_filev = NULL;
5654 drop_busy = FALSE;
5655}
5656
5657/*
5658 * Handle a file drop. The code is here because a drop is *nearly* like an
5659 * :args command, but not quite (we have a list of exact filenames, so we
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01005660 * don't want to (a) parse a command line, or (b) expand wildcards). So the
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005661 * code is very similar to :args and hence needs access to a lot of the static
5662 * functions in this file.
5663 *
5664 * The "filev" list must have been allocated using alloc(), as should each item
5665 * in the list. This function takes over responsibility for freeing the "filev"
5666 * list.
5667 */
5668 void
5669handle_drop(
5670 int filec, // the number of files dropped
5671 char_u **filev, // the list of files dropped
5672 int split, // force splitting the window
5673 void (*callback)(void *), // to be called after setting the argument
5674 // list
5675 void *cookie) // argument for "callback" (allocated)
5676{
5677 // Cannot handle recursive drops, finish the pending one.
5678 if (drop_busy)
5679 {
5680 FreeWild(filec, filev);
5681 vim_free(cookie);
5682 return;
5683 }
5684
5685 // When calling handle_drop() more than once in a row we only use the last
5686 // one.
5687 if (drop_filev != NULL)
5688 {
5689 FreeWild(drop_filec, drop_filev);
5690 vim_free(drop_cookie);
5691 }
5692
5693 drop_filec = filec;
5694 drop_filev = filev;
5695 drop_split = split;
5696 drop_callback = callback;
5697 drop_cookie = cookie;
5698
5699 // Postpone this when:
5700 // - editing the command line
5701 // - not possible to change the current buffer
5702 // - updating the screen
5703 // As it may change buffers and window structures that are in use and cause
5704 // freed memory to be used.
5705 if (text_locked() || curbuf_locked() || updating_screen)
5706 return;
5707
5708 handle_drop_internal();
5709}
5710
5711/*
5712 * To be called when text is unlocked, curbuf is unlocked or updating_screen is
5713 * reset: Handle a postponed drop.
5714 */
5715 void
5716handle_any_postponed_drop(void)
5717{
5718 if (!drop_busy && drop_filev != NULL
5719 && !text_locked() && !curbuf_locked() && !updating_screen)
5720 handle_drop_internal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005721}
5722#endif
5723
Bram Moolenaar071d4272004-06-13 20:20:40 +00005724/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005725 * ":preserve".
5726 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005727 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005728ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005729{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005730 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005731 ml_preserve(curbuf, TRUE);
5732}
5733
5734/*
5735 * ":recover".
5736 */
5737 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005738ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005739{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005740 // Set recoverymode right away to avoid the ATTENTION prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005741 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01005742 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
5743 | CCGD_MULTWIN
5744 | (eap->forceit ? CCGD_FORCEIT : 0)
5745 | CCGD_EXCMD)
5746
Bram Moolenaar071d4272004-06-13 20:20:40 +00005747 && (*eap->arg == NUL
5748 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
Bram Moolenaar99499b12019-05-23 21:35:48 +02005749 ml_recover(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005750 recoverymode = FALSE;
5751}
5752
5753/*
5754 * Command modifier used in a wrong way.
5755 */
5756 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005757ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005758{
5759 eap->errmsg = e_invcmd;
5760}
5761
Bram Moolenaar071d4272004-06-13 20:20:40 +00005762/*
5763 * :sview [+command] file split window with new file, read-only
5764 * :split [[+command] file] split window with current or new file
5765 * :vsplit [[+command] file] split window vertically with current or new file
5766 * :new [[+command] file] split window with no or new file
5767 * :vnew [[+command] file] split vertically window with no or new file
5768 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005769 *
5770 * :tabedit open new Tab page with empty window
5771 * :tabedit [+command] file open new Tab page and edit "file"
5772 * :tabnew [[+command] file] just like :tabedit
5773 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005774 */
5775 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005776ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005777{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005778 win_T *old_curwin = curwin;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005779#if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005780 char_u *fname = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005781#endif
5782#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005783 int browse_flag = cmdmod.browse;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005784#endif
Bram Moolenaar39902a02018-06-21 22:10:08 +02005785 int use_tab = eap->cmdidx == CMD_tabedit
5786 || eap->cmdidx == CMD_tabfind
5787 || eap->cmdidx == CMD_tabnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005788
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005789 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005790 return;
5791
Bram Moolenaar4033c552017-09-16 20:54:51 +02005792#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005793 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005794#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005795
Bram Moolenaar4033c552017-09-16 20:54:51 +02005796#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005797 // A ":split" in the quickfix window works like ":new". Don't want two
5798 // quickfix windows. But it's OK when doing ":tab split".
Bram Moolenaar05bb9532008-07-04 09:44:11 +00005799 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005800 {
5801 if (eap->cmdidx == CMD_split)
5802 eap->cmdidx = CMD_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005803 if (eap->cmdidx == CMD_vsplit)
5804 eap->cmdidx = CMD_vnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005805 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02005806#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005807
Bram Moolenaar4033c552017-09-16 20:54:51 +02005808#ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005809 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005810 {
5811 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
5812 FNAME_MESS, TRUE, curbuf->b_ffname);
5813 if (fname == NULL)
5814 goto theend;
5815 eap->arg = fname;
5816 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005817# ifdef FEAT_BROWSE
Bram Moolenaar4033c552017-09-16 20:54:51 +02005818 else
5819# endif
5820#endif
5821#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005822 if (cmdmod.browse
Bram Moolenaar071d4272004-06-13 20:20:40 +00005823 && eap->cmdidx != CMD_vnew
Bram Moolenaar071d4272004-06-13 20:20:40 +00005824 && eap->cmdidx != CMD_new)
5825 {
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005826 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005827# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005828 !gui.in_use &&
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005829# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005830 au_has_group((char_u *)"FileExplorer"))
5831 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005832 // No browsing supported but we do have the file explorer:
5833 // Edit the directory.
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005834 if (*eap->arg == NUL || !mch_isdir(eap->arg))
5835 eap->arg = (char_u *)".";
5836 }
5837 else
5838 {
Bram Moolenaar39902a02018-06-21 22:10:08 +02005839 fname = do_browse(0, (char_u *)(use_tab
5840 ? _("Edit File in new tab page")
5841 : _("Edit File in new window")),
Bram Moolenaar071d4272004-06-13 20:20:40 +00005842 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005843 if (fname == NULL)
5844 goto theend;
5845 eap->arg = fname;
5846 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005847 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005848 cmdmod.browse = FALSE; // Don't browse again in do_ecmd().
Bram Moolenaar4033c552017-09-16 20:54:51 +02005849#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005850
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005851 /*
5852 * Either open new tab page or split the window.
5853 */
Bram Moolenaar39902a02018-06-21 22:10:08 +02005854 if (use_tab)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005855 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005856 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
5857 : eap->addr_count == 0 ? 0
5858 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005859 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00005860 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005861
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005862 // set the alternate buffer for the window we came from
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005863 if (curwin != old_curwin
5864 && win_valid(old_curwin)
5865 && old_curwin->w_buffer != curbuf
5866 && !cmdmod.keepalt)
5867 old_curwin->w_alt_fnum = curbuf->b_fnum;
5868 }
5869 }
5870 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005871 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
5872 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005873 // Reset 'scrollbind' when editing another file, but keep it when
5874 // doing ":split" without arguments.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005875 if (*eap->arg != NUL
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01005876# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005877 || cmdmod.browse
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01005878# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005879 )
Bram Moolenaar3368ea22010-09-21 16:56:35 +02005880 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005881 else
5882 do_check_scrollbind(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005883 do_exedit(eap, old_curwin);
5884 }
5885
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005886# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005887 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005888# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005889
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005890# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005891theend:
5892 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005893# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005894}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005895
5896/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00005897 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005898 */
5899 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005900tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00005901{
5902 exarg_T ea;
5903
Bram Moolenaara80faa82020-04-12 19:37:17 +02005904 CLEAR_FIELD(ea);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00005905 ea.cmdidx = CMD_tabnew;
5906 ea.cmd = (char_u *)"tabn";
5907 ea.arg = (char_u *)"";
5908 ex_splitview(&ea);
5909}
5910
5911/*
5912 * :tabnext command
5913 */
5914 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005915ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005916{
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005917 int tab_number;
5918
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02005919 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005920 return;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005921 switch (eap->cmdidx)
5922 {
5923 case CMD_tabfirst:
5924 case CMD_tabrewind:
5925 goto_tabpage(1);
5926 break;
5927 case CMD_tablast:
5928 goto_tabpage(9999);
5929 break;
5930 case CMD_tabprevious:
5931 case CMD_tabNext:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005932 if (eap->arg && *eap->arg != NUL)
5933 {
5934 char_u *p = eap->arg;
5935 char_u *p_save = p;
5936
5937 tab_number = getdigits(&p);
5938 if (p == p_save || *p_save == '-' || *p != NUL
5939 || tab_number == 0)
5940 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005941 // No numbers as argument.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005942 eap->errmsg = e_invarg;
5943 return;
5944 }
5945 }
5946 else
5947 {
5948 if (eap->addr_count == 0)
5949 tab_number = 1;
5950 else
5951 {
5952 tab_number = eap->line2;
5953 if (tab_number < 1)
5954 {
5955 eap->errmsg = e_invrange;
5956 return;
5957 }
5958 }
5959 }
5960 goto_tabpage(-tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005961 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005962 default: // CMD_tabnext
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005963 tab_number = get_tabpage_arg(eap);
5964 if (eap->errmsg == NULL)
5965 goto_tabpage(tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005966 break;
5967 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00005968}
5969
5970/*
5971 * :tabmove command
5972 */
5973 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005974ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00005975{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02005976 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02005977
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005978 tab_number = get_tabpage_arg(eap);
5979 if (eap->errmsg == NULL)
5980 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005981}
5982
5983/*
5984 * :tabs command: List tabs and their contents.
5985 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005986 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005987ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005988{
5989 tabpage_T *tp;
5990 win_T *wp;
5991 int tabcount = 1;
5992
5993 msg_start();
5994 msg_scroll = TRUE;
5995 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
5996 {
5997 msg_putchar('\n');
5998 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
Bram Moolenaar8820b482017-03-16 17:23:31 +01005999 msg_outtrans_attr(IObuff, HL_ATTR(HLF_T));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006000 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006001 ui_breakcheck();
6002
Bram Moolenaar030f0df2006-02-21 22:02:53 +00006003 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006004 wp = firstwin;
6005 else
6006 wp = tp->tp_firstwin;
6007 for ( ; wp != NULL && !got_int; wp = wp->w_next)
6008 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006009 msg_putchar('\n');
6010 msg_putchar(wp == curwin ? '>' : ' ');
6011 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006012 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
6013 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006014 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02006015 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006016 else
6017 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
6018 IObuff, IOSIZE, TRUE);
6019 msg_outtrans(IObuff);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006020 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006021 ui_breakcheck();
6022 }
6023 }
6024}
6025
Bram Moolenaar071d4272004-06-13 20:20:40 +00006026/*
6027 * ":mode": Set screen mode.
6028 * If no argument given, just get the screen size and redraw.
6029 */
6030 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006031ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006032{
6033 if (*eap->arg == NUL)
6034 shell_resized();
6035 else
Bram Moolenaar3c71aec2020-01-17 19:32:20 +01006036 emsg(_(e_screenmode));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006037}
6038
Bram Moolenaar071d4272004-06-13 20:20:40 +00006039/*
6040 * ":resize".
6041 * set, increment or decrement current window height
6042 */
6043 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006044ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006045{
6046 int n;
6047 win_T *wp = curwin;
6048
6049 if (eap->addr_count > 0)
6050 {
6051 n = eap->line2;
6052 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
6053 ;
6054 }
6055
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006056# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006057 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006058# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006059 n = atol((char *)eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006060 if (cmdmod.split & WSP_VERT)
6061 {
6062 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar02631462017-09-22 15:20:32 +02006063 n += curwin->w_width;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006064 else if (n == 0 && eap->arg[0] == NUL) // default is very wide
Bram Moolenaar071d4272004-06-13 20:20:40 +00006065 n = 9999;
6066 win_setwidth_win((int)n, wp);
6067 }
6068 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006069 {
6070 if (*eap->arg == '-' || *eap->arg == '+')
6071 n += curwin->w_height;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006072 else if (n == 0 && eap->arg[0] == NUL) // default is very high
Bram Moolenaar071d4272004-06-13 20:20:40 +00006073 n = 9999;
6074 win_setheight_win((int)n, wp);
6075 }
6076}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006077
6078/*
6079 * ":find [+command] <file>" command.
6080 */
6081 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006082ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006083{
6084#ifdef FEAT_SEARCHPATH
6085 char_u *fname;
6086 int count;
6087
6088 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
6089 TRUE, curbuf->b_ffname);
6090 if (eap->addr_count > 0)
6091 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006092 // Repeat finding the file "count" times. This matters when it
6093 // appears several times in the path.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006094 count = eap->line2;
6095 while (fname != NULL && --count > 0)
6096 {
6097 vim_free(fname);
6098 fname = find_file_in_path(NULL, 0, FNAME_MESS,
6099 FALSE, curbuf->b_ffname);
6100 }
6101 }
6102
6103 if (fname != NULL)
6104 {
6105 eap->arg = fname;
6106#endif
6107 do_exedit(eap, NULL);
6108#ifdef FEAT_SEARCHPATH
6109 vim_free(fname);
6110 }
6111#endif
6112}
6113
6114/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00006115 * ":open" simulation: for now just work like ":visual".
6116 */
6117 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006118ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006119{
6120 regmatch_T regmatch;
6121 char_u *p;
6122
6123 curwin->w_cursor.lnum = eap->line2;
6124 beginline(BL_SOL | BL_FIX);
6125 if (*eap->arg == '/')
6126 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006127 // ":open /pattern/": put cursor in column found with pattern
Bram Moolenaardf177f62005-02-22 08:39:57 +00006128 ++eap->arg;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006129 p = skip_regexp(eap->arg, '/', p_magic);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006130 *p = NUL;
6131 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
6132 if (regmatch.regprog != NULL)
6133 {
6134 regmatch.rm_ic = p_ic;
6135 p = ml_get_curline();
6136 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006137 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006138 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006139 emsg(_(e_nomatch));
Bram Moolenaar473de612013-06-08 18:19:48 +02006140 vim_regfree(regmatch.regprog);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006141 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006142 // Move to the NUL, ignore any other arguments.
Bram Moolenaardf177f62005-02-22 08:39:57 +00006143 eap->arg += STRLEN(eap->arg);
6144 }
6145 check_cursor();
6146
6147 eap->cmdidx = CMD_visual;
6148 do_exedit(eap, NULL);
6149}
6150
6151/*
6152 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006153 */
6154 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006155ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006156{
6157 do_exedit(eap, NULL);
6158}
6159
6160/*
Bram Moolenaar4b96df52020-01-26 22:00:26 +01006161 * ":edit <file>" command and alike.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006162 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006163 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006164do_exedit(
6165 exarg_T *eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006166 win_T *old_curwin) // curwin before doing a split or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006167{
6168 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006169 int need_hide;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006170 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006171
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01006172 if ((eap->cmdidx != CMD_pedit && ERROR_IF_POPUP_WINDOW)
6173 || ERROR_IF_TERM_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006174 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006175 /*
6176 * ":vi" command ends Ex mode.
6177 */
6178 if (exmode_active && (eap->cmdidx == CMD_visual
6179 || eap->cmdidx == CMD_view))
6180 {
6181 exmode_active = FALSE;
6182 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006183 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006184 // Special case: ":global/pat/visual\NLvi-commands"
Bram Moolenaardf177f62005-02-22 08:39:57 +00006185 if (global_busy)
6186 {
6187 int rd = RedrawingDisabled;
6188 int nwr = no_wait_return;
6189 int ms = msg_scroll;
6190#ifdef FEAT_GUI
6191 int he = hold_gui_events;
6192#endif
6193
6194 if (eap->nextcmd != NULL)
6195 {
6196 stuffReadbuff(eap->nextcmd);
6197 eap->nextcmd = NULL;
6198 }
6199
6200 if (exmode_was != EXMODE_VIM)
6201 settmode(TMODE_RAW);
6202 RedrawingDisabled = 0;
6203 no_wait_return = 0;
6204 need_wait_return = FALSE;
6205 msg_scroll = 0;
6206#ifdef FEAT_GUI
6207 hold_gui_events = 0;
6208#endif
6209 must_redraw = CLEAR;
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01006210 pending_exmode_active = TRUE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006211
6212 main_loop(FALSE, TRUE);
6213
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01006214 pending_exmode_active = FALSE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006215 RedrawingDisabled = rd;
6216 no_wait_return = nwr;
6217 msg_scroll = ms;
6218#ifdef FEAT_GUI
6219 hold_gui_events = he;
6220#endif
6221 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006222 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006223 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006224 }
6225
6226 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006227 || eap->cmdidx == CMD_tabnew
6228 || eap->cmdidx == CMD_tabedit
Bram Moolenaar4033c552017-09-16 20:54:51 +02006229 || eap->cmdidx == CMD_vnew) && *eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006230 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006231 // ":new" or ":tabnew" without argument: edit an new empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006232 setpcmark();
6233 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006234 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
6235 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006236 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006237 else if ((eap->cmdidx != CMD_split && eap->cmdidx != CMD_vsplit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006238 || *eap->arg != NUL
6239#ifdef FEAT_BROWSE
6240 || cmdmod.browse
6241#endif
6242 )
6243 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006244 // Can't edit another file when "curbuf_lock" is set. Only ":edit"
6245 // can bring us here, others are stopped earlier.
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006246 if (*eap->arg != NUL && curbuf_locked())
6247 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006248
Bram Moolenaar071d4272004-06-13 20:20:40 +00006249 n = readonlymode;
6250 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
6251 readonlymode = TRUE;
6252 else if (eap->cmdidx == CMD_enew)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006253 readonlymode = FALSE; // 'readonly' doesn't make sense in an
6254 // empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006255 setpcmark();
6256 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
6257 NULL, eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006258 // ":edit" goes to first line if Vi compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00006259 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
6260 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
6261 ? ECMD_ONE : eap->do_ecmd_lnum,
Bram Moolenaareb44a682017-08-03 22:44:55 +02006262 (buf_hide(curbuf) ? ECMD_HIDE : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006263 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006264 // after a split we can use an existing buffer
Bram Moolenaar7b449342014-03-25 13:03:48 +01006265 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006266 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006267 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006268 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006269 // Editing the file failed. If the window was split, close it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006270 if (old_curwin != NULL)
6271 {
6272 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02006273 if (!need_hide || buf_hide(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006274 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006275#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006276 cleanup_T cs;
6277
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006278 // Reset the error/interrupt/exception state here so that
6279 // aborting() returns FALSE when closing a window.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006280 enter_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02006281#endif
6282#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006283 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006284#endif
Bram Moolenaareb44a682017-08-03 22:44:55 +02006285 win_close(curwin, !need_hide && !buf_hide(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006286
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006287#if defined(FEAT_EVAL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006288 // Restore the error/interrupt/exception state if not
6289 // discarded by a new aborting error, interrupt, or
6290 // uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006291 leave_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02006292#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006293 }
6294 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006295 }
6296 else if (readonlymode && curbuf->b_nwindows == 1)
6297 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006298 // When editing an already visited buffer, 'readonly' won't be set
6299 // but the previous value is kept. With ":view" and ":sview" we
6300 // want the file to be readonly, except when another window is
6301 // editing the same buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006302 curbuf->b_p_ro = TRUE;
6303 }
6304 readonlymode = n;
6305 }
6306 else
6307 {
6308 if (eap->do_ecmd_cmd != NULL)
6309 do_cmdline_cmd(eap->do_ecmd_cmd);
6310#ifdef FEAT_TITLE
6311 n = curwin->w_arg_idx_invalid;
6312#endif
6313 check_arg_idx(curwin);
6314#ifdef FEAT_TITLE
6315 if (n != curwin->w_arg_idx_invalid)
6316 maketitle();
6317#endif
6318 }
6319
Bram Moolenaar071d4272004-06-13 20:20:40 +00006320 /*
6321 * if ":split file" worked, set alternate file name in old window to new
6322 * file
6323 */
6324 if (old_curwin != NULL
6325 && *eap->arg != NUL
6326 && curwin != old_curwin
6327 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00006328 && old_curwin->w_buffer != curbuf
6329 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006330 old_curwin->w_alt_fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006331
6332 ex_no_reprint = TRUE;
6333}
6334
6335#ifndef FEAT_GUI
6336/*
6337 * ":gui" and ":gvim" when there is no GUI.
6338 */
6339 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006340ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006341{
6342 eap->errmsg = e_nogvim;
6343}
6344#endif
6345
Bram Moolenaar4f974752019-02-17 17:44:42 +01006346#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006347 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006348ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006349{
6350 gui_make_tearoff(eap->arg);
6351}
6352#endif
6353
Bram Moolenaar29a2c082018-03-05 21:06:23 +01006354#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
6355 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006356 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006357ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006358{
Bram Moolenaar29a2c082018-03-05 21:06:23 +01006359# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
6360 if (gui.in_use)
6361 gui_make_popup(eap->arg, eap->forceit);
6362# ifdef FEAT_TERM_POPUP_MENU
6363 else
6364# endif
6365# endif
6366# ifdef FEAT_TERM_POPUP_MENU
6367 pum_make_popup(eap->arg, eap->forceit);
6368# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006369}
6370#endif
6371
Bram Moolenaar071d4272004-06-13 20:20:40 +00006372 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006373ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006374{
6375 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006376 msg(_("No swap file"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006377 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01006378 msg((char *)curbuf->b_ml.ml_mfp->mf_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006379}
6380
6381/*
6382 * ":syncbind" forces all 'scrollbind' windows to have the same relative
6383 * offset.
6384 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
6385 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006386 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006387ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006388{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006389 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006390 win_T *save_curwin = curwin;
6391 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006392 long topline;
6393 long y;
6394 linenr_T old_linenr = curwin->w_cursor.lnum;
6395
6396 setpcmark();
6397
6398 /*
6399 * determine max topline
6400 */
6401 if (curwin->w_p_scb)
6402 {
6403 topline = curwin->w_topline;
Bram Moolenaar29323592016-07-24 22:04:11 +02006404 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006405 {
6406 if (wp->w_p_scb && wp->w_buffer)
6407 {
Bram Moolenaar375e3392019-01-31 18:26:10 +01006408 y = wp->w_buffer->b_ml.ml_line_count - get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006409 if (topline > y)
6410 topline = y;
6411 }
6412 }
6413 if (topline < 1)
6414 topline = 1;
6415 }
6416 else
6417 {
6418 topline = 1;
6419 }
6420
6421
6422 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006423 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006424 */
Bram Moolenaar29323592016-07-24 22:04:11 +02006425 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006426 {
6427 if (curwin->w_p_scb)
6428 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006429 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006430 y = topline - curwin->w_topline;
6431 if (y > 0)
6432 scrollup(y, TRUE);
6433 else
6434 scrolldown(-y, TRUE);
6435 curwin->w_scbind_pos = topline;
6436 redraw_later(VALID);
6437 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006438 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006439 }
6440 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006441 curwin = save_curwin;
6442 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006443 if (curwin->w_p_scb)
6444 {
6445 did_syncbind = TRUE;
6446 checkpcmark();
6447 if (old_linenr != curwin->w_cursor.lnum)
6448 {
6449 char_u ctrl_o[2];
6450
6451 ctrl_o[0] = Ctrl_O;
6452 ctrl_o[1] = 0;
6453 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
6454 }
6455 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006456}
6457
6458
6459 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006460ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006461{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006462 int i;
6463 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
6464 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006465
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006466 if (eap->usefilter) // :r!cmd
Bram Moolenaar071d4272004-06-13 20:20:40 +00006467 do_bang(1, eap, FALSE, FALSE, TRUE);
6468 else
6469 {
6470 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
6471 return;
6472
6473#ifdef FEAT_BROWSE
6474 if (cmdmod.browse)
6475 {
6476 char_u *browseFile;
6477
Bram Moolenaar7171abe2004-10-11 10:06:20 +00006478 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006479 NULL, NULL, NULL, curbuf);
6480 if (browseFile != NULL)
6481 {
6482 i = readfile(browseFile, NULL,
6483 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
6484 vim_free(browseFile);
6485 }
6486 else
6487 i = OK;
6488 }
6489 else
6490#endif
6491 if (*eap->arg == NUL)
6492 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006493 if (check_fname() == FAIL) // check for no file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00006494 return;
6495 i = readfile(curbuf->b_ffname, curbuf->b_fname,
6496 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
6497 }
6498 else
6499 {
6500 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
6501 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
6502 i = readfile(eap->arg, NULL,
6503 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
6504
6505 }
Bram Moolenaare13b9af2017-01-13 22:01:02 +01006506 if (i != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006507 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006508#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006509 if (!aborting())
6510#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006511 semsg(_(e_notopen), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006512 }
6513 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00006514 {
6515 if (empty && exmode_active)
6516 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006517 // Delete the empty line that remains. Historically ex does
6518 // this but vi doesn't.
Bram Moolenaardf177f62005-02-22 08:39:57 +00006519 if (eap->line2 == 0)
6520 lnum = curbuf->b_ml.ml_line_count;
6521 else
6522 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00006523 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006524 {
6525 ml_delete(lnum, FALSE);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00006526 if (curwin->w_cursor.lnum > 1
6527 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006528 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00006529 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006530 }
6531 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006532 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006533 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006534 }
6535}
6536
Bram Moolenaarea408852005-06-25 22:49:46 +00006537static char_u *prev_dir = NULL;
6538
6539#if defined(EXITFREE) || defined(PROTO)
6540 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006541free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00006542{
Bram Moolenaard23a8232018-02-10 18:45:26 +01006543 VIM_CLEAR(prev_dir);
6544 VIM_CLEAR(globaldir);
Bram Moolenaarea408852005-06-25 22:49:46 +00006545}
6546#endif
6547
Bram Moolenaarf4258302013-06-02 18:20:17 +02006548/*
6549 * Deal with the side effects of changing the current directory.
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006550 * When 'scope' is CDSCOPE_TABPAGE then this was after an ":tcd" command.
6551 * When 'scope' is CDSCOPE_WINDOW then this was after an ":lcd" command.
Bram Moolenaarf4258302013-06-02 18:20:17 +02006552 */
6553 void
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006554post_chdir(cdscope_T scope)
Bram Moolenaarf4258302013-06-02 18:20:17 +02006555{
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006556 if (scope != CDSCOPE_WINDOW)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006557 // Clear tab local directory for both :cd and :tcd
6558 VIM_CLEAR(curtab->tp_localdir);
Bram Moolenaard23a8232018-02-10 18:45:26 +01006559 VIM_CLEAR(curwin->w_localdir);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006560 if (scope != CDSCOPE_GLOBAL)
Bram Moolenaarf4258302013-06-02 18:20:17 +02006561 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006562 // If still in global directory, need to remember current
6563 // directory as global directory.
Bram Moolenaarf4258302013-06-02 18:20:17 +02006564 if (globaldir == NULL && prev_dir != NULL)
6565 globaldir = vim_strsave(prev_dir);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006566 // Remember this local directory for the window.
Bram Moolenaarf4258302013-06-02 18:20:17 +02006567 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006568 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006569 if (scope == CDSCOPE_TABPAGE)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006570 curtab->tp_localdir = vim_strsave(NameBuff);
6571 else
6572 curwin->w_localdir = vim_strsave(NameBuff);
6573 }
Bram Moolenaarf4258302013-06-02 18:20:17 +02006574 }
6575 else
6576 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006577 // We are now in the global directory, no need to remember its
6578 // name.
Bram Moolenaard23a8232018-02-10 18:45:26 +01006579 VIM_CLEAR(globaldir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006580 }
6581
6582 shorten_fnames(TRUE);
6583}
6584
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006585/*
6586 * Change directory function used by :cd/:tcd/:lcd Ex commands and the
6587 * chdir() function. If 'winlocaldir' is TRUE, then changes the window-local
6588 * directory. If 'tablocaldir' is TRUE, then changes the tab-local directory.
6589 * Otherwise changes the global directory.
6590 * Returns TRUE if the directory is successfully changed.
6591 */
6592 int
6593changedir_func(
6594 char_u *new_dir,
6595 int forceit,
6596 cdscope_T scope)
6597{
6598 char_u *tofree;
6599 int dir_differs;
6600 int retval = FALSE;
6601
Bram Moolenaar7cc96922020-01-31 22:41:38 +01006602 if (new_dir == NULL || allbuf_locked())
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006603 return FALSE;
6604
6605 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged() && !forceit)
6606 {
6607 emsg(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
6608 return FALSE;
6609 }
6610
6611 // ":cd -": Change to previous directory
6612 if (STRCMP(new_dir, "-") == 0)
6613 {
6614 if (prev_dir == NULL)
6615 {
6616 emsg(_("E186: No previous directory"));
6617 return FALSE;
6618 }
6619 new_dir = prev_dir;
6620 }
6621
6622 // Save current directory for next ":cd -"
6623 tofree = prev_dir;
6624 if (mch_dirname(NameBuff, MAXPATHL) == OK)
6625 prev_dir = vim_strsave(NameBuff);
6626 else
6627 prev_dir = NULL;
6628
6629#if defined(UNIX) || defined(VMS)
6630 // for UNIX ":cd" means: go to home directory
6631 if (*new_dir == NUL)
6632 {
6633 // use NameBuff for home directory name
6634# ifdef VMS
6635 char_u *p;
6636
6637 p = mch_getenv((char_u *)"SYS$LOGIN");
6638 if (p == NULL || *p == NUL) // empty is the same as not set
6639 NameBuff[0] = NUL;
6640 else
6641 vim_strncpy(NameBuff, p, MAXPATHL - 1);
6642# else
6643 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
6644# endif
6645 new_dir = NameBuff;
6646 }
6647#endif
6648 dir_differs = new_dir == NULL || prev_dir == NULL
6649 || pathcmp((char *)prev_dir, (char *)new_dir, -1) != 0;
6650 if (new_dir == NULL || (dir_differs && vim_chdir(new_dir)))
6651 emsg(_(e_failed));
6652 else
6653 {
6654 char_u *acmd_fname;
6655
6656 post_chdir(scope);
6657
6658 if (dir_differs)
6659 {
6660 if (scope == CDSCOPE_WINDOW)
6661 acmd_fname = (char_u *)"window";
6662 else if (scope == CDSCOPE_TABPAGE)
6663 acmd_fname = (char_u *)"tabpage";
6664 else
6665 acmd_fname = (char_u *)"global";
6666 apply_autocmds(EVENT_DIRCHANGED, acmd_fname, new_dir, FALSE,
6667 curbuf);
6668 }
6669 retval = TRUE;
6670 }
6671 vim_free(tofree);
6672
6673 return retval;
6674}
Bram Moolenaarea408852005-06-25 22:49:46 +00006675
Bram Moolenaar071d4272004-06-13 20:20:40 +00006676/*
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006677 * ":cd", ":tcd", ":lcd", ":chdir" ":tchdir" and ":lchdir".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006678 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00006679 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006680ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006681{
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01006682 char_u *new_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006683
6684 new_dir = eap->arg;
6685#if !defined(UNIX) && !defined(VMS)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006686 // for non-UNIX ":cd" means: print current directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006687 if (*new_dir == NUL)
6688 ex_pwd(NULL);
6689 else
6690#endif
6691 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006692 cdscope_T scope = CDSCOPE_GLOBAL;
6693
6694 if (eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir)
6695 scope = CDSCOPE_WINDOW;
6696 else if (eap->cmdidx == CMD_tcd || eap->cmdidx == CMD_tchdir)
6697 scope = CDSCOPE_TABPAGE;
6698
6699 if (changedir_func(new_dir, eap->forceit, scope))
Bram Moolenaardf177f62005-02-22 08:39:57 +00006700 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006701 // Echo the new current directory if the command was typed.
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00006702 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006703 ex_pwd(eap);
6704 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006705 }
6706}
6707
6708/*
6709 * ":pwd".
6710 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006711 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006712ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006713{
6714 if (mch_dirname(NameBuff, MAXPATHL) == OK)
6715 {
6716#ifdef BACKSLASH_IN_FILENAME
6717 slash_adjust(NameBuff);
6718#endif
Bram Moolenaar32526b32019-01-19 17:43:09 +01006719 msg((char *)NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006720 }
6721 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006722 emsg(_("E187: Unknown"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006723}
6724
6725/*
6726 * ":=".
6727 */
6728 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006729ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006730{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006731 smsg("%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006732 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006733}
6734
6735 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006736ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006737{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006738 int n;
6739 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006740
6741 if (cursor_valid())
6742 {
6743 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
6744 if (n >= 0)
Bram Moolenaar53f81742017-09-22 14:35:51 +02006745 windgoto((int)n, curwin->w_wincol + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006746 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006747
6748 len = eap->line2;
6749 switch (*eap->arg)
6750 {
6751 case 'm': break;
6752 case NUL: len *= 1000L; break;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006753 default: semsg(_(e_invarg2), eap->arg); return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006754 }
6755 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006756}
6757
6758/*
6759 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
6760 */
6761 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006762do_sleep(long msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006763{
Bram Moolenaar52953192019-08-16 10:27:13 +02006764 long done = 0;
Bram Moolenaar975b5272016-03-15 23:10:59 +01006765 long wait_now;
Bram Moolenaar52953192019-08-16 10:27:13 +02006766# ifdef ELAPSED_FUNC
6767 elapsed_T start_tv;
6768
6769 // Remember at what time we started, so that we know how much longer we
6770 // should wait after waiting for a bit.
6771 ELAPSED_INIT(start_tv);
6772# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006773
6774 cursor_on();
Bram Moolenaarf45d7472018-09-30 18:22:26 +02006775 out_flush_cursor(FALSE, FALSE);
Bram Moolenaar52953192019-08-16 10:27:13 +02006776 while (!got_int && done < msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006777 {
Bram Moolenaar975b5272016-03-15 23:10:59 +01006778 wait_now = msec - done > 1000L ? 1000L : msec - done;
6779#ifdef FEAT_TIMERS
6780 {
6781 long due_time = check_due_timer();
6782
6783 if (due_time > 0 && due_time < wait_now)
6784 wait_now = due_time;
6785 }
6786#endif
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02006787#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar28e67e02019-08-15 23:05:49 +02006788 if (has_any_channel() && wait_now > 20L)
6789 wait_now = 20L;
6790#endif
6791#ifdef FEAT_SOUND
6792 if (has_any_sound_callback() && wait_now > 20L)
6793 wait_now = 20L;
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02006794#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +01006795 ui_delay(wait_now, TRUE);
Bram Moolenaar52953192019-08-16 10:27:13 +02006796
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02006797#ifdef FEAT_JOB_CHANNEL
6798 if (has_any_channel())
6799 ui_breakcheck_force(TRUE);
6800 else
6801#endif
6802 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006803#ifdef MESSAGE_QUEUE
Bram Moolenaar52953192019-08-16 10:27:13 +02006804 // Process the netbeans and clientserver messages that may have been
6805 // received in the call to ui_breakcheck() when the GUI is in use. This
6806 // may occur when running a test case.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006807 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02006808#endif
Bram Moolenaar52953192019-08-16 10:27:13 +02006809
6810# ifdef ELAPSED_FUNC
6811 // actual time passed
6812 done = ELAPSED_FUNC(start_tv);
6813# else
6814 // guestimate time passed (will actually be more)
6815 done += wait_now;
6816# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006817 }
Bram Moolenaar1ebff3d2018-07-07 16:18:13 +02006818
6819 // If CTRL-C was typed to interrupt the sleep, drop the CTRL-C from the
6820 // input buffer, otherwise a following call to input() fails.
6821 if (got_int)
6822 (void)vpeekc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006823}
6824
Bram Moolenaar071d4272004-06-13 20:20:40 +00006825/*
6826 * ":winsize" command (obsolete).
6827 */
6828 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006829ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006830{
6831 int w, h;
6832 char_u *arg = eap->arg;
6833 char_u *p;
6834
6835 w = getdigits(&arg);
6836 arg = skipwhite(arg);
6837 p = arg;
6838 h = getdigits(&arg);
6839 if (*p != NUL && *arg == NUL)
6840 set_shellsize(w, h, TRUE);
6841 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006842 emsg(_("E465: :winsize requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006843}
6844
Bram Moolenaar071d4272004-06-13 20:20:40 +00006845 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006846ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006847{
6848 int xchar = NUL;
6849 char_u *p;
6850
6851 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
6852 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006853 // CTRL-W g and CTRL-W CTRL-G have an extra command character
Bram Moolenaar071d4272004-06-13 20:20:40 +00006854 if (eap->arg[1] == NUL)
6855 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006856 emsg(_(e_invarg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006857 return;
6858 }
6859 xchar = eap->arg[1];
6860 p = eap->arg + 2;
6861 }
6862 else
6863 p = eap->arg + 1;
6864
6865 eap->nextcmd = check_nextcmd(p);
6866 p = skipwhite(p);
6867 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006868 emsg(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02006869 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006870 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006871 // Pass flags on for ":vertical wincmd ]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006872 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00006873 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006874 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
6875 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00006876 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006877 }
6878}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006879
Bram Moolenaar843ee412004-06-30 16:16:41 +00006880#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006881/*
6882 * ":winpos".
6883 */
6884 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006885ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006886{
6887 int x, y;
6888 char_u *arg = eap->arg;
6889 char_u *p;
6890
6891 if (*arg == NUL)
6892 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00006893# if defined(FEAT_GUI) || defined(MSWIN)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006894# ifdef VIMDLL
6895 if (gui.in_use ? gui_mch_get_winpos(&x, &y) != FAIL :
6896 mch_get_winpos(&x, &y) != FAIL)
6897# elif defined(FEAT_GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006898 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00006899# else
6900 if (mch_get_winpos(&x, &y) != FAIL)
6901# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006902 {
6903 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
Bram Moolenaar32526b32019-01-19 17:43:09 +01006904 msg((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006905 }
6906 else
6907# endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006908 emsg(_("E188: Obtaining window position not implemented for this platform"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006909 }
6910 else
6911 {
6912 x = getdigits(&arg);
6913 arg = skipwhite(arg);
6914 p = arg;
6915 y = getdigits(&arg);
6916 if (*p == NUL || *arg != NUL)
6917 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006918 emsg(_("E466: :winpos requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006919 return;
6920 }
6921# ifdef FEAT_GUI
6922 if (gui.in_use)
6923 gui_mch_set_winpos(x, y);
6924 else if (gui.starting)
6925 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006926 // Remember the coordinates for when the window is opened.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006927 gui_win_x = x;
6928 gui_win_y = y;
6929 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006930# if defined(HAVE_TGETENT) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006931 else
6932# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006933# endif
6934# if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar843ee412004-06-30 16:16:41 +00006935 mch_set_winpos(x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006936# endif
6937# ifdef HAVE_TGETENT
6938 if (*T_CWP)
6939 term_set_winpos(x, y);
6940# endif
6941 }
6942}
6943#endif
6944
6945/*
6946 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
6947 */
6948 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006949ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006950{
6951 oparg_T oa;
6952
6953 clear_oparg(&oa);
6954 oa.regname = eap->regname;
6955 oa.start.lnum = eap->line1;
6956 oa.end.lnum = eap->line2;
6957 oa.line_count = eap->line2 - eap->line1 + 1;
6958 oa.motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006959 virtual_op = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006960 if (eap->cmdidx != CMD_yank) // position cursor for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00006961 {
6962 setpcmark();
6963 curwin->w_cursor.lnum = eap->line1;
6964 beginline(BL_SOL | BL_FIX);
6965 }
6966
Bram Moolenaard07c6e12013-11-21 14:21:40 +01006967 if (VIsual_active)
6968 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01006969
Bram Moolenaar071d4272004-06-13 20:20:40 +00006970 switch (eap->cmdidx)
6971 {
6972 case CMD_delete:
6973 oa.op_type = OP_DELETE;
6974 op_delete(&oa);
6975 break;
6976
6977 case CMD_yank:
6978 oa.op_type = OP_YANK;
6979 (void)op_yank(&oa, FALSE, TRUE);
6980 break;
6981
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006982 default: // CMD_rshift or CMD_lshift
Bram Moolenaar6e707362013-06-14 19:15:58 +02006983 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00006984#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02006985 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
6986#else
6987 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00006988#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02006989 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006990 oa.op_type = OP_RSHIFT;
6991 else
6992 oa.op_type = OP_LSHIFT;
6993 op_shift(&oa, FALSE, eap->amount);
6994 break;
6995 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006996 virtual_op = MAYBE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006997 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006998}
6999
7000/*
7001 * ":put".
7002 */
7003 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007004ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007005{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007006 // ":0put" works like ":1put!".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007007 if (eap->line2 == 0)
7008 {
7009 eap->line2 = 1;
7010 eap->forceit = TRUE;
7011 }
7012 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007013 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
7014 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007015}
7016
7017/*
7018 * Handle ":copy" and ":move".
7019 */
7020 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007021ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007022{
7023 long n;
7024
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02007025 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE, FALSE, 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007026 if (eap->arg == NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00007027 {
7028 eap->nextcmd = NULL;
7029 return;
7030 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00007031 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007032
7033 /*
7034 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
7035 */
7036 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
7037 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02007038 emsg(_(e_invrange));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007039 return;
7040 }
7041
7042 if (eap->cmdidx == CMD_move)
7043 {
7044 if (do_move(eap->line1, eap->line2, n) == FAIL)
7045 return;
7046 }
7047 else
7048 ex_copy(eap->line1, eap->line2, n);
7049 u_clearline();
7050 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007051 ex_may_print(eap);
7052}
7053
7054/*
7055 * Print the current line if flags were given to the Ex command.
7056 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02007057 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007058ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007059{
7060 if (eap->flags != 0)
7061 {
7062 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
7063 (eap->flags & EXFLAG_LIST));
7064 ex_no_reprint = TRUE;
7065 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007066}
7067
7068/*
7069 * ":smagic" and ":snomagic".
7070 */
7071 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007072ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007073{
7074 int magic_save = p_magic;
7075
7076 p_magic = (eap->cmdidx == CMD_smagic);
7077 do_sub(eap);
7078 p_magic = magic_save;
7079}
7080
7081/*
7082 * ":join".
7083 */
7084 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007085ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007086{
7087 curwin->w_cursor.lnum = eap->line1;
7088 if (eap->line1 == eap->line2)
7089 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007090 if (eap->addr_count >= 2) // :2,2join does nothing
Bram Moolenaar071d4272004-06-13 20:20:40 +00007091 return;
7092 if (eap->line2 == curbuf->b_ml.ml_line_count)
7093 {
7094 beep_flush();
7095 return;
7096 }
7097 ++eap->line2;
7098 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02007099 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007100 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007101 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007102}
7103
7104/*
7105 * ":[addr]@r" or ":[addr]*r": execute register
7106 */
7107 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007108ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007109{
7110 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00007111 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007112
7113 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4930a762016-09-11 14:39:53 +02007114 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007115
7116#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007117 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00007118#endif
7119
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007120 // get the register name. No name means to use the previous one
Bram Moolenaar071d4272004-06-13 20:20:40 +00007121 c = *eap->arg;
7122 if (c == NUL || (c == '*' && *eap->cmd == '*'))
7123 c = '@';
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007124 // Put the register in the typeahead buffer with the "silent" flag.
Bram Moolenaard333d1e2006-11-07 17:43:47 +00007125 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
7126 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007127 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007128 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00007129 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007130 else
7131 {
7132 int save_efr = exec_from_reg;
7133
7134 exec_from_reg = TRUE;
7135
7136 /*
7137 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00007138 * Continue until the stuff buffer is empty and all added characters
7139 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007140 */
Bram Moolenaar60462872009-11-03 11:40:19 +00007141 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007142 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
7143
7144 exec_from_reg = save_efr;
7145 }
7146}
7147
7148/*
7149 * ":!".
7150 */
7151 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007152ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007153{
7154 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
7155}
7156
7157/*
7158 * ":undo".
7159 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007160 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01007161ex_undo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007162{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007163 if (eap->addr_count == 1) // :undo 123
Bram Moolenaar730cde92010-06-27 05:18:54 +02007164 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00007165 else
7166 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007167}
7168
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007169#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02007170 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007171ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007172{
7173 char_u hash[UNDO_HASH_SIZE];
7174
7175 u_compute_hash(hash);
7176 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
7177}
7178
Bram Moolenaar6df6f472010-07-18 18:04:50 +02007179 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007180ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007181{
7182 char_u hash[UNDO_HASH_SIZE];
7183
7184 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02007185 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007186}
7187#endif
7188
Bram Moolenaar071d4272004-06-13 20:20:40 +00007189/*
7190 * ":redo".
7191 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007192 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007193ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007194{
7195 u_redo(1);
7196}
7197
7198/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007199 * ":earlier" and ":later".
7200 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007201 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007202ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007203{
7204 long count = 0;
7205 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02007206 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007207 char_u *p = eap->arg;
7208
7209 if (*p == NUL)
7210 count = 1;
7211 else if (isdigit(*p))
7212 {
7213 count = getdigits(&p);
7214 switch (*p)
7215 {
7216 case 's': ++p; sec = TRUE; break;
7217 case 'm': ++p; sec = TRUE; count *= 60; break;
7218 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02007219 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
7220 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007221 }
7222 }
7223
7224 if (*p != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007225 semsg(_(e_invarg2), eap->arg);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007226 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02007227 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
7228 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007229}
7230
7231/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007232 * ":redir": start/stop redirection.
7233 */
7234 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007235ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007236{
7237 char *mode;
7238 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00007239 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007240
Bram Moolenaarba768492016-07-08 15:32:54 +02007241#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02007242 if (redir_execute)
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007243 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007244 emsg(_("E930: Cannot use :redir inside execute()"));
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007245 return;
7246 }
Bram Moolenaarba768492016-07-08 15:32:54 +02007247#endif
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007248
Bram Moolenaar071d4272004-06-13 20:20:40 +00007249 if (STRICMP(eap->arg, "END") == 0)
7250 close_redir();
7251 else
7252 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007253 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007254 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007255 ++arg;
7256 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007257 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007258 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007259 mode = "a";
7260 }
7261 else
7262 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00007263 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007264
7265 close_redir();
7266
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007267 // Expand environment variables and "~/".
Bram Moolenaarca472992005-01-21 11:46:23 +00007268 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007269 if (fname == NULL)
7270 return;
7271#ifdef FEAT_BROWSE
7272 if (cmdmod.browse)
7273 {
7274 char_u *browseFile;
7275
Bram Moolenaar7171abe2004-10-11 10:06:20 +00007276 browseFile = do_browse(BROWSE_SAVE,
7277 (char_u *)_("Save Redirection"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +02007278 fname, NULL, NULL,
7279 (char_u *)_(BROWSE_FILTER_ALL_FILES), curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007280 if (browseFile == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007281 return; // operation cancelled
Bram Moolenaar071d4272004-06-13 20:20:40 +00007282 vim_free(fname);
7283 fname = browseFile;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007284 eap->forceit = TRUE; // since dialog already asked
Bram Moolenaar071d4272004-06-13 20:20:40 +00007285 }
7286#endif
7287
7288 redir_fd = open_exfile(fname, eap->forceit, mode);
7289 vim_free(fname);
7290 }
7291#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00007292 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007293 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007294 // redirect to a register a-z (resp. A-Z for appending)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007295 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00007296 ++arg;
7297 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007298# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00007299 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00007300 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00007301# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00007302 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007303 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007304 redir_reg = *arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007305 if (*arg == '>' && arg[1] == '>') // append
Bram Moolenaard9d30582005-05-18 22:10:28 +00007306 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00007307 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007308 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007309 // Can use both "@a" and "@a>".
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00007310 if (*arg == '>')
7311 arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007312 // Make register empty when not using @A-@Z and the
7313 // command is valid.
Bram Moolenaarc188b882007-10-19 14:20:54 +00007314 if (*arg == NUL && !isupper(redir_reg))
7315 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007316 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00007317 }
7318 if (*arg != NUL)
7319 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007320 redir_reg = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007321 semsg(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007322 }
7323 }
7324 else if (*arg == '=' && arg[1] == '>')
7325 {
7326 int append;
7327
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007328 // redirect to a variable
Bram Moolenaardf177f62005-02-22 08:39:57 +00007329 close_redir();
7330 arg += 2;
7331
7332 if (*arg == '>')
7333 {
7334 ++arg;
7335 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007336 }
7337 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00007338 append = FALSE;
7339
7340 if (var_redir_start(skipwhite(arg), append) == OK)
7341 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007342 }
7343#endif
7344
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007345 // TODO: redirect to a buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00007346
Bram Moolenaar071d4272004-06-13 20:20:40 +00007347 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007348 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007349 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00007350
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007351 // Make sure redirection is not off. Can happen for cmdline completion
7352 // that indirectly invokes a command to catch its output.
Bram Moolenaar29b2d262006-09-10 19:07:28 +00007353 if (redir_fd != NULL
7354#ifdef FEAT_EVAL
7355 || redir_reg || redir_vname
7356#endif
7357 )
7358 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007359}
7360
7361/*
7362 * ":redraw": force redraw
7363 */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01007364 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007365ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007366{
7367 int r = RedrawingDisabled;
7368 int p = p_lz;
7369
7370 RedrawingDisabled = 0;
7371 p_lz = FALSE;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01007372 validate_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007373 update_topline();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007374 update_screen(eap->forceit ? CLEAR : VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007375#ifdef FEAT_TITLE
7376 if (need_maketitle)
7377 maketitle();
7378#endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007379#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
7380# ifdef VIMDLL
7381 if (!gui.in_use)
7382# endif
7383 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01007384#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007385 RedrawingDisabled = r;
7386 p_lz = p;
7387
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007388 // Reset msg_didout, so that a message that's there is overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007389 msg_didout = FALSE;
7390 msg_col = 0;
7391
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007392 // No need to wait after an intentional redraw.
Bram Moolenaar56667a52013-07-17 11:54:28 +02007393 need_wait_return = FALSE;
7394
Bram Moolenaar071d4272004-06-13 20:20:40 +00007395 out_flush();
7396}
7397
7398/*
7399 * ":redrawstatus": force redraw of status line(s)
7400 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007401 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007402ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007403{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007404 int r = RedrawingDisabled;
7405 int p = p_lz;
7406
7407 RedrawingDisabled = 0;
7408 p_lz = FALSE;
7409 if (eap->forceit)
7410 status_redraw_all();
7411 else
7412 status_redraw_curbuf();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007413 update_screen(VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007414 RedrawingDisabled = r;
7415 p_lz = p;
7416 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007417}
7418
Bram Moolenaare12bab32019-01-08 22:02:56 +01007419/*
7420 * ":redrawtabline": force redraw of the tabline
7421 */
7422 static void
7423ex_redrawtabline(exarg_T *eap UNUSED)
7424{
7425 int r = RedrawingDisabled;
7426 int p = p_lz;
7427
7428 RedrawingDisabled = 0;
7429 p_lz = FALSE;
7430
7431 draw_tabline();
7432
7433 RedrawingDisabled = r;
7434 p_lz = p;
7435 out_flush();
7436}
7437
Bram Moolenaar071d4272004-06-13 20:20:40 +00007438 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007439close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007440{
7441 if (redir_fd != NULL)
7442 {
7443 fclose(redir_fd);
7444 redir_fd = NULL;
7445 }
7446#ifdef FEAT_EVAL
7447 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007448 if (redir_vname)
7449 {
7450 var_redir_stop();
7451 redir_vname = 0;
7452 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007453#endif
7454}
7455
Bram Moolenaarf96ae0b2019-07-28 15:21:55 +02007456#if (defined(FEAT_SESSION) || defined(FEAT_EVAL)) || defined(PROTO)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007457 int
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02007458vim_mkdir_emsg(char_u *name, int prot UNUSED)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007459{
7460 if (vim_mkdir(name, prot) != 0)
7461 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007462 semsg(_("E739: Cannot create directory: %s"), name);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007463 return FAIL;
7464 }
7465 return OK;
7466}
7467#endif
7468
Bram Moolenaar071d4272004-06-13 20:20:40 +00007469/*
7470 * Open a file for writing for an Ex command, with some checks.
7471 * Return file descriptor, or NULL on failure.
7472 */
7473 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007474open_exfile(
7475 char_u *fname,
7476 int forceit,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007477 char *mode) // "w" for create new file or "a" for append
Bram Moolenaar071d4272004-06-13 20:20:40 +00007478{
7479 FILE *fd;
7480
7481#ifdef UNIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007482 // with Unix it is possible to open a directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007483 if (mch_isdir(fname))
7484 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007485 semsg(_(e_isadir2), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007486 return NULL;
7487 }
7488#endif
7489 if (!forceit && *mode != 'a' && vim_fexists(fname))
7490 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007491 semsg(_("E189: \"%s\" exists (add ! to override)"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007492 return NULL;
7493 }
7494
7495 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007496 semsg(_("E190: Cannot open \"%s\" for writing"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007497
7498 return fd;
7499}
7500
7501/*
7502 * ":mark" and ":k".
7503 */
7504 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007505ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007506{
7507 pos_T pos;
7508
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007509 if (*eap->arg == NUL) // No argument?
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007510 emsg(_(e_argreq));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007511 else if (eap->arg[1] != NUL) // more than one character?
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007512 emsg(_(e_trailing));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007513 else
7514 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007515 pos = curwin->w_cursor; // save curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00007516 curwin->w_cursor.lnum = eap->line2;
7517 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007518 if (setmark(*eap->arg) == FAIL) // set mark
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007519 emsg(_("E191: Argument must be a letter or forward/backward quote"));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007520 curwin->w_cursor = pos; // restore curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00007521 }
7522}
7523
7524/*
7525 * Update w_topline, w_leftcol and the cursor position.
7526 */
7527 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007528update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007529{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007530 check_cursor(); // put cursor on valid line
Bram Moolenaar071d4272004-06-13 20:20:40 +00007531 update_topline();
7532 if (!curwin->w_p_wrap)
7533 validate_cursor();
7534 update_curswant();
7535}
7536
Bram Moolenaar071d4272004-06-13 20:20:40 +00007537/*
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007538 * Save the current State and go to Normal mode.
7539 * Return TRUE if the typeahead could be saved.
7540 */
7541 int
7542save_current_state(save_state_T *sst)
7543{
7544 sst->save_msg_scroll = msg_scroll;
7545 sst->save_restart_edit = restart_edit;
7546 sst->save_msg_didout = msg_didout;
7547 sst->save_State = State;
7548 sst->save_insertmode = p_im;
7549 sst->save_finish_op = finish_op;
7550 sst->save_opcount = opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01007551 sst->save_reg_executing = reg_executing;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007552
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007553 msg_scroll = FALSE; // no msg scrolling in Normal mode
7554 restart_edit = 0; // don't go to Insert mode
7555 p_im = FALSE; // don't use 'insertmode'
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007556
7557 /*
7558 * Save the current typeahead. This is required to allow using ":normal"
7559 * from an event handler and makes sure we don't hang when the argument
7560 * ends with half a command.
7561 */
7562 save_typeahead(&sst->tabuf);
7563 return sst->tabuf.typebuf_valid;
7564}
7565
7566 void
7567restore_current_state(save_state_T *sst)
7568{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007569 // Restore the previous typeahead.
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007570 restore_typeahead(&sst->tabuf);
7571
7572 msg_scroll = sst->save_msg_scroll;
7573 restart_edit = sst->save_restart_edit;
7574 p_im = sst->save_insertmode;
7575 finish_op = sst->save_finish_op;
7576 opcount = sst->save_opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01007577 reg_executing = sst->save_reg_executing;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007578 msg_didout |= sst->save_msg_didout; // don't reset msg_didout now
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007579
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007580 // Restore the state (needed when called from a function executed for
7581 // 'indentexpr'). Update the mouse and cursor, they may have changed.
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007582 State = sst->save_State;
7583#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007584 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007585#endif
7586}
7587
7588/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007589 * ":normal[!] {commands}": Execute normal mode commands.
7590 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01007591 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007592ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007593{
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007594 save_state_T save_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007595 char_u *arg = NULL;
7596 int l;
7597 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007598
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00007599 if (ex_normal_lock > 0)
7600 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007601 emsg(_(e_secure));
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00007602 return;
7603 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007604 if (ex_normal_busy >= p_mmd)
7605 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007606 emsg(_("E192: Recursive use of :normal too deep"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007607 return;
7608 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007609
Bram Moolenaar071d4272004-06-13 20:20:40 +00007610 /*
7611 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
7612 * this for the K_SPECIAL leading byte, otherwise special keys will not
7613 * work.
7614 */
7615 if (has_mbyte)
7616 {
7617 int len = 0;
7618
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007619 // Count the number of characters to be escaped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007620 for (p = eap->arg; *p != NUL; ++p)
7621 {
Bram Moolenaar13505972019-01-24 15:04:48 +01007622#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007623 if (*p == CSI) // leadbyte CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007624 len += 2;
Bram Moolenaar13505972019-01-24 15:04:48 +01007625#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007626 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007627 if (*++p == K_SPECIAL // trailbyte K_SPECIAL or CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01007628#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007629 || *p == CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01007630#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007631 )
7632 len += 2;
7633 }
7634 if (len > 0)
7635 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02007636 arg = alloc(STRLEN(eap->arg) + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007637 if (arg != NULL)
7638 {
7639 len = 0;
7640 for (p = eap->arg; *p != NUL; ++p)
7641 {
7642 arg[len++] = *p;
Bram Moolenaar13505972019-01-24 15:04:48 +01007643#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007644 if (*p == CSI)
7645 {
7646 arg[len++] = KS_EXTRA;
7647 arg[len++] = (int)KE_CSI;
7648 }
Bram Moolenaar13505972019-01-24 15:04:48 +01007649#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007650 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007651 {
7652 arg[len++] = *++p;
7653 if (*p == K_SPECIAL)
7654 {
7655 arg[len++] = KS_SPECIAL;
7656 arg[len++] = KE_FILLER;
7657 }
Bram Moolenaar13505972019-01-24 15:04:48 +01007658#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007659 else if (*p == CSI)
7660 {
7661 arg[len++] = KS_EXTRA;
7662 arg[len++] = (int)KE_CSI;
7663 }
Bram Moolenaar13505972019-01-24 15:04:48 +01007664#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007665 }
7666 arg[len] = NUL;
7667 }
7668 }
7669 }
7670 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007671
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007672 ++ex_normal_busy;
7673 if (save_current_state(&save_state))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007674 {
7675 /*
7676 * Repeat the :normal command for each line in the range. When no
7677 * range given, execute it just once, without positioning the cursor
7678 * first.
7679 */
7680 do
7681 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007682 if (eap->addr_count != 0)
7683 {
7684 curwin->w_cursor.lnum = eap->line1++;
7685 curwin->w_cursor.col = 0;
Bram Moolenaard5d37532017-03-27 23:02:07 +02007686 check_cursor_moved(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007687 }
7688
Bram Moolenaar13505972019-01-24 15:04:48 +01007689 exec_normal_cmd(arg != NULL
7690 ? arg
7691 : eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007692 }
7693 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
7694 }
7695
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007696 // Might not return to the main loop when in an event handler.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007697 update_topline_cursor();
7698
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007699 restore_current_state(&save_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007700 --ex_normal_busy;
Bram Moolenaareda73602014-11-05 09:53:23 +01007701 setmouse();
Bram Moolenaareda73602014-11-05 09:53:23 +01007702#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007703 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaareda73602014-11-05 09:53:23 +01007704#endif
7705
Bram Moolenaar071d4272004-06-13 20:20:40 +00007706 vim_free(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007707}
7708
7709/*
Bram Moolenaar64969662005-12-14 21:59:55 +00007710 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007711 */
7712 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007713ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007714{
Bram Moolenaarfd371682005-01-14 21:42:54 +00007715 if (eap->forceit)
7716 {
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007717 // cursor line can be zero on startup
Bram Moolenaar09ca9322017-09-26 17:40:45 +02007718 if (!curwin->w_cursor.lnum)
7719 curwin->w_cursor.lnum = 1;
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007720 set_cursor_for_append_to_line();
Bram Moolenaarfd371682005-01-14 21:42:54 +00007721 }
7722
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007723 // Ignore the command when already in Insert mode. Inserting an
7724 // expression register that invokes a function can do this.
Bram Moolenaara40c5002005-01-09 21:16:21 +00007725 if (State & INSERT)
7726 return;
7727
Bram Moolenaar64969662005-12-14 21:59:55 +00007728 if (eap->cmdidx == CMD_startinsert)
7729 restart_edit = 'a';
7730 else if (eap->cmdidx == CMD_startreplace)
7731 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00007732 else
Bram Moolenaar64969662005-12-14 21:59:55 +00007733 restart_edit = 'V';
7734
7735 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007736 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00007737 if (eap->cmdidx == CMD_startinsert)
7738 restart_edit = 'i';
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007739 curwin->w_curswant = 0; // avoid MAXCOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007740 }
7741}
7742
7743/*
7744 * ":stopinsert"
7745 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007746 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007747ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007748{
7749 restart_edit = 0;
7750 stop_insert_mode = TRUE;
Bram Moolenaarfd773e92016-04-02 19:39:16 +02007751 clearmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007752}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007753
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007754/*
7755 * Execute normal mode command "cmd".
7756 * "remap" can be REMAP_NONE or REMAP_YES.
7757 */
7758 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007759exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007760{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007761 // Stuff the argument into the typeahead buffer.
Bram Moolenaar25281632016-01-21 23:32:32 +01007762 ins_typebuf(cmd, remap, 0, TRUE, silent);
Bram Moolenaar586c70c2018-10-02 16:23:58 +02007763 exec_normal(FALSE, FALSE, FALSE);
Bram Moolenaar25281632016-01-21 23:32:32 +01007764}
Bram Moolenaar25281632016-01-21 23:32:32 +01007765
Bram Moolenaar25281632016-01-21 23:32:32 +01007766/*
7767 * Execute normal_cmd() until there is no typeahead left.
Bram Moolenaar586c70c2018-10-02 16:23:58 +02007768 * When "use_vpeekc" is TRUE use vpeekc() to check for available chars.
Bram Moolenaar25281632016-01-21 23:32:32 +01007769 */
7770 void
Bram Moolenaar586c70c2018-10-02 16:23:58 +02007771exec_normal(int was_typed, int use_vpeekc, int may_use_terminal_loop UNUSED)
Bram Moolenaar25281632016-01-21 23:32:32 +01007772{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007773 oparg_T oa;
Bram Moolenaar905dd902019-04-07 14:21:47 +02007774 int c;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007775
Bram Moolenaar905dd902019-04-07 14:21:47 +02007776 // When calling vpeekc() from feedkeys() it will return Ctrl_C when there
7777 // is nothing to get, so also check for Ctrl_C.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007778 clear_oparg(&oa);
7779 finish_op = FALSE;
Bram Moolenaar586c70c2018-10-02 16:23:58 +02007780 while ((!stuff_empty()
7781 || ((was_typed || !typebuf_typed()) && typebuf.tb_len > 0)
Bram Moolenaar905dd902019-04-07 14:21:47 +02007782 || (use_vpeekc && (c = vpeekc()) != NUL && c != Ctrl_C))
Bram Moolenaar586c70c2018-10-02 16:23:58 +02007783 && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007784 {
7785 update_topline_cursor();
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02007786#ifdef FEAT_TERMINAL
Bram Moolenaar655a82a2018-05-08 22:01:07 +02007787 if (may_use_terminal_loop && term_use_loop()
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02007788 && oa.op_type == OP_NOP && oa.regname == NUL
7789 && !VIsual_active)
7790 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007791 // If terminal_loop() returns OK we got a key that is handled
7792 // in Normal model. With FAIL we first need to position the
7793 // cursor and the screen needs to be redrawn.
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02007794 if (terminal_loop(TRUE) == OK)
7795 normal_cmd(&oa, TRUE);
7796 }
7797 else
7798#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007799 // execute a Normal mode cmd
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02007800 normal_cmd(&oa, TRUE);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007801 }
7802}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007803
Bram Moolenaar071d4272004-06-13 20:20:40 +00007804#ifdef FEAT_FIND_ID
7805 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007806ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007807{
7808 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
7809 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
7810 (linenr_T)1, (linenr_T)MAXLNUM);
7811}
7812
Bram Moolenaar4033c552017-09-16 20:54:51 +02007813#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007814/*
7815 * ":psearch"
7816 */
7817 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007818ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007819{
7820 g_do_tagpreview = p_pvh;
7821 ex_findpat(eap);
7822 g_do_tagpreview = 0;
7823}
7824#endif
7825
7826 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007827ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007828{
7829 int whole = TRUE;
7830 long n;
7831 char_u *p;
7832 int action;
7833
7834 switch (cmdnames[eap->cmdidx].cmd_name[2])
7835 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007836 case 'e': // ":psearch", ":isearch" and ":dsearch"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007837 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
7838 action = ACTION_GOTO;
7839 else
7840 action = ACTION_SHOW;
7841 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007842 case 'i': // ":ilist" and ":dlist"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007843 action = ACTION_SHOW_ALL;
7844 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007845 case 'u': // ":ijump" and ":djump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007846 action = ACTION_GOTO;
7847 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007848 default: // ":isplit" and ":dsplit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007849 action = ACTION_SPLIT;
7850 break;
7851 }
7852
7853 n = 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007854 if (vim_isdigit(*eap->arg)) // get count
Bram Moolenaar071d4272004-06-13 20:20:40 +00007855 {
7856 n = getdigits(&eap->arg);
7857 eap->arg = skipwhite(eap->arg);
7858 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007859 if (*eap->arg == '/') // Match regexp, not just whole words
Bram Moolenaar071d4272004-06-13 20:20:40 +00007860 {
7861 whole = FALSE;
7862 ++eap->arg;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02007863 p = skip_regexp(eap->arg, '/', p_magic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007864 if (*p)
7865 {
7866 *p++ = NUL;
7867 p = skipwhite(p);
7868
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007869 // Check for trailing illegal characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00007870 if (!ends_excmd(*p))
7871 eap->errmsg = e_trailing;
7872 else
7873 eap->nextcmd = check_nextcmd(p);
7874 }
7875 }
7876 if (!eap->skip)
7877 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
7878 whole, !eap->forceit,
7879 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
7880 n, action, eap->line1, eap->line2);
7881}
7882#endif
7883
Bram Moolenaar071d4272004-06-13 20:20:40 +00007884
Bram Moolenaar4033c552017-09-16 20:54:51 +02007885#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00007886/*
7887 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
7888 */
7889 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007890ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007891{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007892 g_do_tagpreview = p_pvh; // will be reset to 0 in ex_tag_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00007893 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
7894}
7895
7896/*
7897 * ":pedit"
7898 */
7899 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007900ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007901{
7902 win_T *curwin_save = curwin;
7903
Bram Moolenaar3a2505c2020-03-09 16:40:41 +01007904 if (ERROR_IF_ANY_POPUP_WINDOW)
7905 return;
7906
Bram Moolenaar026587b2019-08-17 15:08:00 +02007907 // Open the preview window or popup and make it the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007908 g_do_tagpreview = p_pvh;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02007909 prepare_tagpreview(TRUE, TRUE, FALSE);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02007910
Bram Moolenaar026587b2019-08-17 15:08:00 +02007911 // Edit the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007912 do_exedit(eap, NULL);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02007913
Bram Moolenaar071d4272004-06-13 20:20:40 +00007914 if (curwin != curwin_save && win_valid(curwin_save))
7915 {
Bram Moolenaar026587b2019-08-17 15:08:00 +02007916 // Return cursor to where we were
Bram Moolenaar071d4272004-06-13 20:20:40 +00007917 validate_cursor();
7918 redraw_later(VALID);
7919 win_enter(curwin_save, TRUE);
7920 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01007921# ifdef FEAT_PROP_POPUP
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02007922 else if (WIN_IS_POPUP(curwin))
7923 {
7924 // can't keep focus in popup window
7925 win_enter(firstwin, TRUE);
7926 }
7927# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007928 g_do_tagpreview = 0;
7929}
Bram Moolenaar4033c552017-09-16 20:54:51 +02007930#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007931
7932/*
7933 * ":stag", ":stselect" and ":stjump".
7934 */
7935 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007936ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007937{
7938 postponed_split = -1;
7939 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00007940 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007941 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
7942 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00007943 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007944}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007945
7946/*
7947 * ":tag", ":tselect", ":tjump", ":tnext", etc.
7948 */
7949 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007950ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007951{
7952 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
7953}
7954
7955 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007956ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007957{
7958 int cmd;
7959
7960 switch (name[1])
7961 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007962 case 'j': cmd = DT_JUMP; // ":tjump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007963 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007964 case 's': cmd = DT_SELECT; // ":tselect"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007965 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007966 case 'p': cmd = DT_PREV; // ":tprevious"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007967 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007968 case 'N': cmd = DT_PREV; // ":tNext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007969 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007970 case 'n': cmd = DT_NEXT; // ":tnext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007971 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007972 case 'o': cmd = DT_POP; // ":pop"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007973 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007974 case 'f': // ":tfirst"
7975 case 'r': cmd = DT_FIRST; // ":trewind"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007976 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007977 case 'l': cmd = DT_LAST; // ":tlast"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007978 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007979 default: // ":tag"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007980#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +00007981 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007982 {
Bram Moolenaard4db7712016-11-12 19:16:46 +01007983 ex_cstag(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007984 return;
7985 }
7986#endif
7987 cmd = DT_TAG;
7988 break;
7989 }
7990
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00007991 if (name[0] == 'l')
7992 {
7993#ifndef FEAT_QUICKFIX
7994 ex_ni(eap);
7995 return;
7996#else
7997 cmd = DT_LTAG;
7998#endif
7999 }
8000
Bram Moolenaar071d4272004-06-13 20:20:40 +00008001 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
8002 eap->forceit, TRUE);
8003}
8004
8005/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008006 * Check "str" for starting with a special cmdline variable.
8007 * If found return one of the SPEC_ values and set "*usedlen" to the length of
8008 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
8009 */
8010 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008011find_cmdline_var(char_u *src, int *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008012{
8013 int len;
8014 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +00008015 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008016 "%",
8017#define SPEC_PERC 0
8018 "#",
Bram Moolenaar65f08472017-09-10 18:16:20 +02008019#define SPEC_HASH (SPEC_PERC + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008020 "<cword>", // cursor word
Bram Moolenaar65f08472017-09-10 18:16:20 +02008021#define SPEC_CWORD (SPEC_HASH + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008022 "<cWORD>", // cursor WORD
Bram Moolenaar65f08472017-09-10 18:16:20 +02008023#define SPEC_CCWORD (SPEC_CWORD + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008024 "<cexpr>", // expr under cursor
Bram Moolenaar65f08472017-09-10 18:16:20 +02008025#define SPEC_CEXPR (SPEC_CCWORD + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008026 "<cfile>", // cursor path name
Bram Moolenaar65f08472017-09-10 18:16:20 +02008027#define SPEC_CFILE (SPEC_CEXPR + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008028 "<sfile>", // ":so" file name
Bram Moolenaar65f08472017-09-10 18:16:20 +02008029#define SPEC_SFILE (SPEC_CFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008030 "<slnum>", // ":so" file line number
Bram Moolenaar65f08472017-09-10 18:16:20 +02008031#define SPEC_SLNUM (SPEC_SFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008032 "<afile>", // autocommand file name
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008033#define SPEC_AFILE (SPEC_SLNUM + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008034 "<abuf>", // autocommand buffer number
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008035#define SPEC_ABUF (SPEC_AFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008036 "<amatch>", // autocommand match name
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008037#define SPEC_AMATCH (SPEC_ABUF + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008038 "<sflnum>", // script file line number
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008039#define SPEC_SFLNUM (SPEC_AMATCH + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008040#ifdef FEAT_CLIENTSERVER
8041 "<client>"
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008042# define SPEC_CLIENT (SPEC_SFLNUM + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008043#endif
8044 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008045
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008046 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008047 {
8048 len = (int)STRLEN(spec_str[i]);
8049 if (STRNCMP(src, spec_str[i], len) == 0)
8050 {
8051 *usedlen = len;
8052 return i;
8053 }
8054 }
8055 return -1;
8056}
8057
8058/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008059 * Evaluate cmdline variables.
8060 *
8061 * change '%' to curbuf->b_ffname
8062 * '#' to curwin->w_altfile
8063 * '<cword>' to word under the cursor
8064 * '<cWORD>' to WORD under the cursor
Bram Moolenaar8071cb22019-07-12 17:58:01 +02008065 * '<cexpr>' to C-expression under the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00008066 * '<cfile>' to path name under the cursor
8067 * '<sfile>' to sourced file name
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008068 * '<slnum>' to sourced file line number
Bram Moolenaar071d4272004-06-13 20:20:40 +00008069 * '<afile>' to file name for autocommand
8070 * '<abuf>' to buffer number for autocommand
8071 * '<amatch>' to matching name for autocommand
8072 *
8073 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
8074 * "" for error without a message) and NULL is returned.
8075 * Returns an allocated string if a valid match was found.
8076 * Returns NULL if no match was found. "usedlen" then still contains the
8077 * number of characters to skip.
8078 */
8079 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008080eval_vars(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008081 char_u *src, // pointer into commandline
8082 char_u *srcstart, // beginning of valid memory for src
8083 int *usedlen, // characters after src that are used
8084 linenr_T *lnump, // line number for :e command, or NULL
8085 char **errormsg, // pointer to error message
8086 int *escaped) // return value has escaped white space (can
8087 // be NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008088{
8089 int i;
8090 char_u *s;
8091 char_u *result;
8092 char_u *resultbuf = NULL;
8093 int resultlen;
8094 buf_T *buf;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008095 int valid = VALID_HEAD + VALID_PATH; // assume valid result
Bram Moolenaar071d4272004-06-13 20:20:40 +00008096 int spec_idx;
Bram Moolenaarfd249462018-07-28 16:14:30 +02008097 int tilde_file = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008098 int skip_mod = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008099 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008100
8101 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +00008102 if (escaped != NULL)
8103 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008104
8105 /*
8106 * Check if there is something to do.
8107 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008108 spec_idx = find_cmdline_var(src, usedlen);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008109 if (spec_idx < 0) // no match
Bram Moolenaar071d4272004-06-13 20:20:40 +00008110 {
8111 *usedlen = 1;
8112 return NULL;
8113 }
8114
8115 /*
8116 * Skip when preceded with a backslash "\%" and "\#".
8117 * Note: In "\\%" the % is also not recognized!
8118 */
8119 if (src > srcstart && src[-1] == '\\')
8120 {
8121 *usedlen = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008122 STRMOVE(src - 1, src); // remove backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00008123 return NULL;
8124 }
8125
8126 /*
8127 * word or WORD under cursor
8128 */
Bram Moolenaar65f08472017-09-10 18:16:20 +02008129 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD
8130 || spec_idx == SPEC_CEXPR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008131 {
Bram Moolenaar65f08472017-09-10 18:16:20 +02008132 resultlen = find_ident_under_cursor(&result,
8133 spec_idx == SPEC_CWORD ? (FIND_IDENT | FIND_STRING)
8134 : spec_idx == SPEC_CEXPR ? (FIND_IDENT | FIND_STRING | FIND_EVAL)
8135 : FIND_STRING);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008136 if (resultlen == 0)
8137 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008138 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008139 return NULL;
8140 }
8141 }
8142
8143 /*
8144 * '#': Alternate file name
8145 * '%': Current file name
8146 * File name under the cursor
8147 * File name for autocommand
8148 * and following modifiers
8149 */
8150 else
8151 {
8152 switch (spec_idx)
8153 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008154 case SPEC_PERC: // '%': current file
Bram Moolenaar071d4272004-06-13 20:20:40 +00008155 if (curbuf->b_fname == NULL)
8156 {
8157 result = (char_u *)"";
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008158 valid = 0; // Must have ":p:h" to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00008159 }
8160 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008161 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008162 result = curbuf->b_fname;
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008163 tilde_file = STRCMP(result, "~") == 0;
8164 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008165 break;
8166
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008167 case SPEC_HASH: // '#' or "#99": alternate file
8168 if (src[1] == '#') // "##": the argument list
Bram Moolenaar071d4272004-06-13 20:20:40 +00008169 {
8170 result = arg_all();
8171 resultbuf = result;
8172 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +00008173 if (escaped != NULL)
8174 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008175 skip_mod = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008176 break;
8177 }
8178 s = src + 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008179 if (*s == '<') // "#<99" uses v:oldfiles
Bram Moolenaard812df62008-11-09 12:46:09 +00008180 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008181 i = (int)getdigits(&s);
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008182 if (s == src + 2 && src[1] == '-')
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008183 // just a minus sign, don't skip over it
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008184 s--;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008185 *usedlen = (int)(s - src); // length of what we expand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008186
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008187 if (src[1] == '<' && i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008188 {
Bram Moolenaard812df62008-11-09 12:46:09 +00008189 if (*usedlen < 2)
8190 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008191 // Should we give an error message for #<text?
Bram Moolenaard812df62008-11-09 12:46:09 +00008192 *usedlen = 1;
8193 return NULL;
8194 }
8195#ifdef FEAT_EVAL
8196 result = list_find_str(get_vim_var_list(VV_OLDFILES),
8197 (long)i);
8198 if (result == NULL)
8199 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008200 *errormsg = "";
Bram Moolenaard812df62008-11-09 12:46:09 +00008201 return NULL;
8202 }
8203#else
Bram Moolenaar8e481e82019-01-15 20:07:48 +01008204 *errormsg = _("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008205 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00008206#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008207 }
8208 else
Bram Moolenaard812df62008-11-09 12:46:09 +00008209 {
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008210 if (i == 0 && src[1] == '<' && *usedlen > 1)
8211 *usedlen = 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00008212 buf = buflist_findnr(i);
8213 if (buf == NULL)
8214 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008215 *errormsg = _("E194: No alternate file name to substitute for '#'");
Bram Moolenaard812df62008-11-09 12:46:09 +00008216 return NULL;
8217 }
8218 if (lnump != NULL)
8219 *lnump = ECMD_LAST;
8220 if (buf->b_fname == NULL)
8221 {
8222 result = (char_u *)"";
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008223 valid = 0; // Must have ":p:h" to be valid
Bram Moolenaard812df62008-11-09 12:46:09 +00008224 }
8225 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008226 {
Bram Moolenaard812df62008-11-09 12:46:09 +00008227 result = buf->b_fname;
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008228 tilde_file = STRCMP(result, "~") == 0;
8229 }
Bram Moolenaard812df62008-11-09 12:46:09 +00008230 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008231 break;
8232
8233#ifdef FEAT_SEARCHPATH
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008234 case SPEC_CFILE: // file name under cursor
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008235 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008236 if (result == NULL)
8237 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008238 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008239 return NULL;
8240 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008241 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00008242 break;
8243#endif
8244
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008245 case SPEC_AFILE: // file name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008246 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +00008247 if (result != NULL && !autocmd_fname_full)
8248 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008249 // Still need to turn the fname into a full path. It is
8250 // postponed to avoid a delay when <afile> is not used.
Bram Moolenaarf6dad432008-09-18 19:29:58 +00008251 autocmd_fname_full = TRUE;
8252 result = FullName_save(autocmd_fname, FALSE);
8253 vim_free(autocmd_fname);
8254 autocmd_fname = result;
8255 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008256 if (result == NULL)
8257 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008258 *errormsg = _("E495: no autocommand file name to substitute for \"<afile>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008259 return NULL;
8260 }
Bram Moolenaara0174af2008-01-02 20:08:25 +00008261 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008262 break;
8263
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008264 case SPEC_ABUF: // buffer number for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008265 if (autocmd_bufnr <= 0)
8266 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008267 *errormsg = _("E496: no autocommand buffer number to substitute for \"<abuf>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008268 return NULL;
8269 }
8270 sprintf((char *)strbuf, "%d", autocmd_bufnr);
8271 result = strbuf;
8272 break;
8273
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008274 case SPEC_AMATCH: // match name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008275 result = autocmd_match;
8276 if (result == NULL)
8277 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008278 *errormsg = _("E497: no autocommand match name to substitute for \"<amatch>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008279 return NULL;
8280 }
8281 break;
8282
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008283 case SPEC_SFILE: // file name for ":so" command
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008284 result = estack_sfile();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008285 if (result == NULL)
8286 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008287 *errormsg = _("E498: no :source file name to substitute for \"<sfile>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008288 return NULL;
8289 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008290 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00008291 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008292
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008293 case SPEC_SLNUM: // line in file for ":so" command
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008294 if (SOURCING_NAME == NULL || SOURCING_LNUM == 0)
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008295 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008296 *errormsg = _("E842: no line number to use for \"<slnum>\"");
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008297 return NULL;
8298 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008299 sprintf((char *)strbuf, "%ld", SOURCING_LNUM);
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008300 result = strbuf;
8301 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008302
8303#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008304 case SPEC_SFLNUM: // line in script file
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008305 if (current_sctx.sc_lnum + SOURCING_LNUM == 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008306 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008307 *errormsg = _("E961: no line number to use for \"<sflnum>\"");
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008308 return NULL;
8309 }
8310 sprintf((char *)strbuf, "%ld",
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008311 (long)(current_sctx.sc_lnum + SOURCING_LNUM));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008312 result = strbuf;
8313 break;
8314#endif
8315
8316#ifdef FEAT_CLIENTSERVER
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008317 case SPEC_CLIENT: // Source of last submitted input
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008318 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
8319 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008320 result = strbuf;
8321 break;
8322#endif
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008323
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02008324 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008325 result = (char_u *)""; // avoid gcc warning
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02008326 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008327 }
8328
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008329 resultlen = (int)STRLEN(result); // length of new string
8330 if (src[*usedlen] == '<') // remove the file name extension
Bram Moolenaar071d4272004-06-13 20:20:40 +00008331 {
8332 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008333 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008334 resultlen = (int)(s - result);
8335 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008336 else if (!skip_mod)
8337 {
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008338 valid |= modify_fname(src, tilde_file, usedlen, &result, &resultbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008339 &resultlen);
8340 if (result == NULL)
8341 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008342 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008343 return NULL;
8344 }
8345 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008346 }
8347
8348 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
8349 {
8350 if (valid != VALID_HEAD + VALID_PATH)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008351 // xgettext:no-c-format
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008352 *errormsg = _("E499: Empty file name for '%' or '#', only works with \":p:h\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008353 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008354 *errormsg = _("E500: Evaluates to an empty string");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008355 result = NULL;
8356 }
8357 else
8358 result = vim_strnsave(result, resultlen);
8359 vim_free(resultbuf);
8360 return result;
8361}
8362
8363/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008364 * Expand the <sfile> string in "arg".
8365 *
8366 * Returns an allocated string, or NULL for any error.
8367 */
8368 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008369expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008370{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008371 char *errormsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008372 int len;
8373 char_u *result;
8374 char_u *newres;
8375 char_u *repl;
8376 int srclen;
8377 char_u *p;
8378
8379 result = vim_strsave(arg);
8380 if (result == NULL)
8381 return NULL;
8382
8383 for (p = result; *p; )
8384 {
8385 if (STRNCMP(p, "<sfile>", 7) != 0)
8386 ++p;
8387 else
8388 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008389 // replace "<sfile>" with the sourced file name, and do ":" stuff
Bram Moolenaar63b92542007-03-27 14:57:09 +00008390 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008391 if (errormsg != NULL)
8392 {
8393 if (*errormsg)
8394 emsg(errormsg);
8395 vim_free(result);
8396 return NULL;
8397 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008398 if (repl == NULL) // no match (cannot happen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008399 {
8400 p += srclen;
8401 continue;
8402 }
8403 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
8404 newres = alloc(len);
8405 if (newres == NULL)
8406 {
8407 vim_free(repl);
8408 vim_free(result);
8409 return NULL;
8410 }
8411 mch_memmove(newres, result, (size_t)(p - result));
8412 STRCPY(newres + (p - result), repl);
8413 len = (int)STRLEN(newres);
8414 STRCAT(newres, p + srclen);
8415 vim_free(repl);
8416 vim_free(result);
8417 result = newres;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008418 p = newres + len; // continue after the match
Bram Moolenaar071d4272004-06-13 20:20:40 +00008419 }
8420 }
8421
8422 return result;
8423}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008424
Bram Moolenaar071d4272004-06-13 20:20:40 +00008425#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00008426/*
Bram Moolenaard9462e32011-04-11 21:35:11 +02008427 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +00008428 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +00008429 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008430 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008431dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008432{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008433 if (fname == NULL)
8434 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +02008435 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008436}
8437#endif
8438
8439/*
8440 * ":behave {mswin,xterm}"
8441 */
8442 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008443ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008444{
8445 if (STRCMP(eap->arg, "mswin") == 0)
8446 {
8447 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
8448 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
8449 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
8450 set_option_value((char_u *)"keymodel", 0L,
8451 (char_u *)"startsel,stopsel", 0);
8452 }
8453 else if (STRCMP(eap->arg, "xterm") == 0)
8454 {
8455 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
8456 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
8457 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
8458 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
8459 }
8460 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008461 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008462}
8463
Bram Moolenaar071d4272004-06-13 20:20:40 +00008464static int filetype_detect = FALSE;
8465static int filetype_plugin = FALSE;
8466static int filetype_indent = FALSE;
8467
8468/*
8469 * ":filetype [plugin] [indent] {on,off,detect}"
8470 * on: Load the filetype.vim file to install autocommands for file types.
8471 * off: Load the ftoff.vim file to remove all autocommands for file types.
8472 * plugin on: load filetype.vim and ftplugin.vim
8473 * plugin off: load ftplugof.vim
8474 * indent on: load filetype.vim and indent.vim
8475 * indent off: load indoff.vim
8476 */
8477 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008478ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008479{
8480 char_u *arg = eap->arg;
8481 int plugin = FALSE;
8482 int indent = FALSE;
8483
8484 if (*eap->arg == NUL)
8485 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008486 // Print current status.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008487 smsg("filetype detection:%s plugin:%s indent:%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00008488 filetype_detect ? "ON" : "OFF",
8489 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
8490 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
8491 return;
8492 }
8493
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008494 // Accept "plugin" and "indent" in any order.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008495 for (;;)
8496 {
8497 if (STRNCMP(arg, "plugin", 6) == 0)
8498 {
8499 plugin = TRUE;
8500 arg = skipwhite(arg + 6);
8501 continue;
8502 }
8503 if (STRNCMP(arg, "indent", 6) == 0)
8504 {
8505 indent = TRUE;
8506 arg = skipwhite(arg + 6);
8507 continue;
8508 }
8509 break;
8510 }
8511 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
8512 {
8513 if (*arg == 'o' || !filetype_detect)
8514 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008515 source_runtime((char_u *)FILETYPE_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008516 filetype_detect = TRUE;
8517 if (plugin)
8518 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008519 source_runtime((char_u *)FTPLUGIN_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008520 filetype_plugin = TRUE;
8521 }
8522 if (indent)
8523 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008524 source_runtime((char_u *)INDENT_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008525 filetype_indent = TRUE;
8526 }
8527 }
8528 if (*arg == 'd')
8529 {
Bram Moolenaar1610d052016-06-09 22:53:01 +02008530 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +00008531 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008532 }
8533 }
8534 else if (STRCMP(arg, "off") == 0)
8535 {
8536 if (plugin || indent)
8537 {
8538 if (plugin)
8539 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008540 source_runtime((char_u *)FTPLUGOF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008541 filetype_plugin = FALSE;
8542 }
8543 if (indent)
8544 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008545 source_runtime((char_u *)INDOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008546 filetype_indent = FALSE;
8547 }
8548 }
8549 else
8550 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008551 source_runtime((char_u *)FTOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008552 filetype_detect = FALSE;
8553 }
8554 }
8555 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008556 semsg(_(e_invarg2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008557}
8558
8559/*
Bram Moolenaar3e545692017-06-04 19:00:32 +02008560 * ":setfiletype [FALLBACK] {name}"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008561 */
8562 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008563ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008564{
8565 if (!did_filetype)
Bram Moolenaar3e545692017-06-04 19:00:32 +02008566 {
8567 char_u *arg = eap->arg;
8568
8569 if (STRNCMP(arg, "FALLBACK ", 9) == 0)
8570 arg += 9;
8571
8572 set_option_value((char_u *)"filetype", 0L, arg, OPT_LOCAL);
8573 if (arg != eap->arg)
8574 did_filetype = FALSE;
8575 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008576}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008577
Bram Moolenaar071d4272004-06-13 20:20:40 +00008578 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008579ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008580{
8581#ifdef FEAT_DIGRAPHS
8582 if (*eap->arg != NUL)
8583 putdigraph(eap->arg);
8584 else
Bram Moolenaareae8ae12018-12-14 18:53:02 +01008585 listdigraphs(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008586#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008587 emsg(_("E196: No digraphs in this version"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008588#endif
8589}
8590
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02008591#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
8592 void
8593set_no_hlsearch(int flag)
8594{
8595 no_hlsearch = flag;
8596# ifdef FEAT_EVAL
8597 set_vim_var_nr(VV_HLSEARCH, !no_hlsearch && p_hls);
8598# endif
8599}
8600
Bram Moolenaar071d4272004-06-13 20:20:40 +00008601/*
8602 * ":nohlsearch"
8603 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008604 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008605ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008606{
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02008607 set_no_hlsearch(TRUE);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00008608 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008609}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008610#endif
8611
8612#ifdef FEAT_CRYPT
8613/*
8614 * ":X": Get crypt key
8615 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008616 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008617ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008618{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +01008619 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02008620 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008621}
8622#endif
8623
8624#ifdef FEAT_FOLDING
8625 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008626ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008627{
8628 if (foldManualAllowed(TRUE))
8629 foldCreate(eap->line1, eap->line2);
8630}
8631
8632 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008633ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008634{
8635 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
8636 eap->forceit, FALSE);
8637}
8638
8639 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008640ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008641{
8642 linenr_T lnum;
8643
Bram Moolenaar4facea32019-10-12 20:17:40 +02008644# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02008645 start_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +02008646# endif
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02008647
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008648 // First set the marks for all lines closed/open.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008649 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
8650 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
8651 ml_setmarked(lnum);
8652
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008653 // Execute the command on the marked lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008654 global_exe(eap->arg);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008655 ml_clearmarked(); // clear rest of the marks
Bram Moolenaar4facea32019-10-12 20:17:40 +02008656# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02008657 end_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +02008658# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008659}
8660#endif
Bram Moolenaarf5291f32017-09-14 22:55:37 +02008661
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008662#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar39665952018-08-15 20:59:48 +02008663/*
8664 * Returns TRUE if the supplied Ex cmdidx is for a location list command
8665 * instead of a quickfix command.
8666 */
8667 int
8668is_loclist_cmd(int cmdidx)
8669{
Bram Moolenaar74c8be22018-08-23 22:51:40 +02008670 if (cmdidx < 0 || cmdidx >= CMD_SIZE)
Bram Moolenaar39665952018-08-15 20:59:48 +02008671 return FALSE;
8672 return cmdnames[cmdidx].cmd_name[0] == 'l';
8673}
8674#endif
8675
Bram Moolenaar4facea32019-10-12 20:17:40 +02008676#if defined(FEAT_TIMERS) || defined(PROTO)
Bram Moolenaarf5291f32017-09-14 22:55:37 +02008677 int
8678get_pressedreturn(void)
8679{
8680 return ex_pressedreturn;
8681}
8682
8683 void
8684set_pressedreturn(int val)
8685{
8686 ex_pressedreturn = val;
8687}
8688#endif