blob: 62cab6e22f85812eb2c2189f9144324ccfc0fb2d [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 Moolenaare31ee862020-01-07 20:59:34 +0100650 ESTACK_CHECK_DECLARATION
Bram Moolenaar071d4272004-06-13 20:20:40 +0000651
652#ifdef FEAT_EVAL
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 Moolenaar69b67f72015-09-25 16:59:47 +0200731 vim_memset(&debug_saved, 0, sizeof(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
1298 * converted any more, here.) This enables also the interrupt message
1299 * 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 Moolenaar217e1b82019-12-01 21:41:28 +01001349#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350
1351 /*
1352 * If there was too much output to fit on the command line, ask the user to
1353 * hit return before redrawing the screen. With the ":global" command we do
1354 * this only once after the command is finished.
1355 */
1356 if (did_inc)
1357 {
1358 --RedrawingDisabled;
1359 --no_wait_return;
1360 msg_scroll = FALSE;
1361
1362 /*
1363 * When just finished an ":if"-":else" which was typed, no need to
1364 * wait for hit-return. Also for an error situation.
1365 */
1366 if (retval == FAIL
1367#ifdef FEAT_EVAL
1368 || (did_endif && KeyTyped && !did_emsg)
1369#endif
1370 )
1371 {
1372 need_wait_return = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001373 msg_didany = FALSE; // don't wait when restarting edit
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374 }
1375 else if (need_wait_return)
1376 {
1377 /*
1378 * The msg_start() above clears msg_didout. The wait_return we do
1379 * here should not overwrite the command that may be shown before
1380 * doing that.
1381 */
1382 msg_didout |= msg_didout_before_start;
1383 wait_return(FALSE);
1384 }
1385 }
1386
Bram Moolenaar2a942252012-11-28 23:03:07 +01001387#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001388 did_endif = FALSE; // in case do_cmdline used recursively
Bram Moolenaar2a942252012-11-28 23:03:07 +01001389#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390 /*
1391 * Reset if_level, in case a sourced script file contains more ":if" than
1392 * ":endif" (could be ":if x | foo | endif").
1393 */
1394 if_level = 0;
Bram Moolenaar2e18a122012-11-28 19:10:54 +01001395#endif
Bram Moolenaard4ad0d42012-11-28 17:34:48 +01001396
Bram Moolenaar071d4272004-06-13 20:20:40 +00001397 --call_depth;
1398 return retval;
1399}
1400
1401#ifdef FEAT_EVAL
1402/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001403 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404 */
1405 static char_u *
Bram Moolenaare96a2492019-06-25 04:12:16 +02001406get_loop_line(int c, void *cookie, int indent, int do_concat)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001408 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001409 wcmd_T *wp;
1410 char_u *line;
1411
1412 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1413 {
1414 if (cp->repeating)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001415 return NULL; // trying to read past ":endwhile"/":endfor"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001417 // First time inside the ":while"/":for": get line normally.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418 if (cp->getline == NULL)
Bram Moolenaare96a2492019-06-25 04:12:16 +02001419 line = getcmdline(c, 0L, indent, do_concat);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420 else
Bram Moolenaare96a2492019-06-25 04:12:16 +02001421 line = cp->getline(c, cp->cookie, indent, do_concat);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001422 if (line != NULL && store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423 ++cp->current_line;
1424
1425 return line;
1426 }
1427
1428 KeyTyped = FALSE;
1429 ++cp->current_line;
1430 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001431 SOURCING_LNUM = wp->lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432 return vim_strsave(wp->line);
1433}
1434
1435/*
1436 * Store a line in "gap" so that a ":while" loop can execute it again.
1437 */
1438 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001439store_loop_line(garray_T *gap, char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001440{
1441 if (ga_grow(gap, 1) == FAIL)
1442 return FAIL;
1443 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001444 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = SOURCING_LNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001445 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001446 return OK;
1447}
1448
1449/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001450 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001451 */
1452 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001453free_cmdlines(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001454{
1455 while (gap->ga_len > 0)
1456 {
1457 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1458 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001459 }
1460}
1461#endif
1462
1463/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001464 * If "fgetline" is get_loop_line(), return TRUE if the getline it uses equals
1465 * "func". * Otherwise return TRUE when "fgetline" equals "func".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001467 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001468getline_equal(
Bram Moolenaare96a2492019-06-25 04:12:16 +02001469 char_u *(*fgetline)(int, void *, int, int),
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001470 void *cookie UNUSED, // argument for fgetline()
Bram Moolenaare96a2492019-06-25 04:12:16 +02001471 char_u *(*func)(int, void *, int, int))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472{
1473#ifdef FEAT_EVAL
Bram Moolenaare96a2492019-06-25 04:12:16 +02001474 char_u *(*gp)(int, void *, int, int);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001475 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001477 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1478 // function that's originally used to obtain the lines. This may be
1479 // nested several levels.
Bram Moolenaar89d40322006-08-29 15:30:07 +00001480 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001481 cp = (struct loop_cookie *)cookie;
1482 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483 {
1484 gp = cp->getline;
1485 cp = cp->cookie;
1486 }
1487 return gp == func;
1488#else
Bram Moolenaar89d40322006-08-29 15:30:07 +00001489 return fgetline == func;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490#endif
1491}
1492
Bram Moolenaar071d4272004-06-13 20:20:40 +00001493/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001494 * If "fgetline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001495 * getline function. Otherwise return "cookie".
1496 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001497 void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001498getline_cookie(
Bram Moolenaare96a2492019-06-25 04:12:16 +02001499 char_u *(*fgetline)(int, void *, int, int) UNUSED,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001500 void *cookie) // argument for fgetline()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501{
Bram Moolenaar13505972019-01-24 15:04:48 +01001502#ifdef FEAT_EVAL
Bram Moolenaare96a2492019-06-25 04:12:16 +02001503 char_u *(*gp)(int, void *, int, int);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001504 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001505
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001506 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1507 // cookie that's originally used to obtain the lines. This may be nested
1508 // several levels.
Bram Moolenaar89d40322006-08-29 15:30:07 +00001509 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001510 cp = (struct loop_cookie *)cookie;
1511 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001512 {
1513 gp = cp->getline;
1514 cp = cp->cookie;
1515 }
1516 return cp;
Bram Moolenaar13505972019-01-24 15:04:48 +01001517#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518 return cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001519#endif
Bram Moolenaar13505972019-01-24 15:04:48 +01001520}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001521
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001522
1523/*
1524 * Helper function to apply an offset for buffer commands, i.e. ":bdelete",
1525 * ":bwipeout", etc.
1526 * Returns the buffer number.
1527 */
1528 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001529compute_buffer_local_count(int addr_type, int lnum, int offset)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001530{
1531 buf_T *buf;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001532 buf_T *nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001533 int count = offset;
1534
1535 buf = firstbuf;
1536 while (buf->b_next != NULL && buf->b_fnum < lnum)
1537 buf = buf->b_next;
1538 while (count != 0)
1539 {
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001540 count += (offset < 0) ? 1 : -1;
1541 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1542 if (nextbuf == NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001543 break;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001544 buf = nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001545 if (addr_type == ADDR_LOADED_BUFFERS)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001546 // skip over unloaded buffers
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001547 while (buf->b_ml.ml_mfp == NULL)
1548 {
1549 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1550 if (nextbuf == NULL)
1551 break;
1552 buf = nextbuf;
1553 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001554 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001555 // we might have gone too far, last buffer is not loadedd
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001556 if (addr_type == ADDR_LOADED_BUFFERS)
1557 while (buf->b_ml.ml_mfp == NULL)
1558 {
1559 nextbuf = (offset >= 0) ? buf->b_prev : buf->b_next;
1560 if (nextbuf == NULL)
1561 break;
1562 buf = nextbuf;
1563 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001564 return buf->b_fnum;
1565}
1566
Bram Moolenaarb7316892019-05-01 18:08:42 +02001567/*
1568 * Return the window number of "win".
1569 * When "win" is NULL return the number of windows.
1570 */
Bram Moolenaarf240e182014-11-27 18:33:02 +01001571 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001572current_win_nr(win_T *win)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001573{
1574 win_T *wp;
1575 int nr = 0;
1576
Bram Moolenaar29323592016-07-24 22:04:11 +02001577 FOR_ALL_WINDOWS(wp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001578 {
1579 ++nr;
1580 if (wp == win)
1581 break;
1582 }
1583 return nr;
1584}
1585
1586 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001587current_tab_nr(tabpage_T *tab)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001588{
1589 tabpage_T *tp;
1590 int nr = 0;
1591
Bram Moolenaar29323592016-07-24 22:04:11 +02001592 FOR_ALL_TABPAGES(tp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001593 {
1594 ++nr;
1595 if (tp == tab)
1596 break;
1597 }
1598 return nr;
1599}
1600
1601# define CURRENT_WIN_NR current_win_nr(curwin)
1602# define LAST_WIN_NR current_win_nr(NULL)
1603# define CURRENT_TAB_NR current_tab_nr(curtab)
1604# define LAST_TAB_NR current_tab_nr(NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001605
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606/*
1607 * Execute one Ex command.
1608 *
1609 * If 'sourcing' is TRUE, the command will be included in the error message.
1610 *
1611 * 1. skip comment lines and leading space
1612 * 2. handle command modifiers
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001613 * 3. find the command
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001614 * 4. parse range
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001615 * 5. Parse the command.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001616 * 6. parse arguments
1617 * 7. switch on command name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618 *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001619 * Note: "fgetline" can be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620 *
1621 * This function may be called recursively!
1622 */
1623#if (_MSC_VER == 1200)
1624/*
Bram Moolenaared203462004-06-16 11:19:22 +00001625 * Avoid optimisation bug in VC++ version 6.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626 */
Bram Moolenaar281bdce2005-01-25 21:53:18 +00001627 #pragma optimize( "g", off )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628#endif
1629 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001630do_one_cmd(
Bram Moolenaarddef1292019-12-16 17:10:33 +01001631 char_u **cmdlinep,
1632 int sourcing,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633#ifdef FEAT_EVAL
Bram Moolenaarddef1292019-12-16 17:10:33 +01001634 cstack_T *cstack,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001635#endif
Bram Moolenaarddef1292019-12-16 17:10:33 +01001636 char_u *(*fgetline)(int, void *, int, int),
1637 void *cookie) // argument for fgetline()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638{
Bram Moolenaarddef1292019-12-16 17:10:33 +01001639 char_u *p;
1640 linenr_T lnum;
1641 long n;
1642 char *errormsg = NULL; // error message
1643 char_u *after_modifier = NULL;
1644 exarg_T ea; // Ex command arguments
1645 int save_msg_scroll = msg_scroll;
1646 cmdmod_T save_cmdmod;
1647 int save_reg_executing = reg_executing;
1648 int ni; // set when Not Implemented
1649 char_u *cmd;
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001650#ifdef FEAT_EVAL
1651 int starts_with_colon;
1652#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653
1654 vim_memset(&ea, 0, sizeof(ea));
1655 ea.line1 = 1;
1656 ea.line2 = 1;
1657#ifdef FEAT_EVAL
1658 ++ex_nesting_level;
1659#endif
1660
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001661 // When the last file has not been edited :q has to be typed twice.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001662 if (quitmore
1663#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001664 // avoid that a function call in 'statusline' does this
Bram Moolenaar89d40322006-08-29 15:30:07 +00001665 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001666#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001667 // avoid that an autocommand, e.g. QuitPre, does this
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001668 && !getline_equal(fgetline, cookie, getnextac))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669 --quitmore;
1670
1671 /*
1672 * Reset browse, confirm, etc.. They are restored when returning, for
1673 * recursive calls.
1674 */
1675 save_cmdmod = cmdmod;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001676
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001677 // "#!anything" is handled like a comment.
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001678 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1679 goto doend;
1680
Bram Moolenaar4facea32019-10-12 20:17:40 +02001681 if (p_verbose >= 16)
1682 msg_verbose_cmd(0, *cmdlinep);
1683
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02001684/*
1685 * 1. Skip comment lines and leading white space and colons.
1686 * 2. Handle command modifiers.
1687 */
1688 // The "ea" structure holds the arguments that can be used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689 ea.cmd = *cmdlinep;
Bram Moolenaareffed932018-08-14 13:38:17 +02001690 ea.cmdlinep = cmdlinep;
1691 ea.getline = fgetline;
1692 ea.cookie = cookie;
1693#ifdef FEAT_EVAL
1694 ea.cstack = cstack;
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001695 starts_with_colon = *skipwhite(ea.cmd) == ':';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001696#endif
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02001697 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaareffed932018-08-14 13:38:17 +02001698 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001699
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001700 after_modifier = ea.cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701
1702#ifdef FEAT_EVAL
1703 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
1704 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
1705#else
1706 ea.skip = (if_level > 0);
1707#endif
1708
Bram Moolenaar071d4272004-06-13 20:20:40 +00001709/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001710 * 3. Skip over the range to find the command. Let "p" point to after it.
1711 *
1712 * We need the command to know what kind of range it uses.
1713 */
1714 cmd = ea.cmd;
1715 ea.cmd = skip_range(ea.cmd, NULL);
1716 if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
1717 ea.cmd = skipwhite(ea.cmd + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001718
1719#ifdef FEAT_EVAL
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001720 if (current_sctx.sc_version == SCRIPT_VERSION_VIM9 && !starts_with_colon)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001721 p = find_ex_command(&ea, NULL, lookup_scriptvar, NULL);
1722 else
1723#endif
1724 p = find_ex_command(&ea, NULL, NULL, NULL);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001725
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001726#ifdef FEAT_EVAL
1727# ifdef FEAT_PROFILE
1728 // Count this line for profiling if skip is TRUE.
1729 if (do_profiling == PROF_YES
1730 && (!ea.skip || cstack->cs_idx == 0 || (cstack->cs_idx > 0
1731 && (cstack->cs_flags[cstack->cs_idx - 1] & CSF_ACTIVE))))
1732 {
1733 int skip = did_emsg || got_int || did_throw;
1734
1735 if (ea.cmdidx == CMD_catch)
1736 skip = !skip && !(cstack->cs_idx >= 0
1737 && (cstack->cs_flags[cstack->cs_idx] & CSF_THROWN)
1738 && !(cstack->cs_flags[cstack->cs_idx] & CSF_CAUGHT));
1739 else if (ea.cmdidx == CMD_else || ea.cmdidx == CMD_elseif)
1740 skip = skip || !(cstack->cs_idx >= 0
1741 && !(cstack->cs_flags[cstack->cs_idx]
1742 & (CSF_ACTIVE | CSF_TRUE)));
1743 else if (ea.cmdidx == CMD_finally)
1744 skip = FALSE;
1745 else if (ea.cmdidx != CMD_endif
1746 && ea.cmdidx != CMD_endfor
1747 && ea.cmdidx != CMD_endtry
1748 && ea.cmdidx != CMD_endwhile)
1749 skip = ea.skip;
1750
1751 if (!skip)
1752 {
1753 if (getline_equal(fgetline, cookie, get_func_line))
1754 func_line_exec(getline_cookie(fgetline, cookie));
1755 else if (getline_equal(fgetline, cookie, getsourceline))
1756 script_line_exec();
1757 }
1758 }
1759# endif
1760
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001761 // May go to debug mode. If this happens and the ">quit" debug command is
1762 // used, throw an interrupt exception and skip the next command.
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001763 dbg_check_breakpoint(&ea);
1764 if (!ea.skip && got_int)
1765 {
1766 ea.skip = TRUE;
1767 (void)do_intthrow(cstack);
1768 }
1769#endif
1770
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001771/*
1772 * 4. parse a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00001773 *
1774 * where 'addr' is:
1775 *
1776 * % (entire file)
1777 * $ [+-NUM]
1778 * 'x [+-NUM] (where x denotes a currently defined mark)
1779 * . [+-NUM]
1780 * [+-NUM]..
1781 * NUM
1782 *
1783 * The ea.cmd pointer is updated to point to the first character following the
1784 * range spec. If an initial address is found, but no second, the upper bound
1785 * is equal to the lower.
1786 */
1787
Bram Moolenaar25190db2019-05-04 15:05:28 +02001788 // ea.addr_type for user commands is set by find_ucmd
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001789 if (!IS_USER_CMDIDX(ea.cmdidx))
1790 {
1791 if (ea.cmdidx != CMD_SIZE)
1792 ea.addr_type = cmdnames[(int)ea.cmdidx].cmd_addr_type;
1793 else
1794 ea.addr_type = ADDR_LINES;
1795
Bram Moolenaar25190db2019-05-04 15:05:28 +02001796 // :wincmd range depends on the argument.
Bram Moolenaar164f3262015-01-14 21:22:01 +01001797 if (ea.cmdidx == CMD_wincmd && p != NULL)
1798 get_wincmd_addr_type(skipwhite(p), &ea);
Bram Moolenaar25190db2019-05-04 15:05:28 +02001799#ifdef FEAT_QUICKFIX
1800 // :.cc in quickfix window uses line number
1801 if ((ea.cmdidx == CMD_cc || ea.cmdidx == CMD_ll) && bt_quickfix(curbuf))
1802 ea.addr_type = ADDR_OTHER;
1803#endif
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001804 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001805
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001806 ea.cmd = cmd;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02001807 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaaree8415b2018-08-10 23:13:12 +02001808 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001811 * 5. Parse the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001812 */
1813
1814 /*
1815 * Skip ':' and any white space
1816 */
1817 ea.cmd = skipwhite(ea.cmd);
1818 while (*ea.cmd == ':')
1819 ea.cmd = skipwhite(ea.cmd + 1);
1820
1821 /*
1822 * If we got a line, but no command, then go to the line.
1823 * If we find a '|' or '\n' we set ea.nextcmd.
1824 */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001825 if (*ea.cmd == NUL || *ea.cmd == '"'
1826 || (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001827 {
1828 /*
1829 * strange vi behaviour:
1830 * ":3" jumps to line 3
1831 * ":3|..." prints line 3
1832 * ":|" prints current line
1833 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001834 if (ea.skip) // skip this if inside :if
Bram Moolenaar071d4272004-06-13 20:20:40 +00001835 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001836 if (*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001837 {
1838 ea.cmdidx = CMD_print;
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001839 ea.argt = EX_RANGE+EX_COUNT+EX_TRLBAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840 if ((errormsg = invalid_range(&ea)) == NULL)
1841 {
1842 correct_range(&ea);
1843 ex_print(&ea);
1844 }
1845 }
1846 else if (ea.addr_count != 0)
1847 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001848 if (ea.line2 > curbuf->b_ml.ml_line_count)
1849 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001850 // With '-' in 'cpoptions' a line number past the file is an
1851 // error, otherwise put it at the end of the file.
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001852 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
1853 ea.line2 = -1;
1854 else
1855 ea.line2 = curbuf->b_ml.ml_line_count;
1856 }
1857
1858 if (ea.line2 < 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001859 errormsg = _(e_invrange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001860 else
1861 {
1862 if (ea.line2 == 0)
1863 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001864 else
1865 curwin->w_cursor.lnum = ea.line2;
1866 beginline(BL_SOL | BL_FIX);
1867 }
1868 }
1869 goto doend;
1870 }
1871
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001872 // If this looks like an undefined user command and there are CmdUndefined
1873 // autocommands defined, trigger the matching autocommands.
Bram Moolenaard5005162014-08-22 23:05:54 +02001874 if (p != NULL && ea.cmdidx == CMD_SIZE && !ea.skip
1875 && ASCII_ISUPPER(*ea.cmd)
1876 && has_cmdundefined())
1877 {
Bram Moolenaard5005162014-08-22 23:05:54 +02001878 int ret;
1879
Bram Moolenaar5a31b462014-08-23 14:16:20 +02001880 p = ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02001881 while (ASCII_ISALNUM(*p))
1882 ++p;
Bram Moolenaar120f4a82014-09-09 12:22:06 +02001883 p = vim_strnsave(ea.cmd, (int)(p - ea.cmd));
Bram Moolenaard5005162014-08-22 23:05:54 +02001884 ret = apply_autocmds(EVENT_CMDUNDEFINED, p, p, TRUE, NULL);
1885 vim_free(p);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001886 // If the autocommands did something and didn't cause an error, try
1887 // finding the command again.
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001888 p = (ret
1889#ifdef FEAT_EVAL
1890 && !aborting()
Bram Moolenaard5005162014-08-22 23:05:54 +02001891#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001892 ) ? find_ex_command(&ea, NULL, NULL, NULL) : ea.cmd;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001893 }
Bram Moolenaard5005162014-08-22 23:05:54 +02001894
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895 if (p == NULL)
1896 {
1897 if (!ea.skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001898 errormsg = _("E464: Ambiguous use of user-defined command");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001899 goto doend;
1900 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001901 // Check for wrong commands.
Bram Moolenaar6f9a4762017-06-22 20:39:17 +02001902 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78
1903 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001904 {
1905 errormsg = uc_fun_cmd();
1906 goto doend;
1907 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001908
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909 if (ea.cmdidx == CMD_SIZE)
1910 {
1911 if (!ea.skip)
1912 {
1913 STRCPY(IObuff, _("E492: Not an editor command"));
1914 if (!sourcing)
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001915 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001916 // If the modifier was parsed OK the error must be in the
1917 // following command
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001918 if (after_modifier != NULL)
1919 append_command(after_modifier);
1920 else
1921 append_command(*cmdlinep);
1922 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001923 errormsg = (char *)IObuff;
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001924 did_emsg_syntax = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001925 }
1926 goto doend;
1927 }
1928
Bram Moolenaar958636c2014-10-21 20:01:58 +02001929 ni = (!IS_USER_CMDIDX(ea.cmdidx)
1930 && (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00001931#ifdef HAVE_EX_SCRIPT_NI
1932 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
1933#endif
1934 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001935
1936#ifndef FEAT_EVAL
1937 /*
1938 * When the expression evaluation is disabled, recognize the ":if" and
1939 * ":endif" commands and ignore everything in between it.
1940 */
1941 if (ea.cmdidx == CMD_if)
1942 ++if_level;
1943 if (if_level)
1944 {
1945 if (ea.cmdidx == CMD_endif)
1946 --if_level;
1947 goto doend;
1948 }
1949
1950#endif
1951
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001952 // forced commands
Bram Moolenaar196b3b02008-06-20 16:51:41 +00001953 if (*p == '!' && ea.cmdidx != CMD_substitute
1954 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955 {
1956 ++p;
1957 ea.forceit = TRUE;
1958 }
1959 else
1960 ea.forceit = FALSE;
1961
1962/*
Bram Moolenaarb7316892019-05-01 18:08:42 +02001963 * 6. Parse arguments. Then check for errors.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001964 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02001965 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001966 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001967
1968 if (!ea.skip)
1969 {
1970#ifdef HAVE_SANDBOX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001971 if (sandbox != 0 && !(ea.argt & EX_SBOXOK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972 {
Bram Moolenaar8c62a082019-02-08 14:34:10 +01001973 // Command not allowed in sandbox.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001974 errormsg = _(e_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001975 goto doend;
1976 }
1977#endif
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001978 if (restricted != 0 && (ea.argt & EX_RESTRICT))
Bram Moolenaar8c62a082019-02-08 14:34:10 +01001979 {
1980 errormsg = _("E981: Command not allowed in rvim");
1981 goto doend;
1982 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001983 if (!curbuf->b_p_ma && (ea.argt & EX_MODIFY))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001984 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001985 // Command not allowed in non-'modifiable' buffer
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001986 errormsg = _(e_modifiable);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001987 goto doend;
1988 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001989
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001990 if (text_locked() && !(ea.argt & EX_CMDWIN)
Bram Moolenaar958636c2014-10-21 20:01:58 +02001991 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001992 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001993 // Command not allowed when editing the command line.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001994 errormsg = _(get_text_locked_msg());
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995 goto doend;
1996 }
Bram Moolenaar379fb762018-08-30 15:58:28 +02001997
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001998 // Disallow editing another buffer when "curbuf_lock" is set.
1999 // Do allow ":checktime" (it is postponed).
2000 // Do allow ":edit" (check for an argument later).
2001 // Do allow ":file" with no arguments (check for an argument later).
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002002 if (!(ea.argt & EX_CMDWIN)
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002003 && ea.cmdidx != CMD_checktime
Bram Moolenaar379fb762018-08-30 15:58:28 +02002004 && ea.cmdidx != CMD_edit
2005 && ea.cmdidx != CMD_file
Bram Moolenaar958636c2014-10-21 20:01:58 +02002006 && !IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002007 && curbuf_locked())
2008 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002010 if (!ni && !(ea.argt & EX_RANGE) && ea.addr_count > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002011 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002012 // no range allowed
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002013 errormsg = _(e_norange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002014 goto doend;
2015 }
2016 }
2017
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002018 if (!ni && !(ea.argt & EX_BANG) && ea.forceit) // no <!> allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002019 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002020 errormsg = _(e_nobang);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002021 goto doend;
2022 }
2023
2024 /*
2025 * Don't complain about the range if it is not used
2026 * (could happen if line_count is accidentally set to 0).
2027 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002028 if (!ea.skip && !ni && (ea.argt & EX_RANGE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002029 {
2030 /*
2031 * If the range is backwards, ask for confirmation and, if given, swap
2032 * ea.line1 & ea.line2 so it's forwards again.
2033 * When global command is busy, don't ask, will fail below.
2034 */
2035 if (!global_busy && ea.line1 > ea.line2)
2036 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002037 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002038 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002039 if (sourcing || exmode_active)
2040 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002041 errormsg = _("E493: Backwards range given");
Bram Moolenaara5792f52005-11-23 21:25:05 +00002042 goto doend;
2043 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044 if (ask_yesno((char_u *)
2045 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002046 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002047 }
2048 lnum = ea.line1;
2049 ea.line1 = ea.line2;
2050 ea.line2 = lnum;
2051 }
2052 if ((errormsg = invalid_range(&ea)) != NULL)
2053 goto doend;
2054 }
2055
Bram Moolenaarb7316892019-05-01 18:08:42 +02002056 if ((ea.addr_type == ADDR_OTHER) && ea.addr_count == 0)
2057 // default is 1, not cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002058 ea.line2 = 1;
2059
2060 correct_range(&ea);
2061
2062#ifdef FEAT_FOLDING
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002063 if (((ea.argt & EX_WHOLEFOLD) || ea.addr_count >= 2) && !global_busy
Bram Moolenaara3306952016-01-02 21:41:06 +01002064 && ea.addr_type == ADDR_LINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002065 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002066 // Put the first line at the start of a closed fold, put the last line
2067 // at the end of a closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002068 (void)hasFolding(ea.line1, &ea.line1, NULL);
2069 (void)hasFolding(ea.line2, NULL, &ea.line2);
2070 }
2071#endif
2072
2073#ifdef FEAT_QUICKFIX
2074 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002075 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002076 * option here, so things like % get expanded.
2077 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002078 p = replace_makeprg(&ea, p, cmdlinep);
2079 if (p == NULL)
2080 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002081#endif
2082
2083 /*
2084 * Skip to start of argument.
2085 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2086 */
2087 if (ea.cmdidx == CMD_bang)
2088 ea.arg = p;
2089 else
2090 ea.arg = skipwhite(p);
2091
Bram Moolenaar379fb762018-08-30 15:58:28 +02002092 // ":file" cannot be run with an argument when "curbuf_lock" is set
2093 if (ea.cmdidx == CMD_file && *ea.arg != NUL && curbuf_locked())
2094 goto doend;
2095
Bram Moolenaar071d4272004-06-13 20:20:40 +00002096 /*
2097 * Check for "++opt=val" argument.
2098 * Must be first, allow ":w ++enc=utf8 !cmd"
2099 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002100 if (ea.argt & EX_ARGOPT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002101 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2102 if (getargopt(&ea) == FAIL && !ni)
2103 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002104 errormsg = _(e_invarg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002105 goto doend;
2106 }
2107
2108 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2109 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002110 if (*ea.arg == '>') // append
Bram Moolenaar071d4272004-06-13 20:20:40 +00002111 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002112 if (*++ea.arg != '>') // typed wrong
Bram Moolenaar071d4272004-06-13 20:20:40 +00002113 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002114 errormsg = _("E494: Use w or w>>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002115 goto doend;
2116 }
2117 ea.arg = skipwhite(ea.arg + 1);
2118 ea.append = TRUE;
2119 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002120 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) // :w !filter
Bram Moolenaar071d4272004-06-13 20:20:40 +00002121 {
2122 ++ea.arg;
2123 ea.usefilter = TRUE;
2124 }
2125 }
2126
2127 if (ea.cmdidx == CMD_read)
2128 {
2129 if (ea.forceit)
2130 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002131 ea.usefilter = TRUE; // :r! filter if ea.forceit
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132 ea.forceit = FALSE;
2133 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002134 else if (*ea.arg == '!') // :r !filter
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135 {
2136 ++ea.arg;
2137 ea.usefilter = TRUE;
2138 }
2139 }
2140
2141 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2142 {
2143 ea.amount = 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002144 while (*ea.arg == *ea.cmd) // count number of '>' or '<'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002145 {
2146 ++ea.arg;
2147 ++ea.amount;
2148 }
2149 ea.arg = skipwhite(ea.arg);
2150 }
2151
2152 /*
2153 * Check for "+command" argument, before checking for next command.
2154 * Don't do this for ":read !cmd" and ":write !cmd".
2155 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002156 if ((ea.argt & EX_CMDARG) && !ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002157 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2158
2159 /*
2160 * Check for '|' to separate commands and '"' to start comments.
2161 * Don't do this for ":read !cmd" and ":write !cmd".
2162 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002163 if ((ea.argt & EX_TRLBAR) && !ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164 separate_nextcmd(&ea);
2165
2166 /*
2167 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002168 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2169 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002170 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002171 else if (ea.cmdidx == CMD_bang
Bram Moolenaar67883b42017-07-27 22:57:00 +02002172 || ea.cmdidx == CMD_terminal
Bram Moolenaardf177f62005-02-22 08:39:57 +00002173 || ea.cmdidx == CMD_global
2174 || ea.cmdidx == CMD_vglobal
2175 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176 {
2177 for (p = ea.arg; *p; ++p)
2178 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002179 // Remove one backslash before a newline, so that it's possible to
2180 // pass a newline to the shell and also a newline that is preceded
2181 // with a backslash. This makes it impossible to end a shell
2182 // command in a backslash, but that doesn't appear useful.
2183 // Halving the number of backslashes is incompatible with previous
2184 // versions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002186 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187 else if (*p == '\n')
2188 {
2189 ea.nextcmd = p + 1;
2190 *p = NUL;
2191 break;
2192 }
2193 }
2194 }
2195
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002196 if ((ea.argt & EX_DFLALL) && ea.addr_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002197 {
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002198 buf_T *buf;
2199
Bram Moolenaar071d4272004-06-13 20:20:40 +00002200 ea.line1 = 1;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002201 switch (ea.addr_type)
2202 {
2203 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02002204 case ADDR_OTHER:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002205 ea.line2 = curbuf->b_ml.ml_line_count;
2206 break;
2207 case ADDR_LOADED_BUFFERS:
2208 buf = firstbuf;
2209 while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL)
2210 buf = buf->b_next;
2211 ea.line1 = buf->b_fnum;
2212 buf = lastbuf;
2213 while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL)
2214 buf = buf->b_prev;
2215 ea.line2 = buf->b_fnum;
2216 break;
2217 case ADDR_BUFFERS:
2218 ea.line1 = firstbuf->b_fnum;
2219 ea.line2 = lastbuf->b_fnum;
2220 break;
2221 case ADDR_WINDOWS:
2222 ea.line2 = LAST_WIN_NR;
2223 break;
2224 case ADDR_TABS:
2225 ea.line2 = LAST_TAB_NR;
2226 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01002227 case ADDR_TABS_RELATIVE:
2228 ea.line2 = 1;
2229 break;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002230 case ADDR_ARGUMENTS:
2231 if (ARGCOUNT == 0)
2232 ea.line1 = ea.line2 = 0;
2233 else
2234 ea.line2 = ARGCOUNT;
2235 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02002236 case ADDR_QUICKFIX_VALID:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002237#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02002238 ea.line2 = qf_get_valid_size(&ea);
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002239 if (ea.line2 == 0)
2240 ea.line2 = 1;
Bram Moolenaare906c502015-09-09 21:10:39 +02002241#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002242 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02002243 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02002244 case ADDR_UNSIGNED:
2245 case ADDR_QUICKFIX:
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002246 iemsg(_("INTERNAL: Cannot use EX_DFLALL with ADDR_NONE, ADDR_UNSIGNED or ADDR_QUICKFIX"));
Bram Moolenaarb7316892019-05-01 18:08:42 +02002247 break;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002248 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249 }
2250
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002251 // accept numbered register only when no count allowed (:put)
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002252 if ( (ea.argt & EX_REGSTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002253 && *ea.arg != NUL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002254 // Do not allow register = for user commands
Bram Moolenaar958636c2014-10-21 20:01:58 +02002255 && (!IS_USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002256 && !((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002257 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002258#ifndef FEAT_CLIPBOARD
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002259 // check these explicitly for a more specific error message
Bram Moolenaar85de2062011-05-05 14:26:41 +02002260 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002261 {
Bram Moolenaar99b12722019-01-14 20:16:40 +01002262 errormsg = _(e_invalidreg);
Bram Moolenaar85de2062011-05-05 14:26:41 +02002263 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002264 }
2265#endif
Bram Moolenaar958636c2014-10-21 20:01:58 +02002266 if (valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2267 && !IS_USER_CMDIDX(ea.cmdidx))))
Bram Moolenaar85de2062011-05-05 14:26:41 +02002268 {
2269 ea.regname = *ea.arg++;
2270#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002271 // for '=' register: accept the rest of the line as an expression
Bram Moolenaar85de2062011-05-05 14:26:41 +02002272 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2273 {
2274 set_expr_line(vim_strsave(ea.arg));
2275 ea.arg += STRLEN(ea.arg);
2276 }
2277#endif
2278 ea.arg = skipwhite(ea.arg);
2279 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002280 }
2281
2282 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002283 * Check for a count. When accepting a EX_BUFNAME, don't use "123foo" as a
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284 * count, it's a buffer name.
2285 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002286 if ((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)
2287 && (!(ea.argt & EX_BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01002288 || VIM_ISWHITE(*p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289 {
2290 n = getdigits(&ea.arg);
2291 ea.arg = skipwhite(ea.arg);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002292 if (n <= 0 && !ni && (ea.argt & EX_ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002293 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002294 errormsg = _(e_zerocount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295 goto doend;
2296 }
Bram Moolenaarb7316892019-05-01 18:08:42 +02002297 if (ea.addr_type != ADDR_LINES) // e.g. :buffer 2, :sleep 3
Bram Moolenaar071d4272004-06-13 20:20:40 +00002298 {
2299 ea.line2 = n;
2300 if (ea.addr_count == 0)
2301 ea.addr_count = 1;
2302 }
2303 else
2304 {
2305 ea.line1 = ea.line2;
2306 ea.line2 += n - 1;
2307 ++ea.addr_count;
2308 /*
2309 * Be vi compatible: no error message for out of range.
2310 */
Bram Moolenaarb7316892019-05-01 18:08:42 +02002311 if (ea.line2 > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312 ea.line2 = curbuf->b_ml.ml_line_count;
2313 }
2314 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002315
2316 /*
2317 * Check for flags: 'l', 'p' and '#'.
2318 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002319 if (ea.argt & EX_FLAGS)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002320 get_flags(&ea);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002321 if (!ni && !(ea.argt & EX_EXTRA) && *ea.arg != NUL
2322 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & EX_TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002324 // no arguments allowed but there is something
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002325 errormsg = _(e_trailing);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002326 goto doend;
2327 }
2328
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002329 if (!ni && (ea.argt & EX_NEEDARG) && *ea.arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002330 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002331 errormsg = _(e_argreq);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002332 goto doend;
2333 }
2334
2335#ifdef FEAT_EVAL
2336 /*
2337 * Skip the command when it's not going to be executed.
2338 * The commands like :if, :endif, etc. always need to be executed.
2339 * Also make an exception for commands that handle a trailing command
2340 * themselves.
2341 */
2342 if (ea.skip)
2343 {
2344 switch (ea.cmdidx)
2345 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002346 // commands that need evaluation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347 case CMD_while:
2348 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002349 case CMD_for:
2350 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351 case CMD_if:
2352 case CMD_elseif:
2353 case CMD_else:
2354 case CMD_endif:
2355 case CMD_try:
2356 case CMD_catch:
2357 case CMD_finally:
2358 case CMD_endtry:
2359 case CMD_function:
2360 break;
2361
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002362 // Commands that handle '|' themselves. Check: A command should
2363 // either have the EX_TRLBAR flag, appear in this list or appear in
2364 // the list at ":help :bar".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365 case CMD_aboveleft:
2366 case CMD_and:
2367 case CMD_belowright:
2368 case CMD_botright:
2369 case CMD_browse:
2370 case CMD_call:
2371 case CMD_confirm:
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01002372 case CMD_const:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002373 case CMD_delfunction:
2374 case CMD_djump:
2375 case CMD_dlist:
2376 case CMD_dsearch:
2377 case CMD_dsplit:
2378 case CMD_echo:
2379 case CMD_echoerr:
2380 case CMD_echomsg:
2381 case CMD_echon:
Bram Moolenaara76b3152020-02-16 16:20:21 +01002382 case CMD_eval:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002383 case CMD_execute:
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002384 case CMD_filter:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385 case CMD_help:
2386 case CMD_hide:
2387 case CMD_ijump:
2388 case CMD_ilist:
2389 case CMD_isearch:
2390 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002391 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392 case CMD_keepjumps:
2393 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01002394 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002395 case CMD_leftabove:
2396 case CMD_let:
2397 case CMD_lockmarks:
Bram Moolenaarcc4423a2019-11-26 17:05:00 +01002398 case CMD_lockvar:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002399 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002401 case CMD_mzscheme:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01002402 case CMD_noautocmd:
2403 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002404 case CMD_perl:
2405 case CMD_psearch:
2406 case CMD_python:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002407 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002408 case CMD_python3:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409 case CMD_return:
2410 case CMD_rightbelow:
2411 case CMD_ruby:
2412 case CMD_silent:
2413 case CMD_smagic:
2414 case CMD_snomagic:
2415 case CMD_substitute:
2416 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002417 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002418 case CMD_tcl:
2419 case CMD_throw:
2420 case CMD_tilde:
2421 case CMD_topleft:
2422 case CMD_unlet:
Bram Moolenaarcc4423a2019-11-26 17:05:00 +01002423 case CMD_unlockvar:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424 case CMD_verbose:
2425 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002426 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002427 break;
2428
2429 default: goto doend;
2430 }
2431 }
2432#endif
2433
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002434 if (ea.argt & EX_XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435 {
2436 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2437 goto doend;
2438 }
2439
Bram Moolenaar071d4272004-06-13 20:20:40 +00002440 /*
2441 * Accept buffer name. Cannot be used at the same time with a buffer
2442 * number. Don't do this for a user command.
2443 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002444 if ((ea.argt & EX_BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
Bram Moolenaar958636c2014-10-21 20:01:58 +02002445 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002446 {
2447 /*
2448 * :bdelete, :bwipeout and :bunload take several arguments, separated
2449 * by spaces: find next space (skipping over escaped characters).
2450 * The others take one argument: ignore trailing spaces.
2451 */
2452 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2453 || ea.cmdidx == CMD_bunload)
2454 p = skiptowhite_esc(ea.arg);
2455 else
2456 {
2457 p = ea.arg + STRLEN(ea.arg);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002458 while (p > ea.arg && VIM_ISWHITE(p[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459 --p;
2460 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002461 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & EX_BUFUNL) != 0,
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002462 FALSE, FALSE);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002463 if (ea.line2 < 0) // failed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002464 goto doend;
2465 ea.addr_count = 1;
2466 ea.arg = skipwhite(p);
2467 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002469 // The :try command saves the emsg_silent flag, reset it here when
2470 // ":silent! try" was used, it should only apply to :try itself.
Bram Moolenaareffed932018-08-14 13:38:17 +02002471 if (ea.cmdidx == CMD_try && ea.did_esilent > 0)
Bram Moolenaar2be57332018-02-13 18:05:18 +01002472 {
Bram Moolenaareffed932018-08-14 13:38:17 +02002473 emsg_silent -= ea.did_esilent;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002474 if (emsg_silent < 0)
2475 emsg_silent = 0;
Bram Moolenaareffed932018-08-14 13:38:17 +02002476 ea.did_esilent = 0;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002477 }
2478
Bram Moolenaar071d4272004-06-13 20:20:40 +00002479/*
Bram Moolenaar2be57332018-02-13 18:05:18 +01002480 * 7. Execute the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002482
Bram Moolenaar958636c2014-10-21 20:01:58 +02002483 if (IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484 {
2485 /*
2486 * Execute a user-defined command.
2487 */
2488 do_ucmd(&ea);
2489 }
2490 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491 {
2492 /*
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002493 * Call the function to execute the builtin command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494 */
2495 ea.errmsg = NULL;
2496 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2497 if (ea.errmsg != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002498 errormsg = _(ea.errmsg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002499 }
2500
2501#ifdef FEAT_EVAL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002502 // Set flag that any command was executed, used by ex_vim9script().
Bram Moolenaar16531552020-02-08 16:00:46 +01002503 if (getline_equal(ea.getline, ea.cookie, getsourceline)
2504 && current_sctx.sc_sid > 0)
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002505 SCRIPT_ITEM(current_sctx.sc_sid)->sn_had_command = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002506
Bram Moolenaar071d4272004-06-13 20:20:40 +00002507 /*
2508 * If the command just executed called do_cmdline(), any throw or ":return"
2509 * or ":finish" encountered there must also check the cstack of the still
2510 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2511 * exception, or reanimate a returned function or finished script file and
2512 * return or finish it again.
2513 */
2514 if (need_rethrow)
2515 do_throw(cstack);
2516 else if (check_cstack)
2517 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002518 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002519 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002520 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002521 && current_func_returned())
2522 do_return(&ea, TRUE, FALSE, NULL);
2523 }
2524 need_rethrow = check_cstack = FALSE;
2525#endif
2526
2527doend:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002528 if (curwin->w_cursor.lnum == 0) // can happen with zero line number
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002529 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002530 curwin->w_cursor.lnum = 1;
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002531 curwin->w_cursor.col = 0;
2532 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002533
2534 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2535 {
2536 if (sourcing)
2537 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002538 if (errormsg != (char *)IObuff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002539 {
2540 STRCPY(IObuff, errormsg);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002541 errormsg = (char *)IObuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002543 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544 }
2545 emsg(errormsg);
2546 }
2547#ifdef FEAT_EVAL
2548 do_errthrow(cstack,
Bram Moolenaar958636c2014-10-21 20:01:58 +02002549 (ea.cmdidx != CMD_SIZE && !IS_USER_CMDIDX(ea.cmdidx))
2550 ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002551#endif
2552
Bram Moolenaareffed932018-08-14 13:38:17 +02002553 if (ea.verbose_save >= 0)
2554 p_verbose = ea.verbose_save;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002555
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002556 free_cmdmod();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002557 cmdmod = save_cmdmod;
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01002558 reg_executing = save_reg_executing;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002559
Bram Moolenaareffed932018-08-14 13:38:17 +02002560 if (ea.save_msg_silent != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002562 // messages could be enabled for a serious error, need to check if the
2563 // counters don't become negative
Bram Moolenaareffed932018-08-14 13:38:17 +02002564 if (!did_emsg || msg_silent > ea.save_msg_silent)
2565 msg_silent = ea.save_msg_silent;
2566 emsg_silent -= ea.did_esilent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002567 if (emsg_silent < 0)
2568 emsg_silent = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002569 // Restore msg_scroll, it's set by file I/O commands, even when no
2570 // message is actually displayed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002571 msg_scroll = save_msg_scroll;
Bram Moolenaar77ab2802009-04-22 12:44:48 +00002572
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002573 // "silent reg" or "silent echo x" inside "redir" leaves msg_col
2574 // somewhere in the line. Put it back in the first column.
Bram Moolenaar77ab2802009-04-22 12:44:48 +00002575 if (redirecting())
2576 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002577 }
2578
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002579#ifdef HAVE_SANDBOX
Bram Moolenaareffed932018-08-14 13:38:17 +02002580 if (ea.did_sandbox)
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002581 --sandbox;
2582#endif
2583
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002584 if (ea.nextcmd && *ea.nextcmd == NUL) // not really a next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00002585 ea.nextcmd = NULL;
2586
2587#ifdef FEAT_EVAL
2588 --ex_nesting_level;
2589#endif
2590
2591 return ea.nextcmd;
2592}
2593#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00002594 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595#endif
2596
2597/*
Bram Moolenaareffed932018-08-14 13:38:17 +02002598 * Parse and skip over command modifiers:
2599 * - update eap->cmd
2600 * - store flags in "cmdmod".
2601 * - Set ex_pressedreturn for an empty command line.
2602 * - set msg_silent for ":silent"
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002603 * - set 'eventignore' to "all" for ":noautocmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002604 * - set p_verbose for ":verbose"
2605 * - Increment "sandbox" for ":sandbox"
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002606 * When "skip_only" is TRUE the global variables are not changed, except for
2607 * "cmdmod".
Bram Moolenaareffed932018-08-14 13:38:17 +02002608 * Return FAIL when the command is not to be executed.
2609 * May set "errormsg" to an error message.
2610 */
2611 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002612parse_command_modifiers(exarg_T *eap, char **errormsg, int skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002613{
2614 char_u *p;
2615
2616 vim_memset(&cmdmod, 0, sizeof(cmdmod));
2617 eap->verbose_save = -1;
2618 eap->save_msg_silent = -1;
2619
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002620 // Repeat until no more command modifiers are found.
Bram Moolenaareffed932018-08-14 13:38:17 +02002621 for (;;)
2622 {
Bram Moolenaareffed932018-08-14 13:38:17 +02002623 while (*eap->cmd == ' ' || *eap->cmd == '\t' || *eap->cmd == ':')
2624 ++eap->cmd;
2625
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002626 // in ex mode, an empty line works like :+
Bram Moolenaareffed932018-08-14 13:38:17 +02002627 if (*eap->cmd == NUL && exmode_active
2628 && (getline_equal(eap->getline, eap->cookie, getexmodeline)
2629 || getline_equal(eap->getline, eap->cookie, getexline))
2630 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2631 {
2632 eap->cmd = (char_u *)"+";
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002633 if (!skip_only)
2634 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002635 }
2636
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002637 // ignore comment and empty lines
Bram Moolenaareffed932018-08-14 13:38:17 +02002638 if (*eap->cmd == '"')
2639 return FAIL;
2640 if (*eap->cmd == NUL)
2641 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002642 if (!skip_only)
2643 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002644 return FAIL;
2645 }
2646
Bram Moolenaareffed932018-08-14 13:38:17 +02002647 p = skip_range(eap->cmd, NULL);
2648 switch (*p)
2649 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002650 // When adding an entry, also modify cmd_exists().
Bram Moolenaareffed932018-08-14 13:38:17 +02002651 case 'a': if (!checkforcmd(&eap->cmd, "aboveleft", 3))
2652 break;
2653 cmdmod.split |= WSP_ABOVE;
2654 continue;
2655
2656 case 'b': if (checkforcmd(&eap->cmd, "belowright", 3))
2657 {
2658 cmdmod.split |= WSP_BELOW;
2659 continue;
2660 }
2661 if (checkforcmd(&eap->cmd, "browse", 3))
2662 {
2663#ifdef FEAT_BROWSE_CMD
2664 cmdmod.browse = TRUE;
2665#endif
2666 continue;
2667 }
2668 if (!checkforcmd(&eap->cmd, "botright", 2))
2669 break;
2670 cmdmod.split |= WSP_BOT;
2671 continue;
2672
2673 case 'c': if (!checkforcmd(&eap->cmd, "confirm", 4))
2674 break;
2675#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2676 cmdmod.confirm = TRUE;
2677#endif
2678 continue;
2679
2680 case 'k': if (checkforcmd(&eap->cmd, "keepmarks", 3))
2681 {
2682 cmdmod.keepmarks = TRUE;
2683 continue;
2684 }
2685 if (checkforcmd(&eap->cmd, "keepalt", 5))
2686 {
2687 cmdmod.keepalt = TRUE;
2688 continue;
2689 }
2690 if (checkforcmd(&eap->cmd, "keeppatterns", 5))
2691 {
2692 cmdmod.keeppatterns = TRUE;
2693 continue;
2694 }
2695 if (!checkforcmd(&eap->cmd, "keepjumps", 5))
2696 break;
2697 cmdmod.keepjumps = TRUE;
2698 continue;
2699
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002700 case 'f': // only accept ":filter {pat} cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002701 {
2702 char_u *reg_pat;
2703
2704 if (!checkforcmd(&p, "filter", 4)
2705 || *p == NUL || ends_excmd(*p))
2706 break;
2707 if (*p == '!')
2708 {
2709 cmdmod.filter_force = TRUE;
2710 p = skipwhite(p + 1);
2711 if (*p == NUL || ends_excmd(*p))
2712 break;
2713 }
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002714 if (skip_only)
2715 p = skip_vimgrep_pat(p, NULL, NULL);
2716 else
2717 // NOTE: This puts a NUL after the pattern.
2718 p = skip_vimgrep_pat(p, &reg_pat, NULL);
Bram Moolenaareffed932018-08-14 13:38:17 +02002719 if (p == NULL || *p == NUL)
2720 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002721 if (!skip_only)
2722 {
2723 cmdmod.filter_regmatch.regprog =
Bram Moolenaareffed932018-08-14 13:38:17 +02002724 vim_regcomp(reg_pat, RE_MAGIC);
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002725 if (cmdmod.filter_regmatch.regprog == NULL)
2726 break;
2727 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002728 eap->cmd = p;
2729 continue;
2730 }
2731
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002732 // ":hide" and ":hide | cmd" are not modifiers
Bram Moolenaareffed932018-08-14 13:38:17 +02002733 case 'h': if (p != eap->cmd || !checkforcmd(&p, "hide", 3)
2734 || *p == NUL || ends_excmd(*p))
2735 break;
2736 eap->cmd = p;
2737 cmdmod.hide = TRUE;
2738 continue;
2739
2740 case 'l': if (checkforcmd(&eap->cmd, "lockmarks", 3))
2741 {
2742 cmdmod.lockmarks = TRUE;
2743 continue;
2744 }
2745
2746 if (!checkforcmd(&eap->cmd, "leftabove", 5))
2747 break;
2748 cmdmod.split |= WSP_ABOVE;
2749 continue;
2750
2751 case 'n': if (checkforcmd(&eap->cmd, "noautocmd", 3))
2752 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002753 if (cmdmod.save_ei == NULL && !skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002754 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002755 // Set 'eventignore' to "all". Restore the
2756 // existing option value later.
Bram Moolenaareffed932018-08-14 13:38:17 +02002757 cmdmod.save_ei = vim_strsave(p_ei);
2758 set_string_option_direct((char_u *)"ei", -1,
2759 (char_u *)"all", OPT_FREE, SID_NONE);
2760 }
2761 continue;
2762 }
2763 if (!checkforcmd(&eap->cmd, "noswapfile", 3))
2764 break;
2765 cmdmod.noswapfile = TRUE;
2766 continue;
2767
2768 case 'r': if (!checkforcmd(&eap->cmd, "rightbelow", 6))
2769 break;
2770 cmdmod.split |= WSP_BELOW;
2771 continue;
2772
2773 case 's': if (checkforcmd(&eap->cmd, "sandbox", 3))
2774 {
2775#ifdef HAVE_SANDBOX
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002776 if (!skip_only)
2777 {
2778 if (!eap->did_sandbox)
2779 ++sandbox;
2780 eap->did_sandbox = TRUE;
2781 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002782#endif
2783 continue;
2784 }
2785 if (!checkforcmd(&eap->cmd, "silent", 3))
2786 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002787 if (!skip_only)
2788 {
2789 if (eap->save_msg_silent == -1)
2790 eap->save_msg_silent = msg_silent;
2791 ++msg_silent;
2792 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002793 if (*eap->cmd == '!' && !VIM_ISWHITE(eap->cmd[-1]))
2794 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002795 // ":silent!", but not "silent !cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002796 eap->cmd = skipwhite(eap->cmd + 1);
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002797 if (!skip_only)
2798 {
2799 ++emsg_silent;
2800 ++eap->did_esilent;
2801 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002802 }
2803 continue;
2804
2805 case 't': if (checkforcmd(&p, "tab", 3))
2806 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002807 if (!skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002808 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002809 long tabnr = get_address(eap, &eap->cmd,
2810 ADDR_TABS, eap->skip,
2811 skip_only, FALSE, 1);
2812 if (tabnr == MAXLNUM)
2813 cmdmod.tab = tabpage_index(curtab) + 1;
2814 else
Bram Moolenaareffed932018-08-14 13:38:17 +02002815 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002816 if (tabnr < 0 || tabnr > LAST_TAB_NR)
2817 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002818 *errormsg = _(e_invrange);
Bram Moolenaar548e5982018-12-26 21:45:00 +01002819 return FAIL;
2820 }
2821 cmdmod.tab = tabnr + 1;
Bram Moolenaareffed932018-08-14 13:38:17 +02002822 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002823 }
2824 eap->cmd = p;
2825 continue;
2826 }
2827 if (!checkforcmd(&eap->cmd, "topleft", 2))
2828 break;
2829 cmdmod.split |= WSP_TOP;
2830 continue;
2831
2832 case 'u': if (!checkforcmd(&eap->cmd, "unsilent", 3))
2833 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002834 if (!skip_only)
2835 {
2836 if (eap->save_msg_silent == -1)
2837 eap->save_msg_silent = msg_silent;
2838 msg_silent = 0;
2839 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002840 continue;
2841
2842 case 'v': if (checkforcmd(&eap->cmd, "vertical", 4))
2843 {
2844 cmdmod.split |= WSP_VERT;
2845 continue;
2846 }
2847 if (!checkforcmd(&p, "verbose", 4))
2848 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002849 if (!skip_only)
2850 {
2851 if (eap->verbose_save < 0)
2852 eap->verbose_save = p_verbose;
2853 if (vim_isdigit(*eap->cmd))
2854 p_verbose = atoi((char *)eap->cmd);
2855 else
2856 p_verbose = 1;
2857 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002858 eap->cmd = p;
2859 continue;
2860 }
2861 break;
2862 }
2863
2864 return OK;
2865}
2866
2867/*
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002868 * Free contents of "cmdmod".
2869 */
2870 static void
2871free_cmdmod(void)
2872{
2873 if (cmdmod.save_ei != NULL)
2874 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002875 // Restore 'eventignore' to the value before ":noautocmd".
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002876 set_string_option_direct((char_u *)"ei", -1, cmdmod.save_ei,
2877 OPT_FREE, SID_NONE);
2878 free_string_option(cmdmod.save_ei);
2879 }
2880
2881 if (cmdmod.filter_regmatch.regprog != NULL)
2882 vim_regfree(cmdmod.filter_regmatch.regprog);
2883}
2884
2885/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002886 * Parse the address range, if any, in "eap".
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02002887 * May set the last search pattern, unless "silent" is TRUE.
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002888 * Return FAIL and set "errormsg" or return OK.
2889 */
2890 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002891parse_cmd_address(exarg_T *eap, char **errormsg, int silent)
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002892{
2893 int address_count = 1;
2894 linenr_T lnum;
2895
2896 // Repeat for all ',' or ';' separated addresses.
2897 for (;;)
2898 {
2899 eap->line1 = eap->line2;
2900 switch (eap->addr_type)
2901 {
2902 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02002903 case ADDR_OTHER:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002904 // default is current line number
2905 eap->line2 = curwin->w_cursor.lnum;
2906 break;
2907 case ADDR_WINDOWS:
2908 eap->line2 = CURRENT_WIN_NR;
2909 break;
2910 case ADDR_ARGUMENTS:
2911 eap->line2 = curwin->w_arg_idx + 1;
2912 if (eap->line2 > ARGCOUNT)
2913 eap->line2 = ARGCOUNT;
2914 break;
2915 case ADDR_LOADED_BUFFERS:
2916 case ADDR_BUFFERS:
2917 eap->line2 = curbuf->b_fnum;
2918 break;
2919 case ADDR_TABS:
2920 eap->line2 = CURRENT_TAB_NR;
2921 break;
2922 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02002923 case ADDR_UNSIGNED:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002924 eap->line2 = 1;
2925 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002926 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002927#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02002928 eap->line2 = qf_get_cur_idx(eap);
2929#endif
2930 break;
2931 case ADDR_QUICKFIX_VALID:
2932#ifdef FEAT_QUICKFIX
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002933 eap->line2 = qf_get_cur_valid_idx(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002934#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002935 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02002936 case ADDR_NONE:
2937 // Will give an error later if a range is found.
2938 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002939 }
2940 eap->cmd = skipwhite(eap->cmd);
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02002941 lnum = get_address(eap, &eap->cmd, eap->addr_type, eap->skip, silent,
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002942 eap->addr_count == 0, address_count++);
2943 if (eap->cmd == NULL) // error detected
2944 return FAIL;
2945 if (lnum == MAXLNUM)
2946 {
2947 if (*eap->cmd == '%') // '%' - all lines
2948 {
2949 ++eap->cmd;
2950 switch (eap->addr_type)
2951 {
2952 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02002953 case ADDR_OTHER:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002954 eap->line1 = 1;
2955 eap->line2 = curbuf->b_ml.ml_line_count;
2956 break;
2957 case ADDR_LOADED_BUFFERS:
2958 {
2959 buf_T *buf = firstbuf;
2960
2961 while (buf->b_next != NULL
2962 && buf->b_ml.ml_mfp == NULL)
2963 buf = buf->b_next;
2964 eap->line1 = buf->b_fnum;
2965 buf = lastbuf;
2966 while (buf->b_prev != NULL
2967 && buf->b_ml.ml_mfp == NULL)
2968 buf = buf->b_prev;
2969 eap->line2 = buf->b_fnum;
2970 break;
2971 }
2972 case ADDR_BUFFERS:
2973 eap->line1 = firstbuf->b_fnum;
2974 eap->line2 = lastbuf->b_fnum;
2975 break;
2976 case ADDR_WINDOWS:
2977 case ADDR_TABS:
2978 if (IS_USER_CMDIDX(eap->cmdidx))
2979 {
2980 eap->line1 = 1;
2981 eap->line2 = eap->addr_type == ADDR_WINDOWS
2982 ? LAST_WIN_NR : LAST_TAB_NR;
2983 }
2984 else
2985 {
2986 // there is no Vim command which uses '%' and
2987 // ADDR_WINDOWS or ADDR_TABS
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002988 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002989 return FAIL;
2990 }
2991 break;
2992 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02002993 case ADDR_UNSIGNED:
2994 case ADDR_QUICKFIX:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002995 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002996 return FAIL;
2997 case ADDR_ARGUMENTS:
2998 if (ARGCOUNT == 0)
2999 eap->line1 = eap->line2 = 0;
3000 else
3001 {
3002 eap->line1 = 1;
3003 eap->line2 = ARGCOUNT;
3004 }
3005 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003006 case ADDR_QUICKFIX_VALID:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003007#ifdef FEAT_QUICKFIX
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003008 eap->line1 = 1;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003009 eap->line2 = qf_get_valid_size(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003010 if (eap->line2 == 0)
3011 eap->line2 = 1;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003012#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003013 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003014 case ADDR_NONE:
3015 // Will give an error later if a range is found.
3016 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003017 }
3018 ++eap->addr_count;
3019 }
3020 else if (*eap->cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
3021 {
3022 pos_T *fp;
3023
3024 // '*' - visual area
3025 if (eap->addr_type != ADDR_LINES)
3026 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003027 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003028 return FAIL;
3029 }
3030
3031 ++eap->cmd;
3032 if (!eap->skip)
3033 {
3034 fp = getmark('<', FALSE);
3035 if (check_mark(fp) == FAIL)
3036 return FAIL;
3037 eap->line1 = fp->lnum;
3038 fp = getmark('>', FALSE);
3039 if (check_mark(fp) == FAIL)
3040 return FAIL;
3041 eap->line2 = fp->lnum;
3042 ++eap->addr_count;
3043 }
3044 }
3045 }
3046 else
3047 eap->line2 = lnum;
3048 eap->addr_count++;
3049
3050 if (*eap->cmd == ';')
3051 {
3052 if (!eap->skip)
3053 {
3054 curwin->w_cursor.lnum = eap->line2;
3055 // don't leave the cursor on an illegal line or column
3056 check_cursor();
3057 }
3058 }
3059 else if (*eap->cmd != ',')
3060 break;
3061 ++eap->cmd;
3062 }
3063
3064 // One address given: set start and end lines.
3065 if (eap->addr_count == 1)
3066 {
3067 eap->line1 = eap->line2;
3068 // ... but only implicit: really no address given
3069 if (lnum == MAXLNUM)
3070 eap->addr_count = 0;
3071 }
3072 return OK;
3073}
3074
3075/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003076 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003077 * If there is a match advance "pp" to the argument and return TRUE.
3078 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003079 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003080checkforcmd(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003081 char_u **pp, // start of command
3082 char *cmd, // name of command
3083 int len) // required length
Bram Moolenaar071d4272004-06-13 20:20:40 +00003084{
3085 int i;
3086
3087 for (i = 0; cmd[i] != NUL; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003088 if (((char_u *)cmd)[i] != (*pp)[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003089 break;
3090 if (i >= len && !isalpha((*pp)[i]))
3091 {
3092 *pp = skipwhite(*pp + i);
3093 return TRUE;
3094 }
3095 return FALSE;
3096}
3097
3098/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003099 * Append "cmd" to the error message in IObuff.
3100 * Takes care of limiting the length and handling 0xa0, which would be
3101 * invisible otherwise.
3102 */
3103 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003104append_command(char_u *cmd)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003105{
3106 char_u *s = cmd;
3107 char_u *d;
3108
3109 STRCAT(IObuff, ": ");
3110 d = IObuff + STRLEN(IObuff);
3111 while (*s != NUL && d - IObuff < IOSIZE - 7)
3112 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003113 if (enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) : *s == 0xa0)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003114 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003115 s += enc_utf8 ? 2 : 1;
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003116 STRCPY(d, "<a0>");
3117 d += 4;
3118 }
3119 else
3120 MB_COPY_CHAR(s, d);
3121 }
3122 *d = NUL;
3123}
3124
3125/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003126 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003127 * Start of the name can be found at eap->cmd.
Bram Moolenaar25190db2019-05-04 15:05:28 +02003128 * Sets eap->cmdidx and returns a pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003129 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003130 *
3131 * If "lookup" is not NULL recognize expression without "eval" or "call" and
3132 * assignment without "let". Sets eap->cmdidx to the command while returning
3133 * "eap->cmd".
3134 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003135 * Returns NULL for an ambiguous user command.
3136 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003137 char_u *
3138find_ex_command(
3139 exarg_T *eap,
3140 int *full UNUSED,
3141 int (*lookup)(char_u *, size_t, cctx_T *) UNUSED,
3142 cctx_T *cctx UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143{
3144 int len;
3145 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003146 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003147
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003148#ifdef FEAT_EVAL
3149 /*
3150 * Recognize a Vim9 script function/method call and assignment:
3151 * "lvar = value", "lvar(arg)", "[1, 2 3]->Func()"
3152 */
Bram Moolenaar0c6ceaf2020-02-22 18:36:32 +01003153 p = eap->cmd;
3154 if (lookup != NULL && (*p == '('
3155 || ((p = to_name_const_end(eap->cmd)) > eap->cmd && *p != NUL)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003156 {
3157 int oplen;
3158 int heredoc;
3159
3160 // "funcname(" is always a function call.
3161 // "varname[]" is an expression.
3162 // "g:varname" is an expression.
3163 // "varname->expr" is an expression.
Bram Moolenaar0c6ceaf2020-02-22 18:36:32 +01003164 // "(..." is an expression.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003165 if (*p == '('
3166 || *p == '['
3167 || p[1] == ':'
3168 || (*p == '-' && p[1] == '>'))
3169 {
3170 eap->cmdidx = CMD_eval;
3171 return eap->cmd;
3172 }
3173
3174 oplen = assignment_len(skipwhite(p), &heredoc);
3175 if (oplen > 0)
3176 {
3177 // Recognize an assignment if we recognize the variable name:
3178 // "g:var = expr"
3179 // "var = expr" where "var" is a local var name.
3180 if (((p - eap->cmd) > 2 && eap->cmd[1] == ':')
3181 || lookup(eap->cmd, p - eap->cmd, cctx) >= 0)
3182 {
3183 eap->cmdidx = CMD_let;
3184 return eap->cmd;
3185 }
3186 }
3187 }
3188#endif
3189
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190 /*
3191 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00003192 * Exceptions:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193 * - the 'k' command can directly be followed by any character.
3194 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003195 * but :sre[wind] is another command, as are :scr[iptnames],
Bram Moolenaar071d4272004-06-13 20:20:40 +00003196 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00003197 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003198 */
3199 p = eap->cmd;
3200 if (*p == 'k')
3201 {
3202 eap->cmdidx = CMD_k;
3203 ++p;
3204 }
3205 else if (p[0] == 's'
Bram Moolenaar204b93f2015-08-04 22:02:51 +02003206 && ((p[1] == 'c' && (p[2] == NUL || (p[2] != 's' && p[2] != 'r'
3207 && (p[3] == NUL || (p[3] != 'i' && p[4] != 'p')))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208 || p[1] == 'g'
3209 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
3210 || p[1] == 'I'
3211 || (p[1] == 'r' && p[2] != 'e')))
3212 {
3213 eap->cmdidx = CMD_substitute;
3214 ++p;
3215 }
3216 else
3217 {
3218 while (ASCII_ISALPHA(*p))
3219 ++p;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003220 // for python 3.x support ":py3", ":python3", ":py3file", etc.
Bram Moolenaar55d5c032010-07-17 23:52:29 +02003221 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003222 {
Bram Moolenaarb6590522010-07-21 16:00:43 +02003223 while (ASCII_ISALNUM(*p))
3224 ++p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003225 }
3226 else if (*p == '9' && STRNCMP("vim9", eap->cmd, 4) == 0)
3227 {
3228 // include "9" for "vim9script"
3229 ++p;
3230 while (ASCII_ISALPHA(*p))
3231 ++p;
3232 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02003233
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003234 // check for non-alpha command
Bram Moolenaar071d4272004-06-13 20:20:40 +00003235 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3236 ++p;
3237 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003238 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
3239 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003240 // Check for ":dl", ":dell", etc. to ":deletel": that's
3241 // :delete with the 'l' flag. Same for 'p'.
Bram Moolenaardf177f62005-02-22 08:39:57 +00003242 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003243 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00003244 break;
3245 if (i == len - 1)
3246 {
3247 --len;
3248 if (p[-1] == 'l')
3249 eap->flags |= EXFLAG_LIST;
3250 else
3251 eap->flags |= EXFLAG_PRINT;
3252 }
3253 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003254
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003255 if (ASCII_ISLOWER(eap->cmd[0]))
3256 {
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003257 int c1 = eap->cmd[0];
Bram Moolenaar94f82cb2019-07-24 22:30:27 +02003258 int c2 = len == 1 ? NUL : eap->cmd[1];
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003259
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003260 if (command_count != (int)CMD_SIZE)
3261 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003262 iemsg(_("E943: Command table needs to be updated, run 'make cmdidxs'"));
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003263 getout(1);
3264 }
3265
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003266 // Use a precomputed index for fast look-up in cmdnames[]
3267 // taking into account the first 2 letters of eap->cmd.
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003268 eap->cmdidx = cmdidxs1[CharOrdLow(c1)];
3269 if (ASCII_ISLOWER(c2))
3270 eap->cmdidx += cmdidxs2[CharOrdLow(c1)][CharOrdLow(c2)];
3271 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272 else
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003273 eap->cmdidx = CMD_bang;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003274
3275 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
3276 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
3277 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
3278 (size_t)len) == 0)
3279 {
3280#ifdef FEAT_EVAL
3281 if (full != NULL
3282 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
3283 *full = TRUE;
3284#endif
3285 break;
3286 }
3287
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003288 // Look for a user defined command as a last resort. Let ":Print" be
3289 // overruled by a user defined command.
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01003290 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
3291 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292 {
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003293 // User defined commands may contain digits.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003294 while (ASCII_ISALNUM(*p))
3295 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003296 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003298 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003299 eap->cmdidx = CMD_SIZE;
3300 }
3301
3302 return p;
3303}
3304
3305#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003306static struct cmdmod
3307{
3308 char *name;
3309 int minlen;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003310 int has_count; // :123verbose :3tab
Bram Moolenaared53fb92007-11-24 20:50:24 +00003311} cmdmods[] = {
3312 {"aboveleft", 3, FALSE},
3313 {"belowright", 3, FALSE},
3314 {"botright", 2, FALSE},
3315 {"browse", 3, FALSE},
3316 {"confirm", 4, FALSE},
Bram Moolenaar7b668e82016-08-23 23:51:21 +02003317 {"filter", 4, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003318 {"hide", 3, FALSE},
3319 {"keepalt", 5, FALSE},
3320 {"keepjumps", 5, FALSE},
3321 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01003322 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003323 {"leftabove", 5, FALSE},
3324 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003325 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003326 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003327 {"rightbelow", 6, FALSE},
3328 {"sandbox", 3, FALSE},
3329 {"silent", 3, FALSE},
3330 {"tab", 3, TRUE},
3331 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003332 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003333 {"verbose", 4, TRUE},
3334 {"vertical", 4, FALSE},
3335};
3336
3337/*
3338 * Return length of a command modifier (including optional count).
3339 * Return zero when it's not a modifier.
3340 */
3341 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003342modifier_len(char_u *cmd)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003343{
3344 int i, j;
3345 char_u *p = cmd;
3346
3347 if (VIM_ISDIGIT(*cmd))
3348 p = skipwhite(skipdigits(cmd));
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003349 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003350 {
3351 for (j = 0; p[j] != NUL; ++j)
3352 if (p[j] != cmdmods[i].name[j])
3353 break;
Bram Moolenaar2d473ab2013-06-12 17:12:24 +02003354 if (!ASCII_ISALPHA(p[j]) && j >= cmdmods[i].minlen
Bram Moolenaared53fb92007-11-24 20:50:24 +00003355 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003356 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003357 }
3358 return 0;
3359}
3360
Bram Moolenaar071d4272004-06-13 20:20:40 +00003361/*
3362 * Return > 0 if an Ex command "name" exists.
3363 * Return 2 if there is an exact match.
3364 * Return 3 if there is an ambiguous match.
3365 */
3366 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003367cmd_exists(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368{
3369 exarg_T ea;
3370 int full = FALSE;
3371 int i;
3372 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003373 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003375 // Check command modifiers.
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003376 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377 {
3378 for (j = 0; name[j] != NUL; ++j)
3379 if (name[j] != cmdmods[i].name[j])
3380 break;
3381 if (name[j] == NUL && j >= cmdmods[i].minlen)
3382 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3383 }
3384
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003385 // Check built-in commands and user defined commands.
3386 // For ":2match" and ":3match" we need to skip the number.
Bram Moolenaara9587612006-05-04 21:47:50 +00003387 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003389 p = find_ex_command(&ea, &full, NULL, NULL);
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003390 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003391 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003392 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3393 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003394 if (*skipwhite(p) != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003395 return 0; // trailing garbage
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3397}
3398#endif
3399
Bram Moolenaard0190392019-08-23 21:17:35 +02003400 cmdidx_T
3401excmd_get_cmdidx(char_u *cmd, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402{
Bram Moolenaard0190392019-08-23 21:17:35 +02003403 cmdidx_T idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404
Bram Moolenaard0190392019-08-23 21:17:35 +02003405 for (idx = (cmdidx_T)0; (int)idx < (int)CMD_SIZE;
3406 idx = (cmdidx_T)((int)idx + 1))
3407 if (STRNCMP(cmdnames[(int)idx].cmd_name, cmd, (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408 break;
3409
Bram Moolenaard0190392019-08-23 21:17:35 +02003410 return idx;
3411}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412
Bram Moolenaard0190392019-08-23 21:17:35 +02003413 long
3414excmd_get_argt(cmdidx_T idx)
3415{
3416 return (long)cmdnames[(int)idx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003417}
3418
3419/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003420 * Skip a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421 *
3422 * Backslashed delimiters after / or ? will be skipped, and commands will
3423 * not be expanded between /'s and ?'s or after "'".
3424 *
Bram Moolenaardf177f62005-02-22 08:39:57 +00003425 * Also skip white space and ":" characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426 * Returns the "cmd" pointer advanced to beyond the range.
3427 */
3428 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003429skip_range(
3430 char_u *cmd,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003431 int *ctx) // pointer to xp_context or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003433 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01003435 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;\\", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436 {
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01003437 if (*cmd == '\\')
3438 {
3439 if (cmd[1] == '?' || cmd[1] == '/' || cmd[1] == '&')
3440 ++cmd;
3441 else
3442 break;
3443 }
3444 else if (*cmd == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445 {
3446 if (*++cmd == NUL && ctx != NULL)
3447 *ctx = EXPAND_NOTHING;
3448 }
3449 else if (*cmd == '/' || *cmd == '?')
3450 {
3451 delim = *cmd++;
3452 while (*cmd != NUL && *cmd != delim)
3453 if (*cmd++ == '\\' && *cmd != NUL)
3454 ++cmd;
3455 if (*cmd == NUL && ctx != NULL)
3456 *ctx = EXPAND_NOTHING;
3457 }
3458 if (*cmd != NUL)
3459 ++cmd;
3460 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00003461
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003462 // Skip ":" and white space.
Bram Moolenaardf177f62005-02-22 08:39:57 +00003463 while (*cmd == ':')
3464 cmd = skipwhite(cmd + 1);
3465
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466 return cmd;
3467}
3468
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003469 static void
3470addr_error(cmd_addr_T addr_type)
3471{
3472 if (addr_type == ADDR_NONE)
3473 emsg(_(e_norange));
3474 else
3475 emsg(_(e_invrange));
3476}
3477
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478/*
Bram Moolenaar198cb662018-09-06 21:44:17 +02003479 * Get a single EX address.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480 *
3481 * Set ptr to the next character after the part that was interpreted.
3482 * Set ptr to NULL when an error is encountered.
Bram Moolenaar198cb662018-09-06 21:44:17 +02003483 * This may set the last used search pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003484 *
3485 * Return MAXLNUM when no Ex address was found.
3486 */
3487 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003488get_address(
3489 exarg_T *eap UNUSED,
3490 char_u **ptr,
Bram Moolenaar1b03a192019-12-08 17:08:29 +01003491 cmd_addr_T addr_type,
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003492 int skip, // only skip the address, don't use it
3493 int silent, // no errors or side effects
3494 int to_other_file, // flag: may jump to other file
3495 int address_count UNUSED) // 1 for first address, >1 after comma
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496{
3497 int c;
3498 int i;
3499 long n;
3500 char_u *cmd;
3501 pos_T pos;
3502 pos_T *fp;
3503 linenr_T lnum;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003504 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505
3506 cmd = skipwhite(*ptr);
3507 lnum = MAXLNUM;
3508 do
3509 {
3510 switch (*cmd)
3511 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003512 case '.': // '.' - Cursor position
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003513 ++cmd;
3514 switch (addr_type)
3515 {
3516 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003517 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518 lnum = curwin->w_cursor.lnum;
3519 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003520 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003521 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003522 break;
3523 case ADDR_ARGUMENTS:
3524 lnum = curwin->w_arg_idx + 1;
3525 break;
3526 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003527 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003528 lnum = curbuf->b_fnum;
3529 break;
3530 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003531 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003532 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003533 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003534 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003535 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003536 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003537 cmd = NULL;
3538 goto error;
3539 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003540 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003541#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02003542 lnum = qf_get_cur_idx(eap);
3543#endif
3544 break;
3545 case ADDR_QUICKFIX_VALID:
3546#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003547 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02003548#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003549 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003550 }
3551 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003552
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003553 case '$': // '$' - last line
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003554 ++cmd;
3555 switch (addr_type)
3556 {
3557 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003558 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559 lnum = curbuf->b_ml.ml_line_count;
3560 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003561 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003562 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003563 break;
3564 case ADDR_ARGUMENTS:
3565 lnum = ARGCOUNT;
3566 break;
3567 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003568 buf = lastbuf;
3569 while (buf->b_ml.ml_mfp == NULL)
3570 {
3571 if (buf->b_prev == NULL)
3572 break;
3573 buf = buf->b_prev;
3574 }
3575 lnum = buf->b_fnum;
3576 break;
3577 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003578 lnum = lastbuf->b_fnum;
3579 break;
3580 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003581 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003582 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003583 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003584 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003585 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003586 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003587 cmd = NULL;
3588 goto error;
3589 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003590 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003591#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003592 lnum = qf_get_size(eap);
3593 if (lnum == 0)
3594 lnum = 1;
Bram Moolenaare906c502015-09-09 21:10:39 +02003595#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003596 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003597 case ADDR_QUICKFIX_VALID:
3598#ifdef FEAT_QUICKFIX
3599 lnum = qf_get_valid_size(eap);
3600 if (lnum == 0)
3601 lnum = 1;
3602#endif
3603 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003604 }
3605 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003606
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003607 case '\'': // ''' - mark
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003608 if (*++cmd == NUL)
3609 {
3610 cmd = NULL;
3611 goto error;
3612 }
3613 if (addr_type != ADDR_LINES)
3614 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003615 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003616 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003617 goto error;
3618 }
3619 if (skip)
3620 ++cmd;
3621 else
3622 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003623 // Only accept a mark in another file when it is
3624 // used by itself: ":'M".
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003625 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
3626 ++cmd;
3627 if (fp == (pos_T *)-1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003628 // Jumped to another file.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003629 lnum = curwin->w_cursor.lnum;
3630 else
3631 {
3632 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633 {
3634 cmd = NULL;
3635 goto error;
3636 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003637 lnum = fp->lnum;
3638 }
3639 }
3640 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003641
3642 case '/':
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003643 case '?': // '/' or '?' - search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003644 c = *cmd++;
3645 if (addr_type != ADDR_LINES)
3646 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003647 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003648 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003649 goto error;
3650 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003651 if (skip) // skip "/pat/"
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003652 {
3653 cmd = skip_regexp(cmd, c, (int)p_magic, NULL);
3654 if (*cmd == c)
3655 ++cmd;
3656 }
3657 else
3658 {
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003659 int flags;
3660
3661 pos = curwin->w_cursor; // save curwin->w_cursor
3662
3663 // When '/' or '?' follows another address, start from
3664 // there.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003665 if (lnum != MAXLNUM)
3666 curwin->w_cursor.lnum = lnum;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003667
3668 // Start a forward search at the end of the line (unless
3669 // before the first line).
3670 // Start a backward search at the start of the line.
3671 // This makes sure we never match in the current
3672 // line, and can match anywhere in the
3673 // next/previous line.
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01003674 if (c == '/' && curwin->w_cursor.lnum > 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003675 curwin->w_cursor.col = MAXCOL;
3676 else
3677 curwin->w_cursor.col = 0;
3678 searchcmdlen = 0;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003679 flags = silent ? 0 : SEARCH_HIS | SEARCH_MSG;
Bram Moolenaarc036e872020-02-21 21:30:52 +01003680 if (!do_search(NULL, c, c, cmd, 1L, flags, NULL))
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003681 {
3682 curwin->w_cursor = pos;
3683 cmd = NULL;
3684 goto error;
3685 }
3686 lnum = curwin->w_cursor.lnum;
3687 curwin->w_cursor = pos;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003688 // adjust command string pointer
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003689 cmd += searchcmdlen;
3690 }
3691 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003692
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003693 case '\\': // "\?", "\/" or "\&", repeat search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003694 ++cmd;
3695 if (addr_type != ADDR_LINES)
3696 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003697 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003698 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003699 goto error;
3700 }
3701 if (*cmd == '&')
3702 i = RE_SUBST;
3703 else if (*cmd == '?' || *cmd == '/')
3704 i = RE_SEARCH;
3705 else
3706 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003707 emsg(_(e_backslash));
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003708 cmd = NULL;
3709 goto error;
3710 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003712 if (!skip)
3713 {
3714 /*
3715 * When search follows another address, start from
3716 * there.
3717 */
3718 if (lnum != MAXLNUM)
3719 pos.lnum = lnum;
3720 else
3721 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003723 /*
3724 * Start the search just like for the above
3725 * do_search().
3726 */
3727 if (*cmd != '?')
3728 pos.col = MAXCOL;
3729 else
3730 pos.col = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02003731 pos.coladd = 0;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01003732 if (searchit(curwin, curbuf, &pos, NULL,
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003733 *cmd == '?' ? BACKWARD : FORWARD,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02003734 (char_u *)"", 1L, SEARCH_MSG, i, NULL) != FAIL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003735 lnum = pos.lnum;
3736 else
3737 {
3738 cmd = NULL;
3739 goto error;
3740 }
3741 }
3742 ++cmd;
3743 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744
3745 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003746 if (VIM_ISDIGIT(*cmd)) // absolute line number
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003747 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003748 }
3749
3750 for (;;)
3751 {
3752 cmd = skipwhite(cmd);
3753 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
3754 break;
3755
3756 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003757 {
3758 switch (addr_type)
3759 {
3760 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003761 case ADDR_OTHER:
3762 // "+1" is same as ".+1"
Bram Moolenaarf240e182014-11-27 18:33:02 +01003763 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003764 break;
3765 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003766 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003767 break;
3768 case ADDR_ARGUMENTS:
3769 lnum = curwin->w_arg_idx + 1;
3770 break;
3771 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003772 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003773 lnum = curbuf->b_fnum;
3774 break;
3775 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003776 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003777 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003778 case ADDR_TABS_RELATIVE:
3779 lnum = 1;
3780 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003781 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003782#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02003783 lnum = qf_get_cur_idx(eap);
3784#endif
3785 break;
3786 case ADDR_QUICKFIX_VALID:
3787#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003788 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02003789#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003790 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003791 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003792 case ADDR_UNSIGNED:
3793 lnum = 0;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003794 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003795 }
3796 }
3797
Bram Moolenaar071d4272004-06-13 20:20:40 +00003798 if (VIM_ISDIGIT(*cmd))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003799 i = '+'; // "number" is same as "+number"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003800 else
3801 i = *cmd++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003802 if (!VIM_ISDIGIT(*cmd)) // '+' is '+1', but '+0' is not '+1'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803 n = 1;
3804 else
3805 n = getdigits(&cmd);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003806
3807 if (addr_type == ADDR_TABS_RELATIVE)
3808 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003809 emsg(_(e_invrange));
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003810 cmd = NULL;
3811 goto error;
3812 }
3813 else if (addr_type == ADDR_LOADED_BUFFERS
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003814 || addr_type == ADDR_BUFFERS)
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003815 lnum = compute_buffer_local_count(
3816 addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003817 else
Bram Moolenaarded27822017-01-02 14:27:34 +01003818 {
3819#ifdef FEAT_FOLDING
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003820 // Relative line addressing, need to adjust for folded lines
3821 // now, but only do it after the first address.
Bram Moolenaarded27822017-01-02 14:27:34 +01003822 if (addr_type == ADDR_LINES && (i == '-' || i == '+')
3823 && address_count >= 2)
3824 (void)hasFolding(lnum, NULL, &lnum);
3825#endif
3826 if (i == '-')
3827 lnum -= n;
3828 else
3829 lnum += n;
3830 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831 }
3832 } while (*cmd == '/' || *cmd == '?');
3833
3834error:
3835 *ptr = cmd;
3836 return lnum;
3837}
3838
3839/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00003840 * Get flags from an Ex command argument.
3841 */
3842 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003843get_flags(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00003844{
3845 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
3846 {
3847 if (*eap->arg == 'l')
3848 eap->flags |= EXFLAG_LIST;
3849 else if (*eap->arg == 'p')
3850 eap->flags |= EXFLAG_PRINT;
3851 else
3852 eap->flags |= EXFLAG_NR;
3853 eap->arg = skipwhite(eap->arg + 1);
3854 }
3855}
3856
3857/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003858 * Function called for command which is Not Implemented. NI!
3859 */
3860 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003861ex_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862{
3863 if (!eap->skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003864 eap->errmsg = N_("E319: Sorry, the command is not available in this version");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865}
3866
Bram Moolenaar7bb75552007-07-16 18:39:49 +00003867#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003868/*
3869 * Function called for script command which is Not Implemented. NI!
3870 * Skips over ":perl <<EOF" constructs.
3871 */
3872 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003873ex_script_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874{
3875 if (!eap->skip)
3876 ex_ni(eap);
3877 else
3878 vim_free(script_get(eap, eap->arg));
3879}
3880#endif
3881
3882/*
3883 * Check range in Ex command for validity.
3884 * Return NULL when valid, error message when invalid.
3885 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003886 static char *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003887invalid_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003888{
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003889 buf_T *buf;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003890
Bram Moolenaar071d4272004-06-13 20:20:40 +00003891 if ( eap->line1 < 0
3892 || eap->line2 < 0
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003893 || eap->line1 > eap->line2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003894 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003895
Bram Moolenaar8071cb22019-07-12 17:58:01 +02003896 if (eap->argt & EX_RANGE)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003897 {
Bram Moolenaarb7316892019-05-01 18:08:42 +02003898 switch (eap->addr_type)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003899 {
3900 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003901 if (eap->line2 > curbuf->b_ml.ml_line_count
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003902#ifdef FEAT_DIFF
3903 + (eap->cmdidx == CMD_diffget)
3904#endif
3905 )
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003906 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003907 break;
3908 case ADDR_ARGUMENTS:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003909 // add 1 if ARGCOUNT is 0
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003910 if (eap->line2 > ARGCOUNT + (!ARGCOUNT))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003911 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003912 break;
3913 case ADDR_BUFFERS:
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02003914 // Only a boundary check, not whether the buffers actually
3915 // exist.
3916 if (eap->line1 < 1 || eap->line2 > get_highest_fnum())
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003917 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003918 break;
3919 case ADDR_LOADED_BUFFERS:
3920 buf = firstbuf;
3921 while (buf->b_ml.ml_mfp == NULL)
3922 {
3923 if (buf->b_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003924 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003925 buf = buf->b_next;
3926 }
3927 if (eap->line1 < buf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003928 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003929 buf = lastbuf;
3930 while (buf->b_ml.ml_mfp == NULL)
3931 {
3932 if (buf->b_prev == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003933 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003934 buf = buf->b_prev;
3935 }
3936 if (eap->line2 > buf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003937 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003938 break;
3939 case ADDR_WINDOWS:
Bram Moolenaar8be63882015-01-14 11:25:05 +01003940 if (eap->line2 > LAST_WIN_NR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003941 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003942 break;
3943 case ADDR_TABS:
3944 if (eap->line2 > LAST_TAB_NR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003945 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003946 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003947 case ADDR_TABS_RELATIVE:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003948 case ADDR_OTHER:
3949 // Any range is OK.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003950 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003951 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003952#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02003953 // No error for value that is too big, will use the last entry.
3954 if (eap->line2 <= 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003955 return _(e_invrange);
Bram Moolenaare906c502015-09-09 21:10:39 +02003956#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003957 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003958 case ADDR_QUICKFIX_VALID:
3959#ifdef FEAT_QUICKFIX
3960 if ((eap->line2 != 1 && eap->line2 > qf_get_valid_size(eap))
3961 || eap->line2 < 0)
3962 return _(e_invrange);
3963#endif
3964 break;
3965 case ADDR_UNSIGNED:
3966 if (eap->line2 < 0)
3967 return _(e_invrange);
3968 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003969 case ADDR_NONE:
3970 // Will give an error elsewhere.
3971 break;
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003972 }
3973 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974 return NULL;
3975}
3976
3977/*
3978 * Correct the range for zero line number, if required.
3979 */
3980 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003981correct_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003982{
Bram Moolenaar8071cb22019-07-12 17:58:01 +02003983 if (!(eap->argt & EX_ZEROR)) // zero in range not allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00003984 {
3985 if (eap->line1 == 0)
3986 eap->line1 = 1;
3987 if (eap->line2 == 0)
3988 eap->line2 = 1;
3989 }
3990}
3991
Bram Moolenaar748bf032005-02-02 23:04:36 +00003992#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00003993/*
3994 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
3995 * pattern. Otherwise return eap->arg.
3996 */
3997 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003998skip_grep_pat(exarg_T *eap)
Bram Moolenaar748bf032005-02-02 23:04:36 +00003999{
4000 char_u *p = eap->arg;
4001
Bram Moolenaara37420f2006-02-04 22:37:47 +00004002 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4003 || eap->cmdidx == CMD_vimgrepadd
4004 || eap->cmdidx == CMD_lvimgrepadd
4005 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004006 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004007 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004008 if (p == NULL)
4009 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004010 }
4011 return p;
4012}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004013
4014/*
4015 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4016 * in the command line, so that things like % get expanded.
4017 */
4018 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004019replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004020{
4021 char_u *new_cmdline;
4022 char_u *program;
4023 char_u *pos;
4024 char_u *ptr;
4025 int len;
4026 int i;
4027
4028 /*
4029 * Don't do it when ":vimgrep" is used for ":grep".
4030 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004031 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4032 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4033 || eap->cmdidx == CMD_grepadd
4034 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004035 && !grep_internal(eap->cmdidx))
4036 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004037 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4038 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004039 {
4040 if (*curbuf->b_p_gp == NUL)
4041 program = p_gp;
4042 else
4043 program = curbuf->b_p_gp;
4044 }
4045 else
4046 {
4047 if (*curbuf->b_p_mp == NUL)
4048 program = p_mp;
4049 else
4050 program = curbuf->b_p_mp;
4051 }
4052
4053 p = skipwhite(p);
4054
4055 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4056 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004057 // replace $* by given arguments
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004058 i = 1;
4059 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4060 ++i;
4061 len = (int)STRLEN(p);
Bram Moolenaar51e14382019-05-25 20:21:28 +02004062 new_cmdline = alloc(STRLEN(program) + i * (len - 2) + 1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004063 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004064 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004065 ptr = new_cmdline;
4066 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4067 {
4068 i = (int)(pos - program);
4069 STRNCPY(ptr, program, i);
4070 STRCPY(ptr += i, p);
4071 ptr += len;
4072 program = pos + 2;
4073 }
4074 STRCPY(ptr, program);
4075 }
4076 else
4077 {
Bram Moolenaar51e14382019-05-25 20:21:28 +02004078 new_cmdline = alloc(STRLEN(program) + STRLEN(p) + 2);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004079 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004080 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004081 STRCPY(new_cmdline, program);
4082 STRCAT(new_cmdline, " ");
4083 STRCAT(new_cmdline, p);
4084 }
4085 msg_make(p);
4086
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004087 // 'eap->cmd' is not set here, because it is not used at CMD_make
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004088 vim_free(*cmdlinep);
4089 *cmdlinep = new_cmdline;
4090 p = new_cmdline;
4091 }
4092 return p;
4093}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004094#endif
4095
Bram Moolenaar071d4272004-06-13 20:20:40 +00004096/*
4097 * Expand file name in Ex command argument.
Bram Moolenaar0abb4272019-06-15 16:06:00 +02004098 * When an error is detected, "errormsgp" is set to a non-NULL pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099 * Return FAIL for failure, OK otherwise.
4100 */
4101 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004102expand_filename(
4103 exarg_T *eap,
4104 char_u **cmdlinep,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004105 char **errormsgp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004107 int has_wildcards; // need to expand wildcards
Bram Moolenaar071d4272004-06-13 20:20:40 +00004108 char_u *repl;
4109 int srclen;
4110 char_u *p;
4111 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004112 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004113
Bram Moolenaar748bf032005-02-02 23:04:36 +00004114#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004115 // Skip a regexp pattern for ":vimgrep[add] pat file..."
Bram Moolenaar748bf032005-02-02 23:04:36 +00004116 p = skip_grep_pat(eap);
4117#else
4118 p = eap->arg;
4119#endif
4120
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121 /*
4122 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4123 * the file name contains a wildcard it should not cause expanding.
4124 * (it will be expanded anyway if there is a wildcard before replacing).
4125 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004126 has_wildcards = mch_has_wildcard(p);
4127 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004129#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004130 // Skip over `=expr`, wildcards in it are not expanded.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004131 if (p[0] == '`' && p[1] == '=')
4132 {
4133 p += 2;
4134 (void)skip_expr(&p);
4135 if (*p == '`')
4136 ++p;
4137 continue;
4138 }
4139#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004140 /*
4141 * Quick check if this cannot be the start of a special string.
4142 * Also removes backslash before '%', '#' and '<'.
4143 */
4144 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4145 {
4146 ++p;
4147 continue;
4148 }
4149
4150 /*
4151 * Try to find a match at this position.
4152 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00004153 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
4154 errormsgp, &escaped);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004155 if (*errormsgp != NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156 return FAIL;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004157 if (repl == NULL) // no match found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004158 {
4159 p += srclen;
4160 continue;
4161 }
4162
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004163 // Wildcards won't be expanded below, the replacement is taken
4164 // literally. But do expand "~/file", "~user/file" and "$HOME/file".
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004165 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4166 {
4167 char_u *l = repl;
4168
4169 repl = expand_env_save(repl);
4170 vim_free(l);
4171 }
4172
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004173 // Need to escape white space et al. with a backslash.
4174 // Don't do this for:
4175 // - replacement that already has been escaped: "##"
4176 // - shell commands (may have to use quotes instead).
4177 // - non-unix systems when there is a single argument (spaces don't
4178 // separate arguments then).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00004180 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00004181 && eap->cmdidx != CMD_bang
Bram Moolenaar071d4272004-06-13 20:20:40 +00004182 && eap->cmdidx != CMD_grep
4183 && eap->cmdidx != CMD_grepadd
Bram Moolenaarbf15b8d2017-06-04 20:43:48 +02004184 && eap->cmdidx != CMD_hardcopy
Bram Moolenaar67883b42017-07-27 22:57:00 +02004185 && eap->cmdidx != CMD_lgrep
4186 && eap->cmdidx != CMD_lgrepadd
4187 && eap->cmdidx != CMD_lmake
4188 && eap->cmdidx != CMD_make
4189 && eap->cmdidx != CMD_terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190#ifndef UNIX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004191 && !(eap->argt & EX_NOSPC)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192#endif
4193 )
4194 {
4195 char_u *l;
4196#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004197 // Don't escape a backslash here, because rem_backslash() doesn't
4198 // remove it later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004199 static char_u *nobslash = (char_u *)" \t\"|";
4200# define ESCAPE_CHARS nobslash
4201#else
4202# define ESCAPE_CHARS escape_chars
4203#endif
4204
4205 for (l = repl; *l; ++l)
4206 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
4207 {
4208 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
4209 if (l != NULL)
4210 {
4211 vim_free(repl);
4212 repl = l;
4213 }
4214 break;
4215 }
4216 }
4217
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004218 // For a shell command a '!' must be escaped.
Bram Moolenaar67883b42017-07-27 22:57:00 +02004219 if ((eap->usefilter || eap->cmdidx == CMD_bang
4220 || eap->cmdidx == CMD_terminal)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004221 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222 {
4223 char_u *l;
4224
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004225 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004226 if (l != NULL)
4227 {
4228 vim_free(repl);
4229 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230 }
4231 }
4232
4233 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
4234 vim_free(repl);
4235 if (p == NULL)
4236 return FAIL;
4237 }
4238
4239 /*
4240 * One file argument: Expand wildcards.
4241 * Don't do this with ":r !command" or ":w !command".
4242 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004243 if ((eap->argt & EX_NOSPC) && !eap->usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244 {
4245 /*
4246 * May do this twice:
4247 * 1. Replace environment variables.
4248 * 2. Replace any other wildcards, remove backslashes.
4249 */
4250 for (n = 1; n <= 2; ++n)
4251 {
4252 if (n == 2)
4253 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254 /*
4255 * Halve the number of backslashes (this is Vi compatible).
4256 * For Unix and OS/2, when wildcards are expanded, this is
4257 * done by ExpandOne() below.
4258 */
Bram Moolenaare7fedb62015-12-31 19:07:19 +01004259#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004260 if (!has_wildcards)
4261#endif
4262 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263 }
4264
4265 if (has_wildcards)
4266 {
4267 if (n == 1)
4268 {
4269 /*
4270 * First loop: May expand environment variables. This
4271 * can be done much faster with expand_env() than with
4272 * something else (e.g., calling a shell).
4273 * After expanding environment variables, check again
4274 * if there are still wildcards present.
4275 */
4276 if (vim_strchr(eap->arg, '$') != NULL
4277 || vim_strchr(eap->arg, '~') != NULL)
4278 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00004279 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004280 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281 has_wildcards = mch_has_wildcard(NameBuff);
4282 p = NameBuff;
4283 }
4284 else
4285 p = NULL;
4286 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004287 else // n == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004288 {
4289 expand_T xpc;
Bram Moolenaarb40c2572019-10-19 21:01:05 +02004290 int options = WILD_LIST_NOTFOUND
4291 | WILD_NOERROR | WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004292
4293 ExpandInit(&xpc);
4294 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004295 if (p_wic)
4296 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004297 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01004298 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004299 if (p == NULL)
4300 return FAIL;
4301 }
4302 if (p != NULL)
4303 {
4304 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
4305 p, cmdlinep);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004306 if (n == 2) // p came from ExpandOne()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307 vim_free(p);
4308 }
4309 }
4310 }
4311 }
4312 return OK;
4313}
4314
4315/*
4316 * Replace part of the command line, keeping eap->cmd, eap->arg and
4317 * eap->nextcmd correct.
4318 * "src" points to the part that is to be replaced, of length "srclen".
4319 * "repl" is the replacement string.
4320 * Returns a pointer to the character after the replaced string.
4321 * Returns NULL for failure.
4322 */
4323 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004324repl_cmdline(
4325 exarg_T *eap,
4326 char_u *src,
4327 int srclen,
4328 char_u *repl,
4329 char_u **cmdlinep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004330{
4331 int len;
4332 int i;
4333 char_u *new_cmdline;
4334
4335 /*
4336 * The new command line is build in new_cmdline[].
4337 * First allocate it.
4338 * Careful: a "+cmd" argument may have been NUL terminated.
4339 */
4340 len = (int)STRLEN(repl);
4341 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004342 if (eap->nextcmd != NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004343 i += (int)STRLEN(eap->nextcmd);// add space for next command
Bram Moolenaar964b3742019-05-24 18:54:09 +02004344 if ((new_cmdline = alloc(i)) == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004345 return NULL; // out of memory!
Bram Moolenaar071d4272004-06-13 20:20:40 +00004346
4347 /*
4348 * Copy the stuff before the expanded part.
4349 * Copy the expanded stuff.
4350 * Copy what came after the expanded part.
4351 * Copy the next commands, if there are any.
4352 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004353 i = (int)(src - *cmdlinep); // length of part before match
Bram Moolenaar071d4272004-06-13 20:20:40 +00004354 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00004355
Bram Moolenaar071d4272004-06-13 20:20:40 +00004356 mch_memmove(new_cmdline + i, repl, (size_t)len);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004357 i += len; // remember the end of the string
Bram Moolenaar071d4272004-06-13 20:20:40 +00004358 STRCPY(new_cmdline + i, src + srclen);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004359 src = new_cmdline + i; // remember where to continue
Bram Moolenaar071d4272004-06-13 20:20:40 +00004360
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004361 if (eap->nextcmd != NULL) // append next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362 {
4363 i = (int)STRLEN(new_cmdline) + 1;
4364 STRCPY(new_cmdline + i, eap->nextcmd);
4365 eap->nextcmd = new_cmdline + i;
4366 }
4367 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
4368 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
4369 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
4370 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
4371 vim_free(*cmdlinep);
4372 *cmdlinep = new_cmdline;
4373
4374 return src;
4375}
4376
4377/*
4378 * Check for '|' to separate commands and '"' to start comments.
4379 */
4380 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004381separate_nextcmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004382{
4383 char_u *p;
4384
Bram Moolenaar86b68352004-12-27 21:59:20 +00004385#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004386 p = skip_grep_pat(eap);
4387#else
4388 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004389#endif
4390
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004391 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392 {
4393 if (*p == Ctrl_V)
4394 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004395 if (eap->argt & (EX_CTRLV | EX_XFILE))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004396 ++p; // skip CTRL-V and next char
Bram Moolenaar071d4272004-06-13 20:20:40 +00004397 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004398 // remove CTRL-V and skip next char
Bram Moolenaara7241f52008-06-24 20:39:31 +00004399 STRMOVE(p, p + 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004400 if (*p == NUL) // stop at NUL after CTRL-V
Bram Moolenaar071d4272004-06-13 20:20:40 +00004401 break;
4402 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004403
4404#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004405 // Skip over `=expr` when wildcards are expanded.
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004406 else if (p[0] == '`' && p[1] == '=' && (eap->argt & EX_XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004407 {
4408 p += 2;
4409 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004410 }
4411#endif
4412
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004413 // Check for '"': start of comment or '|': next command
4414 // :@" and :*" do not start a comment!
4415 // :redir @" doesn't either.
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004416 else if ((*p == '"' && !(eap->argt & EX_NOTRLCOM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004417 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
4418 || p != eap->arg)
4419 && (eap->cmdidx != CMD_redir
4420 || p != eap->arg + 1 || p[-1] != '@'))
4421 || *p == '|' || *p == '\n')
4422 {
4423 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004424 * We remove the '\' before the '|', unless EX_CTRLV is used
Bram Moolenaar071d4272004-06-13 20:20:40 +00004425 * AND 'b' is present in 'cpoptions'.
4426 */
4427 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004428 || !(eap->argt & EX_CTRLV)) && *(p - 1) == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004429 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004430 STRMOVE(p - 1, p); // remove the '\'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004431 --p;
4432 }
4433 else
4434 {
4435 eap->nextcmd = check_nextcmd(p);
4436 *p = NUL;
4437 break;
4438 }
4439 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004441
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004442 if (!(eap->argt & EX_NOTRLCOM)) // remove trailing spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00004443 del_trailing_spaces(eap->arg);
4444}
4445
4446/*
4447 * get + command from ex argument
4448 */
4449 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004450getargcmd(char_u **argp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451{
4452 char_u *arg = *argp;
4453 char_u *command = NULL;
4454
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004455 if (*arg == '+') // +[command]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004456 {
4457 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02004458 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004459 command = dollar_command;
4460 else
4461 {
4462 command = arg;
4463 arg = skip_cmd_arg(command, TRUE);
4464 if (*arg != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004465 *arg++ = NUL; // terminate command with NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004466 }
4467
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004468 arg = skipwhite(arg); // skip over spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00004469 *argp = arg;
4470 }
4471 return command;
4472}
4473
4474/*
4475 * Find end of "+command" argument. Skip over "\ " and "\\".
4476 */
Bram Moolenaard0190392019-08-23 21:17:35 +02004477 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004478skip_cmd_arg(
4479 char_u *p,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004480 int rembs) // TRUE to halve the number of backslashes
Bram Moolenaar071d4272004-06-13 20:20:40 +00004481{
4482 while (*p && !vim_isspace(*p))
4483 {
4484 if (*p == '\\' && p[1] != NUL)
4485 {
4486 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00004487 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004488 else
4489 ++p;
4490 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004491 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004492 }
4493 return p;
4494}
4495
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004496 int
4497get_bad_opt(char_u *p, exarg_T *eap)
4498{
4499 if (STRICMP(p, "keep") == 0)
4500 eap->bad_char = BAD_KEEP;
4501 else if (STRICMP(p, "drop") == 0)
4502 eap->bad_char = BAD_DROP;
4503 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
4504 eap->bad_char = *p;
Bram Moolenaar75808492018-06-12 12:39:41 +02004505 else
4506 return FAIL;
4507 return OK;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004508}
4509
Bram Moolenaar071d4272004-06-13 20:20:40 +00004510/*
4511 * Get "++opt=arg" argument.
4512 * Return FAIL or OK.
4513 */
4514 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004515getargopt(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004516{
4517 char_u *arg = eap->arg + 2;
4518 int *pp = NULL;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004519 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004520 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004521
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004522 // ":edit ++[no]bin[ary] file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004523 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
4524 {
4525 if (*arg == 'n')
4526 {
4527 arg += 2;
4528 eap->force_bin = FORCE_NOBIN;
4529 }
4530 else
4531 eap->force_bin = FORCE_BIN;
4532 if (!checkforcmd(&arg, "binary", 3))
4533 return FAIL;
4534 eap->arg = skipwhite(arg);
4535 return OK;
4536 }
4537
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004538 // ":read ++edit file"
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004539 if (STRNCMP(arg, "edit", 4) == 0)
4540 {
4541 eap->read_edit = TRUE;
4542 eap->arg = skipwhite(arg + 4);
4543 return OK;
4544 }
4545
Bram Moolenaar071d4272004-06-13 20:20:40 +00004546 if (STRNCMP(arg, "ff", 2) == 0)
4547 {
4548 arg += 2;
4549 pp = &eap->force_ff;
4550 }
4551 else if (STRNCMP(arg, "fileformat", 10) == 0)
4552 {
4553 arg += 10;
4554 pp = &eap->force_ff;
4555 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004556 else if (STRNCMP(arg, "enc", 3) == 0)
4557 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01004558 if (STRNCMP(arg, "encoding", 8) == 0)
4559 arg += 8;
4560 else
4561 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004562 pp = &eap->force_enc;
4563 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004564 else if (STRNCMP(arg, "bad", 3) == 0)
4565 {
4566 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004567 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004568 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004569
4570 if (pp == NULL || *arg != '=')
4571 return FAIL;
4572
4573 ++arg;
4574 *pp = (int)(arg - eap->cmd);
4575 arg = skip_cmd_arg(arg, FALSE);
4576 eap->arg = skipwhite(arg);
4577 *arg = NUL;
4578
Bram Moolenaar071d4272004-06-13 20:20:40 +00004579 if (pp == &eap->force_ff)
4580 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004581 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
4582 return FAIL;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004583 eap->force_ff = eap->cmd[eap->force_ff];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004584 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004585 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004586 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004587 // Make 'fileencoding' lower case.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004588 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
4589 *p = TOLOWER_ASC(*p);
4590 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004591 else
4592 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004593 // Check ++bad= argument. Must be a single-byte character, "keep" or
4594 // "drop".
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004595 if (get_bad_opt(eap->cmd + bad_char_idx, eap) == FAIL)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004596 return FAIL;
4597 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004598
4599 return OK;
4600}
4601
Bram Moolenaar071d4272004-06-13 20:20:40 +00004602 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004603ex_autocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004604{
4605 /*
Bram Moolenaar26d4b892018-07-04 22:26:28 +02004606 * Disallow autocommands from .exrc and .vimrc in current
Bram Moolenaar071d4272004-06-13 20:20:40 +00004607 * directory for security reasons.
4608 */
4609 if (secure)
4610 {
4611 secure = 2;
4612 eap->errmsg = e_curdir;
4613 }
4614 else if (eap->cmdidx == CMD_autocmd)
4615 do_autocmd(eap->arg, eap->forceit);
4616 else
4617 do_augroup(eap->arg, eap->forceit);
4618}
4619
4620/*
4621 * ":doautocmd": Apply the automatic commands to the current buffer.
4622 */
4623 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004624ex_doautocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004625{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01004626 char_u *arg = eap->arg;
4627 int call_do_modelines = check_nomodeline(&arg);
Bram Moolenaar1610d052016-06-09 22:53:01 +02004628 int did_aucmd;
Bram Moolenaar60542ac2012-02-12 20:14:01 +01004629
Bram Moolenaar1610d052016-06-09 22:53:01 +02004630 (void)do_doautocmd(arg, TRUE, &did_aucmd);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004631 // Only when there is no <nomodeline>.
Bram Moolenaar1610d052016-06-09 22:53:01 +02004632 if (call_do_modelines && did_aucmd)
Bram Moolenaar60542ac2012-02-12 20:14:01 +01004633 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004635
Bram Moolenaar071d4272004-06-13 20:20:40 +00004636/*
4637 * :[N]bunload[!] [N] [bufname] unload buffer
4638 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
4639 * :[N]bwipeout[!] [N] [bufname] delete buffer really
4640 */
4641 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004642ex_bunload(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004643{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004644 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02004645 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004646 eap->errmsg = do_bufdel(
4647 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
4648 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
4649 : DOBUF_UNLOAD, eap->arg,
4650 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
4651}
4652
4653/*
4654 * :[N]buffer [N] to buffer N
4655 * :[N]sbuffer [N] to buffer N
4656 */
4657 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004658ex_buffer(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004659{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004660 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02004661 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004662 if (*eap->arg)
4663 eap->errmsg = e_trailing;
4664 else
4665 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004666 if (eap->addr_count == 0) // default is current buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00004667 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
4668 else
4669 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004670 if (eap->do_ecmd_cmd != NULL)
4671 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004672 }
4673}
4674
4675/*
4676 * :[N]bmodified [N] to next mod. buffer
4677 * :[N]sbmodified [N] to next mod. buffer
4678 */
4679 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004680ex_bmodified(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004681{
4682 goto_buffer(eap, DOBUF_MOD, 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 * :[N]bnext [N] to next buffer
4689 * :[N]sbnext [N] split and to next buffer
4690 */
4691 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004692ex_bnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004693{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004694 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01004695 return;
4696
Bram Moolenaar071d4272004-06-13 20:20:40 +00004697 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004698 if (eap->do_ecmd_cmd != NULL)
4699 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004700}
4701
4702/*
4703 * :[N]bNext [N] to previous buffer
4704 * :[N]bprevious [N] to previous buffer
4705 * :[N]sbNext [N] split and to previous buffer
4706 * :[N]sbprevious [N] split and to previous buffer
4707 */
4708 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004709ex_bprevious(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004710{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004711 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01004712 return;
4713
Bram Moolenaar071d4272004-06-13 20:20:40 +00004714 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004715 if (eap->do_ecmd_cmd != NULL)
4716 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004717}
4718
4719/*
4720 * :brewind to first buffer
4721 * :bfirst to first buffer
4722 * :sbrewind split and to first buffer
4723 * :sbfirst split and to first buffer
4724 */
4725 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004726ex_brewind(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004727{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004728 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01004729 return;
4730
Bram Moolenaar071d4272004-06-13 20:20:40 +00004731 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004732 if (eap->do_ecmd_cmd != NULL)
4733 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004734}
4735
4736/*
4737 * :blast to last buffer
4738 * :sblast split and to last buffer
4739 */
4740 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004741ex_blast(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004742{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004743 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01004744 return;
4745
Bram Moolenaar071d4272004-06-13 20:20:40 +00004746 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004747 if (eap->do_ecmd_cmd != NULL)
4748 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004749}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004750
4751 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004752ends_excmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004753{
4754 return (c == NUL || c == '|' || c == '"' || c == '\n');
4755}
4756
4757#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
4758 || defined(PROTO)
4759/*
4760 * Return the next command, after the first '|' or '\n'.
4761 * Return NULL if not found.
4762 */
4763 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004764find_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004765{
4766 while (*p != '|' && *p != '\n')
4767 {
4768 if (*p == NUL)
4769 return NULL;
4770 ++p;
4771 }
4772 return (p + 1);
4773}
4774#endif
4775
4776/*
Bram Moolenaar2256c992016-11-15 21:17:07 +01004777 * Check if *p is a separator between Ex commands, skipping over white space.
4778 * Return NULL if it isn't, the following character if it is.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004779 */
4780 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004781check_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004782{
Bram Moolenaar2256c992016-11-15 21:17:07 +01004783 char_u *s = skipwhite(p);
4784
4785 if (*s == '|' || *s == '\n')
4786 return (s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004787 else
4788 return NULL;
4789}
4790
4791/*
4792 * - if there are more files to edit
4793 * - and this is the last window
4794 * - and forceit not used
4795 * - and not repeated twice on a row
4796 * return FAIL and give error message if 'message' TRUE
4797 * return OK otherwise
4798 */
4799 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004800check_more(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004801 int message, // when FALSE check only, no messages
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004802 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004803{
4804 int n = ARGCOUNT - curwin->w_arg_idx - 1;
4805
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00004806 if (!forceit && only_one_window()
4807 && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004808 {
4809 if (message)
4810 {
4811#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
4812 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
4813 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02004814 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004815
Bram Moolenaarda6e8912018-08-21 15:12:14 +02004816 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
4817 NGETTEXT("%d more file to edit. Quit anyway?",
4818 "%d more files to edit. Quit anyway?", n), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004819 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
4820 return OK;
4821 return FAIL;
4822 }
4823#endif
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01004824 semsg(NGETTEXT("E173: %d more file to edit",
4825 "E173: %d more files to edit", n), n);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004826 quitmore = 2; // next try to quit is allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004827 }
4828 return FAIL;
4829 }
4830 return OK;
4831}
4832
Bram Moolenaar071d4272004-06-13 20:20:40 +00004833/*
4834 * Function given to ExpandGeneric() to obtain the list of command names.
4835 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004836 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004837get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004838{
4839 if (idx >= (int)CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004840 return get_user_command_name(idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004841 return cmdnames[idx].cmd_name;
4842}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004843
Bram Moolenaar071d4272004-06-13 20:20:40 +00004844 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004845ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004846{
Bram Moolenaare6850792010-05-14 15:28:44 +02004847 if (*eap->arg == NUL)
4848 {
4849#ifdef FEAT_EVAL
4850 char_u *expr = vim_strsave((char_u *)"g:colors_name");
4851 char_u *p = NULL;
4852
4853 if (expr != NULL)
4854 {
4855 ++emsg_off;
4856 p = eval_to_string(expr, NULL, FALSE);
4857 --emsg_off;
4858 vim_free(expr);
4859 }
4860 if (p != NULL)
4861 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004862 msg((char *)p);
Bram Moolenaare6850792010-05-14 15:28:44 +02004863 vim_free(p);
4864 }
4865 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01004866 msg("default");
Bram Moolenaare6850792010-05-14 15:28:44 +02004867#else
Bram Moolenaar32526b32019-01-19 17:43:09 +01004868 msg(_("unknown"));
Bram Moolenaare6850792010-05-14 15:28:44 +02004869#endif
4870 }
4871 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004872 semsg(_("E185: Cannot find color scheme '%s'"), eap->arg);
Bram Moolenaarf58d81a2019-01-28 20:19:05 +01004873
4874#ifdef FEAT_VTP
4875 else if (has_vtp_working())
4876 {
4877 // background color change requires clear + redraw
4878 update_screen(CLEAR);
4879 redrawcmd();
4880 }
4881#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004882}
4883
4884 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004885ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004886{
4887 if (*eap->arg == NUL && eap->cmd[2] == '!')
Bram Moolenaar32526b32019-01-19 17:43:09 +01004888 msg(_("Greetings, Vim user!"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004889 do_highlight(eap->arg, eap->forceit, FALSE);
4890}
4891
4892
4893/*
4894 * Call this function if we thought we were going to exit, but we won't
4895 * (because of an error). May need to restore the terminal mode.
4896 */
4897 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004898not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004899{
4900 exiting = FALSE;
4901 settmode(TMODE_RAW);
4902}
4903
Bram Moolenaar12a96de2018-03-11 14:44:18 +01004904 static int
4905before_quit_autocmds(win_T *wp, int quit_all, int forceit)
4906{
4907 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, wp->w_buffer);
4908
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02004909 // Bail out when autocommands closed the window.
4910 // Refuse to quit when the buffer in the last window is being closed (can
4911 // only happen in autocommands).
Bram Moolenaar12a96de2018-03-11 14:44:18 +01004912 if (!win_valid(wp)
4913 || curbuf_locked()
4914 || (wp->w_buffer->b_nwindows == 1 && wp->w_buffer->b_locked > 0))
4915 return TRUE;
4916
4917 if (quit_all || (check_more(FALSE, forceit) == OK && only_one_window()))
4918 {
4919 apply_autocmds(EVENT_EXITPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02004920 // Refuse to quit when locked or when the window was closed or the
4921 // buffer in the last window is being closed (can only happen in
4922 // autocommands).
4923 if (!win_valid(wp) || curbuf_locked()
Bram Moolenaar12a96de2018-03-11 14:44:18 +01004924 || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
4925 return TRUE;
4926 }
4927
4928 return FALSE;
4929}
4930
Bram Moolenaar071d4272004-06-13 20:20:40 +00004931/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01004932 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar12a96de2018-03-11 14:44:18 +01004933 * ":{nr}quit": quit window {nr}
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02004934 * Also used when closing a terminal window that's the last one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004935 */
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02004936 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004937ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004939 win_T *wp;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004940
Bram Moolenaar071d4272004-06-13 20:20:40 +00004941#ifdef FEAT_CMDWIN
4942 if (cmdwin_type != 0)
4943 {
4944 cmdwin_result = Ctrl_C;
4945 return;
4946 }
4947#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004948 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004949 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004950 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004951 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004952 return;
4953 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004954 if (eap->addr_count > 0)
4955 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01004956 int wnr = eap->line2;
4957
4958 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
4959 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004960 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004961 }
4962 else
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004963 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01004964
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004965 // Refuse to quit when locked.
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02004966 if (curbuf_locked())
4967 return;
Bram Moolenaar12a96de2018-03-11 14:44:18 +01004968
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004969 // Trigger QuitPre and maybe ExitPre
Bram Moolenaar12a96de2018-03-11 14:44:18 +01004970 if (before_quit_autocmds(wp, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004971 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004972
4973#ifdef FEAT_NETBEANS_INTG
4974 netbeansForcedQuit = eap->forceit;
4975#endif
4976
4977 /*
4978 * If there are more files or windows we won't exit.
4979 */
4980 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
4981 exiting = TRUE;
Bram Moolenaarff930ca2017-10-19 17:12:10 +02004982 if ((!buf_hide(wp->w_buffer)
4983 && check_changed(wp->w_buffer, (p_awa ? CCGD_AW : 0)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01004984 | (eap->forceit ? CCGD_FORCEIT : 0)
4985 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004986 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01004987 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004988 {
4989 not_exiting();
4990 }
4991 else
4992 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004993 // quit last window
4994 // Note: only_one_window() returns true, even so a help window is
4995 // still open. In that case only quit, if no address has been
4996 // specified. Example:
4997 // :h|wincmd w|1q - don't quit
4998 // :h|wincmd w|q - quit
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01004999 if (only_one_window() && (ONE_WINDOW || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005000 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02005001 not_exiting();
Bram Moolenaar4033c552017-09-16 20:54:51 +02005002#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005003 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005004#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005005 // close window; may free buffer
Bram Moolenaareb44a682017-08-03 22:44:55 +02005006 win_close(wp, !buf_hide(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005007 }
5008}
5009
5010/*
5011 * ":cquit".
5012 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005013 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005014ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005015{
Bram Moolenaar1860bde2020-01-06 21:47:21 +01005016 // this does not always pass on the exit code to the Manx compiler. why?
5017 getout(eap->addr_count > 0 ? (int)eap->line2 : EXIT_FAILURE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005018}
5019
5020/*
5021 * ":qall": try to quit all windows
5022 */
5023 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005024ex_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005025{
5026# ifdef FEAT_CMDWIN
5027 if (cmdwin_type != 0)
5028 {
5029 if (eap->forceit)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005030 cmdwin_result = K_XF1; // ex_window() takes care of this
Bram Moolenaar071d4272004-06-13 20:20:40 +00005031 else
5032 cmdwin_result = K_XF2;
5033 return;
5034 }
5035# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005036
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005037 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005038 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005039 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005040 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005041 return;
5042 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005043
5044 if (before_quit_autocmds(curwin, TRUE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005045 return;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005046
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01005048 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005049 getout(0);
5050 not_exiting();
5051}
5052
Bram Moolenaar071d4272004-06-13 20:20:40 +00005053/*
5054 * ":close": close current window, unless it is the last one
5055 */
5056 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005057ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005058{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005059 win_T *win;
5060 int winnr = 0;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005061#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00005062 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02005063 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005064 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005065#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005066 if (!text_locked() && !curbuf_locked())
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005067 {
5068 if (eap->addr_count == 0)
5069 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar6d41c782018-06-06 09:11:12 +02005070 else
5071 {
Bram Moolenaar29323592016-07-24 22:04:11 +02005072 FOR_ALL_WINDOWS(win)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005073 {
5074 winnr++;
5075 if (winnr == eap->line2)
5076 break;
5077 }
5078 if (win == NULL)
5079 win = lastwin;
5080 ex_win_close(eap->forceit, win, NULL);
5081 }
5082 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005083}
5084
Bram Moolenaar4033c552017-09-16 20:54:51 +02005085#ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005086/*
5087 * ":pclose": Close any preview window.
5088 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005089 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005090ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005091{
5092 win_T *win;
5093
Bram Moolenaar79648732019-07-18 21:43:07 +02005094 // First close any normal window.
Bram Moolenaar29323592016-07-24 22:04:11 +02005095 FOR_ALL_WINDOWS(win)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005096 if (win->w_p_pvw)
5097 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005098 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar79648732019-07-18 21:43:07 +02005099 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005100 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005101# ifdef FEAT_PROP_POPUP
Bram Moolenaar79648732019-07-18 21:43:07 +02005102 // Also when 'previewpopup' is empty, it might have been cleared.
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02005103 popup_close_preview();
Bram Moolenaar79648732019-07-18 21:43:07 +02005104# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005105}
Bram Moolenaar4033c552017-09-16 20:54:51 +02005106#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005107
Bram Moolenaarf740b292006-02-16 22:11:02 +00005108/*
5109 * Close window "win" and take care of handling closing the last window for a
5110 * modified buffer.
5111 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005112 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005113ex_win_close(
5114 int forceit,
5115 win_T *win,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005116 tabpage_T *tp) // NULL or the tab page "win" is in
Bram Moolenaar071d4272004-06-13 20:20:40 +00005117{
5118 int need_hide;
5119 buf_T *buf = win->w_buffer;
5120
5121 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02005122 if (need_hide && !buf_hide(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005123 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02005124#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005125 if ((p_confirm || cmdmod.confirm) && p_write)
5126 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005127 bufref_T bufref;
5128
5129 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005130 dialog_changed(buf, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005131 if (bufref_valid(&bufref) && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005132 return;
5133 need_hide = FALSE;
5134 }
5135 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005136#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005137 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02005138 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005139 return;
5140 }
5141 }
5142
Bram Moolenaar4033c552017-09-16 20:54:51 +02005143#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005144 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005145#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005146
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005147 // free buffer when not hiding it or when it's a scratch buffer
Bram Moolenaarf740b292006-02-16 22:11:02 +00005148 if (tp == NULL)
Bram Moolenaareb44a682017-08-03 22:44:55 +02005149 win_close(win, !need_hide && !buf_hide(buf));
Bram Moolenaarf740b292006-02-16 22:11:02 +00005150 else
Bram Moolenaareb44a682017-08-03 22:44:55 +02005151 win_close_othertab(win, !need_hide && !buf_hide(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005152}
5153
Bram Moolenaar071d4272004-06-13 20:20:40 +00005154/*
Bram Moolenaardea25702017-01-29 15:18:10 +01005155 * Handle the argument for a tabpage related ex command.
5156 * Returns a tabpage number.
5157 * When an error is encountered then eap->errmsg is set.
5158 */
5159 static int
5160get_tabpage_arg(exarg_T *eap)
5161{
5162 int tab_number;
5163 int unaccept_arg0 = (eap->cmdidx == CMD_tabmove) ? 0 : 1;
5164
5165 if (eap->arg && *eap->arg != NUL)
5166 {
5167 char_u *p = eap->arg;
5168 char_u *p_save;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005169 int relative = 0; // argument +N/-N means: go to N places to the
5170 // right/left relative to the current position.
Bram Moolenaardea25702017-01-29 15:18:10 +01005171
5172 if (*p == '-')
5173 {
5174 relative = -1;
5175 p++;
5176 }
5177 else if (*p == '+')
5178 {
5179 relative = 1;
5180 p++;
5181 }
5182
5183 p_save = p;
5184 tab_number = getdigits(&p);
5185
5186 if (relative == 0)
5187 {
5188 if (STRCMP(p, "$") == 0)
5189 tab_number = LAST_TAB_NR;
5190 else if (p == p_save || *p_save == '-' || *p != NUL
5191 || tab_number > LAST_TAB_NR)
5192 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005193 // No numbers as argument.
Bram Moolenaardea25702017-01-29 15:18:10 +01005194 eap->errmsg = e_invarg;
5195 goto theend;
5196 }
5197 }
5198 else
5199 {
5200 if (*p_save == NUL)
5201 tab_number = 1;
5202 else if (p == p_save || *p_save == '-' || *p != NUL
5203 || tab_number == 0)
5204 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005205 // No numbers as argument.
Bram Moolenaardea25702017-01-29 15:18:10 +01005206 eap->errmsg = e_invarg;
5207 goto theend;
5208 }
5209 tab_number = tab_number * relative + tabpage_index(curtab);
5210 if (!unaccept_arg0 && relative == -1)
5211 --tab_number;
5212 }
5213 if (tab_number < unaccept_arg0 || tab_number > LAST_TAB_NR)
5214 eap->errmsg = e_invarg;
5215 }
5216 else if (eap->addr_count > 0)
5217 {
5218 if (unaccept_arg0 && eap->line2 == 0)
Bram Moolenaarc6251552017-01-29 21:42:20 +01005219 {
Bram Moolenaardea25702017-01-29 15:18:10 +01005220 eap->errmsg = e_invrange;
Bram Moolenaarc6251552017-01-29 21:42:20 +01005221 tab_number = 0;
5222 }
Bram Moolenaardea25702017-01-29 15:18:10 +01005223 else
5224 {
5225 tab_number = eap->line2;
Bram Moolenaar82a12462019-01-22 22:41:42 +01005226 if (!unaccept_arg0 && *skipwhite(*eap->cmdlinep) == '-')
Bram Moolenaardea25702017-01-29 15:18:10 +01005227 {
5228 --tab_number;
5229 if (tab_number < unaccept_arg0)
5230 eap->errmsg = e_invarg;
5231 }
5232 }
5233 }
5234 else
5235 {
5236 switch (eap->cmdidx)
5237 {
5238 case CMD_tabnext:
5239 tab_number = tabpage_index(curtab) + 1;
5240 if (tab_number > LAST_TAB_NR)
5241 tab_number = 1;
5242 break;
5243 case CMD_tabmove:
5244 tab_number = LAST_TAB_NR;
5245 break;
5246 default:
5247 tab_number = tabpage_index(curtab);
5248 }
5249 }
5250
5251theend:
5252 return tab_number;
5253}
5254
5255/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00005256 * ":tabclose": close current tab page, unless it is the last one.
5257 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005258 */
5259 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005260ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005261{
Bram Moolenaarf740b292006-02-16 22:11:02 +00005262 tabpage_T *tp;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005263 int tab_number;
Bram Moolenaarf740b292006-02-16 22:11:02 +00005264
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005265# ifdef FEAT_CMDWIN
5266 if (cmdwin_type != 0)
5267 cmdwin_result = K_IGNORE;
5268 else
5269# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005270 if (first_tabpage->tp_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005271 emsg(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00005272 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005273 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005274 tab_number = get_tabpage_arg(eap);
5275 if (eap->errmsg == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005276 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005277 tp = find_tabpage(tab_number);
Bram Moolenaarf740b292006-02-16 22:11:02 +00005278 if (tp == NULL)
5279 {
5280 beep_flush();
5281 return;
5282 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005283 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005284 {
5285 tabpage_close_other(tp, eap->forceit);
5286 return;
5287 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005288 else if (!text_locked() && !curbuf_locked())
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005289 tabpage_close(eap->forceit);
5290 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005291 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005292}
5293
5294/*
5295 * ":tabonly": close all tab pages except the current one
5296 */
5297 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005298ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005299{
5300 tabpage_T *tp;
5301 int done;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005302 int tab_number;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005303
5304# ifdef FEAT_CMDWIN
5305 if (cmdwin_type != 0)
5306 cmdwin_result = K_IGNORE;
5307 else
5308# endif
5309 if (first_tabpage->tp_next == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005310 msg(_("Already only one tab page"));
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005311 else
5312 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005313 tab_number = get_tabpage_arg(eap);
5314 if (eap->errmsg == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005315 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005316 goto_tabpage(tab_number);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005317 // Repeat this up to a 1000 times, because autocommands may
5318 // mess up the lists.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005319 for (done = 0; done < 1000; ++done)
5320 {
5321 FOR_ALL_TABPAGES(tp)
5322 if (tp->tp_topframe != topframe)
5323 {
5324 tabpage_close_other(tp, eap->forceit);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005325 // if we failed to close it quit
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005326 if (valid_tabpage(tp))
5327 done = 1000;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005328 // start over, "tp" is now invalid
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005329 break;
5330 }
5331 if (first_tabpage->tp_next == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005332 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005333 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005334 }
5335 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005336}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005337
5338/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00005339 * Close the current tab page.
5340 */
5341 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005342tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005343{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005344 // First close all the windows but the current one. If that worked then
5345 // close the last window in this tab, that will close it.
Bram Moolenaar459ca562016-11-10 18:16:33 +01005346 if (!ONE_WINDOW)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005347 close_others(TRUE, forceit);
Bram Moolenaar459ca562016-11-10 18:16:33 +01005348 if (ONE_WINDOW)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005349 ex_win_close(forceit, curwin, NULL);
5350# ifdef FEAT_GUI
5351 need_mouse_correct = TRUE;
5352# endif
5353}
5354
5355/*
5356 * Close tab page "tp", which is not the current tab page.
5357 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005358 * Also takes care of the tab pages line disappearing when closing the
5359 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00005360 */
5361 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005362tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005363{
5364 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005365 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005366 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00005367
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005368 // Limit to 1000 windows, autocommands may add a window while we close
5369 // one. OK, so I'm paranoid...
Bram Moolenaarf740b292006-02-16 22:11:02 +00005370 while (++done < 1000)
5371 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005372 wp = tp->tp_firstwin;
5373 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00005374
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005375 // Autocommands may delete the tab page under our fingers and we may
5376 // fail to close a window with a modified buffer.
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005377 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005378 break;
5379 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005380
Bram Moolenaar29323592016-07-24 22:04:11 +02005381 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar29323592016-07-24 22:04:11 +02005382
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005383 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005384 if (h != tabline_height())
5385 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00005386}
5387
5388/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005389 * ":only".
5390 */
5391 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005392ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005393{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005394 win_T *wp;
5395 int wnr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005396# ifdef FEAT_GUI
5397 need_mouse_correct = TRUE;
5398# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005399 if (eap->addr_count > 0)
5400 {
5401 wnr = eap->line2;
5402 for (wp = firstwin; --wnr > 0; )
5403 {
5404 if (wp->w_next == NULL)
5405 break;
5406 else
5407 wp = wp->w_next;
5408 }
5409 win_goto(wp);
5410 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005411 close_others(TRUE, eap->forceit);
5412}
5413
Bram Moolenaar071d4272004-06-13 20:20:40 +00005414 static void
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01005415ex_hide(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005416{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005417 // ":hide" or ":hide | cmd": hide current window
Bram Moolenaar2256c992016-11-15 21:17:07 +01005418 if (!eap->skip)
5419 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02005420#ifdef FEAT_GUI
Bram Moolenaar2256c992016-11-15 21:17:07 +01005421 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005422#endif
Bram Moolenaar2256c992016-11-15 21:17:07 +01005423 if (eap->addr_count == 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005424 win_close(curwin, FALSE); // don't free buffer
Bram Moolenaar2256c992016-11-15 21:17:07 +01005425 else
5426 {
5427 int winnr = 0;
5428 win_T *win;
Bram Moolenaarf240e182014-11-27 18:33:02 +01005429
Bram Moolenaar2256c992016-11-15 21:17:07 +01005430 FOR_ALL_WINDOWS(win)
5431 {
5432 winnr++;
5433 if (winnr == eap->line2)
5434 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005435 }
Bram Moolenaar2256c992016-11-15 21:17:07 +01005436 if (win == NULL)
5437 win = lastwin;
5438 win_close(win, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005439 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005440 }
5441}
5442
5443/*
5444 * ":stop" and ":suspend": Suspend Vim.
5445 */
5446 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005447ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005448{
5449 /*
5450 * Disallow suspending for "rvim".
5451 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005452 if (!check_restricted())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005453 {
5454 if (!eap->forceit)
5455 autowrite_all();
5456 windgoto((int)Rows - 1, 0);
5457 out_char('\n');
5458 out_flush();
5459 stoptermcap();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005460 out_flush(); // needed for SUN to restore xterm buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00005461#ifdef FEAT_TITLE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005462 mch_restore_title(SAVE_RESTORE_BOTH); // restore window titles
Bram Moolenaar071d4272004-06-13 20:20:40 +00005463#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005464 ui_suspend(); // call machine specific function
Bram Moolenaar071d4272004-06-13 20:20:40 +00005465#ifdef FEAT_TITLE
5466 maketitle();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005467 resettitle(); // force updating the title
Bram Moolenaar071d4272004-06-13 20:20:40 +00005468#endif
5469 starttermcap();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005470 scroll_start(); // scroll screen before redrawing
Bram Moolenaar071d4272004-06-13 20:20:40 +00005471 redraw_later_clear();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005472 shell_resized(); // may have resized window
Bram Moolenaar071d4272004-06-13 20:20:40 +00005473 }
5474}
5475
5476/*
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005477 * ":exit", ":xit" and ":wq": Write file and quite the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005478 */
5479 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005480ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005481{
5482#ifdef FEAT_CMDWIN
5483 if (cmdwin_type != 0)
5484 {
5485 cmdwin_result = Ctrl_C;
5486 return;
5487 }
5488#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005489 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005490 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005491 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005492 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005493 return;
5494 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005495
5496 if (before_quit_autocmds(curwin, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005497 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005498
5499 /*
5500 * if more files or windows we won't exit
5501 */
5502 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
5503 exiting = TRUE;
5504 if ( ((eap->cmdidx == CMD_wq
5505 || curbufIsChanged())
5506 && do_write(eap) == FAIL)
5507 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01005508 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005509 {
5510 not_exiting();
5511 }
5512 else
5513 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005514 if (only_one_window()) // quit last window, exit Vim
Bram Moolenaar071d4272004-06-13 20:20:40 +00005515 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02005516 not_exiting();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005517# ifdef FEAT_GUI
5518 need_mouse_correct = TRUE;
5519# endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005520 // Quit current window, may free the buffer.
Bram Moolenaareb44a682017-08-03 22:44:55 +02005521 win_close(curwin, !buf_hide(curwin->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005522 }
5523}
5524
5525/*
5526 * ":print", ":list", ":number".
5527 */
5528 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005529ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005530{
Bram Moolenaardf177f62005-02-22 08:39:57 +00005531 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005532 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +00005533 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005534 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00005535 for ( ;!got_int; ui_breakcheck())
5536 {
5537 print_line(eap->line1,
5538 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
5539 || (eap->flags & EXFLAG_NR)),
5540 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
5541 if (++eap->line1 > eap->line2)
5542 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005543 out_flush(); // show one line at a time
Bram Moolenaardf177f62005-02-22 08:39:57 +00005544 }
5545 setpcmark();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005546 // put cursor at last line
Bram Moolenaardf177f62005-02-22 08:39:57 +00005547 curwin->w_cursor.lnum = eap->line2;
5548 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005549 }
5550
Bram Moolenaar071d4272004-06-13 20:20:40 +00005551 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005552}
5553
5554#ifdef FEAT_BYTEOFF
5555 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005556ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005557{
5558 goto_byte(eap->line2);
5559}
5560#endif
5561
5562/*
5563 * ":shell".
5564 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005565 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005566ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005567{
5568 do_shell(NULL, 0);
5569}
5570
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005571#if defined(HAVE_DROP_FILE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005572
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005573static int drop_busy = FALSE;
5574static int drop_filec;
5575static char_u **drop_filev = NULL;
5576static int drop_split;
5577static void (*drop_callback)(void *);
5578static void *drop_cookie;
5579
5580 static void
5581handle_drop_internal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005582{
5583 exarg_T ea;
5584 int save_msg_scroll = msg_scroll;
5585
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005586 // Setting the argument list may cause screen updates and being called
5587 // recursively. Avoid that by setting drop_busy.
5588 drop_busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005589
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005590 // Check whether the current buffer is changed. If so, we will need
5591 // to split the current window or data could be lost.
5592 // We don't need to check if the 'hidden' option is set, as in this
5593 // case the buffer won't be lost.
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005594 if (!buf_hide(curbuf) && !drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005595 {
5596 ++emsg_off;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005597 drop_split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005598 --emsg_off;
5599 }
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005600 if (drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005601 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005602 if (win_split(0, 0) == FAIL)
5603 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02005604 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005605
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005606 // When splitting the window, create a new alist. Otherwise the
5607 // existing one is overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005608 alist_unlink(curwin->w_alist);
5609 alist_new();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005610 }
5611
5612 /*
5613 * Set up the new argument list.
5614 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005615 alist_set(ALIST(curwin), drop_filec, drop_filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005616
5617 /*
5618 * Move to the first file.
5619 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005620 // Fake up a minimal "next" command for do_argfile()
Bram Moolenaar071d4272004-06-13 20:20:40 +00005621 vim_memset(&ea, 0, sizeof(ea));
5622 ea.cmd = (char_u *)"next";
5623 do_argfile(&ea, 0);
5624
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005625 // do_ecmd() may set need_start_insertmode, but since we never left Insert
5626 // mode that is not needed here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005627 need_start_insertmode = FALSE;
5628
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005629 // Restore msg_scroll, otherwise a following command may cause scrolling
5630 // unexpectedly. The screen will be redrawn by the caller, thus
5631 // msg_scroll being set by displaying a message is irrelevant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005632 msg_scroll = save_msg_scroll;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005633
5634 if (drop_callback != NULL)
5635 drop_callback(drop_cookie);
5636
5637 drop_filev = NULL;
5638 drop_busy = FALSE;
5639}
5640
5641/*
5642 * Handle a file drop. The code is here because a drop is *nearly* like an
5643 * :args command, but not quite (we have a list of exact filenames, so we
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01005644 * don't want to (a) parse a command line, or (b) expand wildcards). So the
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005645 * code is very similar to :args and hence needs access to a lot of the static
5646 * functions in this file.
5647 *
5648 * The "filev" list must have been allocated using alloc(), as should each item
5649 * in the list. This function takes over responsibility for freeing the "filev"
5650 * list.
5651 */
5652 void
5653handle_drop(
5654 int filec, // the number of files dropped
5655 char_u **filev, // the list of files dropped
5656 int split, // force splitting the window
5657 void (*callback)(void *), // to be called after setting the argument
5658 // list
5659 void *cookie) // argument for "callback" (allocated)
5660{
5661 // Cannot handle recursive drops, finish the pending one.
5662 if (drop_busy)
5663 {
5664 FreeWild(filec, filev);
5665 vim_free(cookie);
5666 return;
5667 }
5668
5669 // When calling handle_drop() more than once in a row we only use the last
5670 // one.
5671 if (drop_filev != NULL)
5672 {
5673 FreeWild(drop_filec, drop_filev);
5674 vim_free(drop_cookie);
5675 }
5676
5677 drop_filec = filec;
5678 drop_filev = filev;
5679 drop_split = split;
5680 drop_callback = callback;
5681 drop_cookie = cookie;
5682
5683 // Postpone this when:
5684 // - editing the command line
5685 // - not possible to change the current buffer
5686 // - updating the screen
5687 // As it may change buffers and window structures that are in use and cause
5688 // freed memory to be used.
5689 if (text_locked() || curbuf_locked() || updating_screen)
5690 return;
5691
5692 handle_drop_internal();
5693}
5694
5695/*
5696 * To be called when text is unlocked, curbuf is unlocked or updating_screen is
5697 * reset: Handle a postponed drop.
5698 */
5699 void
5700handle_any_postponed_drop(void)
5701{
5702 if (!drop_busy && drop_filev != NULL
5703 && !text_locked() && !curbuf_locked() && !updating_screen)
5704 handle_drop_internal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005705}
5706#endif
5707
Bram Moolenaar071d4272004-06-13 20:20:40 +00005708/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005709 * ":preserve".
5710 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005711 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005712ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005713{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005714 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005715 ml_preserve(curbuf, TRUE);
5716}
5717
5718/*
5719 * ":recover".
5720 */
5721 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005722ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005723{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005724 // Set recoverymode right away to avoid the ATTENTION prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005725 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01005726 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
5727 | CCGD_MULTWIN
5728 | (eap->forceit ? CCGD_FORCEIT : 0)
5729 | CCGD_EXCMD)
5730
Bram Moolenaar071d4272004-06-13 20:20:40 +00005731 && (*eap->arg == NUL
5732 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
Bram Moolenaar99499b12019-05-23 21:35:48 +02005733 ml_recover(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005734 recoverymode = FALSE;
5735}
5736
5737/*
5738 * Command modifier used in a wrong way.
5739 */
5740 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005741ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005742{
5743 eap->errmsg = e_invcmd;
5744}
5745
Bram Moolenaar071d4272004-06-13 20:20:40 +00005746/*
5747 * :sview [+command] file split window with new file, read-only
5748 * :split [[+command] file] split window with current or new file
5749 * :vsplit [[+command] file] split window vertically with current or new file
5750 * :new [[+command] file] split window with no or new file
5751 * :vnew [[+command] file] split vertically window with no or new file
5752 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005753 *
5754 * :tabedit open new Tab page with empty window
5755 * :tabedit [+command] file open new Tab page and edit "file"
5756 * :tabnew [[+command] file] just like :tabedit
5757 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005758 */
5759 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005760ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005761{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005762 win_T *old_curwin = curwin;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005763#if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005764 char_u *fname = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005765#endif
5766#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005767 int browse_flag = cmdmod.browse;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005768#endif
Bram Moolenaar39902a02018-06-21 22:10:08 +02005769 int use_tab = eap->cmdidx == CMD_tabedit
5770 || eap->cmdidx == CMD_tabfind
5771 || eap->cmdidx == CMD_tabnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005772
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005773 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005774 return;
5775
Bram Moolenaar4033c552017-09-16 20:54:51 +02005776#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005777 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005778#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005779
Bram Moolenaar4033c552017-09-16 20:54:51 +02005780#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005781 // A ":split" in the quickfix window works like ":new". Don't want two
5782 // quickfix windows. But it's OK when doing ":tab split".
Bram Moolenaar05bb9532008-07-04 09:44:11 +00005783 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005784 {
5785 if (eap->cmdidx == CMD_split)
5786 eap->cmdidx = CMD_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005787 if (eap->cmdidx == CMD_vsplit)
5788 eap->cmdidx = CMD_vnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005789 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02005790#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005791
Bram Moolenaar4033c552017-09-16 20:54:51 +02005792#ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005793 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005794 {
5795 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
5796 FNAME_MESS, TRUE, curbuf->b_ffname);
5797 if (fname == NULL)
5798 goto theend;
5799 eap->arg = fname;
5800 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005801# ifdef FEAT_BROWSE
Bram Moolenaar4033c552017-09-16 20:54:51 +02005802 else
5803# endif
5804#endif
5805#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005806 if (cmdmod.browse
Bram Moolenaar071d4272004-06-13 20:20:40 +00005807 && eap->cmdidx != CMD_vnew
Bram Moolenaar071d4272004-06-13 20:20:40 +00005808 && eap->cmdidx != CMD_new)
5809 {
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005810 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005811# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005812 !gui.in_use &&
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005813# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005814 au_has_group((char_u *)"FileExplorer"))
5815 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005816 // No browsing supported but we do have the file explorer:
5817 // Edit the directory.
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005818 if (*eap->arg == NUL || !mch_isdir(eap->arg))
5819 eap->arg = (char_u *)".";
5820 }
5821 else
5822 {
Bram Moolenaar39902a02018-06-21 22:10:08 +02005823 fname = do_browse(0, (char_u *)(use_tab
5824 ? _("Edit File in new tab page")
5825 : _("Edit File in new window")),
Bram Moolenaar071d4272004-06-13 20:20:40 +00005826 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005827 if (fname == NULL)
5828 goto theend;
5829 eap->arg = fname;
5830 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005831 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005832 cmdmod.browse = FALSE; // Don't browse again in do_ecmd().
Bram Moolenaar4033c552017-09-16 20:54:51 +02005833#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005834
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005835 /*
5836 * Either open new tab page or split the window.
5837 */
Bram Moolenaar39902a02018-06-21 22:10:08 +02005838 if (use_tab)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005839 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005840 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
5841 : eap->addr_count == 0 ? 0
5842 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005843 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00005844 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005845
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005846 // set the alternate buffer for the window we came from
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005847 if (curwin != old_curwin
5848 && win_valid(old_curwin)
5849 && old_curwin->w_buffer != curbuf
5850 && !cmdmod.keepalt)
5851 old_curwin->w_alt_fnum = curbuf->b_fnum;
5852 }
5853 }
5854 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005855 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
5856 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005857 // Reset 'scrollbind' when editing another file, but keep it when
5858 // doing ":split" without arguments.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005859 if (*eap->arg != NUL
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01005860# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005861 || cmdmod.browse
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01005862# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005863 )
Bram Moolenaar3368ea22010-09-21 16:56:35 +02005864 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005865 else
5866 do_check_scrollbind(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005867 do_exedit(eap, old_curwin);
5868 }
5869
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005870# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005871 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005872# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005873
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005874# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005875theend:
5876 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005877# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005878}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005879
5880/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00005881 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005882 */
5883 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005884tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00005885{
5886 exarg_T ea;
5887
5888 vim_memset(&ea, 0, sizeof(ea));
5889 ea.cmdidx = CMD_tabnew;
5890 ea.cmd = (char_u *)"tabn";
5891 ea.arg = (char_u *)"";
5892 ex_splitview(&ea);
5893}
5894
5895/*
5896 * :tabnext command
5897 */
5898 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005899ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005900{
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005901 int tab_number;
5902
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02005903 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005904 return;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005905 switch (eap->cmdidx)
5906 {
5907 case CMD_tabfirst:
5908 case CMD_tabrewind:
5909 goto_tabpage(1);
5910 break;
5911 case CMD_tablast:
5912 goto_tabpage(9999);
5913 break;
5914 case CMD_tabprevious:
5915 case CMD_tabNext:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005916 if (eap->arg && *eap->arg != NUL)
5917 {
5918 char_u *p = eap->arg;
5919 char_u *p_save = p;
5920
5921 tab_number = getdigits(&p);
5922 if (p == p_save || *p_save == '-' || *p != NUL
5923 || tab_number == 0)
5924 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005925 // No numbers as argument.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005926 eap->errmsg = e_invarg;
5927 return;
5928 }
5929 }
5930 else
5931 {
5932 if (eap->addr_count == 0)
5933 tab_number = 1;
5934 else
5935 {
5936 tab_number = eap->line2;
5937 if (tab_number < 1)
5938 {
5939 eap->errmsg = e_invrange;
5940 return;
5941 }
5942 }
5943 }
5944 goto_tabpage(-tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005945 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005946 default: // CMD_tabnext
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005947 tab_number = get_tabpage_arg(eap);
5948 if (eap->errmsg == NULL)
5949 goto_tabpage(tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005950 break;
5951 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00005952}
5953
5954/*
5955 * :tabmove command
5956 */
5957 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005958ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00005959{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02005960 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02005961
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005962 tab_number = get_tabpage_arg(eap);
5963 if (eap->errmsg == NULL)
5964 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005965}
5966
5967/*
5968 * :tabs command: List tabs and their contents.
5969 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005970 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005971ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005972{
5973 tabpage_T *tp;
5974 win_T *wp;
5975 int tabcount = 1;
5976
5977 msg_start();
5978 msg_scroll = TRUE;
5979 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
5980 {
5981 msg_putchar('\n');
5982 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
Bram Moolenaar8820b482017-03-16 17:23:31 +01005983 msg_outtrans_attr(IObuff, HL_ATTR(HLF_T));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005984 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005985 ui_breakcheck();
5986
Bram Moolenaar030f0df2006-02-21 22:02:53 +00005987 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005988 wp = firstwin;
5989 else
5990 wp = tp->tp_firstwin;
5991 for ( ; wp != NULL && !got_int; wp = wp->w_next)
5992 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00005993 msg_putchar('\n');
5994 msg_putchar(wp == curwin ? '>' : ' ');
5995 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005996 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
5997 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005998 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005999 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006000 else
6001 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
6002 IObuff, IOSIZE, TRUE);
6003 msg_outtrans(IObuff);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006004 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006005 ui_breakcheck();
6006 }
6007 }
6008}
6009
Bram Moolenaar071d4272004-06-13 20:20:40 +00006010/*
6011 * ":mode": Set screen mode.
6012 * If no argument given, just get the screen size and redraw.
6013 */
6014 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006015ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006016{
6017 if (*eap->arg == NUL)
6018 shell_resized();
6019 else
Bram Moolenaar3c71aec2020-01-17 19:32:20 +01006020 emsg(_(e_screenmode));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006021}
6022
Bram Moolenaar071d4272004-06-13 20:20:40 +00006023/*
6024 * ":resize".
6025 * set, increment or decrement current window height
6026 */
6027 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006028ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006029{
6030 int n;
6031 win_T *wp = curwin;
6032
6033 if (eap->addr_count > 0)
6034 {
6035 n = eap->line2;
6036 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
6037 ;
6038 }
6039
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006040# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006041 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006042# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006043 n = atol((char *)eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006044 if (cmdmod.split & WSP_VERT)
6045 {
6046 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar02631462017-09-22 15:20:32 +02006047 n += curwin->w_width;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006048 else if (n == 0 && eap->arg[0] == NUL) // default is very wide
Bram Moolenaar071d4272004-06-13 20:20:40 +00006049 n = 9999;
6050 win_setwidth_win((int)n, wp);
6051 }
6052 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006053 {
6054 if (*eap->arg == '-' || *eap->arg == '+')
6055 n += curwin->w_height;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006056 else if (n == 0 && eap->arg[0] == NUL) // default is very high
Bram Moolenaar071d4272004-06-13 20:20:40 +00006057 n = 9999;
6058 win_setheight_win((int)n, wp);
6059 }
6060}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006061
6062/*
6063 * ":find [+command] <file>" command.
6064 */
6065 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006066ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006067{
6068#ifdef FEAT_SEARCHPATH
6069 char_u *fname;
6070 int count;
6071
6072 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
6073 TRUE, curbuf->b_ffname);
6074 if (eap->addr_count > 0)
6075 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006076 // Repeat finding the file "count" times. This matters when it
6077 // appears several times in the path.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006078 count = eap->line2;
6079 while (fname != NULL && --count > 0)
6080 {
6081 vim_free(fname);
6082 fname = find_file_in_path(NULL, 0, FNAME_MESS,
6083 FALSE, curbuf->b_ffname);
6084 }
6085 }
6086
6087 if (fname != NULL)
6088 {
6089 eap->arg = fname;
6090#endif
6091 do_exedit(eap, NULL);
6092#ifdef FEAT_SEARCHPATH
6093 vim_free(fname);
6094 }
6095#endif
6096}
6097
6098/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00006099 * ":open" simulation: for now just work like ":visual".
6100 */
6101 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006102ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006103{
6104 regmatch_T regmatch;
6105 char_u *p;
6106
6107 curwin->w_cursor.lnum = eap->line2;
6108 beginline(BL_SOL | BL_FIX);
6109 if (*eap->arg == '/')
6110 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006111 // ":open /pattern/": put cursor in column found with pattern
Bram Moolenaardf177f62005-02-22 08:39:57 +00006112 ++eap->arg;
6113 p = skip_regexp(eap->arg, '/', p_magic, NULL);
6114 *p = NUL;
6115 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
6116 if (regmatch.regprog != NULL)
6117 {
6118 regmatch.rm_ic = p_ic;
6119 p = ml_get_curline();
6120 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006121 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006122 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006123 emsg(_(e_nomatch));
Bram Moolenaar473de612013-06-08 18:19:48 +02006124 vim_regfree(regmatch.regprog);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006125 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006126 // Move to the NUL, ignore any other arguments.
Bram Moolenaardf177f62005-02-22 08:39:57 +00006127 eap->arg += STRLEN(eap->arg);
6128 }
6129 check_cursor();
6130
6131 eap->cmdidx = CMD_visual;
6132 do_exedit(eap, NULL);
6133}
6134
6135/*
6136 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006137 */
6138 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006139ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006140{
6141 do_exedit(eap, NULL);
6142}
6143
6144/*
Bram Moolenaar4b96df52020-01-26 22:00:26 +01006145 * ":edit <file>" command and alike.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006146 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006147 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006148do_exedit(
6149 exarg_T *eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006150 win_T *old_curwin) // curwin before doing a split or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006151{
6152 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006153 int need_hide;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006154 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006155
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01006156 if ((eap->cmdidx != CMD_pedit && ERROR_IF_POPUP_WINDOW)
6157 || ERROR_IF_TERM_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006158 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006159 /*
6160 * ":vi" command ends Ex mode.
6161 */
6162 if (exmode_active && (eap->cmdidx == CMD_visual
6163 || eap->cmdidx == CMD_view))
6164 {
6165 exmode_active = FALSE;
6166 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006167 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006168 // Special case: ":global/pat/visual\NLvi-commands"
Bram Moolenaardf177f62005-02-22 08:39:57 +00006169 if (global_busy)
6170 {
6171 int rd = RedrawingDisabled;
6172 int nwr = no_wait_return;
6173 int ms = msg_scroll;
6174#ifdef FEAT_GUI
6175 int he = hold_gui_events;
6176#endif
6177
6178 if (eap->nextcmd != NULL)
6179 {
6180 stuffReadbuff(eap->nextcmd);
6181 eap->nextcmd = NULL;
6182 }
6183
6184 if (exmode_was != EXMODE_VIM)
6185 settmode(TMODE_RAW);
6186 RedrawingDisabled = 0;
6187 no_wait_return = 0;
6188 need_wait_return = FALSE;
6189 msg_scroll = 0;
6190#ifdef FEAT_GUI
6191 hold_gui_events = 0;
6192#endif
6193 must_redraw = CLEAR;
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01006194 pending_exmode_active = TRUE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006195
6196 main_loop(FALSE, TRUE);
6197
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01006198 pending_exmode_active = FALSE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006199 RedrawingDisabled = rd;
6200 no_wait_return = nwr;
6201 msg_scroll = ms;
6202#ifdef FEAT_GUI
6203 hold_gui_events = he;
6204#endif
6205 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006206 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006207 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006208 }
6209
6210 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006211 || eap->cmdidx == CMD_tabnew
6212 || eap->cmdidx == CMD_tabedit
Bram Moolenaar4033c552017-09-16 20:54:51 +02006213 || eap->cmdidx == CMD_vnew) && *eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006214 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006215 // ":new" or ":tabnew" without argument: edit an new empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006216 setpcmark();
6217 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006218 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
6219 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006220 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006221 else if ((eap->cmdidx != CMD_split && eap->cmdidx != CMD_vsplit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006222 || *eap->arg != NUL
6223#ifdef FEAT_BROWSE
6224 || cmdmod.browse
6225#endif
6226 )
6227 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006228 // Can't edit another file when "curbuf_lock" is set. Only ":edit"
6229 // can bring us here, others are stopped earlier.
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006230 if (*eap->arg != NUL && curbuf_locked())
6231 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006232
Bram Moolenaar071d4272004-06-13 20:20:40 +00006233 n = readonlymode;
6234 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
6235 readonlymode = TRUE;
6236 else if (eap->cmdidx == CMD_enew)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006237 readonlymode = FALSE; // 'readonly' doesn't make sense in an
6238 // empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006239 setpcmark();
6240 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
6241 NULL, eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006242 // ":edit" goes to first line if Vi compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00006243 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
6244 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
6245 ? ECMD_ONE : eap->do_ecmd_lnum,
Bram Moolenaareb44a682017-08-03 22:44:55 +02006246 (buf_hide(curbuf) ? ECMD_HIDE : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006247 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006248 // after a split we can use an existing buffer
Bram Moolenaar7b449342014-03-25 13:03:48 +01006249 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006250 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006251 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006252 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006253 // Editing the file failed. If the window was split, close it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006254 if (old_curwin != NULL)
6255 {
6256 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02006257 if (!need_hide || buf_hide(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006258 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006259#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006260 cleanup_T cs;
6261
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006262 // Reset the error/interrupt/exception state here so that
6263 // aborting() returns FALSE when closing a window.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006264 enter_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02006265#endif
6266#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006267 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006268#endif
Bram Moolenaareb44a682017-08-03 22:44:55 +02006269 win_close(curwin, !need_hide && !buf_hide(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006270
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006271#if defined(FEAT_EVAL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006272 // Restore the error/interrupt/exception state if not
6273 // discarded by a new aborting error, interrupt, or
6274 // uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006275 leave_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02006276#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006277 }
6278 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006279 }
6280 else if (readonlymode && curbuf->b_nwindows == 1)
6281 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006282 // When editing an already visited buffer, 'readonly' won't be set
6283 // but the previous value is kept. With ":view" and ":sview" we
6284 // want the file to be readonly, except when another window is
6285 // editing the same buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006286 curbuf->b_p_ro = TRUE;
6287 }
6288 readonlymode = n;
6289 }
6290 else
6291 {
6292 if (eap->do_ecmd_cmd != NULL)
6293 do_cmdline_cmd(eap->do_ecmd_cmd);
6294#ifdef FEAT_TITLE
6295 n = curwin->w_arg_idx_invalid;
6296#endif
6297 check_arg_idx(curwin);
6298#ifdef FEAT_TITLE
6299 if (n != curwin->w_arg_idx_invalid)
6300 maketitle();
6301#endif
6302 }
6303
Bram Moolenaar071d4272004-06-13 20:20:40 +00006304 /*
6305 * if ":split file" worked, set alternate file name in old window to new
6306 * file
6307 */
6308 if (old_curwin != NULL
6309 && *eap->arg != NUL
6310 && curwin != old_curwin
6311 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00006312 && old_curwin->w_buffer != curbuf
6313 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006314 old_curwin->w_alt_fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006315
6316 ex_no_reprint = TRUE;
6317}
6318
6319#ifndef FEAT_GUI
6320/*
6321 * ":gui" and ":gvim" when there is no GUI.
6322 */
6323 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006324ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006325{
6326 eap->errmsg = e_nogvim;
6327}
6328#endif
6329
Bram Moolenaar4f974752019-02-17 17:44:42 +01006330#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006331 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006332ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006333{
6334 gui_make_tearoff(eap->arg);
6335}
6336#endif
6337
Bram Moolenaar29a2c082018-03-05 21:06:23 +01006338#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
6339 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006340 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006341ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006342{
Bram Moolenaar29a2c082018-03-05 21:06:23 +01006343# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
6344 if (gui.in_use)
6345 gui_make_popup(eap->arg, eap->forceit);
6346# ifdef FEAT_TERM_POPUP_MENU
6347 else
6348# endif
6349# endif
6350# ifdef FEAT_TERM_POPUP_MENU
6351 pum_make_popup(eap->arg, eap->forceit);
6352# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006353}
6354#endif
6355
Bram Moolenaar071d4272004-06-13 20:20:40 +00006356 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006357ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006358{
6359 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006360 msg(_("No swap file"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006361 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01006362 msg((char *)curbuf->b_ml.ml_mfp->mf_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006363}
6364
6365/*
6366 * ":syncbind" forces all 'scrollbind' windows to have the same relative
6367 * offset.
6368 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
6369 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006370 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006371ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006372{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006373 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006374 win_T *save_curwin = curwin;
6375 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006376 long topline;
6377 long y;
6378 linenr_T old_linenr = curwin->w_cursor.lnum;
6379
6380 setpcmark();
6381
6382 /*
6383 * determine max topline
6384 */
6385 if (curwin->w_p_scb)
6386 {
6387 topline = curwin->w_topline;
Bram Moolenaar29323592016-07-24 22:04:11 +02006388 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006389 {
6390 if (wp->w_p_scb && wp->w_buffer)
6391 {
Bram Moolenaar375e3392019-01-31 18:26:10 +01006392 y = wp->w_buffer->b_ml.ml_line_count - get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006393 if (topline > y)
6394 topline = y;
6395 }
6396 }
6397 if (topline < 1)
6398 topline = 1;
6399 }
6400 else
6401 {
6402 topline = 1;
6403 }
6404
6405
6406 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006407 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006408 */
Bram Moolenaar29323592016-07-24 22:04:11 +02006409 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006410 {
6411 if (curwin->w_p_scb)
6412 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006413 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006414 y = topline - curwin->w_topline;
6415 if (y > 0)
6416 scrollup(y, TRUE);
6417 else
6418 scrolldown(-y, TRUE);
6419 curwin->w_scbind_pos = topline;
6420 redraw_later(VALID);
6421 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006422 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006423 }
6424 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006425 curwin = save_curwin;
6426 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006427 if (curwin->w_p_scb)
6428 {
6429 did_syncbind = TRUE;
6430 checkpcmark();
6431 if (old_linenr != curwin->w_cursor.lnum)
6432 {
6433 char_u ctrl_o[2];
6434
6435 ctrl_o[0] = Ctrl_O;
6436 ctrl_o[1] = 0;
6437 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
6438 }
6439 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006440}
6441
6442
6443 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006444ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006445{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006446 int i;
6447 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
6448 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006449
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006450 if (eap->usefilter) // :r!cmd
Bram Moolenaar071d4272004-06-13 20:20:40 +00006451 do_bang(1, eap, FALSE, FALSE, TRUE);
6452 else
6453 {
6454 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
6455 return;
6456
6457#ifdef FEAT_BROWSE
6458 if (cmdmod.browse)
6459 {
6460 char_u *browseFile;
6461
Bram Moolenaar7171abe2004-10-11 10:06:20 +00006462 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006463 NULL, NULL, NULL, curbuf);
6464 if (browseFile != NULL)
6465 {
6466 i = readfile(browseFile, NULL,
6467 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
6468 vim_free(browseFile);
6469 }
6470 else
6471 i = OK;
6472 }
6473 else
6474#endif
6475 if (*eap->arg == NUL)
6476 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006477 if (check_fname() == FAIL) // check for no file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00006478 return;
6479 i = readfile(curbuf->b_ffname, curbuf->b_fname,
6480 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
6481 }
6482 else
6483 {
6484 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
6485 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
6486 i = readfile(eap->arg, NULL,
6487 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
6488
6489 }
Bram Moolenaare13b9af2017-01-13 22:01:02 +01006490 if (i != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006491 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006492#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006493 if (!aborting())
6494#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006495 semsg(_(e_notopen), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006496 }
6497 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00006498 {
6499 if (empty && exmode_active)
6500 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006501 // Delete the empty line that remains. Historically ex does
6502 // this but vi doesn't.
Bram Moolenaardf177f62005-02-22 08:39:57 +00006503 if (eap->line2 == 0)
6504 lnum = curbuf->b_ml.ml_line_count;
6505 else
6506 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00006507 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006508 {
6509 ml_delete(lnum, FALSE);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00006510 if (curwin->w_cursor.lnum > 1
6511 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006512 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00006513 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006514 }
6515 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006516 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006517 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006518 }
6519}
6520
Bram Moolenaarea408852005-06-25 22:49:46 +00006521static char_u *prev_dir = NULL;
6522
6523#if defined(EXITFREE) || defined(PROTO)
6524 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006525free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00006526{
Bram Moolenaard23a8232018-02-10 18:45:26 +01006527 VIM_CLEAR(prev_dir);
6528 VIM_CLEAR(globaldir);
Bram Moolenaarea408852005-06-25 22:49:46 +00006529}
6530#endif
6531
Bram Moolenaarf4258302013-06-02 18:20:17 +02006532/*
6533 * Deal with the side effects of changing the current directory.
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006534 * When 'scope' is CDSCOPE_TABPAGE then this was after an ":tcd" command.
6535 * When 'scope' is CDSCOPE_WINDOW then this was after an ":lcd" command.
Bram Moolenaarf4258302013-06-02 18:20:17 +02006536 */
6537 void
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006538post_chdir(cdscope_T scope)
Bram Moolenaarf4258302013-06-02 18:20:17 +02006539{
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006540 if (scope != CDSCOPE_WINDOW)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006541 // Clear tab local directory for both :cd and :tcd
6542 VIM_CLEAR(curtab->tp_localdir);
Bram Moolenaard23a8232018-02-10 18:45:26 +01006543 VIM_CLEAR(curwin->w_localdir);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006544 if (scope != CDSCOPE_GLOBAL)
Bram Moolenaarf4258302013-06-02 18:20:17 +02006545 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006546 // If still in global directory, need to remember current
6547 // directory as global directory.
Bram Moolenaarf4258302013-06-02 18:20:17 +02006548 if (globaldir == NULL && prev_dir != NULL)
6549 globaldir = vim_strsave(prev_dir);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006550 // Remember this local directory for the window.
Bram Moolenaarf4258302013-06-02 18:20:17 +02006551 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006552 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006553 if (scope == CDSCOPE_TABPAGE)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006554 curtab->tp_localdir = vim_strsave(NameBuff);
6555 else
6556 curwin->w_localdir = vim_strsave(NameBuff);
6557 }
Bram Moolenaarf4258302013-06-02 18:20:17 +02006558 }
6559 else
6560 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006561 // We are now in the global directory, no need to remember its
6562 // name.
Bram Moolenaard23a8232018-02-10 18:45:26 +01006563 VIM_CLEAR(globaldir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006564 }
6565
6566 shorten_fnames(TRUE);
6567}
6568
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006569/*
6570 * Change directory function used by :cd/:tcd/:lcd Ex commands and the
6571 * chdir() function. If 'winlocaldir' is TRUE, then changes the window-local
6572 * directory. If 'tablocaldir' is TRUE, then changes the tab-local directory.
6573 * Otherwise changes the global directory.
6574 * Returns TRUE if the directory is successfully changed.
6575 */
6576 int
6577changedir_func(
6578 char_u *new_dir,
6579 int forceit,
6580 cdscope_T scope)
6581{
6582 char_u *tofree;
6583 int dir_differs;
6584 int retval = FALSE;
6585
Bram Moolenaar7cc96922020-01-31 22:41:38 +01006586 if (new_dir == NULL || allbuf_locked())
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006587 return FALSE;
6588
6589 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged() && !forceit)
6590 {
6591 emsg(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
6592 return FALSE;
6593 }
6594
6595 // ":cd -": Change to previous directory
6596 if (STRCMP(new_dir, "-") == 0)
6597 {
6598 if (prev_dir == NULL)
6599 {
6600 emsg(_("E186: No previous directory"));
6601 return FALSE;
6602 }
6603 new_dir = prev_dir;
6604 }
6605
6606 // Save current directory for next ":cd -"
6607 tofree = prev_dir;
6608 if (mch_dirname(NameBuff, MAXPATHL) == OK)
6609 prev_dir = vim_strsave(NameBuff);
6610 else
6611 prev_dir = NULL;
6612
6613#if defined(UNIX) || defined(VMS)
6614 // for UNIX ":cd" means: go to home directory
6615 if (*new_dir == NUL)
6616 {
6617 // use NameBuff for home directory name
6618# ifdef VMS
6619 char_u *p;
6620
6621 p = mch_getenv((char_u *)"SYS$LOGIN");
6622 if (p == NULL || *p == NUL) // empty is the same as not set
6623 NameBuff[0] = NUL;
6624 else
6625 vim_strncpy(NameBuff, p, MAXPATHL - 1);
6626# else
6627 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
6628# endif
6629 new_dir = NameBuff;
6630 }
6631#endif
6632 dir_differs = new_dir == NULL || prev_dir == NULL
6633 || pathcmp((char *)prev_dir, (char *)new_dir, -1) != 0;
6634 if (new_dir == NULL || (dir_differs && vim_chdir(new_dir)))
6635 emsg(_(e_failed));
6636 else
6637 {
6638 char_u *acmd_fname;
6639
6640 post_chdir(scope);
6641
6642 if (dir_differs)
6643 {
6644 if (scope == CDSCOPE_WINDOW)
6645 acmd_fname = (char_u *)"window";
6646 else if (scope == CDSCOPE_TABPAGE)
6647 acmd_fname = (char_u *)"tabpage";
6648 else
6649 acmd_fname = (char_u *)"global";
6650 apply_autocmds(EVENT_DIRCHANGED, acmd_fname, new_dir, FALSE,
6651 curbuf);
6652 }
6653 retval = TRUE;
6654 }
6655 vim_free(tofree);
6656
6657 return retval;
6658}
Bram Moolenaarea408852005-06-25 22:49:46 +00006659
Bram Moolenaar071d4272004-06-13 20:20:40 +00006660/*
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006661 * ":cd", ":tcd", ":lcd", ":chdir" ":tchdir" and ":lchdir".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006662 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00006663 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006664ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006665{
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01006666 char_u *new_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006667
6668 new_dir = eap->arg;
6669#if !defined(UNIX) && !defined(VMS)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006670 // for non-UNIX ":cd" means: print current directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006671 if (*new_dir == NUL)
6672 ex_pwd(NULL);
6673 else
6674#endif
6675 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006676 cdscope_T scope = CDSCOPE_GLOBAL;
6677
6678 if (eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir)
6679 scope = CDSCOPE_WINDOW;
6680 else if (eap->cmdidx == CMD_tcd || eap->cmdidx == CMD_tchdir)
6681 scope = CDSCOPE_TABPAGE;
6682
6683 if (changedir_func(new_dir, eap->forceit, scope))
Bram Moolenaardf177f62005-02-22 08:39:57 +00006684 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006685 // Echo the new current directory if the command was typed.
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00006686 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006687 ex_pwd(eap);
6688 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006689 }
6690}
6691
6692/*
6693 * ":pwd".
6694 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006695 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006696ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006697{
6698 if (mch_dirname(NameBuff, MAXPATHL) == OK)
6699 {
6700#ifdef BACKSLASH_IN_FILENAME
6701 slash_adjust(NameBuff);
6702#endif
Bram Moolenaar32526b32019-01-19 17:43:09 +01006703 msg((char *)NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006704 }
6705 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006706 emsg(_("E187: Unknown"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006707}
6708
6709/*
6710 * ":=".
6711 */
6712 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006713ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006714{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006715 smsg("%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006716 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006717}
6718
6719 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006720ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006721{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006722 int n;
6723 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006724
6725 if (cursor_valid())
6726 {
6727 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
6728 if (n >= 0)
Bram Moolenaar53f81742017-09-22 14:35:51 +02006729 windgoto((int)n, curwin->w_wincol + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006730 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006731
6732 len = eap->line2;
6733 switch (*eap->arg)
6734 {
6735 case 'm': break;
6736 case NUL: len *= 1000L; break;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006737 default: semsg(_(e_invarg2), eap->arg); return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006738 }
6739 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006740}
6741
6742/*
6743 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
6744 */
6745 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006746do_sleep(long msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006747{
Bram Moolenaar52953192019-08-16 10:27:13 +02006748 long done = 0;
Bram Moolenaar975b5272016-03-15 23:10:59 +01006749 long wait_now;
Bram Moolenaar52953192019-08-16 10:27:13 +02006750# ifdef ELAPSED_FUNC
6751 elapsed_T start_tv;
6752
6753 // Remember at what time we started, so that we know how much longer we
6754 // should wait after waiting for a bit.
6755 ELAPSED_INIT(start_tv);
6756# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006757
6758 cursor_on();
Bram Moolenaarf45d7472018-09-30 18:22:26 +02006759 out_flush_cursor(FALSE, FALSE);
Bram Moolenaar52953192019-08-16 10:27:13 +02006760 while (!got_int && done < msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006761 {
Bram Moolenaar975b5272016-03-15 23:10:59 +01006762 wait_now = msec - done > 1000L ? 1000L : msec - done;
6763#ifdef FEAT_TIMERS
6764 {
6765 long due_time = check_due_timer();
6766
6767 if (due_time > 0 && due_time < wait_now)
6768 wait_now = due_time;
6769 }
6770#endif
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02006771#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar28e67e02019-08-15 23:05:49 +02006772 if (has_any_channel() && wait_now > 20L)
6773 wait_now = 20L;
6774#endif
6775#ifdef FEAT_SOUND
6776 if (has_any_sound_callback() && wait_now > 20L)
6777 wait_now = 20L;
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02006778#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +01006779 ui_delay(wait_now, TRUE);
Bram Moolenaar52953192019-08-16 10:27:13 +02006780
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02006781#ifdef FEAT_JOB_CHANNEL
6782 if (has_any_channel())
6783 ui_breakcheck_force(TRUE);
6784 else
6785#endif
6786 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006787#ifdef MESSAGE_QUEUE
Bram Moolenaar52953192019-08-16 10:27:13 +02006788 // Process the netbeans and clientserver messages that may have been
6789 // received in the call to ui_breakcheck() when the GUI is in use. This
6790 // may occur when running a test case.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006791 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02006792#endif
Bram Moolenaar52953192019-08-16 10:27:13 +02006793
6794# ifdef ELAPSED_FUNC
6795 // actual time passed
6796 done = ELAPSED_FUNC(start_tv);
6797# else
6798 // guestimate time passed (will actually be more)
6799 done += wait_now;
6800# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006801 }
Bram Moolenaar1ebff3d2018-07-07 16:18:13 +02006802
6803 // If CTRL-C was typed to interrupt the sleep, drop the CTRL-C from the
6804 // input buffer, otherwise a following call to input() fails.
6805 if (got_int)
6806 (void)vpeekc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006807}
6808
Bram Moolenaar071d4272004-06-13 20:20:40 +00006809/*
6810 * ":winsize" command (obsolete).
6811 */
6812 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006813ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006814{
6815 int w, h;
6816 char_u *arg = eap->arg;
6817 char_u *p;
6818
6819 w = getdigits(&arg);
6820 arg = skipwhite(arg);
6821 p = arg;
6822 h = getdigits(&arg);
6823 if (*p != NUL && *arg == NUL)
6824 set_shellsize(w, h, TRUE);
6825 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006826 emsg(_("E465: :winsize requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006827}
6828
Bram Moolenaar071d4272004-06-13 20:20:40 +00006829 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006830ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006831{
6832 int xchar = NUL;
6833 char_u *p;
6834
6835 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
6836 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006837 // CTRL-W g and CTRL-W CTRL-G have an extra command character
Bram Moolenaar071d4272004-06-13 20:20:40 +00006838 if (eap->arg[1] == NUL)
6839 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006840 emsg(_(e_invarg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006841 return;
6842 }
6843 xchar = eap->arg[1];
6844 p = eap->arg + 2;
6845 }
6846 else
6847 p = eap->arg + 1;
6848
6849 eap->nextcmd = check_nextcmd(p);
6850 p = skipwhite(p);
6851 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006852 emsg(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02006853 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006854 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006855 // Pass flags on for ":vertical wincmd ]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006856 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00006857 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006858 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
6859 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00006860 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006861 }
6862}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006863
Bram Moolenaar843ee412004-06-30 16:16:41 +00006864#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006865/*
6866 * ":winpos".
6867 */
6868 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006869ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006870{
6871 int x, y;
6872 char_u *arg = eap->arg;
6873 char_u *p;
6874
6875 if (*arg == NUL)
6876 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00006877# if defined(FEAT_GUI) || defined(MSWIN)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006878# ifdef VIMDLL
6879 if (gui.in_use ? gui_mch_get_winpos(&x, &y) != FAIL :
6880 mch_get_winpos(&x, &y) != FAIL)
6881# elif defined(FEAT_GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006882 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00006883# else
6884 if (mch_get_winpos(&x, &y) != FAIL)
6885# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006886 {
6887 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
Bram Moolenaar32526b32019-01-19 17:43:09 +01006888 msg((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006889 }
6890 else
6891# endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006892 emsg(_("E188: Obtaining window position not implemented for this platform"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006893 }
6894 else
6895 {
6896 x = getdigits(&arg);
6897 arg = skipwhite(arg);
6898 p = arg;
6899 y = getdigits(&arg);
6900 if (*p == NUL || *arg != NUL)
6901 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006902 emsg(_("E466: :winpos requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006903 return;
6904 }
6905# ifdef FEAT_GUI
6906 if (gui.in_use)
6907 gui_mch_set_winpos(x, y);
6908 else if (gui.starting)
6909 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006910 // Remember the coordinates for when the window is opened.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006911 gui_win_x = x;
6912 gui_win_y = y;
6913 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006914# if defined(HAVE_TGETENT) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006915 else
6916# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006917# endif
6918# if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar843ee412004-06-30 16:16:41 +00006919 mch_set_winpos(x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006920# endif
6921# ifdef HAVE_TGETENT
6922 if (*T_CWP)
6923 term_set_winpos(x, y);
6924# endif
6925 }
6926}
6927#endif
6928
6929/*
6930 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
6931 */
6932 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006933ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006934{
6935 oparg_T oa;
6936
6937 clear_oparg(&oa);
6938 oa.regname = eap->regname;
6939 oa.start.lnum = eap->line1;
6940 oa.end.lnum = eap->line2;
6941 oa.line_count = eap->line2 - eap->line1 + 1;
6942 oa.motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006943 virtual_op = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006944 if (eap->cmdidx != CMD_yank) // position cursor for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00006945 {
6946 setpcmark();
6947 curwin->w_cursor.lnum = eap->line1;
6948 beginline(BL_SOL | BL_FIX);
6949 }
6950
Bram Moolenaard07c6e12013-11-21 14:21:40 +01006951 if (VIsual_active)
6952 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01006953
Bram Moolenaar071d4272004-06-13 20:20:40 +00006954 switch (eap->cmdidx)
6955 {
6956 case CMD_delete:
6957 oa.op_type = OP_DELETE;
6958 op_delete(&oa);
6959 break;
6960
6961 case CMD_yank:
6962 oa.op_type = OP_YANK;
6963 (void)op_yank(&oa, FALSE, TRUE);
6964 break;
6965
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006966 default: // CMD_rshift or CMD_lshift
Bram Moolenaar6e707362013-06-14 19:15:58 +02006967 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00006968#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02006969 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
6970#else
6971 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00006972#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02006973 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006974 oa.op_type = OP_RSHIFT;
6975 else
6976 oa.op_type = OP_LSHIFT;
6977 op_shift(&oa, FALSE, eap->amount);
6978 break;
6979 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006980 virtual_op = MAYBE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006981 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006982}
6983
6984/*
6985 * ":put".
6986 */
6987 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006988ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006989{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006990 // ":0put" works like ":1put!".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006991 if (eap->line2 == 0)
6992 {
6993 eap->line2 = 1;
6994 eap->forceit = TRUE;
6995 }
6996 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006997 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
6998 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006999}
7000
7001/*
7002 * Handle ":copy" and ":move".
7003 */
7004 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007005ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007006{
7007 long n;
7008
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02007009 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE, FALSE, 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007010 if (eap->arg == NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00007011 {
7012 eap->nextcmd = NULL;
7013 return;
7014 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00007015 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007016
7017 /*
7018 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
7019 */
7020 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
7021 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02007022 emsg(_(e_invrange));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007023 return;
7024 }
7025
7026 if (eap->cmdidx == CMD_move)
7027 {
7028 if (do_move(eap->line1, eap->line2, n) == FAIL)
7029 return;
7030 }
7031 else
7032 ex_copy(eap->line1, eap->line2, n);
7033 u_clearline();
7034 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007035 ex_may_print(eap);
7036}
7037
7038/*
7039 * Print the current line if flags were given to the Ex command.
7040 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02007041 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007042ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007043{
7044 if (eap->flags != 0)
7045 {
7046 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
7047 (eap->flags & EXFLAG_LIST));
7048 ex_no_reprint = TRUE;
7049 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007050}
7051
7052/*
7053 * ":smagic" and ":snomagic".
7054 */
7055 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007056ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007057{
7058 int magic_save = p_magic;
7059
7060 p_magic = (eap->cmdidx == CMD_smagic);
7061 do_sub(eap);
7062 p_magic = magic_save;
7063}
7064
7065/*
7066 * ":join".
7067 */
7068 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007069ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007070{
7071 curwin->w_cursor.lnum = eap->line1;
7072 if (eap->line1 == eap->line2)
7073 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007074 if (eap->addr_count >= 2) // :2,2join does nothing
Bram Moolenaar071d4272004-06-13 20:20:40 +00007075 return;
7076 if (eap->line2 == curbuf->b_ml.ml_line_count)
7077 {
7078 beep_flush();
7079 return;
7080 }
7081 ++eap->line2;
7082 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02007083 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007084 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007085 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007086}
7087
7088/*
7089 * ":[addr]@r" or ":[addr]*r": execute register
7090 */
7091 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007092ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007093{
7094 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00007095 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007096
7097 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4930a762016-09-11 14:39:53 +02007098 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007099
7100#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007101 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00007102#endif
7103
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007104 // get the register name. No name means to use the previous one
Bram Moolenaar071d4272004-06-13 20:20:40 +00007105 c = *eap->arg;
7106 if (c == NUL || (c == '*' && *eap->cmd == '*'))
7107 c = '@';
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007108 // Put the register in the typeahead buffer with the "silent" flag.
Bram Moolenaard333d1e2006-11-07 17:43:47 +00007109 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
7110 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007111 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007112 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00007113 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007114 else
7115 {
7116 int save_efr = exec_from_reg;
7117
7118 exec_from_reg = TRUE;
7119
7120 /*
7121 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00007122 * Continue until the stuff buffer is empty and all added characters
7123 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007124 */
Bram Moolenaar60462872009-11-03 11:40:19 +00007125 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007126 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
7127
7128 exec_from_reg = save_efr;
7129 }
7130}
7131
7132/*
7133 * ":!".
7134 */
7135 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007136ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007137{
7138 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
7139}
7140
7141/*
7142 * ":undo".
7143 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007144 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01007145ex_undo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007146{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007147 if (eap->addr_count == 1) // :undo 123
Bram Moolenaar730cde92010-06-27 05:18:54 +02007148 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00007149 else
7150 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007151}
7152
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007153#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02007154 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007155ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007156{
7157 char_u hash[UNDO_HASH_SIZE];
7158
7159 u_compute_hash(hash);
7160 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
7161}
7162
Bram Moolenaar6df6f472010-07-18 18:04:50 +02007163 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007164ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007165{
7166 char_u hash[UNDO_HASH_SIZE];
7167
7168 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02007169 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007170}
7171#endif
7172
Bram Moolenaar071d4272004-06-13 20:20:40 +00007173/*
7174 * ":redo".
7175 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007176 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007177ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007178{
7179 u_redo(1);
7180}
7181
7182/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007183 * ":earlier" and ":later".
7184 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007185 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007186ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007187{
7188 long count = 0;
7189 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02007190 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007191 char_u *p = eap->arg;
7192
7193 if (*p == NUL)
7194 count = 1;
7195 else if (isdigit(*p))
7196 {
7197 count = getdigits(&p);
7198 switch (*p)
7199 {
7200 case 's': ++p; sec = TRUE; break;
7201 case 'm': ++p; sec = TRUE; count *= 60; break;
7202 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02007203 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
7204 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007205 }
7206 }
7207
7208 if (*p != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007209 semsg(_(e_invarg2), eap->arg);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007210 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02007211 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
7212 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007213}
7214
7215/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007216 * ":redir": start/stop redirection.
7217 */
7218 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007219ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007220{
7221 char *mode;
7222 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00007223 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007224
Bram Moolenaarba768492016-07-08 15:32:54 +02007225#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02007226 if (redir_execute)
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007227 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007228 emsg(_("E930: Cannot use :redir inside execute()"));
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007229 return;
7230 }
Bram Moolenaarba768492016-07-08 15:32:54 +02007231#endif
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007232
Bram Moolenaar071d4272004-06-13 20:20:40 +00007233 if (STRICMP(eap->arg, "END") == 0)
7234 close_redir();
7235 else
7236 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007237 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007238 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007239 ++arg;
7240 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007241 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007242 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007243 mode = "a";
7244 }
7245 else
7246 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00007247 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007248
7249 close_redir();
7250
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007251 // Expand environment variables and "~/".
Bram Moolenaarca472992005-01-21 11:46:23 +00007252 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007253 if (fname == NULL)
7254 return;
7255#ifdef FEAT_BROWSE
7256 if (cmdmod.browse)
7257 {
7258 char_u *browseFile;
7259
Bram Moolenaar7171abe2004-10-11 10:06:20 +00007260 browseFile = do_browse(BROWSE_SAVE,
7261 (char_u *)_("Save Redirection"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +02007262 fname, NULL, NULL,
7263 (char_u *)_(BROWSE_FILTER_ALL_FILES), curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007264 if (browseFile == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007265 return; // operation cancelled
Bram Moolenaar071d4272004-06-13 20:20:40 +00007266 vim_free(fname);
7267 fname = browseFile;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007268 eap->forceit = TRUE; // since dialog already asked
Bram Moolenaar071d4272004-06-13 20:20:40 +00007269 }
7270#endif
7271
7272 redir_fd = open_exfile(fname, eap->forceit, mode);
7273 vim_free(fname);
7274 }
7275#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00007276 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007277 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007278 // redirect to a register a-z (resp. A-Z for appending)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007279 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00007280 ++arg;
7281 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007282# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00007283 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00007284 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00007285# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00007286 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007287 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007288 redir_reg = *arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007289 if (*arg == '>' && arg[1] == '>') // append
Bram Moolenaard9d30582005-05-18 22:10:28 +00007290 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00007291 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007292 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007293 // Can use both "@a" and "@a>".
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00007294 if (*arg == '>')
7295 arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007296 // Make register empty when not using @A-@Z and the
7297 // command is valid.
Bram Moolenaarc188b882007-10-19 14:20:54 +00007298 if (*arg == NUL && !isupper(redir_reg))
7299 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007300 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00007301 }
7302 if (*arg != NUL)
7303 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007304 redir_reg = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007305 semsg(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007306 }
7307 }
7308 else if (*arg == '=' && arg[1] == '>')
7309 {
7310 int append;
7311
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007312 // redirect to a variable
Bram Moolenaardf177f62005-02-22 08:39:57 +00007313 close_redir();
7314 arg += 2;
7315
7316 if (*arg == '>')
7317 {
7318 ++arg;
7319 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007320 }
7321 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00007322 append = FALSE;
7323
7324 if (var_redir_start(skipwhite(arg), append) == OK)
7325 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007326 }
7327#endif
7328
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007329 // TODO: redirect to a buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00007330
Bram Moolenaar071d4272004-06-13 20:20:40 +00007331 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007332 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007333 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00007334
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007335 // Make sure redirection is not off. Can happen for cmdline completion
7336 // that indirectly invokes a command to catch its output.
Bram Moolenaar29b2d262006-09-10 19:07:28 +00007337 if (redir_fd != NULL
7338#ifdef FEAT_EVAL
7339 || redir_reg || redir_vname
7340#endif
7341 )
7342 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007343}
7344
7345/*
7346 * ":redraw": force redraw
7347 */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01007348 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007349ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007350{
7351 int r = RedrawingDisabled;
7352 int p = p_lz;
7353
7354 RedrawingDisabled = 0;
7355 p_lz = FALSE;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01007356 validate_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007357 update_topline();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007358 update_screen(eap->forceit ? CLEAR : VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007359#ifdef FEAT_TITLE
7360 if (need_maketitle)
7361 maketitle();
7362#endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007363#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
7364# ifdef VIMDLL
7365 if (!gui.in_use)
7366# endif
7367 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01007368#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007369 RedrawingDisabled = r;
7370 p_lz = p;
7371
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007372 // Reset msg_didout, so that a message that's there is overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007373 msg_didout = FALSE;
7374 msg_col = 0;
7375
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007376 // No need to wait after an intentional redraw.
Bram Moolenaar56667a52013-07-17 11:54:28 +02007377 need_wait_return = FALSE;
7378
Bram Moolenaar071d4272004-06-13 20:20:40 +00007379 out_flush();
7380}
7381
7382/*
7383 * ":redrawstatus": force redraw of status line(s)
7384 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007385 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007386ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007387{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007388 int r = RedrawingDisabled;
7389 int p = p_lz;
7390
7391 RedrawingDisabled = 0;
7392 p_lz = FALSE;
7393 if (eap->forceit)
7394 status_redraw_all();
7395 else
7396 status_redraw_curbuf();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007397 update_screen(VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007398 RedrawingDisabled = r;
7399 p_lz = p;
7400 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007401}
7402
Bram Moolenaare12bab32019-01-08 22:02:56 +01007403/*
7404 * ":redrawtabline": force redraw of the tabline
7405 */
7406 static void
7407ex_redrawtabline(exarg_T *eap UNUSED)
7408{
7409 int r = RedrawingDisabled;
7410 int p = p_lz;
7411
7412 RedrawingDisabled = 0;
7413 p_lz = FALSE;
7414
7415 draw_tabline();
7416
7417 RedrawingDisabled = r;
7418 p_lz = p;
7419 out_flush();
7420}
7421
Bram Moolenaar071d4272004-06-13 20:20:40 +00007422 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007423close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007424{
7425 if (redir_fd != NULL)
7426 {
7427 fclose(redir_fd);
7428 redir_fd = NULL;
7429 }
7430#ifdef FEAT_EVAL
7431 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007432 if (redir_vname)
7433 {
7434 var_redir_stop();
7435 redir_vname = 0;
7436 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007437#endif
7438}
7439
Bram Moolenaarf96ae0b2019-07-28 15:21:55 +02007440#if (defined(FEAT_SESSION) || defined(FEAT_EVAL)) || defined(PROTO)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007441 int
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02007442vim_mkdir_emsg(char_u *name, int prot UNUSED)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007443{
7444 if (vim_mkdir(name, prot) != 0)
7445 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007446 semsg(_("E739: Cannot create directory: %s"), name);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007447 return FAIL;
7448 }
7449 return OK;
7450}
7451#endif
7452
Bram Moolenaar071d4272004-06-13 20:20:40 +00007453/*
7454 * Open a file for writing for an Ex command, with some checks.
7455 * Return file descriptor, or NULL on failure.
7456 */
7457 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007458open_exfile(
7459 char_u *fname,
7460 int forceit,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007461 char *mode) // "w" for create new file or "a" for append
Bram Moolenaar071d4272004-06-13 20:20:40 +00007462{
7463 FILE *fd;
7464
7465#ifdef UNIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007466 // with Unix it is possible to open a directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007467 if (mch_isdir(fname))
7468 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007469 semsg(_(e_isadir2), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007470 return NULL;
7471 }
7472#endif
7473 if (!forceit && *mode != 'a' && vim_fexists(fname))
7474 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007475 semsg(_("E189: \"%s\" exists (add ! to override)"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007476 return NULL;
7477 }
7478
7479 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007480 semsg(_("E190: Cannot open \"%s\" for writing"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007481
7482 return fd;
7483}
7484
7485/*
7486 * ":mark" and ":k".
7487 */
7488 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007489ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007490{
7491 pos_T pos;
7492
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007493 if (*eap->arg == NUL) // No argument?
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007494 emsg(_(e_argreq));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007495 else if (eap->arg[1] != NUL) // more than one character?
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007496 emsg(_(e_trailing));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007497 else
7498 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007499 pos = curwin->w_cursor; // save curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00007500 curwin->w_cursor.lnum = eap->line2;
7501 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007502 if (setmark(*eap->arg) == FAIL) // set mark
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007503 emsg(_("E191: Argument must be a letter or forward/backward quote"));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007504 curwin->w_cursor = pos; // restore curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00007505 }
7506}
7507
7508/*
7509 * Update w_topline, w_leftcol and the cursor position.
7510 */
7511 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007512update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007513{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007514 check_cursor(); // put cursor on valid line
Bram Moolenaar071d4272004-06-13 20:20:40 +00007515 update_topline();
7516 if (!curwin->w_p_wrap)
7517 validate_cursor();
7518 update_curswant();
7519}
7520
Bram Moolenaar071d4272004-06-13 20:20:40 +00007521/*
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007522 * Save the current State and go to Normal mode.
7523 * Return TRUE if the typeahead could be saved.
7524 */
7525 int
7526save_current_state(save_state_T *sst)
7527{
7528 sst->save_msg_scroll = msg_scroll;
7529 sst->save_restart_edit = restart_edit;
7530 sst->save_msg_didout = msg_didout;
7531 sst->save_State = State;
7532 sst->save_insertmode = p_im;
7533 sst->save_finish_op = finish_op;
7534 sst->save_opcount = opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01007535 sst->save_reg_executing = reg_executing;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007536
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007537 msg_scroll = FALSE; // no msg scrolling in Normal mode
7538 restart_edit = 0; // don't go to Insert mode
7539 p_im = FALSE; // don't use 'insertmode'
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007540
7541 /*
7542 * Save the current typeahead. This is required to allow using ":normal"
7543 * from an event handler and makes sure we don't hang when the argument
7544 * ends with half a command.
7545 */
7546 save_typeahead(&sst->tabuf);
7547 return sst->tabuf.typebuf_valid;
7548}
7549
7550 void
7551restore_current_state(save_state_T *sst)
7552{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007553 // Restore the previous typeahead.
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007554 restore_typeahead(&sst->tabuf);
7555
7556 msg_scroll = sst->save_msg_scroll;
7557 restart_edit = sst->save_restart_edit;
7558 p_im = sst->save_insertmode;
7559 finish_op = sst->save_finish_op;
7560 opcount = sst->save_opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01007561 reg_executing = sst->save_reg_executing;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007562 msg_didout |= sst->save_msg_didout; // don't reset msg_didout now
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007563
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007564 // Restore the state (needed when called from a function executed for
7565 // 'indentexpr'). Update the mouse and cursor, they may have changed.
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007566 State = sst->save_State;
7567#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007568 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007569#endif
7570}
7571
7572/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007573 * ":normal[!] {commands}": Execute normal mode commands.
7574 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01007575 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007576ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007577{
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007578 save_state_T save_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007579 char_u *arg = NULL;
7580 int l;
7581 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007582
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00007583 if (ex_normal_lock > 0)
7584 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007585 emsg(_(e_secure));
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00007586 return;
7587 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007588 if (ex_normal_busy >= p_mmd)
7589 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007590 emsg(_("E192: Recursive use of :normal too deep"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007591 return;
7592 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007593
Bram Moolenaar071d4272004-06-13 20:20:40 +00007594 /*
7595 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
7596 * this for the K_SPECIAL leading byte, otherwise special keys will not
7597 * work.
7598 */
7599 if (has_mbyte)
7600 {
7601 int len = 0;
7602
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007603 // Count the number of characters to be escaped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007604 for (p = eap->arg; *p != NUL; ++p)
7605 {
Bram Moolenaar13505972019-01-24 15:04:48 +01007606#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007607 if (*p == CSI) // leadbyte CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007608 len += 2;
Bram Moolenaar13505972019-01-24 15:04:48 +01007609#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007610 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007611 if (*++p == K_SPECIAL // trailbyte K_SPECIAL or CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01007612#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007613 || *p == CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01007614#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007615 )
7616 len += 2;
7617 }
7618 if (len > 0)
7619 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02007620 arg = alloc(STRLEN(eap->arg) + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007621 if (arg != NULL)
7622 {
7623 len = 0;
7624 for (p = eap->arg; *p != NUL; ++p)
7625 {
7626 arg[len++] = *p;
Bram Moolenaar13505972019-01-24 15:04:48 +01007627#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007628 if (*p == CSI)
7629 {
7630 arg[len++] = KS_EXTRA;
7631 arg[len++] = (int)KE_CSI;
7632 }
Bram Moolenaar13505972019-01-24 15:04:48 +01007633#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007634 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007635 {
7636 arg[len++] = *++p;
7637 if (*p == K_SPECIAL)
7638 {
7639 arg[len++] = KS_SPECIAL;
7640 arg[len++] = KE_FILLER;
7641 }
Bram Moolenaar13505972019-01-24 15:04:48 +01007642#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007643 else if (*p == CSI)
7644 {
7645 arg[len++] = KS_EXTRA;
7646 arg[len++] = (int)KE_CSI;
7647 }
Bram Moolenaar13505972019-01-24 15:04:48 +01007648#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007649 }
7650 arg[len] = NUL;
7651 }
7652 }
7653 }
7654 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007655
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007656 ++ex_normal_busy;
7657 if (save_current_state(&save_state))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007658 {
7659 /*
7660 * Repeat the :normal command for each line in the range. When no
7661 * range given, execute it just once, without positioning the cursor
7662 * first.
7663 */
7664 do
7665 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007666 if (eap->addr_count != 0)
7667 {
7668 curwin->w_cursor.lnum = eap->line1++;
7669 curwin->w_cursor.col = 0;
Bram Moolenaard5d37532017-03-27 23:02:07 +02007670 check_cursor_moved(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007671 }
7672
Bram Moolenaar13505972019-01-24 15:04:48 +01007673 exec_normal_cmd(arg != NULL
7674 ? arg
7675 : eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007676 }
7677 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
7678 }
7679
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007680 // Might not return to the main loop when in an event handler.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007681 update_topline_cursor();
7682
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007683 restore_current_state(&save_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007684 --ex_normal_busy;
Bram Moolenaareda73602014-11-05 09:53:23 +01007685 setmouse();
Bram Moolenaareda73602014-11-05 09:53:23 +01007686#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007687 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaareda73602014-11-05 09:53:23 +01007688#endif
7689
Bram Moolenaar071d4272004-06-13 20:20:40 +00007690 vim_free(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007691}
7692
7693/*
Bram Moolenaar64969662005-12-14 21:59:55 +00007694 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007695 */
7696 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007697ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007698{
Bram Moolenaarfd371682005-01-14 21:42:54 +00007699 if (eap->forceit)
7700 {
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007701 // cursor line can be zero on startup
Bram Moolenaar09ca9322017-09-26 17:40:45 +02007702 if (!curwin->w_cursor.lnum)
7703 curwin->w_cursor.lnum = 1;
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007704 set_cursor_for_append_to_line();
Bram Moolenaarfd371682005-01-14 21:42:54 +00007705 }
7706
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007707 // Ignore the command when already in Insert mode. Inserting an
7708 // expression register that invokes a function can do this.
Bram Moolenaara40c5002005-01-09 21:16:21 +00007709 if (State & INSERT)
7710 return;
7711
Bram Moolenaar64969662005-12-14 21:59:55 +00007712 if (eap->cmdidx == CMD_startinsert)
7713 restart_edit = 'a';
7714 else if (eap->cmdidx == CMD_startreplace)
7715 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00007716 else
Bram Moolenaar64969662005-12-14 21:59:55 +00007717 restart_edit = 'V';
7718
7719 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007720 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00007721 if (eap->cmdidx == CMD_startinsert)
7722 restart_edit = 'i';
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007723 curwin->w_curswant = 0; // avoid MAXCOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007724 }
7725}
7726
7727/*
7728 * ":stopinsert"
7729 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007730 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007731ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007732{
7733 restart_edit = 0;
7734 stop_insert_mode = TRUE;
Bram Moolenaarfd773e92016-04-02 19:39:16 +02007735 clearmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007736}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007737
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007738/*
7739 * Execute normal mode command "cmd".
7740 * "remap" can be REMAP_NONE or REMAP_YES.
7741 */
7742 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007743exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007744{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007745 // Stuff the argument into the typeahead buffer.
Bram Moolenaar25281632016-01-21 23:32:32 +01007746 ins_typebuf(cmd, remap, 0, TRUE, silent);
Bram Moolenaar586c70c2018-10-02 16:23:58 +02007747 exec_normal(FALSE, FALSE, FALSE);
Bram Moolenaar25281632016-01-21 23:32:32 +01007748}
Bram Moolenaar25281632016-01-21 23:32:32 +01007749
Bram Moolenaar25281632016-01-21 23:32:32 +01007750/*
7751 * Execute normal_cmd() until there is no typeahead left.
Bram Moolenaar586c70c2018-10-02 16:23:58 +02007752 * When "use_vpeekc" is TRUE use vpeekc() to check for available chars.
Bram Moolenaar25281632016-01-21 23:32:32 +01007753 */
7754 void
Bram Moolenaar586c70c2018-10-02 16:23:58 +02007755exec_normal(int was_typed, int use_vpeekc, int may_use_terminal_loop UNUSED)
Bram Moolenaar25281632016-01-21 23:32:32 +01007756{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007757 oparg_T oa;
Bram Moolenaar905dd902019-04-07 14:21:47 +02007758 int c;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007759
Bram Moolenaar905dd902019-04-07 14:21:47 +02007760 // When calling vpeekc() from feedkeys() it will return Ctrl_C when there
7761 // is nothing to get, so also check for Ctrl_C.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007762 clear_oparg(&oa);
7763 finish_op = FALSE;
Bram Moolenaar586c70c2018-10-02 16:23:58 +02007764 while ((!stuff_empty()
7765 || ((was_typed || !typebuf_typed()) && typebuf.tb_len > 0)
Bram Moolenaar905dd902019-04-07 14:21:47 +02007766 || (use_vpeekc && (c = vpeekc()) != NUL && c != Ctrl_C))
Bram Moolenaar586c70c2018-10-02 16:23:58 +02007767 && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007768 {
7769 update_topline_cursor();
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02007770#ifdef FEAT_TERMINAL
Bram Moolenaar655a82a2018-05-08 22:01:07 +02007771 if (may_use_terminal_loop && term_use_loop()
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02007772 && oa.op_type == OP_NOP && oa.regname == NUL
7773 && !VIsual_active)
7774 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007775 // If terminal_loop() returns OK we got a key that is handled
7776 // in Normal model. With FAIL we first need to position the
7777 // cursor and the screen needs to be redrawn.
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02007778 if (terminal_loop(TRUE) == OK)
7779 normal_cmd(&oa, TRUE);
7780 }
7781 else
7782#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007783 // execute a Normal mode cmd
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02007784 normal_cmd(&oa, TRUE);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007785 }
7786}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007787
Bram Moolenaar071d4272004-06-13 20:20:40 +00007788#ifdef FEAT_FIND_ID
7789 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007790ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007791{
7792 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
7793 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
7794 (linenr_T)1, (linenr_T)MAXLNUM);
7795}
7796
Bram Moolenaar4033c552017-09-16 20:54:51 +02007797#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007798/*
7799 * ":psearch"
7800 */
7801 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007802ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007803{
7804 g_do_tagpreview = p_pvh;
7805 ex_findpat(eap);
7806 g_do_tagpreview = 0;
7807}
7808#endif
7809
7810 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007811ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007812{
7813 int whole = TRUE;
7814 long n;
7815 char_u *p;
7816 int action;
7817
7818 switch (cmdnames[eap->cmdidx].cmd_name[2])
7819 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007820 case 'e': // ":psearch", ":isearch" and ":dsearch"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007821 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
7822 action = ACTION_GOTO;
7823 else
7824 action = ACTION_SHOW;
7825 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007826 case 'i': // ":ilist" and ":dlist"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007827 action = ACTION_SHOW_ALL;
7828 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007829 case 'u': // ":ijump" and ":djump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007830 action = ACTION_GOTO;
7831 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007832 default: // ":isplit" and ":dsplit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007833 action = ACTION_SPLIT;
7834 break;
7835 }
7836
7837 n = 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007838 if (vim_isdigit(*eap->arg)) // get count
Bram Moolenaar071d4272004-06-13 20:20:40 +00007839 {
7840 n = getdigits(&eap->arg);
7841 eap->arg = skipwhite(eap->arg);
7842 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007843 if (*eap->arg == '/') // Match regexp, not just whole words
Bram Moolenaar071d4272004-06-13 20:20:40 +00007844 {
7845 whole = FALSE;
7846 ++eap->arg;
7847 p = skip_regexp(eap->arg, '/', p_magic, NULL);
7848 if (*p)
7849 {
7850 *p++ = NUL;
7851 p = skipwhite(p);
7852
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007853 // Check for trailing illegal characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00007854 if (!ends_excmd(*p))
7855 eap->errmsg = e_trailing;
7856 else
7857 eap->nextcmd = check_nextcmd(p);
7858 }
7859 }
7860 if (!eap->skip)
7861 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
7862 whole, !eap->forceit,
7863 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
7864 n, action, eap->line1, eap->line2);
7865}
7866#endif
7867
Bram Moolenaar071d4272004-06-13 20:20:40 +00007868
Bram Moolenaar4033c552017-09-16 20:54:51 +02007869#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00007870/*
7871 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
7872 */
7873 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007874ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007875{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007876 g_do_tagpreview = p_pvh; // will be reset to 0 in ex_tag_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00007877 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
7878}
7879
7880/*
7881 * ":pedit"
7882 */
7883 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007884ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007885{
7886 win_T *curwin_save = curwin;
7887
Bram Moolenaar026587b2019-08-17 15:08:00 +02007888 // Open the preview window or popup and make it the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007889 g_do_tagpreview = p_pvh;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02007890 prepare_tagpreview(TRUE, TRUE, FALSE);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02007891
Bram Moolenaar026587b2019-08-17 15:08:00 +02007892 // Edit the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007893 do_exedit(eap, NULL);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02007894
Bram Moolenaar071d4272004-06-13 20:20:40 +00007895 if (curwin != curwin_save && win_valid(curwin_save))
7896 {
Bram Moolenaar026587b2019-08-17 15:08:00 +02007897 // Return cursor to where we were
Bram Moolenaar071d4272004-06-13 20:20:40 +00007898 validate_cursor();
7899 redraw_later(VALID);
7900 win_enter(curwin_save, TRUE);
7901 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01007902# ifdef FEAT_PROP_POPUP
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02007903 else if (WIN_IS_POPUP(curwin))
7904 {
7905 // can't keep focus in popup window
7906 win_enter(firstwin, TRUE);
7907 }
7908# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007909 g_do_tagpreview = 0;
7910}
Bram Moolenaar4033c552017-09-16 20:54:51 +02007911#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007912
7913/*
7914 * ":stag", ":stselect" and ":stjump".
7915 */
7916 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007917ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007918{
7919 postponed_split = -1;
7920 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00007921 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007922 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
7923 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00007924 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007925}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007926
7927/*
7928 * ":tag", ":tselect", ":tjump", ":tnext", etc.
7929 */
7930 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007931ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007932{
7933 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
7934}
7935
7936 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007937ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007938{
7939 int cmd;
7940
7941 switch (name[1])
7942 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007943 case 'j': cmd = DT_JUMP; // ":tjump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007944 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007945 case 's': cmd = DT_SELECT; // ":tselect"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007946 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007947 case 'p': cmd = DT_PREV; // ":tprevious"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007948 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007949 case 'N': cmd = DT_PREV; // ":tNext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007950 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007951 case 'n': cmd = DT_NEXT; // ":tnext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007952 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007953 case 'o': cmd = DT_POP; // ":pop"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007954 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007955 case 'f': // ":tfirst"
7956 case 'r': cmd = DT_FIRST; // ":trewind"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007957 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007958 case 'l': cmd = DT_LAST; // ":tlast"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007959 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007960 default: // ":tag"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007961#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +00007962 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007963 {
Bram Moolenaard4db7712016-11-12 19:16:46 +01007964 ex_cstag(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007965 return;
7966 }
7967#endif
7968 cmd = DT_TAG;
7969 break;
7970 }
7971
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00007972 if (name[0] == 'l')
7973 {
7974#ifndef FEAT_QUICKFIX
7975 ex_ni(eap);
7976 return;
7977#else
7978 cmd = DT_LTAG;
7979#endif
7980 }
7981
Bram Moolenaar071d4272004-06-13 20:20:40 +00007982 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
7983 eap->forceit, TRUE);
7984}
7985
7986/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007987 * Check "str" for starting with a special cmdline variable.
7988 * If found return one of the SPEC_ values and set "*usedlen" to the length of
7989 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
7990 */
7991 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007992find_cmdline_var(char_u *src, int *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007993{
7994 int len;
7995 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +00007996 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007997 "%",
7998#define SPEC_PERC 0
7999 "#",
Bram Moolenaar65f08472017-09-10 18:16:20 +02008000#define SPEC_HASH (SPEC_PERC + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008001 "<cword>", // cursor word
Bram Moolenaar65f08472017-09-10 18:16:20 +02008002#define SPEC_CWORD (SPEC_HASH + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008003 "<cWORD>", // cursor WORD
Bram Moolenaar65f08472017-09-10 18:16:20 +02008004#define SPEC_CCWORD (SPEC_CWORD + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008005 "<cexpr>", // expr under cursor
Bram Moolenaar65f08472017-09-10 18:16:20 +02008006#define SPEC_CEXPR (SPEC_CCWORD + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008007 "<cfile>", // cursor path name
Bram Moolenaar65f08472017-09-10 18:16:20 +02008008#define SPEC_CFILE (SPEC_CEXPR + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008009 "<sfile>", // ":so" file name
Bram Moolenaar65f08472017-09-10 18:16:20 +02008010#define SPEC_SFILE (SPEC_CFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008011 "<slnum>", // ":so" file line number
Bram Moolenaar65f08472017-09-10 18:16:20 +02008012#define SPEC_SLNUM (SPEC_SFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008013 "<afile>", // autocommand file name
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008014#define SPEC_AFILE (SPEC_SLNUM + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008015 "<abuf>", // autocommand buffer number
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008016#define SPEC_ABUF (SPEC_AFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008017 "<amatch>", // autocommand match name
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008018#define SPEC_AMATCH (SPEC_ABUF + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008019 "<sflnum>", // script file line number
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008020#define SPEC_SFLNUM (SPEC_AMATCH + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008021#ifdef FEAT_CLIENTSERVER
8022 "<client>"
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008023# define SPEC_CLIENT (SPEC_SFLNUM + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008024#endif
8025 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008026
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008027 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008028 {
8029 len = (int)STRLEN(spec_str[i]);
8030 if (STRNCMP(src, spec_str[i], len) == 0)
8031 {
8032 *usedlen = len;
8033 return i;
8034 }
8035 }
8036 return -1;
8037}
8038
8039/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008040 * Evaluate cmdline variables.
8041 *
8042 * change '%' to curbuf->b_ffname
8043 * '#' to curwin->w_altfile
8044 * '<cword>' to word under the cursor
8045 * '<cWORD>' to WORD under the cursor
Bram Moolenaar8071cb22019-07-12 17:58:01 +02008046 * '<cexpr>' to C-expression under the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00008047 * '<cfile>' to path name under the cursor
8048 * '<sfile>' to sourced file name
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008049 * '<slnum>' to sourced file line number
Bram Moolenaar071d4272004-06-13 20:20:40 +00008050 * '<afile>' to file name for autocommand
8051 * '<abuf>' to buffer number for autocommand
8052 * '<amatch>' to matching name for autocommand
8053 *
8054 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
8055 * "" for error without a message) and NULL is returned.
8056 * Returns an allocated string if a valid match was found.
8057 * Returns NULL if no match was found. "usedlen" then still contains the
8058 * number of characters to skip.
8059 */
8060 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008061eval_vars(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008062 char_u *src, // pointer into commandline
8063 char_u *srcstart, // beginning of valid memory for src
8064 int *usedlen, // characters after src that are used
8065 linenr_T *lnump, // line number for :e command, or NULL
8066 char **errormsg, // pointer to error message
8067 int *escaped) // return value has escaped white space (can
8068 // be NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008069{
8070 int i;
8071 char_u *s;
8072 char_u *result;
8073 char_u *resultbuf = NULL;
8074 int resultlen;
8075 buf_T *buf;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008076 int valid = VALID_HEAD + VALID_PATH; // assume valid result
Bram Moolenaar071d4272004-06-13 20:20:40 +00008077 int spec_idx;
Bram Moolenaarfd249462018-07-28 16:14:30 +02008078 int tilde_file = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008079 int skip_mod = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008080 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008081
8082 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +00008083 if (escaped != NULL)
8084 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008085
8086 /*
8087 * Check if there is something to do.
8088 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008089 spec_idx = find_cmdline_var(src, usedlen);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008090 if (spec_idx < 0) // no match
Bram Moolenaar071d4272004-06-13 20:20:40 +00008091 {
8092 *usedlen = 1;
8093 return NULL;
8094 }
8095
8096 /*
8097 * Skip when preceded with a backslash "\%" and "\#".
8098 * Note: In "\\%" the % is also not recognized!
8099 */
8100 if (src > srcstart && src[-1] == '\\')
8101 {
8102 *usedlen = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008103 STRMOVE(src - 1, src); // remove backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00008104 return NULL;
8105 }
8106
8107 /*
8108 * word or WORD under cursor
8109 */
Bram Moolenaar65f08472017-09-10 18:16:20 +02008110 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD
8111 || spec_idx == SPEC_CEXPR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008112 {
Bram Moolenaar65f08472017-09-10 18:16:20 +02008113 resultlen = find_ident_under_cursor(&result,
8114 spec_idx == SPEC_CWORD ? (FIND_IDENT | FIND_STRING)
8115 : spec_idx == SPEC_CEXPR ? (FIND_IDENT | FIND_STRING | FIND_EVAL)
8116 : FIND_STRING);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008117 if (resultlen == 0)
8118 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008119 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008120 return NULL;
8121 }
8122 }
8123
8124 /*
8125 * '#': Alternate file name
8126 * '%': Current file name
8127 * File name under the cursor
8128 * File name for autocommand
8129 * and following modifiers
8130 */
8131 else
8132 {
8133 switch (spec_idx)
8134 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008135 case SPEC_PERC: // '%': current file
Bram Moolenaar071d4272004-06-13 20:20:40 +00008136 if (curbuf->b_fname == NULL)
8137 {
8138 result = (char_u *)"";
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008139 valid = 0; // Must have ":p:h" to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00008140 }
8141 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008142 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008143 result = curbuf->b_fname;
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008144 tilde_file = STRCMP(result, "~") == 0;
8145 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008146 break;
8147
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008148 case SPEC_HASH: // '#' or "#99": alternate file
8149 if (src[1] == '#') // "##": the argument list
Bram Moolenaar071d4272004-06-13 20:20:40 +00008150 {
8151 result = arg_all();
8152 resultbuf = result;
8153 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +00008154 if (escaped != NULL)
8155 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008156 skip_mod = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008157 break;
8158 }
8159 s = src + 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008160 if (*s == '<') // "#<99" uses v:oldfiles
Bram Moolenaard812df62008-11-09 12:46:09 +00008161 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008162 i = (int)getdigits(&s);
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008163 if (s == src + 2 && src[1] == '-')
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008164 // just a minus sign, don't skip over it
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008165 s--;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008166 *usedlen = (int)(s - src); // length of what we expand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008167
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008168 if (src[1] == '<' && i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008169 {
Bram Moolenaard812df62008-11-09 12:46:09 +00008170 if (*usedlen < 2)
8171 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008172 // Should we give an error message for #<text?
Bram Moolenaard812df62008-11-09 12:46:09 +00008173 *usedlen = 1;
8174 return NULL;
8175 }
8176#ifdef FEAT_EVAL
8177 result = list_find_str(get_vim_var_list(VV_OLDFILES),
8178 (long)i);
8179 if (result == NULL)
8180 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008181 *errormsg = "";
Bram Moolenaard812df62008-11-09 12:46:09 +00008182 return NULL;
8183 }
8184#else
Bram Moolenaar8e481e82019-01-15 20:07:48 +01008185 *errormsg = _("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008186 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00008187#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008188 }
8189 else
Bram Moolenaard812df62008-11-09 12:46:09 +00008190 {
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008191 if (i == 0 && src[1] == '<' && *usedlen > 1)
8192 *usedlen = 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00008193 buf = buflist_findnr(i);
8194 if (buf == NULL)
8195 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008196 *errormsg = _("E194: No alternate file name to substitute for '#'");
Bram Moolenaard812df62008-11-09 12:46:09 +00008197 return NULL;
8198 }
8199 if (lnump != NULL)
8200 *lnump = ECMD_LAST;
8201 if (buf->b_fname == NULL)
8202 {
8203 result = (char_u *)"";
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008204 valid = 0; // Must have ":p:h" to be valid
Bram Moolenaard812df62008-11-09 12:46:09 +00008205 }
8206 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008207 {
Bram Moolenaard812df62008-11-09 12:46:09 +00008208 result = buf->b_fname;
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008209 tilde_file = STRCMP(result, "~") == 0;
8210 }
Bram Moolenaard812df62008-11-09 12:46:09 +00008211 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008212 break;
8213
8214#ifdef FEAT_SEARCHPATH
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008215 case SPEC_CFILE: // file name under cursor
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008216 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008217 if (result == NULL)
8218 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008219 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008220 return NULL;
8221 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008222 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00008223 break;
8224#endif
8225
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008226 case SPEC_AFILE: // file name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008227 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +00008228 if (result != NULL && !autocmd_fname_full)
8229 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008230 // Still need to turn the fname into a full path. It is
8231 // postponed to avoid a delay when <afile> is not used.
Bram Moolenaarf6dad432008-09-18 19:29:58 +00008232 autocmd_fname_full = TRUE;
8233 result = FullName_save(autocmd_fname, FALSE);
8234 vim_free(autocmd_fname);
8235 autocmd_fname = result;
8236 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008237 if (result == NULL)
8238 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008239 *errormsg = _("E495: no autocommand file name to substitute for \"<afile>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008240 return NULL;
8241 }
Bram Moolenaara0174af2008-01-02 20:08:25 +00008242 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008243 break;
8244
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008245 case SPEC_ABUF: // buffer number for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008246 if (autocmd_bufnr <= 0)
8247 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008248 *errormsg = _("E496: no autocommand buffer number to substitute for \"<abuf>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008249 return NULL;
8250 }
8251 sprintf((char *)strbuf, "%d", autocmd_bufnr);
8252 result = strbuf;
8253 break;
8254
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008255 case SPEC_AMATCH: // match name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008256 result = autocmd_match;
8257 if (result == NULL)
8258 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008259 *errormsg = _("E497: no autocommand match name to substitute for \"<amatch>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008260 return NULL;
8261 }
8262 break;
8263
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008264 case SPEC_SFILE: // file name for ":so" command
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008265 result = estack_sfile();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008266 if (result == NULL)
8267 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008268 *errormsg = _("E498: no :source file name to substitute for \"<sfile>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008269 return NULL;
8270 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008271 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00008272 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008273
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008274 case SPEC_SLNUM: // line in file for ":so" command
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008275 if (SOURCING_NAME == NULL || SOURCING_LNUM == 0)
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008276 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008277 *errormsg = _("E842: no line number to use for \"<slnum>\"");
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008278 return NULL;
8279 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008280 sprintf((char *)strbuf, "%ld", SOURCING_LNUM);
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008281 result = strbuf;
8282 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008283
8284#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008285 case SPEC_SFLNUM: // line in script file
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008286 if (current_sctx.sc_lnum + SOURCING_LNUM == 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008287 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008288 *errormsg = _("E961: no line number to use for \"<sflnum>\"");
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008289 return NULL;
8290 }
8291 sprintf((char *)strbuf, "%ld",
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008292 (long)(current_sctx.sc_lnum + SOURCING_LNUM));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008293 result = strbuf;
8294 break;
8295#endif
8296
8297#ifdef FEAT_CLIENTSERVER
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008298 case SPEC_CLIENT: // Source of last submitted input
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008299 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
8300 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008301 result = strbuf;
8302 break;
8303#endif
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008304
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02008305 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008306 result = (char_u *)""; // avoid gcc warning
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02008307 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008308 }
8309
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008310 resultlen = (int)STRLEN(result); // length of new string
8311 if (src[*usedlen] == '<') // remove the file name extension
Bram Moolenaar071d4272004-06-13 20:20:40 +00008312 {
8313 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008314 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008315 resultlen = (int)(s - result);
8316 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008317 else if (!skip_mod)
8318 {
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008319 valid |= modify_fname(src, tilde_file, usedlen, &result, &resultbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008320 &resultlen);
8321 if (result == NULL)
8322 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008323 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008324 return NULL;
8325 }
8326 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008327 }
8328
8329 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
8330 {
8331 if (valid != VALID_HEAD + VALID_PATH)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008332 // xgettext:no-c-format
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008333 *errormsg = _("E499: Empty file name for '%' or '#', only works with \":p:h\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008334 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008335 *errormsg = _("E500: Evaluates to an empty string");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008336 result = NULL;
8337 }
8338 else
8339 result = vim_strnsave(result, resultlen);
8340 vim_free(resultbuf);
8341 return result;
8342}
8343
8344/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008345 * Expand the <sfile> string in "arg".
8346 *
8347 * Returns an allocated string, or NULL for any error.
8348 */
8349 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008350expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008351{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008352 char *errormsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008353 int len;
8354 char_u *result;
8355 char_u *newres;
8356 char_u *repl;
8357 int srclen;
8358 char_u *p;
8359
8360 result = vim_strsave(arg);
8361 if (result == NULL)
8362 return NULL;
8363
8364 for (p = result; *p; )
8365 {
8366 if (STRNCMP(p, "<sfile>", 7) != 0)
8367 ++p;
8368 else
8369 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008370 // replace "<sfile>" with the sourced file name, and do ":" stuff
Bram Moolenaar63b92542007-03-27 14:57:09 +00008371 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008372 if (errormsg != NULL)
8373 {
8374 if (*errormsg)
8375 emsg(errormsg);
8376 vim_free(result);
8377 return NULL;
8378 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008379 if (repl == NULL) // no match (cannot happen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008380 {
8381 p += srclen;
8382 continue;
8383 }
8384 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
8385 newres = alloc(len);
8386 if (newres == NULL)
8387 {
8388 vim_free(repl);
8389 vim_free(result);
8390 return NULL;
8391 }
8392 mch_memmove(newres, result, (size_t)(p - result));
8393 STRCPY(newres + (p - result), repl);
8394 len = (int)STRLEN(newres);
8395 STRCAT(newres, p + srclen);
8396 vim_free(repl);
8397 vim_free(result);
8398 result = newres;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008399 p = newres + len; // continue after the match
Bram Moolenaar071d4272004-06-13 20:20:40 +00008400 }
8401 }
8402
8403 return result;
8404}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008405
Bram Moolenaar071d4272004-06-13 20:20:40 +00008406#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00008407/*
Bram Moolenaard9462e32011-04-11 21:35:11 +02008408 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +00008409 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +00008410 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008411 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008412dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008413{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008414 if (fname == NULL)
8415 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +02008416 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008417}
8418#endif
8419
8420/*
8421 * ":behave {mswin,xterm}"
8422 */
8423 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008424ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008425{
8426 if (STRCMP(eap->arg, "mswin") == 0)
8427 {
8428 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
8429 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
8430 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
8431 set_option_value((char_u *)"keymodel", 0L,
8432 (char_u *)"startsel,stopsel", 0);
8433 }
8434 else if (STRCMP(eap->arg, "xterm") == 0)
8435 {
8436 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
8437 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
8438 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
8439 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
8440 }
8441 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008442 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008443}
8444
Bram Moolenaar071d4272004-06-13 20:20:40 +00008445static int filetype_detect = FALSE;
8446static int filetype_plugin = FALSE;
8447static int filetype_indent = FALSE;
8448
8449/*
8450 * ":filetype [plugin] [indent] {on,off,detect}"
8451 * on: Load the filetype.vim file to install autocommands for file types.
8452 * off: Load the ftoff.vim file to remove all autocommands for file types.
8453 * plugin on: load filetype.vim and ftplugin.vim
8454 * plugin off: load ftplugof.vim
8455 * indent on: load filetype.vim and indent.vim
8456 * indent off: load indoff.vim
8457 */
8458 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008459ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008460{
8461 char_u *arg = eap->arg;
8462 int plugin = FALSE;
8463 int indent = FALSE;
8464
8465 if (*eap->arg == NUL)
8466 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008467 // Print current status.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008468 smsg("filetype detection:%s plugin:%s indent:%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00008469 filetype_detect ? "ON" : "OFF",
8470 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
8471 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
8472 return;
8473 }
8474
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008475 // Accept "plugin" and "indent" in any order.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008476 for (;;)
8477 {
8478 if (STRNCMP(arg, "plugin", 6) == 0)
8479 {
8480 plugin = TRUE;
8481 arg = skipwhite(arg + 6);
8482 continue;
8483 }
8484 if (STRNCMP(arg, "indent", 6) == 0)
8485 {
8486 indent = TRUE;
8487 arg = skipwhite(arg + 6);
8488 continue;
8489 }
8490 break;
8491 }
8492 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
8493 {
8494 if (*arg == 'o' || !filetype_detect)
8495 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008496 source_runtime((char_u *)FILETYPE_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008497 filetype_detect = TRUE;
8498 if (plugin)
8499 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008500 source_runtime((char_u *)FTPLUGIN_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008501 filetype_plugin = TRUE;
8502 }
8503 if (indent)
8504 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008505 source_runtime((char_u *)INDENT_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008506 filetype_indent = TRUE;
8507 }
8508 }
8509 if (*arg == 'd')
8510 {
Bram Moolenaar1610d052016-06-09 22:53:01 +02008511 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +00008512 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008513 }
8514 }
8515 else if (STRCMP(arg, "off") == 0)
8516 {
8517 if (plugin || indent)
8518 {
8519 if (plugin)
8520 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008521 source_runtime((char_u *)FTPLUGOF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008522 filetype_plugin = FALSE;
8523 }
8524 if (indent)
8525 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008526 source_runtime((char_u *)INDOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008527 filetype_indent = FALSE;
8528 }
8529 }
8530 else
8531 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008532 source_runtime((char_u *)FTOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008533 filetype_detect = FALSE;
8534 }
8535 }
8536 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008537 semsg(_(e_invarg2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008538}
8539
8540/*
Bram Moolenaar3e545692017-06-04 19:00:32 +02008541 * ":setfiletype [FALLBACK] {name}"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008542 */
8543 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008544ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008545{
8546 if (!did_filetype)
Bram Moolenaar3e545692017-06-04 19:00:32 +02008547 {
8548 char_u *arg = eap->arg;
8549
8550 if (STRNCMP(arg, "FALLBACK ", 9) == 0)
8551 arg += 9;
8552
8553 set_option_value((char_u *)"filetype", 0L, arg, OPT_LOCAL);
8554 if (arg != eap->arg)
8555 did_filetype = FALSE;
8556 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008557}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008558
Bram Moolenaar071d4272004-06-13 20:20:40 +00008559 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008560ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008561{
8562#ifdef FEAT_DIGRAPHS
8563 if (*eap->arg != NUL)
8564 putdigraph(eap->arg);
8565 else
Bram Moolenaareae8ae12018-12-14 18:53:02 +01008566 listdigraphs(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008567#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008568 emsg(_("E196: No digraphs in this version"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008569#endif
8570}
8571
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02008572#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
8573 void
8574set_no_hlsearch(int flag)
8575{
8576 no_hlsearch = flag;
8577# ifdef FEAT_EVAL
8578 set_vim_var_nr(VV_HLSEARCH, !no_hlsearch && p_hls);
8579# endif
8580}
8581
Bram Moolenaar071d4272004-06-13 20:20:40 +00008582/*
8583 * ":nohlsearch"
8584 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008585 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008586ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008587{
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02008588 set_no_hlsearch(TRUE);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00008589 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008590}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008591#endif
8592
8593#ifdef FEAT_CRYPT
8594/*
8595 * ":X": Get crypt key
8596 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008597 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008598ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008599{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +01008600 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02008601 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008602}
8603#endif
8604
8605#ifdef FEAT_FOLDING
8606 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008607ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008608{
8609 if (foldManualAllowed(TRUE))
8610 foldCreate(eap->line1, eap->line2);
8611}
8612
8613 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008614ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008615{
8616 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
8617 eap->forceit, FALSE);
8618}
8619
8620 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008621ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008622{
8623 linenr_T lnum;
8624
Bram Moolenaar4facea32019-10-12 20:17:40 +02008625# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02008626 start_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +02008627# endif
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02008628
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008629 // First set the marks for all lines closed/open.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008630 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
8631 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
8632 ml_setmarked(lnum);
8633
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008634 // Execute the command on the marked lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008635 global_exe(eap->arg);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008636 ml_clearmarked(); // clear rest of the marks
Bram Moolenaar4facea32019-10-12 20:17:40 +02008637# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02008638 end_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +02008639# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008640}
8641#endif
Bram Moolenaarf5291f32017-09-14 22:55:37 +02008642
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008643#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar39665952018-08-15 20:59:48 +02008644/*
8645 * Returns TRUE if the supplied Ex cmdidx is for a location list command
8646 * instead of a quickfix command.
8647 */
8648 int
8649is_loclist_cmd(int cmdidx)
8650{
Bram Moolenaar74c8be22018-08-23 22:51:40 +02008651 if (cmdidx < 0 || cmdidx >= CMD_SIZE)
Bram Moolenaar39665952018-08-15 20:59:48 +02008652 return FALSE;
8653 return cmdnames[cmdidx].cmd_name[0] == 'l';
8654}
8655#endif
8656
Bram Moolenaar4facea32019-10-12 20:17:40 +02008657#if defined(FEAT_TIMERS) || defined(PROTO)
Bram Moolenaarf5291f32017-09-14 22:55:37 +02008658 int
8659get_pressedreturn(void)
8660{
8661 return ex_pressedreturn;
8662}
8663
8664 void
8665set_pressedreturn(int val)
8666{
8667 ex_pressedreturn = val;
8668}
8669#endif