blob: 659af19111dcd7af627b55cdd84bacc1c6587e11 [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 Moolenaar071d4272004-06-13 20:20:40 +00001650
1651 vim_memset(&ea, 0, sizeof(ea));
1652 ea.line1 = 1;
1653 ea.line2 = 1;
1654#ifdef FEAT_EVAL
1655 ++ex_nesting_level;
1656#endif
1657
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001658 // When the last file has not been edited :q has to be typed twice.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001659 if (quitmore
1660#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001661 // avoid that a function call in 'statusline' does this
Bram Moolenaar89d40322006-08-29 15:30:07 +00001662 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001663#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001664 // avoid that an autocommand, e.g. QuitPre, does this
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001665 && !getline_equal(fgetline, cookie, getnextac))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001666 --quitmore;
1667
1668 /*
1669 * Reset browse, confirm, etc.. They are restored when returning, for
1670 * recursive calls.
1671 */
1672 save_cmdmod = cmdmod;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001673
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001674 // "#!anything" is handled like a comment.
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001675 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1676 goto doend;
1677
Bram Moolenaar4facea32019-10-12 20:17:40 +02001678 if (p_verbose >= 16)
1679 msg_verbose_cmd(0, *cmdlinep);
1680
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02001681/*
1682 * 1. Skip comment lines and leading white space and colons.
1683 * 2. Handle command modifiers.
1684 */
1685 // The "ea" structure holds the arguments that can be used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001686 ea.cmd = *cmdlinep;
Bram Moolenaareffed932018-08-14 13:38:17 +02001687 ea.cmdlinep = cmdlinep;
1688 ea.getline = fgetline;
1689 ea.cookie = cookie;
1690#ifdef FEAT_EVAL
1691 ea.cstack = cstack;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001692#endif
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02001693 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaareffed932018-08-14 13:38:17 +02001694 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001695
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001696 after_modifier = ea.cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001697
1698#ifdef FEAT_EVAL
1699 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
1700 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
1701#else
1702 ea.skip = (if_level > 0);
1703#endif
1704
Bram Moolenaar071d4272004-06-13 20:20:40 +00001705/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001706 * 3. Skip over the range to find the command. Let "p" point to after it.
1707 *
1708 * We need the command to know what kind of range it uses.
1709 */
1710 cmd = ea.cmd;
1711 ea.cmd = skip_range(ea.cmd, NULL);
1712 if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
1713 ea.cmd = skipwhite(ea.cmd + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001714
1715#ifdef FEAT_EVAL
1716 if (current_sctx.sc_version == SCRIPT_VERSION_VIM9)
1717 p = find_ex_command(&ea, NULL, lookup_scriptvar, NULL);
1718 else
1719#endif
1720 p = find_ex_command(&ea, NULL, NULL, NULL);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001721
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001722#ifdef FEAT_EVAL
1723# ifdef FEAT_PROFILE
1724 // Count this line for profiling if skip is TRUE.
1725 if (do_profiling == PROF_YES
1726 && (!ea.skip || cstack->cs_idx == 0 || (cstack->cs_idx > 0
1727 && (cstack->cs_flags[cstack->cs_idx - 1] & CSF_ACTIVE))))
1728 {
1729 int skip = did_emsg || got_int || did_throw;
1730
1731 if (ea.cmdidx == CMD_catch)
1732 skip = !skip && !(cstack->cs_idx >= 0
1733 && (cstack->cs_flags[cstack->cs_idx] & CSF_THROWN)
1734 && !(cstack->cs_flags[cstack->cs_idx] & CSF_CAUGHT));
1735 else if (ea.cmdidx == CMD_else || ea.cmdidx == CMD_elseif)
1736 skip = skip || !(cstack->cs_idx >= 0
1737 && !(cstack->cs_flags[cstack->cs_idx]
1738 & (CSF_ACTIVE | CSF_TRUE)));
1739 else if (ea.cmdidx == CMD_finally)
1740 skip = FALSE;
1741 else if (ea.cmdidx != CMD_endif
1742 && ea.cmdidx != CMD_endfor
1743 && ea.cmdidx != CMD_endtry
1744 && ea.cmdidx != CMD_endwhile)
1745 skip = ea.skip;
1746
1747 if (!skip)
1748 {
1749 if (getline_equal(fgetline, cookie, get_func_line))
1750 func_line_exec(getline_cookie(fgetline, cookie));
1751 else if (getline_equal(fgetline, cookie, getsourceline))
1752 script_line_exec();
1753 }
1754 }
1755# endif
1756
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001757 // May go to debug mode. If this happens and the ">quit" debug command is
1758 // used, throw an interrupt exception and skip the next command.
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001759 dbg_check_breakpoint(&ea);
1760 if (!ea.skip && got_int)
1761 {
1762 ea.skip = TRUE;
1763 (void)do_intthrow(cstack);
1764 }
1765#endif
1766
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001767/*
1768 * 4. parse a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00001769 *
1770 * where 'addr' is:
1771 *
1772 * % (entire file)
1773 * $ [+-NUM]
1774 * 'x [+-NUM] (where x denotes a currently defined mark)
1775 * . [+-NUM]
1776 * [+-NUM]..
1777 * NUM
1778 *
1779 * The ea.cmd pointer is updated to point to the first character following the
1780 * range spec. If an initial address is found, but no second, the upper bound
1781 * is equal to the lower.
1782 */
1783
Bram Moolenaar25190db2019-05-04 15:05:28 +02001784 // ea.addr_type for user commands is set by find_ucmd
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001785 if (!IS_USER_CMDIDX(ea.cmdidx))
1786 {
1787 if (ea.cmdidx != CMD_SIZE)
1788 ea.addr_type = cmdnames[(int)ea.cmdidx].cmd_addr_type;
1789 else
1790 ea.addr_type = ADDR_LINES;
1791
Bram Moolenaar25190db2019-05-04 15:05:28 +02001792 // :wincmd range depends on the argument.
Bram Moolenaar164f3262015-01-14 21:22:01 +01001793 if (ea.cmdidx == CMD_wincmd && p != NULL)
1794 get_wincmd_addr_type(skipwhite(p), &ea);
Bram Moolenaar25190db2019-05-04 15:05:28 +02001795#ifdef FEAT_QUICKFIX
1796 // :.cc in quickfix window uses line number
1797 if ((ea.cmdidx == CMD_cc || ea.cmdidx == CMD_ll) && bt_quickfix(curbuf))
1798 ea.addr_type = ADDR_OTHER;
1799#endif
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001800 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001801
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001802 ea.cmd = cmd;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02001803 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaaree8415b2018-08-10 23:13:12 +02001804 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001805
Bram Moolenaar071d4272004-06-13 20:20:40 +00001806/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001807 * 5. Parse the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808 */
1809
1810 /*
1811 * Skip ':' and any white space
1812 */
1813 ea.cmd = skipwhite(ea.cmd);
1814 while (*ea.cmd == ':')
1815 ea.cmd = skipwhite(ea.cmd + 1);
1816
1817 /*
1818 * If we got a line, but no command, then go to the line.
1819 * If we find a '|' or '\n' we set ea.nextcmd.
1820 */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001821 if (*ea.cmd == NUL || *ea.cmd == '"'
1822 || (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823 {
1824 /*
1825 * strange vi behaviour:
1826 * ":3" jumps to line 3
1827 * ":3|..." prints line 3
1828 * ":|" prints current line
1829 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001830 if (ea.skip) // skip this if inside :if
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001832 if (*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833 {
1834 ea.cmdidx = CMD_print;
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001835 ea.argt = EX_RANGE+EX_COUNT+EX_TRLBAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836 if ((errormsg = invalid_range(&ea)) == NULL)
1837 {
1838 correct_range(&ea);
1839 ex_print(&ea);
1840 }
1841 }
1842 else if (ea.addr_count != 0)
1843 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001844 if (ea.line2 > curbuf->b_ml.ml_line_count)
1845 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001846 // With '-' in 'cpoptions' a line number past the file is an
1847 // error, otherwise put it at the end of the file.
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001848 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
1849 ea.line2 = -1;
1850 else
1851 ea.line2 = curbuf->b_ml.ml_line_count;
1852 }
1853
1854 if (ea.line2 < 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001855 errormsg = _(e_invrange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001856 else
1857 {
1858 if (ea.line2 == 0)
1859 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001860 else
1861 curwin->w_cursor.lnum = ea.line2;
1862 beginline(BL_SOL | BL_FIX);
1863 }
1864 }
1865 goto doend;
1866 }
1867
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001868 // If this looks like an undefined user command and there are CmdUndefined
1869 // autocommands defined, trigger the matching autocommands.
Bram Moolenaard5005162014-08-22 23:05:54 +02001870 if (p != NULL && ea.cmdidx == CMD_SIZE && !ea.skip
1871 && ASCII_ISUPPER(*ea.cmd)
1872 && has_cmdundefined())
1873 {
Bram Moolenaard5005162014-08-22 23:05:54 +02001874 int ret;
1875
Bram Moolenaar5a31b462014-08-23 14:16:20 +02001876 p = ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02001877 while (ASCII_ISALNUM(*p))
1878 ++p;
Bram Moolenaar120f4a82014-09-09 12:22:06 +02001879 p = vim_strnsave(ea.cmd, (int)(p - ea.cmd));
Bram Moolenaard5005162014-08-22 23:05:54 +02001880 ret = apply_autocmds(EVENT_CMDUNDEFINED, p, p, TRUE, NULL);
1881 vim_free(p);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001882 // If the autocommands did something and didn't cause an error, try
1883 // finding the command again.
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001884 p = (ret
1885#ifdef FEAT_EVAL
1886 && !aborting()
Bram Moolenaard5005162014-08-22 23:05:54 +02001887#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001888 ) ? find_ex_command(&ea, NULL, NULL, NULL) : ea.cmd;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001889 }
Bram Moolenaard5005162014-08-22 23:05:54 +02001890
Bram Moolenaar071d4272004-06-13 20:20:40 +00001891 if (p == NULL)
1892 {
1893 if (!ea.skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001894 errormsg = _("E464: Ambiguous use of user-defined command");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895 goto doend;
1896 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001897 // Check for wrong commands.
Bram Moolenaar6f9a4762017-06-22 20:39:17 +02001898 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78
1899 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900 {
1901 errormsg = uc_fun_cmd();
1902 goto doend;
1903 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001904
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905 if (ea.cmdidx == CMD_SIZE)
1906 {
1907 if (!ea.skip)
1908 {
1909 STRCPY(IObuff, _("E492: Not an editor command"));
1910 if (!sourcing)
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001911 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001912 // If the modifier was parsed OK the error must be in the
1913 // following command
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001914 if (after_modifier != NULL)
1915 append_command(after_modifier);
1916 else
1917 append_command(*cmdlinep);
1918 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001919 errormsg = (char *)IObuff;
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001920 did_emsg_syntax = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001921 }
1922 goto doend;
1923 }
1924
Bram Moolenaar958636c2014-10-21 20:01:58 +02001925 ni = (!IS_USER_CMDIDX(ea.cmdidx)
1926 && (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00001927#ifdef HAVE_EX_SCRIPT_NI
1928 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
1929#endif
1930 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001931
1932#ifndef FEAT_EVAL
1933 /*
1934 * When the expression evaluation is disabled, recognize the ":if" and
1935 * ":endif" commands and ignore everything in between it.
1936 */
1937 if (ea.cmdidx == CMD_if)
1938 ++if_level;
1939 if (if_level)
1940 {
1941 if (ea.cmdidx == CMD_endif)
1942 --if_level;
1943 goto doend;
1944 }
1945
1946#endif
1947
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001948 // forced commands
Bram Moolenaar196b3b02008-06-20 16:51:41 +00001949 if (*p == '!' && ea.cmdidx != CMD_substitute
1950 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951 {
1952 ++p;
1953 ea.forceit = TRUE;
1954 }
1955 else
1956 ea.forceit = FALSE;
1957
1958/*
Bram Moolenaarb7316892019-05-01 18:08:42 +02001959 * 6. Parse arguments. Then check for errors.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001960 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02001961 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001962 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001963
1964 if (!ea.skip)
1965 {
1966#ifdef HAVE_SANDBOX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001967 if (sandbox != 0 && !(ea.argt & EX_SBOXOK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001968 {
Bram Moolenaar8c62a082019-02-08 14:34:10 +01001969 // Command not allowed in sandbox.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001970 errormsg = _(e_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001971 goto doend;
1972 }
1973#endif
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001974 if (restricted != 0 && (ea.argt & EX_RESTRICT))
Bram Moolenaar8c62a082019-02-08 14:34:10 +01001975 {
1976 errormsg = _("E981: Command not allowed in rvim");
1977 goto doend;
1978 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001979 if (!curbuf->b_p_ma && (ea.argt & EX_MODIFY))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001980 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001981 // Command not allowed in non-'modifiable' buffer
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001982 errormsg = _(e_modifiable);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001983 goto doend;
1984 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001985
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001986 if (text_locked() && !(ea.argt & EX_CMDWIN)
Bram Moolenaar958636c2014-10-21 20:01:58 +02001987 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001988 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001989 // Command not allowed when editing the command line.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001990 errormsg = _(get_text_locked_msg());
Bram Moolenaar071d4272004-06-13 20:20:40 +00001991 goto doend;
1992 }
Bram Moolenaar379fb762018-08-30 15:58:28 +02001993
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001994 // Disallow editing another buffer when "curbuf_lock" is set.
1995 // Do allow ":checktime" (it is postponed).
1996 // Do allow ":edit" (check for an argument later).
1997 // Do allow ":file" with no arguments (check for an argument later).
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001998 if (!(ea.argt & EX_CMDWIN)
Bram Moolenaar5555acc2006-04-07 21:33:12 +00001999 && ea.cmdidx != CMD_checktime
Bram Moolenaar379fb762018-08-30 15:58:28 +02002000 && ea.cmdidx != CMD_edit
2001 && ea.cmdidx != CMD_file
Bram Moolenaar958636c2014-10-21 20:01:58 +02002002 && !IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002003 && curbuf_locked())
2004 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002005
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002006 if (!ni && !(ea.argt & EX_RANGE) && ea.addr_count > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002008 // no range allowed
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002009 errormsg = _(e_norange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002010 goto doend;
2011 }
2012 }
2013
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002014 if (!ni && !(ea.argt & EX_BANG) && ea.forceit) // no <!> allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002016 errormsg = _(e_nobang);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002017 goto doend;
2018 }
2019
2020 /*
2021 * Don't complain about the range if it is not used
2022 * (could happen if line_count is accidentally set to 0).
2023 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002024 if (!ea.skip && !ni && (ea.argt & EX_RANGE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025 {
2026 /*
2027 * If the range is backwards, ask for confirmation and, if given, swap
2028 * ea.line1 & ea.line2 so it's forwards again.
2029 * When global command is busy, don't ask, will fail below.
2030 */
2031 if (!global_busy && ea.line1 > ea.line2)
2032 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002033 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002034 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002035 if (sourcing || exmode_active)
2036 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002037 errormsg = _("E493: Backwards range given");
Bram Moolenaara5792f52005-11-23 21:25:05 +00002038 goto doend;
2039 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002040 if (ask_yesno((char_u *)
2041 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002042 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002043 }
2044 lnum = ea.line1;
2045 ea.line1 = ea.line2;
2046 ea.line2 = lnum;
2047 }
2048 if ((errormsg = invalid_range(&ea)) != NULL)
2049 goto doend;
2050 }
2051
Bram Moolenaarb7316892019-05-01 18:08:42 +02002052 if ((ea.addr_type == ADDR_OTHER) && ea.addr_count == 0)
2053 // default is 1, not cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002054 ea.line2 = 1;
2055
2056 correct_range(&ea);
2057
2058#ifdef FEAT_FOLDING
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002059 if (((ea.argt & EX_WHOLEFOLD) || ea.addr_count >= 2) && !global_busy
Bram Moolenaara3306952016-01-02 21:41:06 +01002060 && ea.addr_type == ADDR_LINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002061 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002062 // Put the first line at the start of a closed fold, put the last line
2063 // at the end of a closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064 (void)hasFolding(ea.line1, &ea.line1, NULL);
2065 (void)hasFolding(ea.line2, NULL, &ea.line2);
2066 }
2067#endif
2068
2069#ifdef FEAT_QUICKFIX
2070 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002071 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002072 * option here, so things like % get expanded.
2073 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002074 p = replace_makeprg(&ea, p, cmdlinep);
2075 if (p == NULL)
2076 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002077#endif
2078
2079 /*
2080 * Skip to start of argument.
2081 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2082 */
2083 if (ea.cmdidx == CMD_bang)
2084 ea.arg = p;
2085 else
2086 ea.arg = skipwhite(p);
2087
Bram Moolenaar379fb762018-08-30 15:58:28 +02002088 // ":file" cannot be run with an argument when "curbuf_lock" is set
2089 if (ea.cmdidx == CMD_file && *ea.arg != NUL && curbuf_locked())
2090 goto doend;
2091
Bram Moolenaar071d4272004-06-13 20:20:40 +00002092 /*
2093 * Check for "++opt=val" argument.
2094 * Must be first, allow ":w ++enc=utf8 !cmd"
2095 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002096 if (ea.argt & EX_ARGOPT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002097 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2098 if (getargopt(&ea) == FAIL && !ni)
2099 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002100 errormsg = _(e_invarg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002101 goto doend;
2102 }
2103
2104 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2105 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002106 if (*ea.arg == '>') // append
Bram Moolenaar071d4272004-06-13 20:20:40 +00002107 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002108 if (*++ea.arg != '>') // typed wrong
Bram Moolenaar071d4272004-06-13 20:20:40 +00002109 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002110 errormsg = _("E494: Use w or w>>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002111 goto doend;
2112 }
2113 ea.arg = skipwhite(ea.arg + 1);
2114 ea.append = TRUE;
2115 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002116 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) // :w !filter
Bram Moolenaar071d4272004-06-13 20:20:40 +00002117 {
2118 ++ea.arg;
2119 ea.usefilter = TRUE;
2120 }
2121 }
2122
2123 if (ea.cmdidx == CMD_read)
2124 {
2125 if (ea.forceit)
2126 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002127 ea.usefilter = TRUE; // :r! filter if ea.forceit
Bram Moolenaar071d4272004-06-13 20:20:40 +00002128 ea.forceit = FALSE;
2129 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002130 else if (*ea.arg == '!') // :r !filter
Bram Moolenaar071d4272004-06-13 20:20:40 +00002131 {
2132 ++ea.arg;
2133 ea.usefilter = TRUE;
2134 }
2135 }
2136
2137 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2138 {
2139 ea.amount = 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002140 while (*ea.arg == *ea.cmd) // count number of '>' or '<'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002141 {
2142 ++ea.arg;
2143 ++ea.amount;
2144 }
2145 ea.arg = skipwhite(ea.arg);
2146 }
2147
2148 /*
2149 * Check for "+command" argument, before checking for next command.
2150 * Don't do this for ":read !cmd" and ":write !cmd".
2151 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002152 if ((ea.argt & EX_CMDARG) && !ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002153 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2154
2155 /*
2156 * Check for '|' to separate commands and '"' to start comments.
2157 * Don't do this for ":read !cmd" and ":write !cmd".
2158 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002159 if ((ea.argt & EX_TRLBAR) && !ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160 separate_nextcmd(&ea);
2161
2162 /*
2163 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002164 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2165 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002167 else if (ea.cmdidx == CMD_bang
Bram Moolenaar67883b42017-07-27 22:57:00 +02002168 || ea.cmdidx == CMD_terminal
Bram Moolenaardf177f62005-02-22 08:39:57 +00002169 || ea.cmdidx == CMD_global
2170 || ea.cmdidx == CMD_vglobal
2171 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002172 {
2173 for (p = ea.arg; *p; ++p)
2174 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002175 // Remove one backslash before a newline, so that it's possible to
2176 // pass a newline to the shell and also a newline that is preceded
2177 // with a backslash. This makes it impossible to end a shell
2178 // command in a backslash, but that doesn't appear useful.
2179 // Halving the number of backslashes is incompatible with previous
2180 // versions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002181 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002182 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002183 else if (*p == '\n')
2184 {
2185 ea.nextcmd = p + 1;
2186 *p = NUL;
2187 break;
2188 }
2189 }
2190 }
2191
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002192 if ((ea.argt & EX_DFLALL) && ea.addr_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002193 {
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002194 buf_T *buf;
2195
Bram Moolenaar071d4272004-06-13 20:20:40 +00002196 ea.line1 = 1;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002197 switch (ea.addr_type)
2198 {
2199 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02002200 case ADDR_OTHER:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002201 ea.line2 = curbuf->b_ml.ml_line_count;
2202 break;
2203 case ADDR_LOADED_BUFFERS:
2204 buf = firstbuf;
2205 while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL)
2206 buf = buf->b_next;
2207 ea.line1 = buf->b_fnum;
2208 buf = lastbuf;
2209 while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL)
2210 buf = buf->b_prev;
2211 ea.line2 = buf->b_fnum;
2212 break;
2213 case ADDR_BUFFERS:
2214 ea.line1 = firstbuf->b_fnum;
2215 ea.line2 = lastbuf->b_fnum;
2216 break;
2217 case ADDR_WINDOWS:
2218 ea.line2 = LAST_WIN_NR;
2219 break;
2220 case ADDR_TABS:
2221 ea.line2 = LAST_TAB_NR;
2222 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01002223 case ADDR_TABS_RELATIVE:
2224 ea.line2 = 1;
2225 break;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002226 case ADDR_ARGUMENTS:
2227 if (ARGCOUNT == 0)
2228 ea.line1 = ea.line2 = 0;
2229 else
2230 ea.line2 = ARGCOUNT;
2231 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02002232 case ADDR_QUICKFIX_VALID:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002233#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02002234 ea.line2 = qf_get_valid_size(&ea);
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002235 if (ea.line2 == 0)
2236 ea.line2 = 1;
Bram Moolenaare906c502015-09-09 21:10:39 +02002237#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002238 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02002239 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02002240 case ADDR_UNSIGNED:
2241 case ADDR_QUICKFIX:
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002242 iemsg(_("INTERNAL: Cannot use EX_DFLALL with ADDR_NONE, ADDR_UNSIGNED or ADDR_QUICKFIX"));
Bram Moolenaarb7316892019-05-01 18:08:42 +02002243 break;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002244 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002245 }
2246
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002247 // accept numbered register only when no count allowed (:put)
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002248 if ( (ea.argt & EX_REGSTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249 && *ea.arg != NUL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002250 // Do not allow register = for user commands
Bram Moolenaar958636c2014-10-21 20:01:58 +02002251 && (!IS_USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002252 && !((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002253 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002254#ifndef FEAT_CLIPBOARD
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002255 // check these explicitly for a more specific error message
Bram Moolenaar85de2062011-05-05 14:26:41 +02002256 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002257 {
Bram Moolenaar99b12722019-01-14 20:16:40 +01002258 errormsg = _(e_invalidreg);
Bram Moolenaar85de2062011-05-05 14:26:41 +02002259 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002260 }
2261#endif
Bram Moolenaar958636c2014-10-21 20:01:58 +02002262 if (valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2263 && !IS_USER_CMDIDX(ea.cmdidx))))
Bram Moolenaar85de2062011-05-05 14:26:41 +02002264 {
2265 ea.regname = *ea.arg++;
2266#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002267 // for '=' register: accept the rest of the line as an expression
Bram Moolenaar85de2062011-05-05 14:26:41 +02002268 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2269 {
2270 set_expr_line(vim_strsave(ea.arg));
2271 ea.arg += STRLEN(ea.arg);
2272 }
2273#endif
2274 ea.arg = skipwhite(ea.arg);
2275 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276 }
2277
2278 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002279 * Check for a count. When accepting a EX_BUFNAME, don't use "123foo" as a
Bram Moolenaar071d4272004-06-13 20:20:40 +00002280 * count, it's a buffer name.
2281 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002282 if ((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)
2283 && (!(ea.argt & EX_BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01002284 || VIM_ISWHITE(*p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285 {
2286 n = getdigits(&ea.arg);
2287 ea.arg = skipwhite(ea.arg);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002288 if (n <= 0 && !ni && (ea.argt & EX_ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002290 errormsg = _(e_zerocount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291 goto doend;
2292 }
Bram Moolenaarb7316892019-05-01 18:08:42 +02002293 if (ea.addr_type != ADDR_LINES) // e.g. :buffer 2, :sleep 3
Bram Moolenaar071d4272004-06-13 20:20:40 +00002294 {
2295 ea.line2 = n;
2296 if (ea.addr_count == 0)
2297 ea.addr_count = 1;
2298 }
2299 else
2300 {
2301 ea.line1 = ea.line2;
2302 ea.line2 += n - 1;
2303 ++ea.addr_count;
2304 /*
2305 * Be vi compatible: no error message for out of range.
2306 */
Bram Moolenaarb7316892019-05-01 18:08:42 +02002307 if (ea.line2 > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308 ea.line2 = curbuf->b_ml.ml_line_count;
2309 }
2310 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002311
2312 /*
2313 * Check for flags: 'l', 'p' and '#'.
2314 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002315 if (ea.argt & EX_FLAGS)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002316 get_flags(&ea);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002317 if (!ni && !(ea.argt & EX_EXTRA) && *ea.arg != NUL
2318 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & EX_TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002319 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002320 // no arguments allowed but there is something
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002321 errormsg = _(e_trailing);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002322 goto doend;
2323 }
2324
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002325 if (!ni && (ea.argt & EX_NEEDARG) && *ea.arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002326 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002327 errormsg = _(e_argreq);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002328 goto doend;
2329 }
2330
2331#ifdef FEAT_EVAL
2332 /*
2333 * Skip the command when it's not going to be executed.
2334 * The commands like :if, :endif, etc. always need to be executed.
2335 * Also make an exception for commands that handle a trailing command
2336 * themselves.
2337 */
2338 if (ea.skip)
2339 {
2340 switch (ea.cmdidx)
2341 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002342 // commands that need evaluation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343 case CMD_while:
2344 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002345 case CMD_for:
2346 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347 case CMD_if:
2348 case CMD_elseif:
2349 case CMD_else:
2350 case CMD_endif:
2351 case CMD_try:
2352 case CMD_catch:
2353 case CMD_finally:
2354 case CMD_endtry:
2355 case CMD_function:
2356 break;
2357
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002358 // Commands that handle '|' themselves. Check: A command should
2359 // either have the EX_TRLBAR flag, appear in this list or appear in
2360 // the list at ":help :bar".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361 case CMD_aboveleft:
2362 case CMD_and:
2363 case CMD_belowright:
2364 case CMD_botright:
2365 case CMD_browse:
2366 case CMD_call:
2367 case CMD_confirm:
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01002368 case CMD_const:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369 case CMD_delfunction:
2370 case CMD_djump:
2371 case CMD_dlist:
2372 case CMD_dsearch:
2373 case CMD_dsplit:
2374 case CMD_echo:
2375 case CMD_echoerr:
2376 case CMD_echomsg:
2377 case CMD_echon:
2378 case CMD_execute:
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002379 case CMD_filter:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002380 case CMD_help:
2381 case CMD_hide:
2382 case CMD_ijump:
2383 case CMD_ilist:
2384 case CMD_isearch:
2385 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002386 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387 case CMD_keepjumps:
2388 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01002389 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002390 case CMD_leftabove:
2391 case CMD_let:
2392 case CMD_lockmarks:
Bram Moolenaarcc4423a2019-11-26 17:05:00 +01002393 case CMD_lockvar:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002394 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002395 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002396 case CMD_mzscheme:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01002397 case CMD_noautocmd:
2398 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399 case CMD_perl:
2400 case CMD_psearch:
2401 case CMD_python:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002402 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002403 case CMD_python3:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002404 case CMD_return:
2405 case CMD_rightbelow:
2406 case CMD_ruby:
2407 case CMD_silent:
2408 case CMD_smagic:
2409 case CMD_snomagic:
2410 case CMD_substitute:
2411 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002412 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413 case CMD_tcl:
2414 case CMD_throw:
2415 case CMD_tilde:
2416 case CMD_topleft:
2417 case CMD_unlet:
Bram Moolenaarcc4423a2019-11-26 17:05:00 +01002418 case CMD_unlockvar:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419 case CMD_verbose:
2420 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002421 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422 break;
2423
2424 default: goto doend;
2425 }
2426 }
2427#endif
2428
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002429 if (ea.argt & EX_XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430 {
2431 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2432 goto doend;
2433 }
2434
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435 /*
2436 * Accept buffer name. Cannot be used at the same time with a buffer
2437 * number. Don't do this for a user command.
2438 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002439 if ((ea.argt & EX_BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
Bram Moolenaar958636c2014-10-21 20:01:58 +02002440 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002441 {
2442 /*
2443 * :bdelete, :bwipeout and :bunload take several arguments, separated
2444 * by spaces: find next space (skipping over escaped characters).
2445 * The others take one argument: ignore trailing spaces.
2446 */
2447 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2448 || ea.cmdidx == CMD_bunload)
2449 p = skiptowhite_esc(ea.arg);
2450 else
2451 {
2452 p = ea.arg + STRLEN(ea.arg);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002453 while (p > ea.arg && VIM_ISWHITE(p[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454 --p;
2455 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002456 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & EX_BUFUNL) != 0,
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002457 FALSE, FALSE);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002458 if (ea.line2 < 0) // failed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459 goto doend;
2460 ea.addr_count = 1;
2461 ea.arg = skipwhite(p);
2462 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002464 // The :try command saves the emsg_silent flag, reset it here when
2465 // ":silent! try" was used, it should only apply to :try itself.
Bram Moolenaareffed932018-08-14 13:38:17 +02002466 if (ea.cmdidx == CMD_try && ea.did_esilent > 0)
Bram Moolenaar2be57332018-02-13 18:05:18 +01002467 {
Bram Moolenaareffed932018-08-14 13:38:17 +02002468 emsg_silent -= ea.did_esilent;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002469 if (emsg_silent < 0)
2470 emsg_silent = 0;
Bram Moolenaareffed932018-08-14 13:38:17 +02002471 ea.did_esilent = 0;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002472 }
2473
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474/*
Bram Moolenaar2be57332018-02-13 18:05:18 +01002475 * 7. Execute the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002476 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477
Bram Moolenaar958636c2014-10-21 20:01:58 +02002478 if (IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002479 {
2480 /*
2481 * Execute a user-defined command.
2482 */
2483 do_ucmd(&ea);
2484 }
2485 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486 {
2487 /*
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002488 * Call the function to execute the builtin command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002489 */
2490 ea.errmsg = NULL;
2491 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2492 if (ea.errmsg != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002493 errormsg = _(ea.errmsg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494 }
2495
2496#ifdef FEAT_EVAL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002497 // Set flag that any command was executed, used by ex_vim9script().
Bram Moolenaar16531552020-02-08 16:00:46 +01002498 if (getline_equal(ea.getline, ea.cookie, getsourceline)
2499 && current_sctx.sc_sid > 0)
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002500 SCRIPT_ITEM(current_sctx.sc_sid)->sn_had_command = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002501
Bram Moolenaar071d4272004-06-13 20:20:40 +00002502 /*
2503 * If the command just executed called do_cmdline(), any throw or ":return"
2504 * or ":finish" encountered there must also check the cstack of the still
2505 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2506 * exception, or reanimate a returned function or finished script file and
2507 * return or finish it again.
2508 */
2509 if (need_rethrow)
2510 do_throw(cstack);
2511 else if (check_cstack)
2512 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002513 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002515 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002516 && current_func_returned())
2517 do_return(&ea, TRUE, FALSE, NULL);
2518 }
2519 need_rethrow = check_cstack = FALSE;
2520#endif
2521
2522doend:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002523 if (curwin->w_cursor.lnum == 0) // can happen with zero line number
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002524 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002525 curwin->w_cursor.lnum = 1;
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002526 curwin->w_cursor.col = 0;
2527 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002528
2529 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2530 {
2531 if (sourcing)
2532 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002533 if (errormsg != (char *)IObuff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002534 {
2535 STRCPY(IObuff, errormsg);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002536 errormsg = (char *)IObuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002537 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002538 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002539 }
2540 emsg(errormsg);
2541 }
2542#ifdef FEAT_EVAL
2543 do_errthrow(cstack,
Bram Moolenaar958636c2014-10-21 20:01:58 +02002544 (ea.cmdidx != CMD_SIZE && !IS_USER_CMDIDX(ea.cmdidx))
2545 ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546#endif
2547
Bram Moolenaareffed932018-08-14 13:38:17 +02002548 if (ea.verbose_save >= 0)
2549 p_verbose = ea.verbose_save;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002550
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002551 free_cmdmod();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002552 cmdmod = save_cmdmod;
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01002553 reg_executing = save_reg_executing;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554
Bram Moolenaareffed932018-08-14 13:38:17 +02002555 if (ea.save_msg_silent != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002556 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002557 // messages could be enabled for a serious error, need to check if the
2558 // counters don't become negative
Bram Moolenaareffed932018-08-14 13:38:17 +02002559 if (!did_emsg || msg_silent > ea.save_msg_silent)
2560 msg_silent = ea.save_msg_silent;
2561 emsg_silent -= ea.did_esilent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562 if (emsg_silent < 0)
2563 emsg_silent = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002564 // Restore msg_scroll, it's set by file I/O commands, even when no
2565 // message is actually displayed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002566 msg_scroll = save_msg_scroll;
Bram Moolenaar77ab2802009-04-22 12:44:48 +00002567
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002568 // "silent reg" or "silent echo x" inside "redir" leaves msg_col
2569 // somewhere in the line. Put it back in the first column.
Bram Moolenaar77ab2802009-04-22 12:44:48 +00002570 if (redirecting())
2571 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002572 }
2573
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002574#ifdef HAVE_SANDBOX
Bram Moolenaareffed932018-08-14 13:38:17 +02002575 if (ea.did_sandbox)
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002576 --sandbox;
2577#endif
2578
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002579 if (ea.nextcmd && *ea.nextcmd == NUL) // not really a next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580 ea.nextcmd = NULL;
2581
2582#ifdef FEAT_EVAL
2583 --ex_nesting_level;
2584#endif
2585
2586 return ea.nextcmd;
2587}
2588#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00002589 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002590#endif
2591
2592/*
Bram Moolenaareffed932018-08-14 13:38:17 +02002593 * Parse and skip over command modifiers:
2594 * - update eap->cmd
2595 * - store flags in "cmdmod".
2596 * - Set ex_pressedreturn for an empty command line.
2597 * - set msg_silent for ":silent"
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002598 * - set 'eventignore' to "all" for ":noautocmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002599 * - set p_verbose for ":verbose"
2600 * - Increment "sandbox" for ":sandbox"
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002601 * When "skip_only" is TRUE the global variables are not changed, except for
2602 * "cmdmod".
Bram Moolenaareffed932018-08-14 13:38:17 +02002603 * Return FAIL when the command is not to be executed.
2604 * May set "errormsg" to an error message.
2605 */
2606 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002607parse_command_modifiers(exarg_T *eap, char **errormsg, int skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002608{
2609 char_u *p;
2610
2611 vim_memset(&cmdmod, 0, sizeof(cmdmod));
2612 eap->verbose_save = -1;
2613 eap->save_msg_silent = -1;
2614
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002615 // Repeat until no more command modifiers are found.
Bram Moolenaareffed932018-08-14 13:38:17 +02002616 for (;;)
2617 {
Bram Moolenaareffed932018-08-14 13:38:17 +02002618 while (*eap->cmd == ' ' || *eap->cmd == '\t' || *eap->cmd == ':')
2619 ++eap->cmd;
2620
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002621 // in ex mode, an empty line works like :+
Bram Moolenaareffed932018-08-14 13:38:17 +02002622 if (*eap->cmd == NUL && exmode_active
2623 && (getline_equal(eap->getline, eap->cookie, getexmodeline)
2624 || getline_equal(eap->getline, eap->cookie, getexline))
2625 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2626 {
2627 eap->cmd = (char_u *)"+";
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002628 if (!skip_only)
2629 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002630 }
2631
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002632 // ignore comment and empty lines
Bram Moolenaareffed932018-08-14 13:38:17 +02002633 if (*eap->cmd == '"')
2634 return FAIL;
2635 if (*eap->cmd == NUL)
2636 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002637 if (!skip_only)
2638 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002639 return FAIL;
2640 }
2641
Bram Moolenaareffed932018-08-14 13:38:17 +02002642 p = skip_range(eap->cmd, NULL);
2643 switch (*p)
2644 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002645 // When adding an entry, also modify cmd_exists().
Bram Moolenaareffed932018-08-14 13:38:17 +02002646 case 'a': if (!checkforcmd(&eap->cmd, "aboveleft", 3))
2647 break;
2648 cmdmod.split |= WSP_ABOVE;
2649 continue;
2650
2651 case 'b': if (checkforcmd(&eap->cmd, "belowright", 3))
2652 {
2653 cmdmod.split |= WSP_BELOW;
2654 continue;
2655 }
2656 if (checkforcmd(&eap->cmd, "browse", 3))
2657 {
2658#ifdef FEAT_BROWSE_CMD
2659 cmdmod.browse = TRUE;
2660#endif
2661 continue;
2662 }
2663 if (!checkforcmd(&eap->cmd, "botright", 2))
2664 break;
2665 cmdmod.split |= WSP_BOT;
2666 continue;
2667
2668 case 'c': if (!checkforcmd(&eap->cmd, "confirm", 4))
2669 break;
2670#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2671 cmdmod.confirm = TRUE;
2672#endif
2673 continue;
2674
2675 case 'k': if (checkforcmd(&eap->cmd, "keepmarks", 3))
2676 {
2677 cmdmod.keepmarks = TRUE;
2678 continue;
2679 }
2680 if (checkforcmd(&eap->cmd, "keepalt", 5))
2681 {
2682 cmdmod.keepalt = TRUE;
2683 continue;
2684 }
2685 if (checkforcmd(&eap->cmd, "keeppatterns", 5))
2686 {
2687 cmdmod.keeppatterns = TRUE;
2688 continue;
2689 }
2690 if (!checkforcmd(&eap->cmd, "keepjumps", 5))
2691 break;
2692 cmdmod.keepjumps = TRUE;
2693 continue;
2694
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002695 case 'f': // only accept ":filter {pat} cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002696 {
2697 char_u *reg_pat;
2698
2699 if (!checkforcmd(&p, "filter", 4)
2700 || *p == NUL || ends_excmd(*p))
2701 break;
2702 if (*p == '!')
2703 {
2704 cmdmod.filter_force = TRUE;
2705 p = skipwhite(p + 1);
2706 if (*p == NUL || ends_excmd(*p))
2707 break;
2708 }
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002709 if (skip_only)
2710 p = skip_vimgrep_pat(p, NULL, NULL);
2711 else
2712 // NOTE: This puts a NUL after the pattern.
2713 p = skip_vimgrep_pat(p, &reg_pat, NULL);
Bram Moolenaareffed932018-08-14 13:38:17 +02002714 if (p == NULL || *p == NUL)
2715 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002716 if (!skip_only)
2717 {
2718 cmdmod.filter_regmatch.regprog =
Bram Moolenaareffed932018-08-14 13:38:17 +02002719 vim_regcomp(reg_pat, RE_MAGIC);
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002720 if (cmdmod.filter_regmatch.regprog == NULL)
2721 break;
2722 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002723 eap->cmd = p;
2724 continue;
2725 }
2726
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002727 // ":hide" and ":hide | cmd" are not modifiers
Bram Moolenaareffed932018-08-14 13:38:17 +02002728 case 'h': if (p != eap->cmd || !checkforcmd(&p, "hide", 3)
2729 || *p == NUL || ends_excmd(*p))
2730 break;
2731 eap->cmd = p;
2732 cmdmod.hide = TRUE;
2733 continue;
2734
2735 case 'l': if (checkforcmd(&eap->cmd, "lockmarks", 3))
2736 {
2737 cmdmod.lockmarks = TRUE;
2738 continue;
2739 }
2740
2741 if (!checkforcmd(&eap->cmd, "leftabove", 5))
2742 break;
2743 cmdmod.split |= WSP_ABOVE;
2744 continue;
2745
2746 case 'n': if (checkforcmd(&eap->cmd, "noautocmd", 3))
2747 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002748 if (cmdmod.save_ei == NULL && !skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002749 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002750 // Set 'eventignore' to "all". Restore the
2751 // existing option value later.
Bram Moolenaareffed932018-08-14 13:38:17 +02002752 cmdmod.save_ei = vim_strsave(p_ei);
2753 set_string_option_direct((char_u *)"ei", -1,
2754 (char_u *)"all", OPT_FREE, SID_NONE);
2755 }
2756 continue;
2757 }
2758 if (!checkforcmd(&eap->cmd, "noswapfile", 3))
2759 break;
2760 cmdmod.noswapfile = TRUE;
2761 continue;
2762
2763 case 'r': if (!checkforcmd(&eap->cmd, "rightbelow", 6))
2764 break;
2765 cmdmod.split |= WSP_BELOW;
2766 continue;
2767
2768 case 's': if (checkforcmd(&eap->cmd, "sandbox", 3))
2769 {
2770#ifdef HAVE_SANDBOX
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002771 if (!skip_only)
2772 {
2773 if (!eap->did_sandbox)
2774 ++sandbox;
2775 eap->did_sandbox = TRUE;
2776 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002777#endif
2778 continue;
2779 }
2780 if (!checkforcmd(&eap->cmd, "silent", 3))
2781 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002782 if (!skip_only)
2783 {
2784 if (eap->save_msg_silent == -1)
2785 eap->save_msg_silent = msg_silent;
2786 ++msg_silent;
2787 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002788 if (*eap->cmd == '!' && !VIM_ISWHITE(eap->cmd[-1]))
2789 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002790 // ":silent!", but not "silent !cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002791 eap->cmd = skipwhite(eap->cmd + 1);
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002792 if (!skip_only)
2793 {
2794 ++emsg_silent;
2795 ++eap->did_esilent;
2796 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002797 }
2798 continue;
2799
2800 case 't': if (checkforcmd(&p, "tab", 3))
2801 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002802 if (!skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002803 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002804 long tabnr = get_address(eap, &eap->cmd,
2805 ADDR_TABS, eap->skip,
2806 skip_only, FALSE, 1);
2807 if (tabnr == MAXLNUM)
2808 cmdmod.tab = tabpage_index(curtab) + 1;
2809 else
Bram Moolenaareffed932018-08-14 13:38:17 +02002810 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002811 if (tabnr < 0 || tabnr > LAST_TAB_NR)
2812 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002813 *errormsg = _(e_invrange);
Bram Moolenaar548e5982018-12-26 21:45:00 +01002814 return FAIL;
2815 }
2816 cmdmod.tab = tabnr + 1;
Bram Moolenaareffed932018-08-14 13:38:17 +02002817 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002818 }
2819 eap->cmd = p;
2820 continue;
2821 }
2822 if (!checkforcmd(&eap->cmd, "topleft", 2))
2823 break;
2824 cmdmod.split |= WSP_TOP;
2825 continue;
2826
2827 case 'u': if (!checkforcmd(&eap->cmd, "unsilent", 3))
2828 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002829 if (!skip_only)
2830 {
2831 if (eap->save_msg_silent == -1)
2832 eap->save_msg_silent = msg_silent;
2833 msg_silent = 0;
2834 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002835 continue;
2836
2837 case 'v': if (checkforcmd(&eap->cmd, "vertical", 4))
2838 {
2839 cmdmod.split |= WSP_VERT;
2840 continue;
2841 }
2842 if (!checkforcmd(&p, "verbose", 4))
2843 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002844 if (!skip_only)
2845 {
2846 if (eap->verbose_save < 0)
2847 eap->verbose_save = p_verbose;
2848 if (vim_isdigit(*eap->cmd))
2849 p_verbose = atoi((char *)eap->cmd);
2850 else
2851 p_verbose = 1;
2852 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002853 eap->cmd = p;
2854 continue;
2855 }
2856 break;
2857 }
2858
2859 return OK;
2860}
2861
2862/*
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002863 * Free contents of "cmdmod".
2864 */
2865 static void
2866free_cmdmod(void)
2867{
2868 if (cmdmod.save_ei != NULL)
2869 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002870 // Restore 'eventignore' to the value before ":noautocmd".
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002871 set_string_option_direct((char_u *)"ei", -1, cmdmod.save_ei,
2872 OPT_FREE, SID_NONE);
2873 free_string_option(cmdmod.save_ei);
2874 }
2875
2876 if (cmdmod.filter_regmatch.regprog != NULL)
2877 vim_regfree(cmdmod.filter_regmatch.regprog);
2878}
2879
2880/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002881 * Parse the address range, if any, in "eap".
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02002882 * May set the last search pattern, unless "silent" is TRUE.
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002883 * Return FAIL and set "errormsg" or return OK.
2884 */
2885 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002886parse_cmd_address(exarg_T *eap, char **errormsg, int silent)
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002887{
2888 int address_count = 1;
2889 linenr_T lnum;
2890
2891 // Repeat for all ',' or ';' separated addresses.
2892 for (;;)
2893 {
2894 eap->line1 = eap->line2;
2895 switch (eap->addr_type)
2896 {
2897 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02002898 case ADDR_OTHER:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002899 // default is current line number
2900 eap->line2 = curwin->w_cursor.lnum;
2901 break;
2902 case ADDR_WINDOWS:
2903 eap->line2 = CURRENT_WIN_NR;
2904 break;
2905 case ADDR_ARGUMENTS:
2906 eap->line2 = curwin->w_arg_idx + 1;
2907 if (eap->line2 > ARGCOUNT)
2908 eap->line2 = ARGCOUNT;
2909 break;
2910 case ADDR_LOADED_BUFFERS:
2911 case ADDR_BUFFERS:
2912 eap->line2 = curbuf->b_fnum;
2913 break;
2914 case ADDR_TABS:
2915 eap->line2 = CURRENT_TAB_NR;
2916 break;
2917 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02002918 case ADDR_UNSIGNED:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002919 eap->line2 = 1;
2920 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002921 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002922#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02002923 eap->line2 = qf_get_cur_idx(eap);
2924#endif
2925 break;
2926 case ADDR_QUICKFIX_VALID:
2927#ifdef FEAT_QUICKFIX
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002928 eap->line2 = qf_get_cur_valid_idx(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002929#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002930 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02002931 case ADDR_NONE:
2932 // Will give an error later if a range is found.
2933 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002934 }
2935 eap->cmd = skipwhite(eap->cmd);
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02002936 lnum = get_address(eap, &eap->cmd, eap->addr_type, eap->skip, silent,
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002937 eap->addr_count == 0, address_count++);
2938 if (eap->cmd == NULL) // error detected
2939 return FAIL;
2940 if (lnum == MAXLNUM)
2941 {
2942 if (*eap->cmd == '%') // '%' - all lines
2943 {
2944 ++eap->cmd;
2945 switch (eap->addr_type)
2946 {
2947 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02002948 case ADDR_OTHER:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002949 eap->line1 = 1;
2950 eap->line2 = curbuf->b_ml.ml_line_count;
2951 break;
2952 case ADDR_LOADED_BUFFERS:
2953 {
2954 buf_T *buf = firstbuf;
2955
2956 while (buf->b_next != NULL
2957 && buf->b_ml.ml_mfp == NULL)
2958 buf = buf->b_next;
2959 eap->line1 = buf->b_fnum;
2960 buf = lastbuf;
2961 while (buf->b_prev != NULL
2962 && buf->b_ml.ml_mfp == NULL)
2963 buf = buf->b_prev;
2964 eap->line2 = buf->b_fnum;
2965 break;
2966 }
2967 case ADDR_BUFFERS:
2968 eap->line1 = firstbuf->b_fnum;
2969 eap->line2 = lastbuf->b_fnum;
2970 break;
2971 case ADDR_WINDOWS:
2972 case ADDR_TABS:
2973 if (IS_USER_CMDIDX(eap->cmdidx))
2974 {
2975 eap->line1 = 1;
2976 eap->line2 = eap->addr_type == ADDR_WINDOWS
2977 ? LAST_WIN_NR : LAST_TAB_NR;
2978 }
2979 else
2980 {
2981 // there is no Vim command which uses '%' and
2982 // ADDR_WINDOWS or ADDR_TABS
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002983 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002984 return FAIL;
2985 }
2986 break;
2987 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02002988 case ADDR_UNSIGNED:
2989 case ADDR_QUICKFIX:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002990 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002991 return FAIL;
2992 case ADDR_ARGUMENTS:
2993 if (ARGCOUNT == 0)
2994 eap->line1 = eap->line2 = 0;
2995 else
2996 {
2997 eap->line1 = 1;
2998 eap->line2 = ARGCOUNT;
2999 }
3000 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003001 case ADDR_QUICKFIX_VALID:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003002#ifdef FEAT_QUICKFIX
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003003 eap->line1 = 1;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003004 eap->line2 = qf_get_valid_size(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003005 if (eap->line2 == 0)
3006 eap->line2 = 1;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003007#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003008 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003009 case ADDR_NONE:
3010 // Will give an error later if a range is found.
3011 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003012 }
3013 ++eap->addr_count;
3014 }
3015 else if (*eap->cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
3016 {
3017 pos_T *fp;
3018
3019 // '*' - visual area
3020 if (eap->addr_type != ADDR_LINES)
3021 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003022 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003023 return FAIL;
3024 }
3025
3026 ++eap->cmd;
3027 if (!eap->skip)
3028 {
3029 fp = getmark('<', FALSE);
3030 if (check_mark(fp) == FAIL)
3031 return FAIL;
3032 eap->line1 = fp->lnum;
3033 fp = getmark('>', FALSE);
3034 if (check_mark(fp) == FAIL)
3035 return FAIL;
3036 eap->line2 = fp->lnum;
3037 ++eap->addr_count;
3038 }
3039 }
3040 }
3041 else
3042 eap->line2 = lnum;
3043 eap->addr_count++;
3044
3045 if (*eap->cmd == ';')
3046 {
3047 if (!eap->skip)
3048 {
3049 curwin->w_cursor.lnum = eap->line2;
3050 // don't leave the cursor on an illegal line or column
3051 check_cursor();
3052 }
3053 }
3054 else if (*eap->cmd != ',')
3055 break;
3056 ++eap->cmd;
3057 }
3058
3059 // One address given: set start and end lines.
3060 if (eap->addr_count == 1)
3061 {
3062 eap->line1 = eap->line2;
3063 // ... but only implicit: really no address given
3064 if (lnum == MAXLNUM)
3065 eap->addr_count = 0;
3066 }
3067 return OK;
3068}
3069
3070/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003071 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003072 * If there is a match advance "pp" to the argument and return TRUE.
3073 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003074 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003075checkforcmd(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003076 char_u **pp, // start of command
3077 char *cmd, // name of command
3078 int len) // required length
Bram Moolenaar071d4272004-06-13 20:20:40 +00003079{
3080 int i;
3081
3082 for (i = 0; cmd[i] != NUL; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003083 if (((char_u *)cmd)[i] != (*pp)[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003084 break;
3085 if (i >= len && !isalpha((*pp)[i]))
3086 {
3087 *pp = skipwhite(*pp + i);
3088 return TRUE;
3089 }
3090 return FALSE;
3091}
3092
3093/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003094 * Append "cmd" to the error message in IObuff.
3095 * Takes care of limiting the length and handling 0xa0, which would be
3096 * invisible otherwise.
3097 */
3098 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003099append_command(char_u *cmd)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003100{
3101 char_u *s = cmd;
3102 char_u *d;
3103
3104 STRCAT(IObuff, ": ");
3105 d = IObuff + STRLEN(IObuff);
3106 while (*s != NUL && d - IObuff < IOSIZE - 7)
3107 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003108 if (enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) : *s == 0xa0)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003109 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003110 s += enc_utf8 ? 2 : 1;
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003111 STRCPY(d, "<a0>");
3112 d += 4;
3113 }
3114 else
3115 MB_COPY_CHAR(s, d);
3116 }
3117 *d = NUL;
3118}
3119
3120/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003121 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003122 * Start of the name can be found at eap->cmd.
Bram Moolenaar25190db2019-05-04 15:05:28 +02003123 * Sets eap->cmdidx and returns a pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003124 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003125 *
3126 * If "lookup" is not NULL recognize expression without "eval" or "call" and
3127 * assignment without "let". Sets eap->cmdidx to the command while returning
3128 * "eap->cmd".
3129 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003130 * Returns NULL for an ambiguous user command.
3131 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003132 char_u *
3133find_ex_command(
3134 exarg_T *eap,
3135 int *full UNUSED,
3136 int (*lookup)(char_u *, size_t, cctx_T *) UNUSED,
3137 cctx_T *cctx UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003138{
3139 int len;
3140 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003141 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003143#ifdef FEAT_EVAL
3144 /*
3145 * Recognize a Vim9 script function/method call and assignment:
3146 * "lvar = value", "lvar(arg)", "[1, 2 3]->Func()"
3147 */
3148 if (lookup != NULL && (p = to_name_const_end(eap->cmd)) > eap->cmd
3149 && *p != NUL)
3150 {
3151 int oplen;
3152 int heredoc;
3153
3154 // "funcname(" is always a function call.
3155 // "varname[]" is an expression.
3156 // "g:varname" is an expression.
3157 // "varname->expr" is an expression.
3158 if (*p == '('
3159 || *p == '['
3160 || p[1] == ':'
3161 || (*p == '-' && p[1] == '>'))
3162 {
3163 eap->cmdidx = CMD_eval;
3164 return eap->cmd;
3165 }
3166
3167 oplen = assignment_len(skipwhite(p), &heredoc);
3168 if (oplen > 0)
3169 {
3170 // Recognize an assignment if we recognize the variable name:
3171 // "g:var = expr"
3172 // "var = expr" where "var" is a local var name.
3173 if (((p - eap->cmd) > 2 && eap->cmd[1] == ':')
3174 || lookup(eap->cmd, p - eap->cmd, cctx) >= 0)
3175 {
3176 eap->cmdidx = CMD_let;
3177 return eap->cmd;
3178 }
3179 }
3180 }
3181#endif
3182
Bram Moolenaar071d4272004-06-13 20:20:40 +00003183 /*
3184 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00003185 * Exceptions:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003186 * - the 'k' command can directly be followed by any character.
3187 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003188 * but :sre[wind] is another command, as are :scr[iptnames],
Bram Moolenaar071d4272004-06-13 20:20:40 +00003189 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00003190 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191 */
3192 p = eap->cmd;
3193 if (*p == 'k')
3194 {
3195 eap->cmdidx = CMD_k;
3196 ++p;
3197 }
3198 else if (p[0] == 's'
Bram Moolenaar204b93f2015-08-04 22:02:51 +02003199 && ((p[1] == 'c' && (p[2] == NUL || (p[2] != 's' && p[2] != 'r'
3200 && (p[3] == NUL || (p[3] != 'i' && p[4] != 'p')))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201 || p[1] == 'g'
3202 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
3203 || p[1] == 'I'
3204 || (p[1] == 'r' && p[2] != 'e')))
3205 {
3206 eap->cmdidx = CMD_substitute;
3207 ++p;
3208 }
3209 else
3210 {
3211 while (ASCII_ISALPHA(*p))
3212 ++p;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003213 // for python 3.x support ":py3", ":python3", ":py3file", etc.
Bram Moolenaar55d5c032010-07-17 23:52:29 +02003214 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003215 {
Bram Moolenaarb6590522010-07-21 16:00:43 +02003216 while (ASCII_ISALNUM(*p))
3217 ++p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003218 }
3219 else if (*p == '9' && STRNCMP("vim9", eap->cmd, 4) == 0)
3220 {
3221 // include "9" for "vim9script"
3222 ++p;
3223 while (ASCII_ISALPHA(*p))
3224 ++p;
3225 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02003226
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003227 // check for non-alpha command
Bram Moolenaar071d4272004-06-13 20:20:40 +00003228 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3229 ++p;
3230 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003231 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
3232 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003233 // Check for ":dl", ":dell", etc. to ":deletel": that's
3234 // :delete with the 'l' flag. Same for 'p'.
Bram Moolenaardf177f62005-02-22 08:39:57 +00003235 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003236 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00003237 break;
3238 if (i == len - 1)
3239 {
3240 --len;
3241 if (p[-1] == 'l')
3242 eap->flags |= EXFLAG_LIST;
3243 else
3244 eap->flags |= EXFLAG_PRINT;
3245 }
3246 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003247
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003248 if (ASCII_ISLOWER(eap->cmd[0]))
3249 {
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003250 int c1 = eap->cmd[0];
Bram Moolenaar94f82cb2019-07-24 22:30:27 +02003251 int c2 = len == 1 ? NUL : eap->cmd[1];
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003252
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003253 if (command_count != (int)CMD_SIZE)
3254 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003255 iemsg(_("E943: Command table needs to be updated, run 'make cmdidxs'"));
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003256 getout(1);
3257 }
3258
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003259 // Use a precomputed index for fast look-up in cmdnames[]
3260 // taking into account the first 2 letters of eap->cmd.
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003261 eap->cmdidx = cmdidxs1[CharOrdLow(c1)];
3262 if (ASCII_ISLOWER(c2))
3263 eap->cmdidx += cmdidxs2[CharOrdLow(c1)][CharOrdLow(c2)];
3264 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265 else
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003266 eap->cmdidx = CMD_bang;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003267
3268 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
3269 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
3270 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
3271 (size_t)len) == 0)
3272 {
3273#ifdef FEAT_EVAL
3274 if (full != NULL
3275 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
3276 *full = TRUE;
3277#endif
3278 break;
3279 }
3280
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003281 // Look for a user defined command as a last resort. Let ":Print" be
3282 // overruled by a user defined command.
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01003283 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
3284 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003285 {
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003286 // User defined commands may contain digits.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003287 while (ASCII_ISALNUM(*p))
3288 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003289 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003290 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003291 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292 eap->cmdidx = CMD_SIZE;
3293 }
3294
3295 return p;
3296}
3297
3298#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003299static struct cmdmod
3300{
3301 char *name;
3302 int minlen;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003303 int has_count; // :123verbose :3tab
Bram Moolenaared53fb92007-11-24 20:50:24 +00003304} cmdmods[] = {
3305 {"aboveleft", 3, FALSE},
3306 {"belowright", 3, FALSE},
3307 {"botright", 2, FALSE},
3308 {"browse", 3, FALSE},
3309 {"confirm", 4, FALSE},
Bram Moolenaar7b668e82016-08-23 23:51:21 +02003310 {"filter", 4, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003311 {"hide", 3, FALSE},
3312 {"keepalt", 5, FALSE},
3313 {"keepjumps", 5, FALSE},
3314 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01003315 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003316 {"leftabove", 5, FALSE},
3317 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003318 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003319 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003320 {"rightbelow", 6, FALSE},
3321 {"sandbox", 3, FALSE},
3322 {"silent", 3, FALSE},
3323 {"tab", 3, TRUE},
3324 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003325 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003326 {"verbose", 4, TRUE},
3327 {"vertical", 4, FALSE},
3328};
3329
3330/*
3331 * Return length of a command modifier (including optional count).
3332 * Return zero when it's not a modifier.
3333 */
3334 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003335modifier_len(char_u *cmd)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003336{
3337 int i, j;
3338 char_u *p = cmd;
3339
3340 if (VIM_ISDIGIT(*cmd))
3341 p = skipwhite(skipdigits(cmd));
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003342 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003343 {
3344 for (j = 0; p[j] != NUL; ++j)
3345 if (p[j] != cmdmods[i].name[j])
3346 break;
Bram Moolenaar2d473ab2013-06-12 17:12:24 +02003347 if (!ASCII_ISALPHA(p[j]) && j >= cmdmods[i].minlen
Bram Moolenaared53fb92007-11-24 20:50:24 +00003348 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003349 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003350 }
3351 return 0;
3352}
3353
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354/*
3355 * Return > 0 if an Ex command "name" exists.
3356 * Return 2 if there is an exact match.
3357 * Return 3 if there is an ambiguous match.
3358 */
3359 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003360cmd_exists(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003361{
3362 exarg_T ea;
3363 int full = FALSE;
3364 int i;
3365 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003366 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003367
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003368 // Check command modifiers.
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003369 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003370 {
3371 for (j = 0; name[j] != NUL; ++j)
3372 if (name[j] != cmdmods[i].name[j])
3373 break;
3374 if (name[j] == NUL && j >= cmdmods[i].minlen)
3375 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3376 }
3377
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003378 // Check built-in commands and user defined commands.
3379 // For ":2match" and ":3match" we need to skip the number.
Bram Moolenaara9587612006-05-04 21:47:50 +00003380 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003382 p = find_ex_command(&ea, &full, NULL, NULL);
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003383 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003385 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3386 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003387 if (*skipwhite(p) != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003388 return 0; // trailing garbage
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3390}
3391#endif
3392
Bram Moolenaard0190392019-08-23 21:17:35 +02003393 cmdidx_T
3394excmd_get_cmdidx(char_u *cmd, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003395{
Bram Moolenaard0190392019-08-23 21:17:35 +02003396 cmdidx_T idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397
Bram Moolenaard0190392019-08-23 21:17:35 +02003398 for (idx = (cmdidx_T)0; (int)idx < (int)CMD_SIZE;
3399 idx = (cmdidx_T)((int)idx + 1))
3400 if (STRNCMP(cmdnames[(int)idx].cmd_name, cmd, (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401 break;
3402
Bram Moolenaard0190392019-08-23 21:17:35 +02003403 return idx;
3404}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405
Bram Moolenaard0190392019-08-23 21:17:35 +02003406 long
3407excmd_get_argt(cmdidx_T idx)
3408{
3409 return (long)cmdnames[(int)idx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410}
3411
3412/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003413 * Skip a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414 *
3415 * Backslashed delimiters after / or ? will be skipped, and commands will
3416 * not be expanded between /'s and ?'s or after "'".
3417 *
Bram Moolenaardf177f62005-02-22 08:39:57 +00003418 * Also skip white space and ":" characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003419 * Returns the "cmd" pointer advanced to beyond the range.
3420 */
3421 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003422skip_range(
3423 char_u *cmd,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003424 int *ctx) // pointer to xp_context or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003425{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003426 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01003428 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;\\", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429 {
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01003430 if (*cmd == '\\')
3431 {
3432 if (cmd[1] == '?' || cmd[1] == '/' || cmd[1] == '&')
3433 ++cmd;
3434 else
3435 break;
3436 }
3437 else if (*cmd == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438 {
3439 if (*++cmd == NUL && ctx != NULL)
3440 *ctx = EXPAND_NOTHING;
3441 }
3442 else if (*cmd == '/' || *cmd == '?')
3443 {
3444 delim = *cmd++;
3445 while (*cmd != NUL && *cmd != delim)
3446 if (*cmd++ == '\\' && *cmd != NUL)
3447 ++cmd;
3448 if (*cmd == NUL && ctx != NULL)
3449 *ctx = EXPAND_NOTHING;
3450 }
3451 if (*cmd != NUL)
3452 ++cmd;
3453 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00003454
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003455 // Skip ":" and white space.
Bram Moolenaardf177f62005-02-22 08:39:57 +00003456 while (*cmd == ':')
3457 cmd = skipwhite(cmd + 1);
3458
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459 return cmd;
3460}
3461
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003462 static void
3463addr_error(cmd_addr_T addr_type)
3464{
3465 if (addr_type == ADDR_NONE)
3466 emsg(_(e_norange));
3467 else
3468 emsg(_(e_invrange));
3469}
3470
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471/*
Bram Moolenaar198cb662018-09-06 21:44:17 +02003472 * Get a single EX address.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473 *
3474 * Set ptr to the next character after the part that was interpreted.
3475 * Set ptr to NULL when an error is encountered.
Bram Moolenaar198cb662018-09-06 21:44:17 +02003476 * This may set the last used search pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003477 *
3478 * Return MAXLNUM when no Ex address was found.
3479 */
3480 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003481get_address(
3482 exarg_T *eap UNUSED,
3483 char_u **ptr,
Bram Moolenaar1b03a192019-12-08 17:08:29 +01003484 cmd_addr_T addr_type,
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003485 int skip, // only skip the address, don't use it
3486 int silent, // no errors or side effects
3487 int to_other_file, // flag: may jump to other file
3488 int address_count UNUSED) // 1 for first address, >1 after comma
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489{
3490 int c;
3491 int i;
3492 long n;
3493 char_u *cmd;
3494 pos_T pos;
3495 pos_T *fp;
3496 linenr_T lnum;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003497 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498
3499 cmd = skipwhite(*ptr);
3500 lnum = MAXLNUM;
3501 do
3502 {
3503 switch (*cmd)
3504 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003505 case '.': // '.' - Cursor position
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003506 ++cmd;
3507 switch (addr_type)
3508 {
3509 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003510 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511 lnum = curwin->w_cursor.lnum;
3512 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003513 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003514 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003515 break;
3516 case ADDR_ARGUMENTS:
3517 lnum = curwin->w_arg_idx + 1;
3518 break;
3519 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003520 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003521 lnum = curbuf->b_fnum;
3522 break;
3523 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003524 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003525 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003526 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003527 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003528 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003529 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003530 cmd = NULL;
3531 goto error;
3532 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003533 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003534#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02003535 lnum = qf_get_cur_idx(eap);
3536#endif
3537 break;
3538 case ADDR_QUICKFIX_VALID:
3539#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003540 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02003541#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003542 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003543 }
3544 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003546 case '$': // '$' - last line
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003547 ++cmd;
3548 switch (addr_type)
3549 {
3550 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003551 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003552 lnum = curbuf->b_ml.ml_line_count;
3553 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003554 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003555 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003556 break;
3557 case ADDR_ARGUMENTS:
3558 lnum = ARGCOUNT;
3559 break;
3560 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003561 buf = lastbuf;
3562 while (buf->b_ml.ml_mfp == NULL)
3563 {
3564 if (buf->b_prev == NULL)
3565 break;
3566 buf = buf->b_prev;
3567 }
3568 lnum = buf->b_fnum;
3569 break;
3570 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003571 lnum = lastbuf->b_fnum;
3572 break;
3573 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003574 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003575 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003576 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003577 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003578 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003579 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003580 cmd = NULL;
3581 goto error;
3582 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003583 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003584#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003585 lnum = qf_get_size(eap);
3586 if (lnum == 0)
3587 lnum = 1;
Bram Moolenaare906c502015-09-09 21:10:39 +02003588#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003589 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003590 case ADDR_QUICKFIX_VALID:
3591#ifdef FEAT_QUICKFIX
3592 lnum = qf_get_valid_size(eap);
3593 if (lnum == 0)
3594 lnum = 1;
3595#endif
3596 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003597 }
3598 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003600 case '\'': // ''' - mark
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003601 if (*++cmd == NUL)
3602 {
3603 cmd = NULL;
3604 goto error;
3605 }
3606 if (addr_type != ADDR_LINES)
3607 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003608 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003609 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003610 goto error;
3611 }
3612 if (skip)
3613 ++cmd;
3614 else
3615 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003616 // Only accept a mark in another file when it is
3617 // used by itself: ":'M".
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003618 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
3619 ++cmd;
3620 if (fp == (pos_T *)-1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003621 // Jumped to another file.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003622 lnum = curwin->w_cursor.lnum;
3623 else
3624 {
3625 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626 {
3627 cmd = NULL;
3628 goto error;
3629 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003630 lnum = fp->lnum;
3631 }
3632 }
3633 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003634
3635 case '/':
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003636 case '?': // '/' or '?' - search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003637 c = *cmd++;
3638 if (addr_type != ADDR_LINES)
3639 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003640 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003641 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003642 goto error;
3643 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003644 if (skip) // skip "/pat/"
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003645 {
3646 cmd = skip_regexp(cmd, c, (int)p_magic, NULL);
3647 if (*cmd == c)
3648 ++cmd;
3649 }
3650 else
3651 {
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003652 int flags;
3653
3654 pos = curwin->w_cursor; // save curwin->w_cursor
3655
3656 // When '/' or '?' follows another address, start from
3657 // there.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003658 if (lnum != MAXLNUM)
3659 curwin->w_cursor.lnum = lnum;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003660
3661 // Start a forward search at the end of the line (unless
3662 // before the first line).
3663 // Start a backward search at the start of the line.
3664 // This makes sure we never match in the current
3665 // line, and can match anywhere in the
3666 // next/previous line.
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01003667 if (c == '/' && curwin->w_cursor.lnum > 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003668 curwin->w_cursor.col = MAXCOL;
3669 else
3670 curwin->w_cursor.col = 0;
3671 searchcmdlen = 0;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003672 flags = silent ? 0 : SEARCH_HIS | SEARCH_MSG;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02003673 if (!do_search(NULL, c, cmd, 1L, flags, NULL))
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003674 {
3675 curwin->w_cursor = pos;
3676 cmd = NULL;
3677 goto error;
3678 }
3679 lnum = curwin->w_cursor.lnum;
3680 curwin->w_cursor = pos;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003681 // adjust command string pointer
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003682 cmd += searchcmdlen;
3683 }
3684 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003685
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003686 case '\\': // "\?", "\/" or "\&", repeat search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003687 ++cmd;
3688 if (addr_type != ADDR_LINES)
3689 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003690 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003691 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003692 goto error;
3693 }
3694 if (*cmd == '&')
3695 i = RE_SUBST;
3696 else if (*cmd == '?' || *cmd == '/')
3697 i = RE_SEARCH;
3698 else
3699 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003700 emsg(_(e_backslash));
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003701 cmd = NULL;
3702 goto error;
3703 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003704
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003705 if (!skip)
3706 {
3707 /*
3708 * When search follows another address, start from
3709 * there.
3710 */
3711 if (lnum != MAXLNUM)
3712 pos.lnum = lnum;
3713 else
3714 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003715
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003716 /*
3717 * Start the search just like for the above
3718 * do_search().
3719 */
3720 if (*cmd != '?')
3721 pos.col = MAXCOL;
3722 else
3723 pos.col = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02003724 pos.coladd = 0;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01003725 if (searchit(curwin, curbuf, &pos, NULL,
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003726 *cmd == '?' ? BACKWARD : FORWARD,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02003727 (char_u *)"", 1L, SEARCH_MSG, i, NULL) != FAIL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003728 lnum = pos.lnum;
3729 else
3730 {
3731 cmd = NULL;
3732 goto error;
3733 }
3734 }
3735 ++cmd;
3736 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737
3738 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003739 if (VIM_ISDIGIT(*cmd)) // absolute line number
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003740 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003741 }
3742
3743 for (;;)
3744 {
3745 cmd = skipwhite(cmd);
3746 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
3747 break;
3748
3749 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003750 {
3751 switch (addr_type)
3752 {
3753 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003754 case ADDR_OTHER:
3755 // "+1" is same as ".+1"
Bram Moolenaarf240e182014-11-27 18:33:02 +01003756 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003757 break;
3758 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003759 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003760 break;
3761 case ADDR_ARGUMENTS:
3762 lnum = curwin->w_arg_idx + 1;
3763 break;
3764 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003765 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003766 lnum = curbuf->b_fnum;
3767 break;
3768 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003769 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003770 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003771 case ADDR_TABS_RELATIVE:
3772 lnum = 1;
3773 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003774 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003775#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02003776 lnum = qf_get_cur_idx(eap);
3777#endif
3778 break;
3779 case ADDR_QUICKFIX_VALID:
3780#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003781 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02003782#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003783 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003784 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003785 case ADDR_UNSIGNED:
3786 lnum = 0;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003787 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003788 }
3789 }
3790
Bram Moolenaar071d4272004-06-13 20:20:40 +00003791 if (VIM_ISDIGIT(*cmd))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003792 i = '+'; // "number" is same as "+number"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003793 else
3794 i = *cmd++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003795 if (!VIM_ISDIGIT(*cmd)) // '+' is '+1', but '+0' is not '+1'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796 n = 1;
3797 else
3798 n = getdigits(&cmd);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003799
3800 if (addr_type == ADDR_TABS_RELATIVE)
3801 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003802 emsg(_(e_invrange));
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003803 cmd = NULL;
3804 goto error;
3805 }
3806 else if (addr_type == ADDR_LOADED_BUFFERS
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003807 || addr_type == ADDR_BUFFERS)
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003808 lnum = compute_buffer_local_count(
3809 addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810 else
Bram Moolenaarded27822017-01-02 14:27:34 +01003811 {
3812#ifdef FEAT_FOLDING
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003813 // Relative line addressing, need to adjust for folded lines
3814 // now, but only do it after the first address.
Bram Moolenaarded27822017-01-02 14:27:34 +01003815 if (addr_type == ADDR_LINES && (i == '-' || i == '+')
3816 && address_count >= 2)
3817 (void)hasFolding(lnum, NULL, &lnum);
3818#endif
3819 if (i == '-')
3820 lnum -= n;
3821 else
3822 lnum += n;
3823 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003824 }
3825 } while (*cmd == '/' || *cmd == '?');
3826
3827error:
3828 *ptr = cmd;
3829 return lnum;
3830}
3831
3832/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00003833 * Get flags from an Ex command argument.
3834 */
3835 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003836get_flags(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00003837{
3838 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
3839 {
3840 if (*eap->arg == 'l')
3841 eap->flags |= EXFLAG_LIST;
3842 else if (*eap->arg == 'p')
3843 eap->flags |= EXFLAG_PRINT;
3844 else
3845 eap->flags |= EXFLAG_NR;
3846 eap->arg = skipwhite(eap->arg + 1);
3847 }
3848}
3849
3850/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851 * Function called for command which is Not Implemented. NI!
3852 */
3853 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003854ex_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003855{
3856 if (!eap->skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003857 eap->errmsg = N_("E319: Sorry, the command is not available in this version");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003858}
3859
Bram Moolenaar7bb75552007-07-16 18:39:49 +00003860#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861/*
3862 * Function called for script command which is Not Implemented. NI!
3863 * Skips over ":perl <<EOF" constructs.
3864 */
3865 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003866ex_script_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867{
3868 if (!eap->skip)
3869 ex_ni(eap);
3870 else
3871 vim_free(script_get(eap, eap->arg));
3872}
3873#endif
3874
3875/*
3876 * Check range in Ex command for validity.
3877 * Return NULL when valid, error message when invalid.
3878 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003879 static char *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003880invalid_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003881{
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003882 buf_T *buf;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003883
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884 if ( eap->line1 < 0
3885 || eap->line2 < 0
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003886 || eap->line1 > eap->line2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003887 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003888
Bram Moolenaar8071cb22019-07-12 17:58:01 +02003889 if (eap->argt & EX_RANGE)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003890 {
Bram Moolenaarb7316892019-05-01 18:08:42 +02003891 switch (eap->addr_type)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003892 {
3893 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003894 if (eap->line2 > curbuf->b_ml.ml_line_count
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003895#ifdef FEAT_DIFF
3896 + (eap->cmdidx == CMD_diffget)
3897#endif
3898 )
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003899 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003900 break;
3901 case ADDR_ARGUMENTS:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003902 // add 1 if ARGCOUNT is 0
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003903 if (eap->line2 > ARGCOUNT + (!ARGCOUNT))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003904 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003905 break;
3906 case ADDR_BUFFERS:
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02003907 // Only a boundary check, not whether the buffers actually
3908 // exist.
3909 if (eap->line1 < 1 || eap->line2 > get_highest_fnum())
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003910 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003911 break;
3912 case ADDR_LOADED_BUFFERS:
3913 buf = firstbuf;
3914 while (buf->b_ml.ml_mfp == NULL)
3915 {
3916 if (buf->b_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003917 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003918 buf = buf->b_next;
3919 }
3920 if (eap->line1 < buf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003921 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003922 buf = lastbuf;
3923 while (buf->b_ml.ml_mfp == NULL)
3924 {
3925 if (buf->b_prev == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003926 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003927 buf = buf->b_prev;
3928 }
3929 if (eap->line2 > buf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003930 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003931 break;
3932 case ADDR_WINDOWS:
Bram Moolenaar8be63882015-01-14 11:25:05 +01003933 if (eap->line2 > LAST_WIN_NR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003934 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003935 break;
3936 case ADDR_TABS:
3937 if (eap->line2 > LAST_TAB_NR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003938 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003939 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003940 case ADDR_TABS_RELATIVE:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003941 case ADDR_OTHER:
3942 // Any range is OK.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003943 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003944 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003945#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02003946 // No error for value that is too big, will use the last entry.
3947 if (eap->line2 <= 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003948 return _(e_invrange);
Bram Moolenaare906c502015-09-09 21:10:39 +02003949#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003950 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003951 case ADDR_QUICKFIX_VALID:
3952#ifdef FEAT_QUICKFIX
3953 if ((eap->line2 != 1 && eap->line2 > qf_get_valid_size(eap))
3954 || eap->line2 < 0)
3955 return _(e_invrange);
3956#endif
3957 break;
3958 case ADDR_UNSIGNED:
3959 if (eap->line2 < 0)
3960 return _(e_invrange);
3961 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003962 case ADDR_NONE:
3963 // Will give an error elsewhere.
3964 break;
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003965 }
3966 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003967 return NULL;
3968}
3969
3970/*
3971 * Correct the range for zero line number, if required.
3972 */
3973 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003974correct_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003975{
Bram Moolenaar8071cb22019-07-12 17:58:01 +02003976 if (!(eap->argt & EX_ZEROR)) // zero in range not allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977 {
3978 if (eap->line1 == 0)
3979 eap->line1 = 1;
3980 if (eap->line2 == 0)
3981 eap->line2 = 1;
3982 }
3983}
3984
Bram Moolenaar748bf032005-02-02 23:04:36 +00003985#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00003986/*
3987 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
3988 * pattern. Otherwise return eap->arg.
3989 */
3990 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003991skip_grep_pat(exarg_T *eap)
Bram Moolenaar748bf032005-02-02 23:04:36 +00003992{
3993 char_u *p = eap->arg;
3994
Bram Moolenaara37420f2006-02-04 22:37:47 +00003995 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
3996 || eap->cmdidx == CMD_vimgrepadd
3997 || eap->cmdidx == CMD_lvimgrepadd
3998 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00003999 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004000 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004001 if (p == NULL)
4002 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004003 }
4004 return p;
4005}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004006
4007/*
4008 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4009 * in the command line, so that things like % get expanded.
4010 */
4011 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004012replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004013{
4014 char_u *new_cmdline;
4015 char_u *program;
4016 char_u *pos;
4017 char_u *ptr;
4018 int len;
4019 int i;
4020
4021 /*
4022 * Don't do it when ":vimgrep" is used for ":grep".
4023 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004024 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4025 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4026 || eap->cmdidx == CMD_grepadd
4027 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004028 && !grep_internal(eap->cmdidx))
4029 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004030 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4031 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004032 {
4033 if (*curbuf->b_p_gp == NUL)
4034 program = p_gp;
4035 else
4036 program = curbuf->b_p_gp;
4037 }
4038 else
4039 {
4040 if (*curbuf->b_p_mp == NUL)
4041 program = p_mp;
4042 else
4043 program = curbuf->b_p_mp;
4044 }
4045
4046 p = skipwhite(p);
4047
4048 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4049 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004050 // replace $* by given arguments
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004051 i = 1;
4052 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4053 ++i;
4054 len = (int)STRLEN(p);
Bram Moolenaar51e14382019-05-25 20:21:28 +02004055 new_cmdline = alloc(STRLEN(program) + i * (len - 2) + 1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004056 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004057 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004058 ptr = new_cmdline;
4059 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4060 {
4061 i = (int)(pos - program);
4062 STRNCPY(ptr, program, i);
4063 STRCPY(ptr += i, p);
4064 ptr += len;
4065 program = pos + 2;
4066 }
4067 STRCPY(ptr, program);
4068 }
4069 else
4070 {
Bram Moolenaar51e14382019-05-25 20:21:28 +02004071 new_cmdline = alloc(STRLEN(program) + STRLEN(p) + 2);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004072 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004073 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004074 STRCPY(new_cmdline, program);
4075 STRCAT(new_cmdline, " ");
4076 STRCAT(new_cmdline, p);
4077 }
4078 msg_make(p);
4079
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004080 // 'eap->cmd' is not set here, because it is not used at CMD_make
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004081 vim_free(*cmdlinep);
4082 *cmdlinep = new_cmdline;
4083 p = new_cmdline;
4084 }
4085 return p;
4086}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004087#endif
4088
Bram Moolenaar071d4272004-06-13 20:20:40 +00004089/*
4090 * Expand file name in Ex command argument.
Bram Moolenaar0abb4272019-06-15 16:06:00 +02004091 * When an error is detected, "errormsgp" is set to a non-NULL pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092 * Return FAIL for failure, OK otherwise.
4093 */
4094 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004095expand_filename(
4096 exarg_T *eap,
4097 char_u **cmdlinep,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004098 char **errormsgp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004100 int has_wildcards; // need to expand wildcards
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101 char_u *repl;
4102 int srclen;
4103 char_u *p;
4104 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004105 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106
Bram Moolenaar748bf032005-02-02 23:04:36 +00004107#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004108 // Skip a regexp pattern for ":vimgrep[add] pat file..."
Bram Moolenaar748bf032005-02-02 23:04:36 +00004109 p = skip_grep_pat(eap);
4110#else
4111 p = eap->arg;
4112#endif
4113
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114 /*
4115 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4116 * the file name contains a wildcard it should not cause expanding.
4117 * (it will be expanded anyway if there is a wildcard before replacing).
4118 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004119 has_wildcards = mch_has_wildcard(p);
4120 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004122#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004123 // Skip over `=expr`, wildcards in it are not expanded.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004124 if (p[0] == '`' && p[1] == '=')
4125 {
4126 p += 2;
4127 (void)skip_expr(&p);
4128 if (*p == '`')
4129 ++p;
4130 continue;
4131 }
4132#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004133 /*
4134 * Quick check if this cannot be the start of a special string.
4135 * Also removes backslash before '%', '#' and '<'.
4136 */
4137 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4138 {
4139 ++p;
4140 continue;
4141 }
4142
4143 /*
4144 * Try to find a match at this position.
4145 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00004146 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
4147 errormsgp, &escaped);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004148 if (*errormsgp != NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00004149 return FAIL;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004150 if (repl == NULL) // no match found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004151 {
4152 p += srclen;
4153 continue;
4154 }
4155
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004156 // Wildcards won't be expanded below, the replacement is taken
4157 // literally. But do expand "~/file", "~user/file" and "$HOME/file".
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004158 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4159 {
4160 char_u *l = repl;
4161
4162 repl = expand_env_save(repl);
4163 vim_free(l);
4164 }
4165
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004166 // Need to escape white space et al. with a backslash.
4167 // Don't do this for:
4168 // - replacement that already has been escaped: "##"
4169 // - shell commands (may have to use quotes instead).
4170 // - non-unix systems when there is a single argument (spaces don't
4171 // separate arguments then).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00004173 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174 && eap->cmdidx != CMD_bang
Bram Moolenaar071d4272004-06-13 20:20:40 +00004175 && eap->cmdidx != CMD_grep
4176 && eap->cmdidx != CMD_grepadd
Bram Moolenaarbf15b8d2017-06-04 20:43:48 +02004177 && eap->cmdidx != CMD_hardcopy
Bram Moolenaar67883b42017-07-27 22:57:00 +02004178 && eap->cmdidx != CMD_lgrep
4179 && eap->cmdidx != CMD_lgrepadd
4180 && eap->cmdidx != CMD_lmake
4181 && eap->cmdidx != CMD_make
4182 && eap->cmdidx != CMD_terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00004183#ifndef UNIX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004184 && !(eap->argt & EX_NOSPC)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004185#endif
4186 )
4187 {
4188 char_u *l;
4189#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004190 // Don't escape a backslash here, because rem_backslash() doesn't
4191 // remove it later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192 static char_u *nobslash = (char_u *)" \t\"|";
4193# define ESCAPE_CHARS nobslash
4194#else
4195# define ESCAPE_CHARS escape_chars
4196#endif
4197
4198 for (l = repl; *l; ++l)
4199 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
4200 {
4201 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
4202 if (l != NULL)
4203 {
4204 vim_free(repl);
4205 repl = l;
4206 }
4207 break;
4208 }
4209 }
4210
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004211 // For a shell command a '!' must be escaped.
Bram Moolenaar67883b42017-07-27 22:57:00 +02004212 if ((eap->usefilter || eap->cmdidx == CMD_bang
4213 || eap->cmdidx == CMD_terminal)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004214 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004215 {
4216 char_u *l;
4217
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004218 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219 if (l != NULL)
4220 {
4221 vim_free(repl);
4222 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223 }
4224 }
4225
4226 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
4227 vim_free(repl);
4228 if (p == NULL)
4229 return FAIL;
4230 }
4231
4232 /*
4233 * One file argument: Expand wildcards.
4234 * Don't do this with ":r !command" or ":w !command".
4235 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004236 if ((eap->argt & EX_NOSPC) && !eap->usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004237 {
4238 /*
4239 * May do this twice:
4240 * 1. Replace environment variables.
4241 * 2. Replace any other wildcards, remove backslashes.
4242 */
4243 for (n = 1; n <= 2; ++n)
4244 {
4245 if (n == 2)
4246 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004247 /*
4248 * Halve the number of backslashes (this is Vi compatible).
4249 * For Unix and OS/2, when wildcards are expanded, this is
4250 * done by ExpandOne() below.
4251 */
Bram Moolenaare7fedb62015-12-31 19:07:19 +01004252#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253 if (!has_wildcards)
4254#endif
4255 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256 }
4257
4258 if (has_wildcards)
4259 {
4260 if (n == 1)
4261 {
4262 /*
4263 * First loop: May expand environment variables. This
4264 * can be done much faster with expand_env() than with
4265 * something else (e.g., calling a shell).
4266 * After expanding environment variables, check again
4267 * if there are still wildcards present.
4268 */
4269 if (vim_strchr(eap->arg, '$') != NULL
4270 || vim_strchr(eap->arg, '~') != NULL)
4271 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00004272 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004273 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274 has_wildcards = mch_has_wildcard(NameBuff);
4275 p = NameBuff;
4276 }
4277 else
4278 p = NULL;
4279 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004280 else // n == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281 {
4282 expand_T xpc;
Bram Moolenaarb40c2572019-10-19 21:01:05 +02004283 int options = WILD_LIST_NOTFOUND
4284 | WILD_NOERROR | WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004285
4286 ExpandInit(&xpc);
4287 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004288 if (p_wic)
4289 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004290 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01004291 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004292 if (p == NULL)
4293 return FAIL;
4294 }
4295 if (p != NULL)
4296 {
4297 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
4298 p, cmdlinep);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004299 if (n == 2) // p came from ExpandOne()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004300 vim_free(p);
4301 }
4302 }
4303 }
4304 }
4305 return OK;
4306}
4307
4308/*
4309 * Replace part of the command line, keeping eap->cmd, eap->arg and
4310 * eap->nextcmd correct.
4311 * "src" points to the part that is to be replaced, of length "srclen".
4312 * "repl" is the replacement string.
4313 * Returns a pointer to the character after the replaced string.
4314 * Returns NULL for failure.
4315 */
4316 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004317repl_cmdline(
4318 exarg_T *eap,
4319 char_u *src,
4320 int srclen,
4321 char_u *repl,
4322 char_u **cmdlinep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004323{
4324 int len;
4325 int i;
4326 char_u *new_cmdline;
4327
4328 /*
4329 * The new command line is build in new_cmdline[].
4330 * First allocate it.
4331 * Careful: a "+cmd" argument may have been NUL terminated.
4332 */
4333 len = (int)STRLEN(repl);
4334 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004335 if (eap->nextcmd != NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004336 i += (int)STRLEN(eap->nextcmd);// add space for next command
Bram Moolenaar964b3742019-05-24 18:54:09 +02004337 if ((new_cmdline = alloc(i)) == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004338 return NULL; // out of memory!
Bram Moolenaar071d4272004-06-13 20:20:40 +00004339
4340 /*
4341 * Copy the stuff before the expanded part.
4342 * Copy the expanded stuff.
4343 * Copy what came after the expanded part.
4344 * Copy the next commands, if there are any.
4345 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004346 i = (int)(src - *cmdlinep); // length of part before match
Bram Moolenaar071d4272004-06-13 20:20:40 +00004347 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00004348
Bram Moolenaar071d4272004-06-13 20:20:40 +00004349 mch_memmove(new_cmdline + i, repl, (size_t)len);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004350 i += len; // remember the end of the string
Bram Moolenaar071d4272004-06-13 20:20:40 +00004351 STRCPY(new_cmdline + i, src + srclen);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004352 src = new_cmdline + i; // remember where to continue
Bram Moolenaar071d4272004-06-13 20:20:40 +00004353
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004354 if (eap->nextcmd != NULL) // append next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00004355 {
4356 i = (int)STRLEN(new_cmdline) + 1;
4357 STRCPY(new_cmdline + i, eap->nextcmd);
4358 eap->nextcmd = new_cmdline + i;
4359 }
4360 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
4361 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
4362 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
4363 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
4364 vim_free(*cmdlinep);
4365 *cmdlinep = new_cmdline;
4366
4367 return src;
4368}
4369
4370/*
4371 * Check for '|' to separate commands and '"' to start comments.
4372 */
4373 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004374separate_nextcmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004375{
4376 char_u *p;
4377
Bram Moolenaar86b68352004-12-27 21:59:20 +00004378#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004379 p = skip_grep_pat(eap);
4380#else
4381 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004382#endif
4383
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004384 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385 {
4386 if (*p == Ctrl_V)
4387 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004388 if (eap->argt & (EX_CTRLV | EX_XFILE))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004389 ++p; // skip CTRL-V and next char
Bram Moolenaar071d4272004-06-13 20:20:40 +00004390 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004391 // remove CTRL-V and skip next char
Bram Moolenaara7241f52008-06-24 20:39:31 +00004392 STRMOVE(p, p + 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004393 if (*p == NUL) // stop at NUL after CTRL-V
Bram Moolenaar071d4272004-06-13 20:20:40 +00004394 break;
4395 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004396
4397#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004398 // Skip over `=expr` when wildcards are expanded.
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004399 else if (p[0] == '`' && p[1] == '=' && (eap->argt & EX_XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004400 {
4401 p += 2;
4402 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004403 }
4404#endif
4405
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004406 // Check for '"': start of comment or '|': next command
4407 // :@" and :*" do not start a comment!
4408 // :redir @" doesn't either.
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004409 else if ((*p == '"' && !(eap->argt & EX_NOTRLCOM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004410 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
4411 || p != eap->arg)
4412 && (eap->cmdidx != CMD_redir
4413 || p != eap->arg + 1 || p[-1] != '@'))
4414 || *p == '|' || *p == '\n')
4415 {
4416 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004417 * We remove the '\' before the '|', unless EX_CTRLV is used
Bram Moolenaar071d4272004-06-13 20:20:40 +00004418 * AND 'b' is present in 'cpoptions'.
4419 */
4420 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004421 || !(eap->argt & EX_CTRLV)) && *(p - 1) == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004422 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004423 STRMOVE(p - 1, p); // remove the '\'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004424 --p;
4425 }
4426 else
4427 {
4428 eap->nextcmd = check_nextcmd(p);
4429 *p = NUL;
4430 break;
4431 }
4432 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004433 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004434
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004435 if (!(eap->argt & EX_NOTRLCOM)) // remove trailing spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00004436 del_trailing_spaces(eap->arg);
4437}
4438
4439/*
4440 * get + command from ex argument
4441 */
4442 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004443getargcmd(char_u **argp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004444{
4445 char_u *arg = *argp;
4446 char_u *command = NULL;
4447
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004448 if (*arg == '+') // +[command]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004449 {
4450 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02004451 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004452 command = dollar_command;
4453 else
4454 {
4455 command = arg;
4456 arg = skip_cmd_arg(command, TRUE);
4457 if (*arg != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004458 *arg++ = NUL; // terminate command with NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004459 }
4460
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004461 arg = skipwhite(arg); // skip over spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462 *argp = arg;
4463 }
4464 return command;
4465}
4466
4467/*
4468 * Find end of "+command" argument. Skip over "\ " and "\\".
4469 */
Bram Moolenaard0190392019-08-23 21:17:35 +02004470 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004471skip_cmd_arg(
4472 char_u *p,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004473 int rembs) // TRUE to halve the number of backslashes
Bram Moolenaar071d4272004-06-13 20:20:40 +00004474{
4475 while (*p && !vim_isspace(*p))
4476 {
4477 if (*p == '\\' && p[1] != NUL)
4478 {
4479 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00004480 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004481 else
4482 ++p;
4483 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004484 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004485 }
4486 return p;
4487}
4488
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004489 int
4490get_bad_opt(char_u *p, exarg_T *eap)
4491{
4492 if (STRICMP(p, "keep") == 0)
4493 eap->bad_char = BAD_KEEP;
4494 else if (STRICMP(p, "drop") == 0)
4495 eap->bad_char = BAD_DROP;
4496 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
4497 eap->bad_char = *p;
Bram Moolenaar75808492018-06-12 12:39:41 +02004498 else
4499 return FAIL;
4500 return OK;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004501}
4502
Bram Moolenaar071d4272004-06-13 20:20:40 +00004503/*
4504 * Get "++opt=arg" argument.
4505 * Return FAIL or OK.
4506 */
4507 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004508getargopt(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004509{
4510 char_u *arg = eap->arg + 2;
4511 int *pp = NULL;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004512 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004513 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004514
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004515 // ":edit ++[no]bin[ary] file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004516 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
4517 {
4518 if (*arg == 'n')
4519 {
4520 arg += 2;
4521 eap->force_bin = FORCE_NOBIN;
4522 }
4523 else
4524 eap->force_bin = FORCE_BIN;
4525 if (!checkforcmd(&arg, "binary", 3))
4526 return FAIL;
4527 eap->arg = skipwhite(arg);
4528 return OK;
4529 }
4530
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004531 // ":read ++edit file"
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004532 if (STRNCMP(arg, "edit", 4) == 0)
4533 {
4534 eap->read_edit = TRUE;
4535 eap->arg = skipwhite(arg + 4);
4536 return OK;
4537 }
4538
Bram Moolenaar071d4272004-06-13 20:20:40 +00004539 if (STRNCMP(arg, "ff", 2) == 0)
4540 {
4541 arg += 2;
4542 pp = &eap->force_ff;
4543 }
4544 else if (STRNCMP(arg, "fileformat", 10) == 0)
4545 {
4546 arg += 10;
4547 pp = &eap->force_ff;
4548 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004549 else if (STRNCMP(arg, "enc", 3) == 0)
4550 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01004551 if (STRNCMP(arg, "encoding", 8) == 0)
4552 arg += 8;
4553 else
4554 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004555 pp = &eap->force_enc;
4556 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004557 else if (STRNCMP(arg, "bad", 3) == 0)
4558 {
4559 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004560 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004561 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004562
4563 if (pp == NULL || *arg != '=')
4564 return FAIL;
4565
4566 ++arg;
4567 *pp = (int)(arg - eap->cmd);
4568 arg = skip_cmd_arg(arg, FALSE);
4569 eap->arg = skipwhite(arg);
4570 *arg = NUL;
4571
Bram Moolenaar071d4272004-06-13 20:20:40 +00004572 if (pp == &eap->force_ff)
4573 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004574 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
4575 return FAIL;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004576 eap->force_ff = eap->cmd[eap->force_ff];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004577 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004578 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004579 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004580 // Make 'fileencoding' lower case.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004581 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
4582 *p = TOLOWER_ASC(*p);
4583 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004584 else
4585 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004586 // Check ++bad= argument. Must be a single-byte character, "keep" or
4587 // "drop".
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004588 if (get_bad_opt(eap->cmd + bad_char_idx, eap) == FAIL)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004589 return FAIL;
4590 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004591
4592 return OK;
4593}
4594
Bram Moolenaar071d4272004-06-13 20:20:40 +00004595 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004596ex_autocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004597{
4598 /*
Bram Moolenaar26d4b892018-07-04 22:26:28 +02004599 * Disallow autocommands from .exrc and .vimrc in current
Bram Moolenaar071d4272004-06-13 20:20:40 +00004600 * directory for security reasons.
4601 */
4602 if (secure)
4603 {
4604 secure = 2;
4605 eap->errmsg = e_curdir;
4606 }
4607 else if (eap->cmdidx == CMD_autocmd)
4608 do_autocmd(eap->arg, eap->forceit);
4609 else
4610 do_augroup(eap->arg, eap->forceit);
4611}
4612
4613/*
4614 * ":doautocmd": Apply the automatic commands to the current buffer.
4615 */
4616 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004617ex_doautocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004618{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01004619 char_u *arg = eap->arg;
4620 int call_do_modelines = check_nomodeline(&arg);
Bram Moolenaar1610d052016-06-09 22:53:01 +02004621 int did_aucmd;
Bram Moolenaar60542ac2012-02-12 20:14:01 +01004622
Bram Moolenaar1610d052016-06-09 22:53:01 +02004623 (void)do_doautocmd(arg, TRUE, &did_aucmd);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004624 // Only when there is no <nomodeline>.
Bram Moolenaar1610d052016-06-09 22:53:01 +02004625 if (call_do_modelines && did_aucmd)
Bram Moolenaar60542ac2012-02-12 20:14:01 +01004626 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004627}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004628
Bram Moolenaar071d4272004-06-13 20:20:40 +00004629/*
4630 * :[N]bunload[!] [N] [bufname] unload buffer
4631 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
4632 * :[N]bwipeout[!] [N] [bufname] delete buffer really
4633 */
4634 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004635ex_bunload(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004636{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004637 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02004638 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004639 eap->errmsg = do_bufdel(
4640 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
4641 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
4642 : DOBUF_UNLOAD, eap->arg,
4643 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
4644}
4645
4646/*
4647 * :[N]buffer [N] to buffer N
4648 * :[N]sbuffer [N] to buffer N
4649 */
4650 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004651ex_buffer(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004652{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004653 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02004654 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004655 if (*eap->arg)
4656 eap->errmsg = e_trailing;
4657 else
4658 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004659 if (eap->addr_count == 0) // default is current buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00004660 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
4661 else
4662 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004663 if (eap->do_ecmd_cmd != NULL)
4664 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004665 }
4666}
4667
4668/*
4669 * :[N]bmodified [N] to next mod. buffer
4670 * :[N]sbmodified [N] to next mod. buffer
4671 */
4672 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004673ex_bmodified(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004674{
4675 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004676 if (eap->do_ecmd_cmd != NULL)
4677 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004678}
4679
4680/*
4681 * :[N]bnext [N] to next buffer
4682 * :[N]sbnext [N] split and to next buffer
4683 */
4684 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004685ex_bnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004686{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004687 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01004688 return;
4689
Bram Moolenaar071d4272004-06-13 20:20:40 +00004690 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004691 if (eap->do_ecmd_cmd != NULL)
4692 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004693}
4694
4695/*
4696 * :[N]bNext [N] to previous buffer
4697 * :[N]bprevious [N] to previous buffer
4698 * :[N]sbNext [N] split and to previous buffer
4699 * :[N]sbprevious [N] split and to previous buffer
4700 */
4701 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004702ex_bprevious(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004703{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004704 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01004705 return;
4706
Bram Moolenaar071d4272004-06-13 20:20:40 +00004707 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004708 if (eap->do_ecmd_cmd != NULL)
4709 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004710}
4711
4712/*
4713 * :brewind to first buffer
4714 * :bfirst to first buffer
4715 * :sbrewind split and to first buffer
4716 * :sbfirst split and to first buffer
4717 */
4718 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004719ex_brewind(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004720{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004721 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01004722 return;
4723
Bram Moolenaar071d4272004-06-13 20:20:40 +00004724 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004725 if (eap->do_ecmd_cmd != NULL)
4726 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004727}
4728
4729/*
4730 * :blast to last buffer
4731 * :sblast split and to last buffer
4732 */
4733 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004734ex_blast(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004735{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004736 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01004737 return;
4738
Bram Moolenaar071d4272004-06-13 20:20:40 +00004739 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004740 if (eap->do_ecmd_cmd != NULL)
4741 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004742}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004743
4744 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004745ends_excmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004746{
4747 return (c == NUL || c == '|' || c == '"' || c == '\n');
4748}
4749
4750#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
4751 || defined(PROTO)
4752/*
4753 * Return the next command, after the first '|' or '\n'.
4754 * Return NULL if not found.
4755 */
4756 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004757find_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004758{
4759 while (*p != '|' && *p != '\n')
4760 {
4761 if (*p == NUL)
4762 return NULL;
4763 ++p;
4764 }
4765 return (p + 1);
4766}
4767#endif
4768
4769/*
Bram Moolenaar2256c992016-11-15 21:17:07 +01004770 * Check if *p is a separator between Ex commands, skipping over white space.
4771 * Return NULL if it isn't, the following character if it is.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004772 */
4773 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004774check_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004775{
Bram Moolenaar2256c992016-11-15 21:17:07 +01004776 char_u *s = skipwhite(p);
4777
4778 if (*s == '|' || *s == '\n')
4779 return (s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004780 else
4781 return NULL;
4782}
4783
4784/*
4785 * - if there are more files to edit
4786 * - and this is the last window
4787 * - and forceit not used
4788 * - and not repeated twice on a row
4789 * return FAIL and give error message if 'message' TRUE
4790 * return OK otherwise
4791 */
4792 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004793check_more(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004794 int message, // when FALSE check only, no messages
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004795 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004796{
4797 int n = ARGCOUNT - curwin->w_arg_idx - 1;
4798
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00004799 if (!forceit && only_one_window()
4800 && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004801 {
4802 if (message)
4803 {
4804#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
4805 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
4806 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02004807 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004808
Bram Moolenaarda6e8912018-08-21 15:12:14 +02004809 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
4810 NGETTEXT("%d more file to edit. Quit anyway?",
4811 "%d more files to edit. Quit anyway?", n), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004812 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
4813 return OK;
4814 return FAIL;
4815 }
4816#endif
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01004817 semsg(NGETTEXT("E173: %d more file to edit",
4818 "E173: %d more files to edit", n), n);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004819 quitmore = 2; // next try to quit is allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004820 }
4821 return FAIL;
4822 }
4823 return OK;
4824}
4825
Bram Moolenaar071d4272004-06-13 20:20:40 +00004826/*
4827 * Function given to ExpandGeneric() to obtain the list of command names.
4828 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004829 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004830get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004831{
4832 if (idx >= (int)CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004833 return get_user_command_name(idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004834 return cmdnames[idx].cmd_name;
4835}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004836
Bram Moolenaar071d4272004-06-13 20:20:40 +00004837 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004838ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004839{
Bram Moolenaare6850792010-05-14 15:28:44 +02004840 if (*eap->arg == NUL)
4841 {
4842#ifdef FEAT_EVAL
4843 char_u *expr = vim_strsave((char_u *)"g:colors_name");
4844 char_u *p = NULL;
4845
4846 if (expr != NULL)
4847 {
4848 ++emsg_off;
4849 p = eval_to_string(expr, NULL, FALSE);
4850 --emsg_off;
4851 vim_free(expr);
4852 }
4853 if (p != NULL)
4854 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004855 msg((char *)p);
Bram Moolenaare6850792010-05-14 15:28:44 +02004856 vim_free(p);
4857 }
4858 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01004859 msg("default");
Bram Moolenaare6850792010-05-14 15:28:44 +02004860#else
Bram Moolenaar32526b32019-01-19 17:43:09 +01004861 msg(_("unknown"));
Bram Moolenaare6850792010-05-14 15:28:44 +02004862#endif
4863 }
4864 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004865 semsg(_("E185: Cannot find color scheme '%s'"), eap->arg);
Bram Moolenaarf58d81a2019-01-28 20:19:05 +01004866
4867#ifdef FEAT_VTP
4868 else if (has_vtp_working())
4869 {
4870 // background color change requires clear + redraw
4871 update_screen(CLEAR);
4872 redrawcmd();
4873 }
4874#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004875}
4876
4877 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004878ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004879{
4880 if (*eap->arg == NUL && eap->cmd[2] == '!')
Bram Moolenaar32526b32019-01-19 17:43:09 +01004881 msg(_("Greetings, Vim user!"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004882 do_highlight(eap->arg, eap->forceit, FALSE);
4883}
4884
4885
4886/*
4887 * Call this function if we thought we were going to exit, but we won't
4888 * (because of an error). May need to restore the terminal mode.
4889 */
4890 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004891not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004892{
4893 exiting = FALSE;
4894 settmode(TMODE_RAW);
4895}
4896
Bram Moolenaar12a96de2018-03-11 14:44:18 +01004897 static int
4898before_quit_autocmds(win_T *wp, int quit_all, int forceit)
4899{
4900 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, wp->w_buffer);
4901
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02004902 // Bail out when autocommands closed the window.
4903 // Refuse to quit when the buffer in the last window is being closed (can
4904 // only happen in autocommands).
Bram Moolenaar12a96de2018-03-11 14:44:18 +01004905 if (!win_valid(wp)
4906 || curbuf_locked()
4907 || (wp->w_buffer->b_nwindows == 1 && wp->w_buffer->b_locked > 0))
4908 return TRUE;
4909
4910 if (quit_all || (check_more(FALSE, forceit) == OK && only_one_window()))
4911 {
4912 apply_autocmds(EVENT_EXITPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02004913 // Refuse to quit when locked or when the window was closed or the
4914 // buffer in the last window is being closed (can only happen in
4915 // autocommands).
4916 if (!win_valid(wp) || curbuf_locked()
Bram Moolenaar12a96de2018-03-11 14:44:18 +01004917 || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
4918 return TRUE;
4919 }
4920
4921 return FALSE;
4922}
4923
Bram Moolenaar071d4272004-06-13 20:20:40 +00004924/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01004925 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar12a96de2018-03-11 14:44:18 +01004926 * ":{nr}quit": quit window {nr}
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02004927 * Also used when closing a terminal window that's the last one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004928 */
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02004929 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004930ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004931{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004932 win_T *wp;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004933
Bram Moolenaar071d4272004-06-13 20:20:40 +00004934#ifdef FEAT_CMDWIN
4935 if (cmdwin_type != 0)
4936 {
4937 cmdwin_result = Ctrl_C;
4938 return;
4939 }
4940#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004941 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004942 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004943 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004944 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004945 return;
4946 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004947 if (eap->addr_count > 0)
4948 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01004949 int wnr = eap->line2;
4950
4951 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
4952 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004953 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004954 }
4955 else
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004956 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01004957
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004958 // Refuse to quit when locked.
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02004959 if (curbuf_locked())
4960 return;
Bram Moolenaar12a96de2018-03-11 14:44:18 +01004961
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004962 // Trigger QuitPre and maybe ExitPre
Bram Moolenaar12a96de2018-03-11 14:44:18 +01004963 if (before_quit_autocmds(wp, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004964 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004965
4966#ifdef FEAT_NETBEANS_INTG
4967 netbeansForcedQuit = eap->forceit;
4968#endif
4969
4970 /*
4971 * If there are more files or windows we won't exit.
4972 */
4973 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
4974 exiting = TRUE;
Bram Moolenaarff930ca2017-10-19 17:12:10 +02004975 if ((!buf_hide(wp->w_buffer)
4976 && check_changed(wp->w_buffer, (p_awa ? CCGD_AW : 0)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01004977 | (eap->forceit ? CCGD_FORCEIT : 0)
4978 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004979 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01004980 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981 {
4982 not_exiting();
4983 }
4984 else
4985 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004986 // quit last window
4987 // Note: only_one_window() returns true, even so a help window is
4988 // still open. In that case only quit, if no address has been
4989 // specified. Example:
4990 // :h|wincmd w|1q - don't quit
4991 // :h|wincmd w|q - quit
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01004992 if (only_one_window() && (ONE_WINDOW || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004993 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02004994 not_exiting();
Bram Moolenaar4033c552017-09-16 20:54:51 +02004995#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02004997#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004998 // close window; may free buffer
Bram Moolenaareb44a682017-08-03 22:44:55 +02004999 win_close(wp, !buf_hide(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005000 }
5001}
5002
5003/*
5004 * ":cquit".
5005 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005006 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005007ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005008{
Bram Moolenaar1860bde2020-01-06 21:47:21 +01005009 // this does not always pass on the exit code to the Manx compiler. why?
5010 getout(eap->addr_count > 0 ? (int)eap->line2 : EXIT_FAILURE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005011}
5012
5013/*
5014 * ":qall": try to quit all windows
5015 */
5016 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005017ex_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005018{
5019# ifdef FEAT_CMDWIN
5020 if (cmdwin_type != 0)
5021 {
5022 if (eap->forceit)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005023 cmdwin_result = K_XF1; // ex_window() takes care of this
Bram Moolenaar071d4272004-06-13 20:20:40 +00005024 else
5025 cmdwin_result = K_XF2;
5026 return;
5027 }
5028# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005029
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005030 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005031 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005032 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005033 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005034 return;
5035 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005036
5037 if (before_quit_autocmds(curwin, TRUE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005038 return;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005039
Bram Moolenaar071d4272004-06-13 20:20:40 +00005040 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01005041 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005042 getout(0);
5043 not_exiting();
5044}
5045
Bram Moolenaar071d4272004-06-13 20:20:40 +00005046/*
5047 * ":close": close current window, unless it is the last one
5048 */
5049 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005050ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005051{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005052 win_T *win;
5053 int winnr = 0;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005054#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00005055 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02005056 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005058#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005059 if (!text_locked() && !curbuf_locked())
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005060 {
5061 if (eap->addr_count == 0)
5062 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar6d41c782018-06-06 09:11:12 +02005063 else
5064 {
Bram Moolenaar29323592016-07-24 22:04:11 +02005065 FOR_ALL_WINDOWS(win)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005066 {
5067 winnr++;
5068 if (winnr == eap->line2)
5069 break;
5070 }
5071 if (win == NULL)
5072 win = lastwin;
5073 ex_win_close(eap->forceit, win, NULL);
5074 }
5075 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005076}
5077
Bram Moolenaar4033c552017-09-16 20:54:51 +02005078#ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005079/*
5080 * ":pclose": Close any preview window.
5081 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005082 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005083ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005084{
5085 win_T *win;
5086
Bram Moolenaar79648732019-07-18 21:43:07 +02005087 // First close any normal window.
Bram Moolenaar29323592016-07-24 22:04:11 +02005088 FOR_ALL_WINDOWS(win)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005089 if (win->w_p_pvw)
5090 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005091 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar79648732019-07-18 21:43:07 +02005092 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005093 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005094# ifdef FEAT_PROP_POPUP
Bram Moolenaar79648732019-07-18 21:43:07 +02005095 // Also when 'previewpopup' is empty, it might have been cleared.
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02005096 popup_close_preview();
Bram Moolenaar79648732019-07-18 21:43:07 +02005097# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005098}
Bram Moolenaar4033c552017-09-16 20:54:51 +02005099#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005100
Bram Moolenaarf740b292006-02-16 22:11:02 +00005101/*
5102 * Close window "win" and take care of handling closing the last window for a
5103 * modified buffer.
5104 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005105 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005106ex_win_close(
5107 int forceit,
5108 win_T *win,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005109 tabpage_T *tp) // NULL or the tab page "win" is in
Bram Moolenaar071d4272004-06-13 20:20:40 +00005110{
5111 int need_hide;
5112 buf_T *buf = win->w_buffer;
5113
5114 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02005115 if (need_hide && !buf_hide(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005116 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02005117#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118 if ((p_confirm || cmdmod.confirm) && p_write)
5119 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005120 bufref_T bufref;
5121
5122 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005123 dialog_changed(buf, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005124 if (bufref_valid(&bufref) && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005125 return;
5126 need_hide = FALSE;
5127 }
5128 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005129#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005130 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02005131 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005132 return;
5133 }
5134 }
5135
Bram Moolenaar4033c552017-09-16 20:54:51 +02005136#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005137 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005138#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005139
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005140 // free buffer when not hiding it or when it's a scratch buffer
Bram Moolenaarf740b292006-02-16 22:11:02 +00005141 if (tp == NULL)
Bram Moolenaareb44a682017-08-03 22:44:55 +02005142 win_close(win, !need_hide && !buf_hide(buf));
Bram Moolenaarf740b292006-02-16 22:11:02 +00005143 else
Bram Moolenaareb44a682017-08-03 22:44:55 +02005144 win_close_othertab(win, !need_hide && !buf_hide(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145}
5146
Bram Moolenaar071d4272004-06-13 20:20:40 +00005147/*
Bram Moolenaardea25702017-01-29 15:18:10 +01005148 * Handle the argument for a tabpage related ex command.
5149 * Returns a tabpage number.
5150 * When an error is encountered then eap->errmsg is set.
5151 */
5152 static int
5153get_tabpage_arg(exarg_T *eap)
5154{
5155 int tab_number;
5156 int unaccept_arg0 = (eap->cmdidx == CMD_tabmove) ? 0 : 1;
5157
5158 if (eap->arg && *eap->arg != NUL)
5159 {
5160 char_u *p = eap->arg;
5161 char_u *p_save;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005162 int relative = 0; // argument +N/-N means: go to N places to the
5163 // right/left relative to the current position.
Bram Moolenaardea25702017-01-29 15:18:10 +01005164
5165 if (*p == '-')
5166 {
5167 relative = -1;
5168 p++;
5169 }
5170 else if (*p == '+')
5171 {
5172 relative = 1;
5173 p++;
5174 }
5175
5176 p_save = p;
5177 tab_number = getdigits(&p);
5178
5179 if (relative == 0)
5180 {
5181 if (STRCMP(p, "$") == 0)
5182 tab_number = LAST_TAB_NR;
5183 else if (p == p_save || *p_save == '-' || *p != NUL
5184 || tab_number > LAST_TAB_NR)
5185 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005186 // No numbers as argument.
Bram Moolenaardea25702017-01-29 15:18:10 +01005187 eap->errmsg = e_invarg;
5188 goto theend;
5189 }
5190 }
5191 else
5192 {
5193 if (*p_save == NUL)
5194 tab_number = 1;
5195 else if (p == p_save || *p_save == '-' || *p != NUL
5196 || tab_number == 0)
5197 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005198 // No numbers as argument.
Bram Moolenaardea25702017-01-29 15:18:10 +01005199 eap->errmsg = e_invarg;
5200 goto theend;
5201 }
5202 tab_number = tab_number * relative + tabpage_index(curtab);
5203 if (!unaccept_arg0 && relative == -1)
5204 --tab_number;
5205 }
5206 if (tab_number < unaccept_arg0 || tab_number > LAST_TAB_NR)
5207 eap->errmsg = e_invarg;
5208 }
5209 else if (eap->addr_count > 0)
5210 {
5211 if (unaccept_arg0 && eap->line2 == 0)
Bram Moolenaarc6251552017-01-29 21:42:20 +01005212 {
Bram Moolenaardea25702017-01-29 15:18:10 +01005213 eap->errmsg = e_invrange;
Bram Moolenaarc6251552017-01-29 21:42:20 +01005214 tab_number = 0;
5215 }
Bram Moolenaardea25702017-01-29 15:18:10 +01005216 else
5217 {
5218 tab_number = eap->line2;
Bram Moolenaar82a12462019-01-22 22:41:42 +01005219 if (!unaccept_arg0 && *skipwhite(*eap->cmdlinep) == '-')
Bram Moolenaardea25702017-01-29 15:18:10 +01005220 {
5221 --tab_number;
5222 if (tab_number < unaccept_arg0)
5223 eap->errmsg = e_invarg;
5224 }
5225 }
5226 }
5227 else
5228 {
5229 switch (eap->cmdidx)
5230 {
5231 case CMD_tabnext:
5232 tab_number = tabpage_index(curtab) + 1;
5233 if (tab_number > LAST_TAB_NR)
5234 tab_number = 1;
5235 break;
5236 case CMD_tabmove:
5237 tab_number = LAST_TAB_NR;
5238 break;
5239 default:
5240 tab_number = tabpage_index(curtab);
5241 }
5242 }
5243
5244theend:
5245 return tab_number;
5246}
5247
5248/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00005249 * ":tabclose": close current tab page, unless it is the last one.
5250 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005251 */
5252 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005253ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005254{
Bram Moolenaarf740b292006-02-16 22:11:02 +00005255 tabpage_T *tp;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005256 int tab_number;
Bram Moolenaarf740b292006-02-16 22:11:02 +00005257
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005258# ifdef FEAT_CMDWIN
5259 if (cmdwin_type != 0)
5260 cmdwin_result = K_IGNORE;
5261 else
5262# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005263 if (first_tabpage->tp_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005264 emsg(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00005265 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005266 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005267 tab_number = get_tabpage_arg(eap);
5268 if (eap->errmsg == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005269 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005270 tp = find_tabpage(tab_number);
Bram Moolenaarf740b292006-02-16 22:11:02 +00005271 if (tp == NULL)
5272 {
5273 beep_flush();
5274 return;
5275 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005276 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005277 {
5278 tabpage_close_other(tp, eap->forceit);
5279 return;
5280 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005281 else if (!text_locked() && !curbuf_locked())
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005282 tabpage_close(eap->forceit);
5283 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005284 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005285}
5286
5287/*
5288 * ":tabonly": close all tab pages except the current one
5289 */
5290 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005291ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005292{
5293 tabpage_T *tp;
5294 int done;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005295 int tab_number;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005296
5297# ifdef FEAT_CMDWIN
5298 if (cmdwin_type != 0)
5299 cmdwin_result = K_IGNORE;
5300 else
5301# endif
5302 if (first_tabpage->tp_next == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005303 msg(_("Already only one tab page"));
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005304 else
5305 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005306 tab_number = get_tabpage_arg(eap);
5307 if (eap->errmsg == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005308 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005309 goto_tabpage(tab_number);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005310 // Repeat this up to a 1000 times, because autocommands may
5311 // mess up the lists.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005312 for (done = 0; done < 1000; ++done)
5313 {
5314 FOR_ALL_TABPAGES(tp)
5315 if (tp->tp_topframe != topframe)
5316 {
5317 tabpage_close_other(tp, eap->forceit);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005318 // if we failed to close it quit
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005319 if (valid_tabpage(tp))
5320 done = 1000;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005321 // start over, "tp" is now invalid
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005322 break;
5323 }
5324 if (first_tabpage->tp_next == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005325 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005326 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005327 }
5328 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005329}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005330
5331/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00005332 * Close the current tab page.
5333 */
5334 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005335tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005336{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005337 // First close all the windows but the current one. If that worked then
5338 // close the last window in this tab, that will close it.
Bram Moolenaar459ca562016-11-10 18:16:33 +01005339 if (!ONE_WINDOW)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005340 close_others(TRUE, forceit);
Bram Moolenaar459ca562016-11-10 18:16:33 +01005341 if (ONE_WINDOW)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005342 ex_win_close(forceit, curwin, NULL);
5343# ifdef FEAT_GUI
5344 need_mouse_correct = TRUE;
5345# endif
5346}
5347
5348/*
5349 * Close tab page "tp", which is not the current tab page.
5350 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005351 * Also takes care of the tab pages line disappearing when closing the
5352 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00005353 */
5354 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005355tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005356{
5357 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005358 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005359 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00005360
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005361 // Limit to 1000 windows, autocommands may add a window while we close
5362 // one. OK, so I'm paranoid...
Bram Moolenaarf740b292006-02-16 22:11:02 +00005363 while (++done < 1000)
5364 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005365 wp = tp->tp_firstwin;
5366 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00005367
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005368 // Autocommands may delete the tab page under our fingers and we may
5369 // fail to close a window with a modified buffer.
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005370 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005371 break;
5372 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005373
Bram Moolenaar29323592016-07-24 22:04:11 +02005374 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar29323592016-07-24 22:04:11 +02005375
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005376 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005377 if (h != tabline_height())
5378 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00005379}
5380
5381/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005382 * ":only".
5383 */
5384 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005385ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005386{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005387 win_T *wp;
5388 int wnr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005389# ifdef FEAT_GUI
5390 need_mouse_correct = TRUE;
5391# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005392 if (eap->addr_count > 0)
5393 {
5394 wnr = eap->line2;
5395 for (wp = firstwin; --wnr > 0; )
5396 {
5397 if (wp->w_next == NULL)
5398 break;
5399 else
5400 wp = wp->w_next;
5401 }
5402 win_goto(wp);
5403 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005404 close_others(TRUE, eap->forceit);
5405}
5406
Bram Moolenaar071d4272004-06-13 20:20:40 +00005407 static void
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01005408ex_hide(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005409{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005410 // ":hide" or ":hide | cmd": hide current window
Bram Moolenaar2256c992016-11-15 21:17:07 +01005411 if (!eap->skip)
5412 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02005413#ifdef FEAT_GUI
Bram Moolenaar2256c992016-11-15 21:17:07 +01005414 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005415#endif
Bram Moolenaar2256c992016-11-15 21:17:07 +01005416 if (eap->addr_count == 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005417 win_close(curwin, FALSE); // don't free buffer
Bram Moolenaar2256c992016-11-15 21:17:07 +01005418 else
5419 {
5420 int winnr = 0;
5421 win_T *win;
Bram Moolenaarf240e182014-11-27 18:33:02 +01005422
Bram Moolenaar2256c992016-11-15 21:17:07 +01005423 FOR_ALL_WINDOWS(win)
5424 {
5425 winnr++;
5426 if (winnr == eap->line2)
5427 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005428 }
Bram Moolenaar2256c992016-11-15 21:17:07 +01005429 if (win == NULL)
5430 win = lastwin;
5431 win_close(win, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005432 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005433 }
5434}
5435
5436/*
5437 * ":stop" and ":suspend": Suspend Vim.
5438 */
5439 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005440ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005441{
5442 /*
5443 * Disallow suspending for "rvim".
5444 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005445 if (!check_restricted())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005446 {
5447 if (!eap->forceit)
5448 autowrite_all();
5449 windgoto((int)Rows - 1, 0);
5450 out_char('\n');
5451 out_flush();
5452 stoptermcap();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005453 out_flush(); // needed for SUN to restore xterm buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00005454#ifdef FEAT_TITLE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005455 mch_restore_title(SAVE_RESTORE_BOTH); // restore window titles
Bram Moolenaar071d4272004-06-13 20:20:40 +00005456#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005457 ui_suspend(); // call machine specific function
Bram Moolenaar071d4272004-06-13 20:20:40 +00005458#ifdef FEAT_TITLE
5459 maketitle();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005460 resettitle(); // force updating the title
Bram Moolenaar071d4272004-06-13 20:20:40 +00005461#endif
5462 starttermcap();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005463 scroll_start(); // scroll screen before redrawing
Bram Moolenaar071d4272004-06-13 20:20:40 +00005464 redraw_later_clear();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005465 shell_resized(); // may have resized window
Bram Moolenaar071d4272004-06-13 20:20:40 +00005466 }
5467}
5468
5469/*
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005470 * ":exit", ":xit" and ":wq": Write file and quite the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005471 */
5472 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005473ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005474{
5475#ifdef FEAT_CMDWIN
5476 if (cmdwin_type != 0)
5477 {
5478 cmdwin_result = Ctrl_C;
5479 return;
5480 }
5481#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005482 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005483 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005484 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005485 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005486 return;
5487 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005488
5489 if (before_quit_autocmds(curwin, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005490 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005491
5492 /*
5493 * if more files or windows we won't exit
5494 */
5495 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
5496 exiting = TRUE;
5497 if ( ((eap->cmdidx == CMD_wq
5498 || curbufIsChanged())
5499 && do_write(eap) == FAIL)
5500 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01005501 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005502 {
5503 not_exiting();
5504 }
5505 else
5506 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005507 if (only_one_window()) // quit last window, exit Vim
Bram Moolenaar071d4272004-06-13 20:20:40 +00005508 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02005509 not_exiting();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005510# ifdef FEAT_GUI
5511 need_mouse_correct = TRUE;
5512# endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005513 // Quit current window, may free the buffer.
Bram Moolenaareb44a682017-08-03 22:44:55 +02005514 win_close(curwin, !buf_hide(curwin->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005515 }
5516}
5517
5518/*
5519 * ":print", ":list", ":number".
5520 */
5521 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005522ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005523{
Bram Moolenaardf177f62005-02-22 08:39:57 +00005524 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005525 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +00005526 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005527 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00005528 for ( ;!got_int; ui_breakcheck())
5529 {
5530 print_line(eap->line1,
5531 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
5532 || (eap->flags & EXFLAG_NR)),
5533 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
5534 if (++eap->line1 > eap->line2)
5535 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005536 out_flush(); // show one line at a time
Bram Moolenaardf177f62005-02-22 08:39:57 +00005537 }
5538 setpcmark();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005539 // put cursor at last line
Bram Moolenaardf177f62005-02-22 08:39:57 +00005540 curwin->w_cursor.lnum = eap->line2;
5541 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005542 }
5543
Bram Moolenaar071d4272004-06-13 20:20:40 +00005544 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005545}
5546
5547#ifdef FEAT_BYTEOFF
5548 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005549ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005550{
5551 goto_byte(eap->line2);
5552}
5553#endif
5554
5555/*
5556 * ":shell".
5557 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005558 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005559ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005560{
5561 do_shell(NULL, 0);
5562}
5563
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005564#if defined(HAVE_DROP_FILE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005565
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005566static int drop_busy = FALSE;
5567static int drop_filec;
5568static char_u **drop_filev = NULL;
5569static int drop_split;
5570static void (*drop_callback)(void *);
5571static void *drop_cookie;
5572
5573 static void
5574handle_drop_internal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005575{
5576 exarg_T ea;
5577 int save_msg_scroll = msg_scroll;
5578
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005579 // Setting the argument list may cause screen updates and being called
5580 // recursively. Avoid that by setting drop_busy.
5581 drop_busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005582
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005583 // Check whether the current buffer is changed. If so, we will need
5584 // to split the current window or data could be lost.
5585 // We don't need to check if the 'hidden' option is set, as in this
5586 // case the buffer won't be lost.
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005587 if (!buf_hide(curbuf) && !drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005588 {
5589 ++emsg_off;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005590 drop_split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005591 --emsg_off;
5592 }
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005593 if (drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005594 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005595 if (win_split(0, 0) == FAIL)
5596 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02005597 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005598
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005599 // When splitting the window, create a new alist. Otherwise the
5600 // existing one is overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005601 alist_unlink(curwin->w_alist);
5602 alist_new();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005603 }
5604
5605 /*
5606 * Set up the new argument list.
5607 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005608 alist_set(ALIST(curwin), drop_filec, drop_filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005609
5610 /*
5611 * Move to the first file.
5612 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005613 // Fake up a minimal "next" command for do_argfile()
Bram Moolenaar071d4272004-06-13 20:20:40 +00005614 vim_memset(&ea, 0, sizeof(ea));
5615 ea.cmd = (char_u *)"next";
5616 do_argfile(&ea, 0);
5617
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005618 // do_ecmd() may set need_start_insertmode, but since we never left Insert
5619 // mode that is not needed here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005620 need_start_insertmode = FALSE;
5621
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005622 // Restore msg_scroll, otherwise a following command may cause scrolling
5623 // unexpectedly. The screen will be redrawn by the caller, thus
5624 // msg_scroll being set by displaying a message is irrelevant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005625 msg_scroll = save_msg_scroll;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005626
5627 if (drop_callback != NULL)
5628 drop_callback(drop_cookie);
5629
5630 drop_filev = NULL;
5631 drop_busy = FALSE;
5632}
5633
5634/*
5635 * Handle a file drop. The code is here because a drop is *nearly* like an
5636 * :args command, but not quite (we have a list of exact filenames, so we
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01005637 * don't want to (a) parse a command line, or (b) expand wildcards). So the
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005638 * code is very similar to :args and hence needs access to a lot of the static
5639 * functions in this file.
5640 *
5641 * The "filev" list must have been allocated using alloc(), as should each item
5642 * in the list. This function takes over responsibility for freeing the "filev"
5643 * list.
5644 */
5645 void
5646handle_drop(
5647 int filec, // the number of files dropped
5648 char_u **filev, // the list of files dropped
5649 int split, // force splitting the window
5650 void (*callback)(void *), // to be called after setting the argument
5651 // list
5652 void *cookie) // argument for "callback" (allocated)
5653{
5654 // Cannot handle recursive drops, finish the pending one.
5655 if (drop_busy)
5656 {
5657 FreeWild(filec, filev);
5658 vim_free(cookie);
5659 return;
5660 }
5661
5662 // When calling handle_drop() more than once in a row we only use the last
5663 // one.
5664 if (drop_filev != NULL)
5665 {
5666 FreeWild(drop_filec, drop_filev);
5667 vim_free(drop_cookie);
5668 }
5669
5670 drop_filec = filec;
5671 drop_filev = filev;
5672 drop_split = split;
5673 drop_callback = callback;
5674 drop_cookie = cookie;
5675
5676 // Postpone this when:
5677 // - editing the command line
5678 // - not possible to change the current buffer
5679 // - updating the screen
5680 // As it may change buffers and window structures that are in use and cause
5681 // freed memory to be used.
5682 if (text_locked() || curbuf_locked() || updating_screen)
5683 return;
5684
5685 handle_drop_internal();
5686}
5687
5688/*
5689 * To be called when text is unlocked, curbuf is unlocked or updating_screen is
5690 * reset: Handle a postponed drop.
5691 */
5692 void
5693handle_any_postponed_drop(void)
5694{
5695 if (!drop_busy && drop_filev != NULL
5696 && !text_locked() && !curbuf_locked() && !updating_screen)
5697 handle_drop_internal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005698}
5699#endif
5700
Bram Moolenaar071d4272004-06-13 20:20:40 +00005701/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005702 * ":preserve".
5703 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005704 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005705ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005706{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005707 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005708 ml_preserve(curbuf, TRUE);
5709}
5710
5711/*
5712 * ":recover".
5713 */
5714 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005715ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005716{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005717 // Set recoverymode right away to avoid the ATTENTION prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005718 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01005719 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
5720 | CCGD_MULTWIN
5721 | (eap->forceit ? CCGD_FORCEIT : 0)
5722 | CCGD_EXCMD)
5723
Bram Moolenaar071d4272004-06-13 20:20:40 +00005724 && (*eap->arg == NUL
5725 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
Bram Moolenaar99499b12019-05-23 21:35:48 +02005726 ml_recover(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005727 recoverymode = FALSE;
5728}
5729
5730/*
5731 * Command modifier used in a wrong way.
5732 */
5733 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005734ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005735{
5736 eap->errmsg = e_invcmd;
5737}
5738
Bram Moolenaar071d4272004-06-13 20:20:40 +00005739/*
5740 * :sview [+command] file split window with new file, read-only
5741 * :split [[+command] file] split window with current or new file
5742 * :vsplit [[+command] file] split window vertically with current or new file
5743 * :new [[+command] file] split window with no or new file
5744 * :vnew [[+command] file] split vertically window with no or new file
5745 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005746 *
5747 * :tabedit open new Tab page with empty window
5748 * :tabedit [+command] file open new Tab page and edit "file"
5749 * :tabnew [[+command] file] just like :tabedit
5750 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005751 */
5752 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005753ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005754{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005755 win_T *old_curwin = curwin;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005756#if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005757 char_u *fname = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005758#endif
5759#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005760 int browse_flag = cmdmod.browse;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005761#endif
Bram Moolenaar39902a02018-06-21 22:10:08 +02005762 int use_tab = eap->cmdidx == CMD_tabedit
5763 || eap->cmdidx == CMD_tabfind
5764 || eap->cmdidx == CMD_tabnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005765
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005766 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005767 return;
5768
Bram Moolenaar4033c552017-09-16 20:54:51 +02005769#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005770 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005771#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005772
Bram Moolenaar4033c552017-09-16 20:54:51 +02005773#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005774 // A ":split" in the quickfix window works like ":new". Don't want two
5775 // quickfix windows. But it's OK when doing ":tab split".
Bram Moolenaar05bb9532008-07-04 09:44:11 +00005776 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005777 {
5778 if (eap->cmdidx == CMD_split)
5779 eap->cmdidx = CMD_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005780 if (eap->cmdidx == CMD_vsplit)
5781 eap->cmdidx = CMD_vnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005782 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02005783#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005784
Bram Moolenaar4033c552017-09-16 20:54:51 +02005785#ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005786 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005787 {
5788 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
5789 FNAME_MESS, TRUE, curbuf->b_ffname);
5790 if (fname == NULL)
5791 goto theend;
5792 eap->arg = fname;
5793 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005794# ifdef FEAT_BROWSE
Bram Moolenaar4033c552017-09-16 20:54:51 +02005795 else
5796# endif
5797#endif
5798#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005799 if (cmdmod.browse
Bram Moolenaar071d4272004-06-13 20:20:40 +00005800 && eap->cmdidx != CMD_vnew
Bram Moolenaar071d4272004-06-13 20:20:40 +00005801 && eap->cmdidx != CMD_new)
5802 {
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005803 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005804# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005805 !gui.in_use &&
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005806# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005807 au_has_group((char_u *)"FileExplorer"))
5808 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005809 // No browsing supported but we do have the file explorer:
5810 // Edit the directory.
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005811 if (*eap->arg == NUL || !mch_isdir(eap->arg))
5812 eap->arg = (char_u *)".";
5813 }
5814 else
5815 {
Bram Moolenaar39902a02018-06-21 22:10:08 +02005816 fname = do_browse(0, (char_u *)(use_tab
5817 ? _("Edit File in new tab page")
5818 : _("Edit File in new window")),
Bram Moolenaar071d4272004-06-13 20:20:40 +00005819 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005820 if (fname == NULL)
5821 goto theend;
5822 eap->arg = fname;
5823 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005824 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005825 cmdmod.browse = FALSE; // Don't browse again in do_ecmd().
Bram Moolenaar4033c552017-09-16 20:54:51 +02005826#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005827
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005828 /*
5829 * Either open new tab page or split the window.
5830 */
Bram Moolenaar39902a02018-06-21 22:10:08 +02005831 if (use_tab)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005832 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005833 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
5834 : eap->addr_count == 0 ? 0
5835 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005836 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00005837 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005838
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005839 // set the alternate buffer for the window we came from
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005840 if (curwin != old_curwin
5841 && win_valid(old_curwin)
5842 && old_curwin->w_buffer != curbuf
5843 && !cmdmod.keepalt)
5844 old_curwin->w_alt_fnum = curbuf->b_fnum;
5845 }
5846 }
5847 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005848 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
5849 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005850 // Reset 'scrollbind' when editing another file, but keep it when
5851 // doing ":split" without arguments.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005852 if (*eap->arg != NUL
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01005853# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005854 || cmdmod.browse
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01005855# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005856 )
Bram Moolenaar3368ea22010-09-21 16:56:35 +02005857 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005858 else
5859 do_check_scrollbind(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005860 do_exedit(eap, old_curwin);
5861 }
5862
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005863# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005864 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005865# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005866
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005867# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005868theend:
5869 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005870# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005871}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005872
5873/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00005874 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005875 */
5876 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005877tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00005878{
5879 exarg_T ea;
5880
5881 vim_memset(&ea, 0, sizeof(ea));
5882 ea.cmdidx = CMD_tabnew;
5883 ea.cmd = (char_u *)"tabn";
5884 ea.arg = (char_u *)"";
5885 ex_splitview(&ea);
5886}
5887
5888/*
5889 * :tabnext command
5890 */
5891 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005892ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005893{
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005894 int tab_number;
5895
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02005896 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005897 return;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005898 switch (eap->cmdidx)
5899 {
5900 case CMD_tabfirst:
5901 case CMD_tabrewind:
5902 goto_tabpage(1);
5903 break;
5904 case CMD_tablast:
5905 goto_tabpage(9999);
5906 break;
5907 case CMD_tabprevious:
5908 case CMD_tabNext:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005909 if (eap->arg && *eap->arg != NUL)
5910 {
5911 char_u *p = eap->arg;
5912 char_u *p_save = p;
5913
5914 tab_number = getdigits(&p);
5915 if (p == p_save || *p_save == '-' || *p != NUL
5916 || tab_number == 0)
5917 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005918 // No numbers as argument.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005919 eap->errmsg = e_invarg;
5920 return;
5921 }
5922 }
5923 else
5924 {
5925 if (eap->addr_count == 0)
5926 tab_number = 1;
5927 else
5928 {
5929 tab_number = eap->line2;
5930 if (tab_number < 1)
5931 {
5932 eap->errmsg = e_invrange;
5933 return;
5934 }
5935 }
5936 }
5937 goto_tabpage(-tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005938 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005939 default: // CMD_tabnext
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005940 tab_number = get_tabpage_arg(eap);
5941 if (eap->errmsg == NULL)
5942 goto_tabpage(tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005943 break;
5944 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00005945}
5946
5947/*
5948 * :tabmove command
5949 */
5950 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005951ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00005952{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02005953 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02005954
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005955 tab_number = get_tabpage_arg(eap);
5956 if (eap->errmsg == NULL)
5957 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005958}
5959
5960/*
5961 * :tabs command: List tabs and their contents.
5962 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005963 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005964ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005965{
5966 tabpage_T *tp;
5967 win_T *wp;
5968 int tabcount = 1;
5969
5970 msg_start();
5971 msg_scroll = TRUE;
5972 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
5973 {
5974 msg_putchar('\n');
5975 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
Bram Moolenaar8820b482017-03-16 17:23:31 +01005976 msg_outtrans_attr(IObuff, HL_ATTR(HLF_T));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005977 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005978 ui_breakcheck();
5979
Bram Moolenaar030f0df2006-02-21 22:02:53 +00005980 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005981 wp = firstwin;
5982 else
5983 wp = tp->tp_firstwin;
5984 for ( ; wp != NULL && !got_int; wp = wp->w_next)
5985 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00005986 msg_putchar('\n');
5987 msg_putchar(wp == curwin ? '>' : ' ');
5988 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005989 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
5990 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005991 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005992 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005993 else
5994 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
5995 IObuff, IOSIZE, TRUE);
5996 msg_outtrans(IObuff);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005997 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005998 ui_breakcheck();
5999 }
6000 }
6001}
6002
Bram Moolenaar071d4272004-06-13 20:20:40 +00006003/*
6004 * ":mode": Set screen mode.
6005 * If no argument given, just get the screen size and redraw.
6006 */
6007 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006008ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006009{
6010 if (*eap->arg == NUL)
6011 shell_resized();
6012 else
Bram Moolenaar3c71aec2020-01-17 19:32:20 +01006013 emsg(_(e_screenmode));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006014}
6015
Bram Moolenaar071d4272004-06-13 20:20:40 +00006016/*
6017 * ":resize".
6018 * set, increment or decrement current window height
6019 */
6020 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006021ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006022{
6023 int n;
6024 win_T *wp = curwin;
6025
6026 if (eap->addr_count > 0)
6027 {
6028 n = eap->line2;
6029 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
6030 ;
6031 }
6032
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006033# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006034 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006035# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006036 n = atol((char *)eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006037 if (cmdmod.split & WSP_VERT)
6038 {
6039 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar02631462017-09-22 15:20:32 +02006040 n += curwin->w_width;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006041 else if (n == 0 && eap->arg[0] == NUL) // default is very wide
Bram Moolenaar071d4272004-06-13 20:20:40 +00006042 n = 9999;
6043 win_setwidth_win((int)n, wp);
6044 }
6045 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006046 {
6047 if (*eap->arg == '-' || *eap->arg == '+')
6048 n += curwin->w_height;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006049 else if (n == 0 && eap->arg[0] == NUL) // default is very high
Bram Moolenaar071d4272004-06-13 20:20:40 +00006050 n = 9999;
6051 win_setheight_win((int)n, wp);
6052 }
6053}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006054
6055/*
6056 * ":find [+command] <file>" command.
6057 */
6058 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006059ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006060{
6061#ifdef FEAT_SEARCHPATH
6062 char_u *fname;
6063 int count;
6064
6065 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
6066 TRUE, curbuf->b_ffname);
6067 if (eap->addr_count > 0)
6068 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006069 // Repeat finding the file "count" times. This matters when it
6070 // appears several times in the path.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006071 count = eap->line2;
6072 while (fname != NULL && --count > 0)
6073 {
6074 vim_free(fname);
6075 fname = find_file_in_path(NULL, 0, FNAME_MESS,
6076 FALSE, curbuf->b_ffname);
6077 }
6078 }
6079
6080 if (fname != NULL)
6081 {
6082 eap->arg = fname;
6083#endif
6084 do_exedit(eap, NULL);
6085#ifdef FEAT_SEARCHPATH
6086 vim_free(fname);
6087 }
6088#endif
6089}
6090
6091/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00006092 * ":open" simulation: for now just work like ":visual".
6093 */
6094 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006095ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006096{
6097 regmatch_T regmatch;
6098 char_u *p;
6099
6100 curwin->w_cursor.lnum = eap->line2;
6101 beginline(BL_SOL | BL_FIX);
6102 if (*eap->arg == '/')
6103 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006104 // ":open /pattern/": put cursor in column found with pattern
Bram Moolenaardf177f62005-02-22 08:39:57 +00006105 ++eap->arg;
6106 p = skip_regexp(eap->arg, '/', p_magic, NULL);
6107 *p = NUL;
6108 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
6109 if (regmatch.regprog != NULL)
6110 {
6111 regmatch.rm_ic = p_ic;
6112 p = ml_get_curline();
6113 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006114 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006115 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006116 emsg(_(e_nomatch));
Bram Moolenaar473de612013-06-08 18:19:48 +02006117 vim_regfree(regmatch.regprog);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006118 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006119 // Move to the NUL, ignore any other arguments.
Bram Moolenaardf177f62005-02-22 08:39:57 +00006120 eap->arg += STRLEN(eap->arg);
6121 }
6122 check_cursor();
6123
6124 eap->cmdidx = CMD_visual;
6125 do_exedit(eap, NULL);
6126}
6127
6128/*
6129 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006130 */
6131 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006132ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006133{
6134 do_exedit(eap, NULL);
6135}
6136
6137/*
Bram Moolenaar4b96df52020-01-26 22:00:26 +01006138 * ":edit <file>" command and alike.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006139 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006140 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006141do_exedit(
6142 exarg_T *eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006143 win_T *old_curwin) // curwin before doing a split or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006144{
6145 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006146 int need_hide;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006147 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006148
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01006149 if ((eap->cmdidx != CMD_pedit && ERROR_IF_POPUP_WINDOW)
6150 || ERROR_IF_TERM_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006151 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006152 /*
6153 * ":vi" command ends Ex mode.
6154 */
6155 if (exmode_active && (eap->cmdidx == CMD_visual
6156 || eap->cmdidx == CMD_view))
6157 {
6158 exmode_active = FALSE;
6159 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006160 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006161 // Special case: ":global/pat/visual\NLvi-commands"
Bram Moolenaardf177f62005-02-22 08:39:57 +00006162 if (global_busy)
6163 {
6164 int rd = RedrawingDisabled;
6165 int nwr = no_wait_return;
6166 int ms = msg_scroll;
6167#ifdef FEAT_GUI
6168 int he = hold_gui_events;
6169#endif
6170
6171 if (eap->nextcmd != NULL)
6172 {
6173 stuffReadbuff(eap->nextcmd);
6174 eap->nextcmd = NULL;
6175 }
6176
6177 if (exmode_was != EXMODE_VIM)
6178 settmode(TMODE_RAW);
6179 RedrawingDisabled = 0;
6180 no_wait_return = 0;
6181 need_wait_return = FALSE;
6182 msg_scroll = 0;
6183#ifdef FEAT_GUI
6184 hold_gui_events = 0;
6185#endif
6186 must_redraw = CLEAR;
6187
6188 main_loop(FALSE, TRUE);
6189
6190 RedrawingDisabled = rd;
6191 no_wait_return = nwr;
6192 msg_scroll = ms;
6193#ifdef FEAT_GUI
6194 hold_gui_events = he;
6195#endif
6196 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006197 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006198 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006199 }
6200
6201 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006202 || eap->cmdidx == CMD_tabnew
6203 || eap->cmdidx == CMD_tabedit
Bram Moolenaar4033c552017-09-16 20:54:51 +02006204 || eap->cmdidx == CMD_vnew) && *eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006205 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006206 // ":new" or ":tabnew" without argument: edit an new empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006207 setpcmark();
6208 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006209 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
6210 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006211 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006212 else if ((eap->cmdidx != CMD_split && eap->cmdidx != CMD_vsplit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006213 || *eap->arg != NUL
6214#ifdef FEAT_BROWSE
6215 || cmdmod.browse
6216#endif
6217 )
6218 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006219 // Can't edit another file when "curbuf_lock" is set. Only ":edit"
6220 // can bring us here, others are stopped earlier.
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006221 if (*eap->arg != NUL && curbuf_locked())
6222 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006223
Bram Moolenaar071d4272004-06-13 20:20:40 +00006224 n = readonlymode;
6225 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
6226 readonlymode = TRUE;
6227 else if (eap->cmdidx == CMD_enew)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006228 readonlymode = FALSE; // 'readonly' doesn't make sense in an
6229 // empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006230 setpcmark();
6231 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
6232 NULL, eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006233 // ":edit" goes to first line if Vi compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00006234 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
6235 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
6236 ? ECMD_ONE : eap->do_ecmd_lnum,
Bram Moolenaareb44a682017-08-03 22:44:55 +02006237 (buf_hide(curbuf) ? ECMD_HIDE : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006238 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006239 // after a split we can use an existing buffer
Bram Moolenaar7b449342014-03-25 13:03:48 +01006240 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006241 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006242 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006243 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006244 // Editing the file failed. If the window was split, close it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006245 if (old_curwin != NULL)
6246 {
6247 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02006248 if (!need_hide || buf_hide(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006249 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006250#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006251 cleanup_T cs;
6252
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006253 // Reset the error/interrupt/exception state here so that
6254 // aborting() returns FALSE when closing a window.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006255 enter_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02006256#endif
6257#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006258 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006259#endif
Bram Moolenaareb44a682017-08-03 22:44:55 +02006260 win_close(curwin, !need_hide && !buf_hide(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006261
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006262#if defined(FEAT_EVAL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006263 // Restore the error/interrupt/exception state if not
6264 // discarded by a new aborting error, interrupt, or
6265 // uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006266 leave_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02006267#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006268 }
6269 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006270 }
6271 else if (readonlymode && curbuf->b_nwindows == 1)
6272 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006273 // When editing an already visited buffer, 'readonly' won't be set
6274 // but the previous value is kept. With ":view" and ":sview" we
6275 // want the file to be readonly, except when another window is
6276 // editing the same buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006277 curbuf->b_p_ro = TRUE;
6278 }
6279 readonlymode = n;
6280 }
6281 else
6282 {
6283 if (eap->do_ecmd_cmd != NULL)
6284 do_cmdline_cmd(eap->do_ecmd_cmd);
6285#ifdef FEAT_TITLE
6286 n = curwin->w_arg_idx_invalid;
6287#endif
6288 check_arg_idx(curwin);
6289#ifdef FEAT_TITLE
6290 if (n != curwin->w_arg_idx_invalid)
6291 maketitle();
6292#endif
6293 }
6294
Bram Moolenaar071d4272004-06-13 20:20:40 +00006295 /*
6296 * if ":split file" worked, set alternate file name in old window to new
6297 * file
6298 */
6299 if (old_curwin != NULL
6300 && *eap->arg != NUL
6301 && curwin != old_curwin
6302 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00006303 && old_curwin->w_buffer != curbuf
6304 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006305 old_curwin->w_alt_fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006306
6307 ex_no_reprint = TRUE;
6308}
6309
6310#ifndef FEAT_GUI
6311/*
6312 * ":gui" and ":gvim" when there is no GUI.
6313 */
6314 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006315ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006316{
6317 eap->errmsg = e_nogvim;
6318}
6319#endif
6320
Bram Moolenaar4f974752019-02-17 17:44:42 +01006321#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006322 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006323ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006324{
6325 gui_make_tearoff(eap->arg);
6326}
6327#endif
6328
Bram Moolenaar29a2c082018-03-05 21:06:23 +01006329#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
6330 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006331 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006332ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006333{
Bram Moolenaar29a2c082018-03-05 21:06:23 +01006334# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
6335 if (gui.in_use)
6336 gui_make_popup(eap->arg, eap->forceit);
6337# ifdef FEAT_TERM_POPUP_MENU
6338 else
6339# endif
6340# endif
6341# ifdef FEAT_TERM_POPUP_MENU
6342 pum_make_popup(eap->arg, eap->forceit);
6343# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006344}
6345#endif
6346
Bram Moolenaar071d4272004-06-13 20:20:40 +00006347 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006348ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006349{
6350 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006351 msg(_("No swap file"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006352 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01006353 msg((char *)curbuf->b_ml.ml_mfp->mf_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006354}
6355
6356/*
6357 * ":syncbind" forces all 'scrollbind' windows to have the same relative
6358 * offset.
6359 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
6360 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006361 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006362ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006363{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006364 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006365 win_T *save_curwin = curwin;
6366 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006367 long topline;
6368 long y;
6369 linenr_T old_linenr = curwin->w_cursor.lnum;
6370
6371 setpcmark();
6372
6373 /*
6374 * determine max topline
6375 */
6376 if (curwin->w_p_scb)
6377 {
6378 topline = curwin->w_topline;
Bram Moolenaar29323592016-07-24 22:04:11 +02006379 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006380 {
6381 if (wp->w_p_scb && wp->w_buffer)
6382 {
Bram Moolenaar375e3392019-01-31 18:26:10 +01006383 y = wp->w_buffer->b_ml.ml_line_count - get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006384 if (topline > y)
6385 topline = y;
6386 }
6387 }
6388 if (topline < 1)
6389 topline = 1;
6390 }
6391 else
6392 {
6393 topline = 1;
6394 }
6395
6396
6397 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006398 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006399 */
Bram Moolenaar29323592016-07-24 22:04:11 +02006400 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006401 {
6402 if (curwin->w_p_scb)
6403 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006404 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006405 y = topline - curwin->w_topline;
6406 if (y > 0)
6407 scrollup(y, TRUE);
6408 else
6409 scrolldown(-y, TRUE);
6410 curwin->w_scbind_pos = topline;
6411 redraw_later(VALID);
6412 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006413 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006414 }
6415 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006416 curwin = save_curwin;
6417 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006418 if (curwin->w_p_scb)
6419 {
6420 did_syncbind = TRUE;
6421 checkpcmark();
6422 if (old_linenr != curwin->w_cursor.lnum)
6423 {
6424 char_u ctrl_o[2];
6425
6426 ctrl_o[0] = Ctrl_O;
6427 ctrl_o[1] = 0;
6428 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
6429 }
6430 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006431}
6432
6433
6434 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006435ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006436{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006437 int i;
6438 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
6439 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006440
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006441 if (eap->usefilter) // :r!cmd
Bram Moolenaar071d4272004-06-13 20:20:40 +00006442 do_bang(1, eap, FALSE, FALSE, TRUE);
6443 else
6444 {
6445 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
6446 return;
6447
6448#ifdef FEAT_BROWSE
6449 if (cmdmod.browse)
6450 {
6451 char_u *browseFile;
6452
Bram Moolenaar7171abe2004-10-11 10:06:20 +00006453 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006454 NULL, NULL, NULL, curbuf);
6455 if (browseFile != NULL)
6456 {
6457 i = readfile(browseFile, NULL,
6458 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
6459 vim_free(browseFile);
6460 }
6461 else
6462 i = OK;
6463 }
6464 else
6465#endif
6466 if (*eap->arg == NUL)
6467 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006468 if (check_fname() == FAIL) // check for no file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00006469 return;
6470 i = readfile(curbuf->b_ffname, curbuf->b_fname,
6471 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
6472 }
6473 else
6474 {
6475 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
6476 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
6477 i = readfile(eap->arg, NULL,
6478 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
6479
6480 }
Bram Moolenaare13b9af2017-01-13 22:01:02 +01006481 if (i != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006482 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006483#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006484 if (!aborting())
6485#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006486 semsg(_(e_notopen), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006487 }
6488 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00006489 {
6490 if (empty && exmode_active)
6491 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006492 // Delete the empty line that remains. Historically ex does
6493 // this but vi doesn't.
Bram Moolenaardf177f62005-02-22 08:39:57 +00006494 if (eap->line2 == 0)
6495 lnum = curbuf->b_ml.ml_line_count;
6496 else
6497 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00006498 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006499 {
6500 ml_delete(lnum, FALSE);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00006501 if (curwin->w_cursor.lnum > 1
6502 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006503 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00006504 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006505 }
6506 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006507 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006508 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006509 }
6510}
6511
Bram Moolenaarea408852005-06-25 22:49:46 +00006512static char_u *prev_dir = NULL;
6513
6514#if defined(EXITFREE) || defined(PROTO)
6515 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006516free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00006517{
Bram Moolenaard23a8232018-02-10 18:45:26 +01006518 VIM_CLEAR(prev_dir);
6519 VIM_CLEAR(globaldir);
Bram Moolenaarea408852005-06-25 22:49:46 +00006520}
6521#endif
6522
Bram Moolenaarf4258302013-06-02 18:20:17 +02006523/*
6524 * Deal with the side effects of changing the current directory.
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006525 * When 'scope' is CDSCOPE_TABPAGE then this was after an ":tcd" command.
6526 * When 'scope' is CDSCOPE_WINDOW then this was after an ":lcd" command.
Bram Moolenaarf4258302013-06-02 18:20:17 +02006527 */
6528 void
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006529post_chdir(cdscope_T scope)
Bram Moolenaarf4258302013-06-02 18:20:17 +02006530{
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006531 if (scope != CDSCOPE_WINDOW)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006532 // Clear tab local directory for both :cd and :tcd
6533 VIM_CLEAR(curtab->tp_localdir);
Bram Moolenaard23a8232018-02-10 18:45:26 +01006534 VIM_CLEAR(curwin->w_localdir);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006535 if (scope != CDSCOPE_GLOBAL)
Bram Moolenaarf4258302013-06-02 18:20:17 +02006536 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006537 // If still in global directory, need to remember current
6538 // directory as global directory.
Bram Moolenaarf4258302013-06-02 18:20:17 +02006539 if (globaldir == NULL && prev_dir != NULL)
6540 globaldir = vim_strsave(prev_dir);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006541 // Remember this local directory for the window.
Bram Moolenaarf4258302013-06-02 18:20:17 +02006542 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006543 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006544 if (scope == CDSCOPE_TABPAGE)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006545 curtab->tp_localdir = vim_strsave(NameBuff);
6546 else
6547 curwin->w_localdir = vim_strsave(NameBuff);
6548 }
Bram Moolenaarf4258302013-06-02 18:20:17 +02006549 }
6550 else
6551 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006552 // We are now in the global directory, no need to remember its
6553 // name.
Bram Moolenaard23a8232018-02-10 18:45:26 +01006554 VIM_CLEAR(globaldir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006555 }
6556
6557 shorten_fnames(TRUE);
6558}
6559
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006560/*
6561 * Change directory function used by :cd/:tcd/:lcd Ex commands and the
6562 * chdir() function. If 'winlocaldir' is TRUE, then changes the window-local
6563 * directory. If 'tablocaldir' is TRUE, then changes the tab-local directory.
6564 * Otherwise changes the global directory.
6565 * Returns TRUE if the directory is successfully changed.
6566 */
6567 int
6568changedir_func(
6569 char_u *new_dir,
6570 int forceit,
6571 cdscope_T scope)
6572{
6573 char_u *tofree;
6574 int dir_differs;
6575 int retval = FALSE;
6576
Bram Moolenaar7cc96922020-01-31 22:41:38 +01006577 if (new_dir == NULL || allbuf_locked())
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006578 return FALSE;
6579
6580 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged() && !forceit)
6581 {
6582 emsg(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
6583 return FALSE;
6584 }
6585
6586 // ":cd -": Change to previous directory
6587 if (STRCMP(new_dir, "-") == 0)
6588 {
6589 if (prev_dir == NULL)
6590 {
6591 emsg(_("E186: No previous directory"));
6592 return FALSE;
6593 }
6594 new_dir = prev_dir;
6595 }
6596
6597 // Save current directory for next ":cd -"
6598 tofree = prev_dir;
6599 if (mch_dirname(NameBuff, MAXPATHL) == OK)
6600 prev_dir = vim_strsave(NameBuff);
6601 else
6602 prev_dir = NULL;
6603
6604#if defined(UNIX) || defined(VMS)
6605 // for UNIX ":cd" means: go to home directory
6606 if (*new_dir == NUL)
6607 {
6608 // use NameBuff for home directory name
6609# ifdef VMS
6610 char_u *p;
6611
6612 p = mch_getenv((char_u *)"SYS$LOGIN");
6613 if (p == NULL || *p == NUL) // empty is the same as not set
6614 NameBuff[0] = NUL;
6615 else
6616 vim_strncpy(NameBuff, p, MAXPATHL - 1);
6617# else
6618 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
6619# endif
6620 new_dir = NameBuff;
6621 }
6622#endif
6623 dir_differs = new_dir == NULL || prev_dir == NULL
6624 || pathcmp((char *)prev_dir, (char *)new_dir, -1) != 0;
6625 if (new_dir == NULL || (dir_differs && vim_chdir(new_dir)))
6626 emsg(_(e_failed));
6627 else
6628 {
6629 char_u *acmd_fname;
6630
6631 post_chdir(scope);
6632
6633 if (dir_differs)
6634 {
6635 if (scope == CDSCOPE_WINDOW)
6636 acmd_fname = (char_u *)"window";
6637 else if (scope == CDSCOPE_TABPAGE)
6638 acmd_fname = (char_u *)"tabpage";
6639 else
6640 acmd_fname = (char_u *)"global";
6641 apply_autocmds(EVENT_DIRCHANGED, acmd_fname, new_dir, FALSE,
6642 curbuf);
6643 }
6644 retval = TRUE;
6645 }
6646 vim_free(tofree);
6647
6648 return retval;
6649}
Bram Moolenaarea408852005-06-25 22:49:46 +00006650
Bram Moolenaar071d4272004-06-13 20:20:40 +00006651/*
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006652 * ":cd", ":tcd", ":lcd", ":chdir" ":tchdir" and ":lchdir".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006653 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00006654 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006655ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006656{
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01006657 char_u *new_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006658
6659 new_dir = eap->arg;
6660#if !defined(UNIX) && !defined(VMS)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006661 // for non-UNIX ":cd" means: print current directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006662 if (*new_dir == NUL)
6663 ex_pwd(NULL);
6664 else
6665#endif
6666 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006667 cdscope_T scope = CDSCOPE_GLOBAL;
6668
6669 if (eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir)
6670 scope = CDSCOPE_WINDOW;
6671 else if (eap->cmdidx == CMD_tcd || eap->cmdidx == CMD_tchdir)
6672 scope = CDSCOPE_TABPAGE;
6673
6674 if (changedir_func(new_dir, eap->forceit, scope))
Bram Moolenaardf177f62005-02-22 08:39:57 +00006675 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006676 // Echo the new current directory if the command was typed.
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00006677 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006678 ex_pwd(eap);
6679 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006680 }
6681}
6682
6683/*
6684 * ":pwd".
6685 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006686 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006687ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006688{
6689 if (mch_dirname(NameBuff, MAXPATHL) == OK)
6690 {
6691#ifdef BACKSLASH_IN_FILENAME
6692 slash_adjust(NameBuff);
6693#endif
Bram Moolenaar32526b32019-01-19 17:43:09 +01006694 msg((char *)NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006695 }
6696 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006697 emsg(_("E187: Unknown"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006698}
6699
6700/*
6701 * ":=".
6702 */
6703 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006704ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006705{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006706 smsg("%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006707 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006708}
6709
6710 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006711ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006712{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006713 int n;
6714 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006715
6716 if (cursor_valid())
6717 {
6718 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
6719 if (n >= 0)
Bram Moolenaar53f81742017-09-22 14:35:51 +02006720 windgoto((int)n, curwin->w_wincol + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006721 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006722
6723 len = eap->line2;
6724 switch (*eap->arg)
6725 {
6726 case 'm': break;
6727 case NUL: len *= 1000L; break;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006728 default: semsg(_(e_invarg2), eap->arg); return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006729 }
6730 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006731}
6732
6733/*
6734 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
6735 */
6736 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006737do_sleep(long msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006738{
Bram Moolenaar52953192019-08-16 10:27:13 +02006739 long done = 0;
Bram Moolenaar975b5272016-03-15 23:10:59 +01006740 long wait_now;
Bram Moolenaar52953192019-08-16 10:27:13 +02006741# ifdef ELAPSED_FUNC
6742 elapsed_T start_tv;
6743
6744 // Remember at what time we started, so that we know how much longer we
6745 // should wait after waiting for a bit.
6746 ELAPSED_INIT(start_tv);
6747# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006748
6749 cursor_on();
Bram Moolenaarf45d7472018-09-30 18:22:26 +02006750 out_flush_cursor(FALSE, FALSE);
Bram Moolenaar52953192019-08-16 10:27:13 +02006751 while (!got_int && done < msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006752 {
Bram Moolenaar975b5272016-03-15 23:10:59 +01006753 wait_now = msec - done > 1000L ? 1000L : msec - done;
6754#ifdef FEAT_TIMERS
6755 {
6756 long due_time = check_due_timer();
6757
6758 if (due_time > 0 && due_time < wait_now)
6759 wait_now = due_time;
6760 }
6761#endif
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02006762#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar28e67e02019-08-15 23:05:49 +02006763 if (has_any_channel() && wait_now > 20L)
6764 wait_now = 20L;
6765#endif
6766#ifdef FEAT_SOUND
6767 if (has_any_sound_callback() && wait_now > 20L)
6768 wait_now = 20L;
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02006769#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +01006770 ui_delay(wait_now, TRUE);
Bram Moolenaar52953192019-08-16 10:27:13 +02006771
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02006772#ifdef FEAT_JOB_CHANNEL
6773 if (has_any_channel())
6774 ui_breakcheck_force(TRUE);
6775 else
6776#endif
6777 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006778#ifdef MESSAGE_QUEUE
Bram Moolenaar52953192019-08-16 10:27:13 +02006779 // Process the netbeans and clientserver messages that may have been
6780 // received in the call to ui_breakcheck() when the GUI is in use. This
6781 // may occur when running a test case.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006782 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02006783#endif
Bram Moolenaar52953192019-08-16 10:27:13 +02006784
6785# ifdef ELAPSED_FUNC
6786 // actual time passed
6787 done = ELAPSED_FUNC(start_tv);
6788# else
6789 // guestimate time passed (will actually be more)
6790 done += wait_now;
6791# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006792 }
Bram Moolenaar1ebff3d2018-07-07 16:18:13 +02006793
6794 // If CTRL-C was typed to interrupt the sleep, drop the CTRL-C from the
6795 // input buffer, otherwise a following call to input() fails.
6796 if (got_int)
6797 (void)vpeekc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006798}
6799
Bram Moolenaar071d4272004-06-13 20:20:40 +00006800/*
6801 * ":winsize" command (obsolete).
6802 */
6803 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006804ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006805{
6806 int w, h;
6807 char_u *arg = eap->arg;
6808 char_u *p;
6809
6810 w = getdigits(&arg);
6811 arg = skipwhite(arg);
6812 p = arg;
6813 h = getdigits(&arg);
6814 if (*p != NUL && *arg == NUL)
6815 set_shellsize(w, h, TRUE);
6816 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006817 emsg(_("E465: :winsize requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006818}
6819
Bram Moolenaar071d4272004-06-13 20:20:40 +00006820 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006821ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006822{
6823 int xchar = NUL;
6824 char_u *p;
6825
6826 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
6827 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006828 // CTRL-W g and CTRL-W CTRL-G have an extra command character
Bram Moolenaar071d4272004-06-13 20:20:40 +00006829 if (eap->arg[1] == NUL)
6830 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006831 emsg(_(e_invarg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006832 return;
6833 }
6834 xchar = eap->arg[1];
6835 p = eap->arg + 2;
6836 }
6837 else
6838 p = eap->arg + 1;
6839
6840 eap->nextcmd = check_nextcmd(p);
6841 p = skipwhite(p);
6842 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006843 emsg(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02006844 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006845 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006846 // Pass flags on for ":vertical wincmd ]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006847 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00006848 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006849 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
6850 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00006851 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006852 }
6853}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006854
Bram Moolenaar843ee412004-06-30 16:16:41 +00006855#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006856/*
6857 * ":winpos".
6858 */
6859 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006860ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006861{
6862 int x, y;
6863 char_u *arg = eap->arg;
6864 char_u *p;
6865
6866 if (*arg == NUL)
6867 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00006868# if defined(FEAT_GUI) || defined(MSWIN)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006869# ifdef VIMDLL
6870 if (gui.in_use ? gui_mch_get_winpos(&x, &y) != FAIL :
6871 mch_get_winpos(&x, &y) != FAIL)
6872# elif defined(FEAT_GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006873 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00006874# else
6875 if (mch_get_winpos(&x, &y) != FAIL)
6876# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006877 {
6878 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
Bram Moolenaar32526b32019-01-19 17:43:09 +01006879 msg((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006880 }
6881 else
6882# endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006883 emsg(_("E188: Obtaining window position not implemented for this platform"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006884 }
6885 else
6886 {
6887 x = getdigits(&arg);
6888 arg = skipwhite(arg);
6889 p = arg;
6890 y = getdigits(&arg);
6891 if (*p == NUL || *arg != NUL)
6892 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006893 emsg(_("E466: :winpos requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006894 return;
6895 }
6896# ifdef FEAT_GUI
6897 if (gui.in_use)
6898 gui_mch_set_winpos(x, y);
6899 else if (gui.starting)
6900 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006901 // Remember the coordinates for when the window is opened.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006902 gui_win_x = x;
6903 gui_win_y = y;
6904 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006905# if defined(HAVE_TGETENT) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006906 else
6907# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006908# endif
6909# if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar843ee412004-06-30 16:16:41 +00006910 mch_set_winpos(x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006911# endif
6912# ifdef HAVE_TGETENT
6913 if (*T_CWP)
6914 term_set_winpos(x, y);
6915# endif
6916 }
6917}
6918#endif
6919
6920/*
6921 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
6922 */
6923 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006924ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006925{
6926 oparg_T oa;
6927
6928 clear_oparg(&oa);
6929 oa.regname = eap->regname;
6930 oa.start.lnum = eap->line1;
6931 oa.end.lnum = eap->line2;
6932 oa.line_count = eap->line2 - eap->line1 + 1;
6933 oa.motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006934 virtual_op = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006935 if (eap->cmdidx != CMD_yank) // position cursor for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00006936 {
6937 setpcmark();
6938 curwin->w_cursor.lnum = eap->line1;
6939 beginline(BL_SOL | BL_FIX);
6940 }
6941
Bram Moolenaard07c6e12013-11-21 14:21:40 +01006942 if (VIsual_active)
6943 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01006944
Bram Moolenaar071d4272004-06-13 20:20:40 +00006945 switch (eap->cmdidx)
6946 {
6947 case CMD_delete:
6948 oa.op_type = OP_DELETE;
6949 op_delete(&oa);
6950 break;
6951
6952 case CMD_yank:
6953 oa.op_type = OP_YANK;
6954 (void)op_yank(&oa, FALSE, TRUE);
6955 break;
6956
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006957 default: // CMD_rshift or CMD_lshift
Bram Moolenaar6e707362013-06-14 19:15:58 +02006958 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00006959#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02006960 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
6961#else
6962 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00006963#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02006964 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006965 oa.op_type = OP_RSHIFT;
6966 else
6967 oa.op_type = OP_LSHIFT;
6968 op_shift(&oa, FALSE, eap->amount);
6969 break;
6970 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006971 virtual_op = MAYBE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006972 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006973}
6974
6975/*
6976 * ":put".
6977 */
6978 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006979ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006980{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006981 // ":0put" works like ":1put!".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006982 if (eap->line2 == 0)
6983 {
6984 eap->line2 = 1;
6985 eap->forceit = TRUE;
6986 }
6987 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006988 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
6989 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006990}
6991
6992/*
6993 * Handle ":copy" and ":move".
6994 */
6995 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006996ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006997{
6998 long n;
6999
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02007000 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE, FALSE, 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007001 if (eap->arg == NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00007002 {
7003 eap->nextcmd = NULL;
7004 return;
7005 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00007006 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007007
7008 /*
7009 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
7010 */
7011 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
7012 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02007013 emsg(_(e_invrange));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007014 return;
7015 }
7016
7017 if (eap->cmdidx == CMD_move)
7018 {
7019 if (do_move(eap->line1, eap->line2, n) == FAIL)
7020 return;
7021 }
7022 else
7023 ex_copy(eap->line1, eap->line2, n);
7024 u_clearline();
7025 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007026 ex_may_print(eap);
7027}
7028
7029/*
7030 * Print the current line if flags were given to the Ex command.
7031 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02007032 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007033ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007034{
7035 if (eap->flags != 0)
7036 {
7037 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
7038 (eap->flags & EXFLAG_LIST));
7039 ex_no_reprint = TRUE;
7040 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007041}
7042
7043/*
7044 * ":smagic" and ":snomagic".
7045 */
7046 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007047ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007048{
7049 int magic_save = p_magic;
7050
7051 p_magic = (eap->cmdidx == CMD_smagic);
7052 do_sub(eap);
7053 p_magic = magic_save;
7054}
7055
7056/*
7057 * ":join".
7058 */
7059 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007060ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007061{
7062 curwin->w_cursor.lnum = eap->line1;
7063 if (eap->line1 == eap->line2)
7064 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007065 if (eap->addr_count >= 2) // :2,2join does nothing
Bram Moolenaar071d4272004-06-13 20:20:40 +00007066 return;
7067 if (eap->line2 == curbuf->b_ml.ml_line_count)
7068 {
7069 beep_flush();
7070 return;
7071 }
7072 ++eap->line2;
7073 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02007074 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007075 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007076 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007077}
7078
7079/*
7080 * ":[addr]@r" or ":[addr]*r": execute register
7081 */
7082 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007083ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007084{
7085 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00007086 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007087
7088 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4930a762016-09-11 14:39:53 +02007089 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007090
7091#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007092 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00007093#endif
7094
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007095 // get the register name. No name means to use the previous one
Bram Moolenaar071d4272004-06-13 20:20:40 +00007096 c = *eap->arg;
7097 if (c == NUL || (c == '*' && *eap->cmd == '*'))
7098 c = '@';
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007099 // Put the register in the typeahead buffer with the "silent" flag.
Bram Moolenaard333d1e2006-11-07 17:43:47 +00007100 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
7101 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007102 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007103 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00007104 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007105 else
7106 {
7107 int save_efr = exec_from_reg;
7108
7109 exec_from_reg = TRUE;
7110
7111 /*
7112 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00007113 * Continue until the stuff buffer is empty and all added characters
7114 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007115 */
Bram Moolenaar60462872009-11-03 11:40:19 +00007116 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007117 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
7118
7119 exec_from_reg = save_efr;
7120 }
7121}
7122
7123/*
7124 * ":!".
7125 */
7126 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007127ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007128{
7129 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
7130}
7131
7132/*
7133 * ":undo".
7134 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007135 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01007136ex_undo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007137{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007138 if (eap->addr_count == 1) // :undo 123
Bram Moolenaar730cde92010-06-27 05:18:54 +02007139 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00007140 else
7141 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007142}
7143
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007144#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02007145 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007146ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007147{
7148 char_u hash[UNDO_HASH_SIZE];
7149
7150 u_compute_hash(hash);
7151 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
7152}
7153
Bram Moolenaar6df6f472010-07-18 18:04:50 +02007154 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007155ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007156{
7157 char_u hash[UNDO_HASH_SIZE];
7158
7159 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02007160 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007161}
7162#endif
7163
Bram Moolenaar071d4272004-06-13 20:20:40 +00007164/*
7165 * ":redo".
7166 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007167 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007168ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007169{
7170 u_redo(1);
7171}
7172
7173/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007174 * ":earlier" and ":later".
7175 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007176 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007177ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007178{
7179 long count = 0;
7180 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02007181 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007182 char_u *p = eap->arg;
7183
7184 if (*p == NUL)
7185 count = 1;
7186 else if (isdigit(*p))
7187 {
7188 count = getdigits(&p);
7189 switch (*p)
7190 {
7191 case 's': ++p; sec = TRUE; break;
7192 case 'm': ++p; sec = TRUE; count *= 60; break;
7193 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02007194 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
7195 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007196 }
7197 }
7198
7199 if (*p != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007200 semsg(_(e_invarg2), eap->arg);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007201 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02007202 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
7203 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007204}
7205
7206/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007207 * ":redir": start/stop redirection.
7208 */
7209 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007210ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007211{
7212 char *mode;
7213 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00007214 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007215
Bram Moolenaarba768492016-07-08 15:32:54 +02007216#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02007217 if (redir_execute)
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007218 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007219 emsg(_("E930: Cannot use :redir inside execute()"));
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007220 return;
7221 }
Bram Moolenaarba768492016-07-08 15:32:54 +02007222#endif
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007223
Bram Moolenaar071d4272004-06-13 20:20:40 +00007224 if (STRICMP(eap->arg, "END") == 0)
7225 close_redir();
7226 else
7227 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007228 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007229 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007230 ++arg;
7231 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007232 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007233 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007234 mode = "a";
7235 }
7236 else
7237 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00007238 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007239
7240 close_redir();
7241
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007242 // Expand environment variables and "~/".
Bram Moolenaarca472992005-01-21 11:46:23 +00007243 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007244 if (fname == NULL)
7245 return;
7246#ifdef FEAT_BROWSE
7247 if (cmdmod.browse)
7248 {
7249 char_u *browseFile;
7250
Bram Moolenaar7171abe2004-10-11 10:06:20 +00007251 browseFile = do_browse(BROWSE_SAVE,
7252 (char_u *)_("Save Redirection"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +02007253 fname, NULL, NULL,
7254 (char_u *)_(BROWSE_FILTER_ALL_FILES), curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007255 if (browseFile == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007256 return; // operation cancelled
Bram Moolenaar071d4272004-06-13 20:20:40 +00007257 vim_free(fname);
7258 fname = browseFile;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007259 eap->forceit = TRUE; // since dialog already asked
Bram Moolenaar071d4272004-06-13 20:20:40 +00007260 }
7261#endif
7262
7263 redir_fd = open_exfile(fname, eap->forceit, mode);
7264 vim_free(fname);
7265 }
7266#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00007267 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007268 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007269 // redirect to a register a-z (resp. A-Z for appending)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007270 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00007271 ++arg;
7272 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007273# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00007274 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00007275 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00007276# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00007277 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007278 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007279 redir_reg = *arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007280 if (*arg == '>' && arg[1] == '>') // append
Bram Moolenaard9d30582005-05-18 22:10:28 +00007281 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00007282 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007283 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007284 // Can use both "@a" and "@a>".
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00007285 if (*arg == '>')
7286 arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007287 // Make register empty when not using @A-@Z and the
7288 // command is valid.
Bram Moolenaarc188b882007-10-19 14:20:54 +00007289 if (*arg == NUL && !isupper(redir_reg))
7290 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007291 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00007292 }
7293 if (*arg != NUL)
7294 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007295 redir_reg = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007296 semsg(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007297 }
7298 }
7299 else if (*arg == '=' && arg[1] == '>')
7300 {
7301 int append;
7302
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007303 // redirect to a variable
Bram Moolenaardf177f62005-02-22 08:39:57 +00007304 close_redir();
7305 arg += 2;
7306
7307 if (*arg == '>')
7308 {
7309 ++arg;
7310 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007311 }
7312 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00007313 append = FALSE;
7314
7315 if (var_redir_start(skipwhite(arg), append) == OK)
7316 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007317 }
7318#endif
7319
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007320 // TODO: redirect to a buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00007321
Bram Moolenaar071d4272004-06-13 20:20:40 +00007322 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007323 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007324 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00007325
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007326 // Make sure redirection is not off. Can happen for cmdline completion
7327 // that indirectly invokes a command to catch its output.
Bram Moolenaar29b2d262006-09-10 19:07:28 +00007328 if (redir_fd != NULL
7329#ifdef FEAT_EVAL
7330 || redir_reg || redir_vname
7331#endif
7332 )
7333 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007334}
7335
7336/*
7337 * ":redraw": force redraw
7338 */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01007339 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007340ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007341{
7342 int r = RedrawingDisabled;
7343 int p = p_lz;
7344
7345 RedrawingDisabled = 0;
7346 p_lz = FALSE;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01007347 validate_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007348 update_topline();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007349 update_screen(eap->forceit ? CLEAR : VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007350#ifdef FEAT_TITLE
7351 if (need_maketitle)
7352 maketitle();
7353#endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007354#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
7355# ifdef VIMDLL
7356 if (!gui.in_use)
7357# endif
7358 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01007359#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007360 RedrawingDisabled = r;
7361 p_lz = p;
7362
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007363 // Reset msg_didout, so that a message that's there is overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007364 msg_didout = FALSE;
7365 msg_col = 0;
7366
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007367 // No need to wait after an intentional redraw.
Bram Moolenaar56667a52013-07-17 11:54:28 +02007368 need_wait_return = FALSE;
7369
Bram Moolenaar071d4272004-06-13 20:20:40 +00007370 out_flush();
7371}
7372
7373/*
7374 * ":redrawstatus": force redraw of status line(s)
7375 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007376 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007377ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007378{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007379 int r = RedrawingDisabled;
7380 int p = p_lz;
7381
7382 RedrawingDisabled = 0;
7383 p_lz = FALSE;
7384 if (eap->forceit)
7385 status_redraw_all();
7386 else
7387 status_redraw_curbuf();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007388 update_screen(VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007389 RedrawingDisabled = r;
7390 p_lz = p;
7391 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007392}
7393
Bram Moolenaare12bab32019-01-08 22:02:56 +01007394/*
7395 * ":redrawtabline": force redraw of the tabline
7396 */
7397 static void
7398ex_redrawtabline(exarg_T *eap UNUSED)
7399{
7400 int r = RedrawingDisabled;
7401 int p = p_lz;
7402
7403 RedrawingDisabled = 0;
7404 p_lz = FALSE;
7405
7406 draw_tabline();
7407
7408 RedrawingDisabled = r;
7409 p_lz = p;
7410 out_flush();
7411}
7412
Bram Moolenaar071d4272004-06-13 20:20:40 +00007413 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007414close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007415{
7416 if (redir_fd != NULL)
7417 {
7418 fclose(redir_fd);
7419 redir_fd = NULL;
7420 }
7421#ifdef FEAT_EVAL
7422 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007423 if (redir_vname)
7424 {
7425 var_redir_stop();
7426 redir_vname = 0;
7427 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007428#endif
7429}
7430
Bram Moolenaarf96ae0b2019-07-28 15:21:55 +02007431#if (defined(FEAT_SESSION) || defined(FEAT_EVAL)) || defined(PROTO)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007432 int
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02007433vim_mkdir_emsg(char_u *name, int prot UNUSED)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007434{
7435 if (vim_mkdir(name, prot) != 0)
7436 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007437 semsg(_("E739: Cannot create directory: %s"), name);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007438 return FAIL;
7439 }
7440 return OK;
7441}
7442#endif
7443
Bram Moolenaar071d4272004-06-13 20:20:40 +00007444/*
7445 * Open a file for writing for an Ex command, with some checks.
7446 * Return file descriptor, or NULL on failure.
7447 */
7448 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007449open_exfile(
7450 char_u *fname,
7451 int forceit,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007452 char *mode) // "w" for create new file or "a" for append
Bram Moolenaar071d4272004-06-13 20:20:40 +00007453{
7454 FILE *fd;
7455
7456#ifdef UNIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007457 // with Unix it is possible to open a directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007458 if (mch_isdir(fname))
7459 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007460 semsg(_(e_isadir2), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007461 return NULL;
7462 }
7463#endif
7464 if (!forceit && *mode != 'a' && vim_fexists(fname))
7465 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007466 semsg(_("E189: \"%s\" exists (add ! to override)"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007467 return NULL;
7468 }
7469
7470 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007471 semsg(_("E190: Cannot open \"%s\" for writing"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007472
7473 return fd;
7474}
7475
7476/*
7477 * ":mark" and ":k".
7478 */
7479 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007480ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007481{
7482 pos_T pos;
7483
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007484 if (*eap->arg == NUL) // No argument?
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007485 emsg(_(e_argreq));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007486 else if (eap->arg[1] != NUL) // more than one character?
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007487 emsg(_(e_trailing));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007488 else
7489 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007490 pos = curwin->w_cursor; // save curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00007491 curwin->w_cursor.lnum = eap->line2;
7492 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007493 if (setmark(*eap->arg) == FAIL) // set mark
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007494 emsg(_("E191: Argument must be a letter or forward/backward quote"));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007495 curwin->w_cursor = pos; // restore curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00007496 }
7497}
7498
7499/*
7500 * Update w_topline, w_leftcol and the cursor position.
7501 */
7502 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007503update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007504{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007505 check_cursor(); // put cursor on valid line
Bram Moolenaar071d4272004-06-13 20:20:40 +00007506 update_topline();
7507 if (!curwin->w_p_wrap)
7508 validate_cursor();
7509 update_curswant();
7510}
7511
Bram Moolenaar071d4272004-06-13 20:20:40 +00007512/*
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007513 * Save the current State and go to Normal mode.
7514 * Return TRUE if the typeahead could be saved.
7515 */
7516 int
7517save_current_state(save_state_T *sst)
7518{
7519 sst->save_msg_scroll = msg_scroll;
7520 sst->save_restart_edit = restart_edit;
7521 sst->save_msg_didout = msg_didout;
7522 sst->save_State = State;
7523 sst->save_insertmode = p_im;
7524 sst->save_finish_op = finish_op;
7525 sst->save_opcount = opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01007526 sst->save_reg_executing = reg_executing;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007527
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007528 msg_scroll = FALSE; // no msg scrolling in Normal mode
7529 restart_edit = 0; // don't go to Insert mode
7530 p_im = FALSE; // don't use 'insertmode'
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007531
7532 /*
7533 * Save the current typeahead. This is required to allow using ":normal"
7534 * from an event handler and makes sure we don't hang when the argument
7535 * ends with half a command.
7536 */
7537 save_typeahead(&sst->tabuf);
7538 return sst->tabuf.typebuf_valid;
7539}
7540
7541 void
7542restore_current_state(save_state_T *sst)
7543{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007544 // Restore the previous typeahead.
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007545 restore_typeahead(&sst->tabuf);
7546
7547 msg_scroll = sst->save_msg_scroll;
7548 restart_edit = sst->save_restart_edit;
7549 p_im = sst->save_insertmode;
7550 finish_op = sst->save_finish_op;
7551 opcount = sst->save_opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01007552 reg_executing = sst->save_reg_executing;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007553 msg_didout |= sst->save_msg_didout; // don't reset msg_didout now
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007554
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007555 // Restore the state (needed when called from a function executed for
7556 // 'indentexpr'). Update the mouse and cursor, they may have changed.
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007557 State = sst->save_State;
7558#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007559 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007560#endif
7561}
7562
7563/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007564 * ":normal[!] {commands}": Execute normal mode commands.
7565 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01007566 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007567ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007568{
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007569 save_state_T save_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007570 char_u *arg = NULL;
7571 int l;
7572 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007573
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00007574 if (ex_normal_lock > 0)
7575 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007576 emsg(_(e_secure));
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00007577 return;
7578 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007579 if (ex_normal_busy >= p_mmd)
7580 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007581 emsg(_("E192: Recursive use of :normal too deep"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007582 return;
7583 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007584
Bram Moolenaar071d4272004-06-13 20:20:40 +00007585 /*
7586 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
7587 * this for the K_SPECIAL leading byte, otherwise special keys will not
7588 * work.
7589 */
7590 if (has_mbyte)
7591 {
7592 int len = 0;
7593
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007594 // Count the number of characters to be escaped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007595 for (p = eap->arg; *p != NUL; ++p)
7596 {
Bram Moolenaar13505972019-01-24 15:04:48 +01007597#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007598 if (*p == CSI) // leadbyte CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007599 len += 2;
Bram Moolenaar13505972019-01-24 15:04:48 +01007600#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007601 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007602 if (*++p == K_SPECIAL // trailbyte K_SPECIAL or CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01007603#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007604 || *p == CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01007605#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007606 )
7607 len += 2;
7608 }
7609 if (len > 0)
7610 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02007611 arg = alloc(STRLEN(eap->arg) + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007612 if (arg != NULL)
7613 {
7614 len = 0;
7615 for (p = eap->arg; *p != NUL; ++p)
7616 {
7617 arg[len++] = *p;
Bram Moolenaar13505972019-01-24 15:04:48 +01007618#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007619 if (*p == CSI)
7620 {
7621 arg[len++] = KS_EXTRA;
7622 arg[len++] = (int)KE_CSI;
7623 }
Bram Moolenaar13505972019-01-24 15:04:48 +01007624#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007625 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007626 {
7627 arg[len++] = *++p;
7628 if (*p == K_SPECIAL)
7629 {
7630 arg[len++] = KS_SPECIAL;
7631 arg[len++] = KE_FILLER;
7632 }
Bram Moolenaar13505972019-01-24 15:04:48 +01007633#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007634 else if (*p == CSI)
7635 {
7636 arg[len++] = KS_EXTRA;
7637 arg[len++] = (int)KE_CSI;
7638 }
Bram Moolenaar13505972019-01-24 15:04:48 +01007639#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007640 }
7641 arg[len] = NUL;
7642 }
7643 }
7644 }
7645 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007646
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007647 ++ex_normal_busy;
7648 if (save_current_state(&save_state))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007649 {
7650 /*
7651 * Repeat the :normal command for each line in the range. When no
7652 * range given, execute it just once, without positioning the cursor
7653 * first.
7654 */
7655 do
7656 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007657 if (eap->addr_count != 0)
7658 {
7659 curwin->w_cursor.lnum = eap->line1++;
7660 curwin->w_cursor.col = 0;
Bram Moolenaard5d37532017-03-27 23:02:07 +02007661 check_cursor_moved(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007662 }
7663
Bram Moolenaar13505972019-01-24 15:04:48 +01007664 exec_normal_cmd(arg != NULL
7665 ? arg
7666 : eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007667 }
7668 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
7669 }
7670
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007671 // Might not return to the main loop when in an event handler.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007672 update_topline_cursor();
7673
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007674 restore_current_state(&save_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007675 --ex_normal_busy;
Bram Moolenaareda73602014-11-05 09:53:23 +01007676 setmouse();
Bram Moolenaareda73602014-11-05 09:53:23 +01007677#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007678 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaareda73602014-11-05 09:53:23 +01007679#endif
7680
Bram Moolenaar071d4272004-06-13 20:20:40 +00007681 vim_free(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007682}
7683
7684/*
Bram Moolenaar64969662005-12-14 21:59:55 +00007685 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007686 */
7687 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007688ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007689{
Bram Moolenaarfd371682005-01-14 21:42:54 +00007690 if (eap->forceit)
7691 {
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007692 // cursor line can be zero on startup
Bram Moolenaar09ca9322017-09-26 17:40:45 +02007693 if (!curwin->w_cursor.lnum)
7694 curwin->w_cursor.lnum = 1;
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007695 set_cursor_for_append_to_line();
Bram Moolenaarfd371682005-01-14 21:42:54 +00007696 }
7697
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007698 // Ignore the command when already in Insert mode. Inserting an
7699 // expression register that invokes a function can do this.
Bram Moolenaara40c5002005-01-09 21:16:21 +00007700 if (State & INSERT)
7701 return;
7702
Bram Moolenaar64969662005-12-14 21:59:55 +00007703 if (eap->cmdidx == CMD_startinsert)
7704 restart_edit = 'a';
7705 else if (eap->cmdidx == CMD_startreplace)
7706 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00007707 else
Bram Moolenaar64969662005-12-14 21:59:55 +00007708 restart_edit = 'V';
7709
7710 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007711 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00007712 if (eap->cmdidx == CMD_startinsert)
7713 restart_edit = 'i';
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007714 curwin->w_curswant = 0; // avoid MAXCOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007715 }
7716}
7717
7718/*
7719 * ":stopinsert"
7720 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007721 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007722ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007723{
7724 restart_edit = 0;
7725 stop_insert_mode = TRUE;
Bram Moolenaarfd773e92016-04-02 19:39:16 +02007726 clearmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007727}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007728
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007729/*
7730 * Execute normal mode command "cmd".
7731 * "remap" can be REMAP_NONE or REMAP_YES.
7732 */
7733 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007734exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007735{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007736 // Stuff the argument into the typeahead buffer.
Bram Moolenaar25281632016-01-21 23:32:32 +01007737 ins_typebuf(cmd, remap, 0, TRUE, silent);
Bram Moolenaar586c70c2018-10-02 16:23:58 +02007738 exec_normal(FALSE, FALSE, FALSE);
Bram Moolenaar25281632016-01-21 23:32:32 +01007739}
Bram Moolenaar25281632016-01-21 23:32:32 +01007740
Bram Moolenaar25281632016-01-21 23:32:32 +01007741/*
7742 * Execute normal_cmd() until there is no typeahead left.
Bram Moolenaar586c70c2018-10-02 16:23:58 +02007743 * When "use_vpeekc" is TRUE use vpeekc() to check for available chars.
Bram Moolenaar25281632016-01-21 23:32:32 +01007744 */
7745 void
Bram Moolenaar586c70c2018-10-02 16:23:58 +02007746exec_normal(int was_typed, int use_vpeekc, int may_use_terminal_loop UNUSED)
Bram Moolenaar25281632016-01-21 23:32:32 +01007747{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007748 oparg_T oa;
Bram Moolenaar905dd902019-04-07 14:21:47 +02007749 int c;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007750
Bram Moolenaar905dd902019-04-07 14:21:47 +02007751 // When calling vpeekc() from feedkeys() it will return Ctrl_C when there
7752 // is nothing to get, so also check for Ctrl_C.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007753 clear_oparg(&oa);
7754 finish_op = FALSE;
Bram Moolenaar586c70c2018-10-02 16:23:58 +02007755 while ((!stuff_empty()
7756 || ((was_typed || !typebuf_typed()) && typebuf.tb_len > 0)
Bram Moolenaar905dd902019-04-07 14:21:47 +02007757 || (use_vpeekc && (c = vpeekc()) != NUL && c != Ctrl_C))
Bram Moolenaar586c70c2018-10-02 16:23:58 +02007758 && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007759 {
7760 update_topline_cursor();
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02007761#ifdef FEAT_TERMINAL
Bram Moolenaar655a82a2018-05-08 22:01:07 +02007762 if (may_use_terminal_loop && term_use_loop()
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02007763 && oa.op_type == OP_NOP && oa.regname == NUL
7764 && !VIsual_active)
7765 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007766 // If terminal_loop() returns OK we got a key that is handled
7767 // in Normal model. With FAIL we first need to position the
7768 // cursor and the screen needs to be redrawn.
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02007769 if (terminal_loop(TRUE) == OK)
7770 normal_cmd(&oa, TRUE);
7771 }
7772 else
7773#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007774 // execute a Normal mode cmd
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02007775 normal_cmd(&oa, TRUE);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007776 }
7777}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007778
Bram Moolenaar071d4272004-06-13 20:20:40 +00007779#ifdef FEAT_FIND_ID
7780 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007781ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007782{
7783 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
7784 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
7785 (linenr_T)1, (linenr_T)MAXLNUM);
7786}
7787
Bram Moolenaar4033c552017-09-16 20:54:51 +02007788#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007789/*
7790 * ":psearch"
7791 */
7792 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007793ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007794{
7795 g_do_tagpreview = p_pvh;
7796 ex_findpat(eap);
7797 g_do_tagpreview = 0;
7798}
7799#endif
7800
7801 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007802ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007803{
7804 int whole = TRUE;
7805 long n;
7806 char_u *p;
7807 int action;
7808
7809 switch (cmdnames[eap->cmdidx].cmd_name[2])
7810 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007811 case 'e': // ":psearch", ":isearch" and ":dsearch"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007812 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
7813 action = ACTION_GOTO;
7814 else
7815 action = ACTION_SHOW;
7816 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007817 case 'i': // ":ilist" and ":dlist"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007818 action = ACTION_SHOW_ALL;
7819 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007820 case 'u': // ":ijump" and ":djump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007821 action = ACTION_GOTO;
7822 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007823 default: // ":isplit" and ":dsplit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007824 action = ACTION_SPLIT;
7825 break;
7826 }
7827
7828 n = 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007829 if (vim_isdigit(*eap->arg)) // get count
Bram Moolenaar071d4272004-06-13 20:20:40 +00007830 {
7831 n = getdigits(&eap->arg);
7832 eap->arg = skipwhite(eap->arg);
7833 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007834 if (*eap->arg == '/') // Match regexp, not just whole words
Bram Moolenaar071d4272004-06-13 20:20:40 +00007835 {
7836 whole = FALSE;
7837 ++eap->arg;
7838 p = skip_regexp(eap->arg, '/', p_magic, NULL);
7839 if (*p)
7840 {
7841 *p++ = NUL;
7842 p = skipwhite(p);
7843
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007844 // Check for trailing illegal characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00007845 if (!ends_excmd(*p))
7846 eap->errmsg = e_trailing;
7847 else
7848 eap->nextcmd = check_nextcmd(p);
7849 }
7850 }
7851 if (!eap->skip)
7852 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
7853 whole, !eap->forceit,
7854 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
7855 n, action, eap->line1, eap->line2);
7856}
7857#endif
7858
Bram Moolenaar071d4272004-06-13 20:20:40 +00007859
Bram Moolenaar4033c552017-09-16 20:54:51 +02007860#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00007861/*
7862 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
7863 */
7864 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007865ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007866{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007867 g_do_tagpreview = p_pvh; // will be reset to 0 in ex_tag_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00007868 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
7869}
7870
7871/*
7872 * ":pedit"
7873 */
7874 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007875ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007876{
7877 win_T *curwin_save = curwin;
7878
Bram Moolenaar026587b2019-08-17 15:08:00 +02007879 // Open the preview window or popup and make it the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007880 g_do_tagpreview = p_pvh;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02007881 prepare_tagpreview(TRUE, TRUE, FALSE);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02007882
Bram Moolenaar026587b2019-08-17 15:08:00 +02007883 // Edit the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007884 do_exedit(eap, NULL);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02007885
Bram Moolenaar071d4272004-06-13 20:20:40 +00007886 if (curwin != curwin_save && win_valid(curwin_save))
7887 {
Bram Moolenaar026587b2019-08-17 15:08:00 +02007888 // Return cursor to where we were
Bram Moolenaar071d4272004-06-13 20:20:40 +00007889 validate_cursor();
7890 redraw_later(VALID);
7891 win_enter(curwin_save, TRUE);
7892 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01007893# ifdef FEAT_PROP_POPUP
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02007894 else if (WIN_IS_POPUP(curwin))
7895 {
7896 // can't keep focus in popup window
7897 win_enter(firstwin, TRUE);
7898 }
7899# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007900 g_do_tagpreview = 0;
7901}
Bram Moolenaar4033c552017-09-16 20:54:51 +02007902#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007903
7904/*
7905 * ":stag", ":stselect" and ":stjump".
7906 */
7907 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007908ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007909{
7910 postponed_split = -1;
7911 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00007912 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007913 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
7914 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00007915 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007916}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007917
7918/*
7919 * ":tag", ":tselect", ":tjump", ":tnext", etc.
7920 */
7921 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007922ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007923{
7924 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
7925}
7926
7927 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007928ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007929{
7930 int cmd;
7931
7932 switch (name[1])
7933 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007934 case 'j': cmd = DT_JUMP; // ":tjump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007935 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007936 case 's': cmd = DT_SELECT; // ":tselect"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007937 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007938 case 'p': cmd = DT_PREV; // ":tprevious"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007939 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007940 case 'N': cmd = DT_PREV; // ":tNext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007941 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007942 case 'n': cmd = DT_NEXT; // ":tnext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007943 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007944 case 'o': cmd = DT_POP; // ":pop"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007945 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007946 case 'f': // ":tfirst"
7947 case 'r': cmd = DT_FIRST; // ":trewind"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007948 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007949 case 'l': cmd = DT_LAST; // ":tlast"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007950 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007951 default: // ":tag"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007952#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +00007953 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007954 {
Bram Moolenaard4db7712016-11-12 19:16:46 +01007955 ex_cstag(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007956 return;
7957 }
7958#endif
7959 cmd = DT_TAG;
7960 break;
7961 }
7962
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00007963 if (name[0] == 'l')
7964 {
7965#ifndef FEAT_QUICKFIX
7966 ex_ni(eap);
7967 return;
7968#else
7969 cmd = DT_LTAG;
7970#endif
7971 }
7972
Bram Moolenaar071d4272004-06-13 20:20:40 +00007973 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
7974 eap->forceit, TRUE);
7975}
7976
7977/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007978 * Check "str" for starting with a special cmdline variable.
7979 * If found return one of the SPEC_ values and set "*usedlen" to the length of
7980 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
7981 */
7982 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007983find_cmdline_var(char_u *src, int *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007984{
7985 int len;
7986 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +00007987 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007988 "%",
7989#define SPEC_PERC 0
7990 "#",
Bram Moolenaar65f08472017-09-10 18:16:20 +02007991#define SPEC_HASH (SPEC_PERC + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007992 "<cword>", // cursor word
Bram Moolenaar65f08472017-09-10 18:16:20 +02007993#define SPEC_CWORD (SPEC_HASH + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007994 "<cWORD>", // cursor WORD
Bram Moolenaar65f08472017-09-10 18:16:20 +02007995#define SPEC_CCWORD (SPEC_CWORD + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007996 "<cexpr>", // expr under cursor
Bram Moolenaar65f08472017-09-10 18:16:20 +02007997#define SPEC_CEXPR (SPEC_CCWORD + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007998 "<cfile>", // cursor path name
Bram Moolenaar65f08472017-09-10 18:16:20 +02007999#define SPEC_CFILE (SPEC_CEXPR + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008000 "<sfile>", // ":so" file name
Bram Moolenaar65f08472017-09-10 18:16:20 +02008001#define SPEC_SFILE (SPEC_CFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008002 "<slnum>", // ":so" file line number
Bram Moolenaar65f08472017-09-10 18:16:20 +02008003#define SPEC_SLNUM (SPEC_SFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008004 "<afile>", // autocommand file name
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008005#define SPEC_AFILE (SPEC_SLNUM + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008006 "<abuf>", // autocommand buffer number
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008007#define SPEC_ABUF (SPEC_AFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008008 "<amatch>", // autocommand match name
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008009#define SPEC_AMATCH (SPEC_ABUF + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008010 "<sflnum>", // script file line number
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008011#define SPEC_SFLNUM (SPEC_AMATCH + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008012#ifdef FEAT_CLIENTSERVER
8013 "<client>"
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008014# define SPEC_CLIENT (SPEC_SFLNUM + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008015#endif
8016 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008017
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008018 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008019 {
8020 len = (int)STRLEN(spec_str[i]);
8021 if (STRNCMP(src, spec_str[i], len) == 0)
8022 {
8023 *usedlen = len;
8024 return i;
8025 }
8026 }
8027 return -1;
8028}
8029
8030/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008031 * Evaluate cmdline variables.
8032 *
8033 * change '%' to curbuf->b_ffname
8034 * '#' to curwin->w_altfile
8035 * '<cword>' to word under the cursor
8036 * '<cWORD>' to WORD under the cursor
Bram Moolenaar8071cb22019-07-12 17:58:01 +02008037 * '<cexpr>' to C-expression under the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00008038 * '<cfile>' to path name under the cursor
8039 * '<sfile>' to sourced file name
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008040 * '<slnum>' to sourced file line number
Bram Moolenaar071d4272004-06-13 20:20:40 +00008041 * '<afile>' to file name for autocommand
8042 * '<abuf>' to buffer number for autocommand
8043 * '<amatch>' to matching name for autocommand
8044 *
8045 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
8046 * "" for error without a message) and NULL is returned.
8047 * Returns an allocated string if a valid match was found.
8048 * Returns NULL if no match was found. "usedlen" then still contains the
8049 * number of characters to skip.
8050 */
8051 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008052eval_vars(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008053 char_u *src, // pointer into commandline
8054 char_u *srcstart, // beginning of valid memory for src
8055 int *usedlen, // characters after src that are used
8056 linenr_T *lnump, // line number for :e command, or NULL
8057 char **errormsg, // pointer to error message
8058 int *escaped) // return value has escaped white space (can
8059 // be NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008060{
8061 int i;
8062 char_u *s;
8063 char_u *result;
8064 char_u *resultbuf = NULL;
8065 int resultlen;
8066 buf_T *buf;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008067 int valid = VALID_HEAD + VALID_PATH; // assume valid result
Bram Moolenaar071d4272004-06-13 20:20:40 +00008068 int spec_idx;
Bram Moolenaarfd249462018-07-28 16:14:30 +02008069 int tilde_file = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008070 int skip_mod = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008071 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008072
8073 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +00008074 if (escaped != NULL)
8075 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008076
8077 /*
8078 * Check if there is something to do.
8079 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008080 spec_idx = find_cmdline_var(src, usedlen);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008081 if (spec_idx < 0) // no match
Bram Moolenaar071d4272004-06-13 20:20:40 +00008082 {
8083 *usedlen = 1;
8084 return NULL;
8085 }
8086
8087 /*
8088 * Skip when preceded with a backslash "\%" and "\#".
8089 * Note: In "\\%" the % is also not recognized!
8090 */
8091 if (src > srcstart && src[-1] == '\\')
8092 {
8093 *usedlen = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008094 STRMOVE(src - 1, src); // remove backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00008095 return NULL;
8096 }
8097
8098 /*
8099 * word or WORD under cursor
8100 */
Bram Moolenaar65f08472017-09-10 18:16:20 +02008101 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD
8102 || spec_idx == SPEC_CEXPR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008103 {
Bram Moolenaar65f08472017-09-10 18:16:20 +02008104 resultlen = find_ident_under_cursor(&result,
8105 spec_idx == SPEC_CWORD ? (FIND_IDENT | FIND_STRING)
8106 : spec_idx == SPEC_CEXPR ? (FIND_IDENT | FIND_STRING | FIND_EVAL)
8107 : FIND_STRING);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008108 if (resultlen == 0)
8109 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008110 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008111 return NULL;
8112 }
8113 }
8114
8115 /*
8116 * '#': Alternate file name
8117 * '%': Current file name
8118 * File name under the cursor
8119 * File name for autocommand
8120 * and following modifiers
8121 */
8122 else
8123 {
8124 switch (spec_idx)
8125 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008126 case SPEC_PERC: // '%': current file
Bram Moolenaar071d4272004-06-13 20:20:40 +00008127 if (curbuf->b_fname == NULL)
8128 {
8129 result = (char_u *)"";
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008130 valid = 0; // Must have ":p:h" to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00008131 }
8132 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008133 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008134 result = curbuf->b_fname;
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008135 tilde_file = STRCMP(result, "~") == 0;
8136 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008137 break;
8138
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008139 case SPEC_HASH: // '#' or "#99": alternate file
8140 if (src[1] == '#') // "##": the argument list
Bram Moolenaar071d4272004-06-13 20:20:40 +00008141 {
8142 result = arg_all();
8143 resultbuf = result;
8144 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +00008145 if (escaped != NULL)
8146 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008147 skip_mod = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008148 break;
8149 }
8150 s = src + 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008151 if (*s == '<') // "#<99" uses v:oldfiles
Bram Moolenaard812df62008-11-09 12:46:09 +00008152 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008153 i = (int)getdigits(&s);
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008154 if (s == src + 2 && src[1] == '-')
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008155 // just a minus sign, don't skip over it
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008156 s--;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008157 *usedlen = (int)(s - src); // length of what we expand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008158
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008159 if (src[1] == '<' && i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008160 {
Bram Moolenaard812df62008-11-09 12:46:09 +00008161 if (*usedlen < 2)
8162 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008163 // Should we give an error message for #<text?
Bram Moolenaard812df62008-11-09 12:46:09 +00008164 *usedlen = 1;
8165 return NULL;
8166 }
8167#ifdef FEAT_EVAL
8168 result = list_find_str(get_vim_var_list(VV_OLDFILES),
8169 (long)i);
8170 if (result == NULL)
8171 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008172 *errormsg = "";
Bram Moolenaard812df62008-11-09 12:46:09 +00008173 return NULL;
8174 }
8175#else
Bram Moolenaar8e481e82019-01-15 20:07:48 +01008176 *errormsg = _("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008177 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00008178#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008179 }
8180 else
Bram Moolenaard812df62008-11-09 12:46:09 +00008181 {
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008182 if (i == 0 && src[1] == '<' && *usedlen > 1)
8183 *usedlen = 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00008184 buf = buflist_findnr(i);
8185 if (buf == NULL)
8186 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008187 *errormsg = _("E194: No alternate file name to substitute for '#'");
Bram Moolenaard812df62008-11-09 12:46:09 +00008188 return NULL;
8189 }
8190 if (lnump != NULL)
8191 *lnump = ECMD_LAST;
8192 if (buf->b_fname == NULL)
8193 {
8194 result = (char_u *)"";
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008195 valid = 0; // Must have ":p:h" to be valid
Bram Moolenaard812df62008-11-09 12:46:09 +00008196 }
8197 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008198 {
Bram Moolenaard812df62008-11-09 12:46:09 +00008199 result = buf->b_fname;
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008200 tilde_file = STRCMP(result, "~") == 0;
8201 }
Bram Moolenaard812df62008-11-09 12:46:09 +00008202 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008203 break;
8204
8205#ifdef FEAT_SEARCHPATH
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008206 case SPEC_CFILE: // file name under cursor
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008207 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008208 if (result == NULL)
8209 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008210 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008211 return NULL;
8212 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008213 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00008214 break;
8215#endif
8216
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008217 case SPEC_AFILE: // file name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008218 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +00008219 if (result != NULL && !autocmd_fname_full)
8220 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008221 // Still need to turn the fname into a full path. It is
8222 // postponed to avoid a delay when <afile> is not used.
Bram Moolenaarf6dad432008-09-18 19:29:58 +00008223 autocmd_fname_full = TRUE;
8224 result = FullName_save(autocmd_fname, FALSE);
8225 vim_free(autocmd_fname);
8226 autocmd_fname = result;
8227 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008228 if (result == NULL)
8229 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008230 *errormsg = _("E495: no autocommand file name to substitute for \"<afile>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008231 return NULL;
8232 }
Bram Moolenaara0174af2008-01-02 20:08:25 +00008233 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008234 break;
8235
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008236 case SPEC_ABUF: // buffer number for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008237 if (autocmd_bufnr <= 0)
8238 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008239 *errormsg = _("E496: no autocommand buffer number to substitute for \"<abuf>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008240 return NULL;
8241 }
8242 sprintf((char *)strbuf, "%d", autocmd_bufnr);
8243 result = strbuf;
8244 break;
8245
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008246 case SPEC_AMATCH: // match name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008247 result = autocmd_match;
8248 if (result == NULL)
8249 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008250 *errormsg = _("E497: no autocommand match name to substitute for \"<amatch>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008251 return NULL;
8252 }
8253 break;
8254
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008255 case SPEC_SFILE: // file name for ":so" command
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008256 result = estack_sfile();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008257 if (result == NULL)
8258 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008259 *errormsg = _("E498: no :source file name to substitute for \"<sfile>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008260 return NULL;
8261 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008262 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00008263 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008264
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008265 case SPEC_SLNUM: // line in file for ":so" command
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008266 if (SOURCING_NAME == NULL || SOURCING_LNUM == 0)
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008267 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008268 *errormsg = _("E842: no line number to use for \"<slnum>\"");
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008269 return NULL;
8270 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008271 sprintf((char *)strbuf, "%ld", SOURCING_LNUM);
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008272 result = strbuf;
8273 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008274
8275#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008276 case SPEC_SFLNUM: // line in script file
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008277 if (current_sctx.sc_lnum + SOURCING_LNUM == 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008278 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008279 *errormsg = _("E961: no line number to use for \"<sflnum>\"");
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008280 return NULL;
8281 }
8282 sprintf((char *)strbuf, "%ld",
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008283 (long)(current_sctx.sc_lnum + SOURCING_LNUM));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008284 result = strbuf;
8285 break;
8286#endif
8287
8288#ifdef FEAT_CLIENTSERVER
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008289 case SPEC_CLIENT: // Source of last submitted input
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008290 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
8291 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008292 result = strbuf;
8293 break;
8294#endif
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008295
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02008296 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008297 result = (char_u *)""; // avoid gcc warning
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02008298 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008299 }
8300
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008301 resultlen = (int)STRLEN(result); // length of new string
8302 if (src[*usedlen] == '<') // remove the file name extension
Bram Moolenaar071d4272004-06-13 20:20:40 +00008303 {
8304 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008305 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008306 resultlen = (int)(s - result);
8307 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008308 else if (!skip_mod)
8309 {
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008310 valid |= modify_fname(src, tilde_file, usedlen, &result, &resultbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008311 &resultlen);
8312 if (result == NULL)
8313 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008314 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008315 return NULL;
8316 }
8317 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008318 }
8319
8320 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
8321 {
8322 if (valid != VALID_HEAD + VALID_PATH)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008323 // xgettext:no-c-format
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008324 *errormsg = _("E499: Empty file name for '%' or '#', only works with \":p:h\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008325 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008326 *errormsg = _("E500: Evaluates to an empty string");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008327 result = NULL;
8328 }
8329 else
8330 result = vim_strnsave(result, resultlen);
8331 vim_free(resultbuf);
8332 return result;
8333}
8334
8335/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008336 * Expand the <sfile> string in "arg".
8337 *
8338 * Returns an allocated string, or NULL for any error.
8339 */
8340 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008341expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008342{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008343 char *errormsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008344 int len;
8345 char_u *result;
8346 char_u *newres;
8347 char_u *repl;
8348 int srclen;
8349 char_u *p;
8350
8351 result = vim_strsave(arg);
8352 if (result == NULL)
8353 return NULL;
8354
8355 for (p = result; *p; )
8356 {
8357 if (STRNCMP(p, "<sfile>", 7) != 0)
8358 ++p;
8359 else
8360 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008361 // replace "<sfile>" with the sourced file name, and do ":" stuff
Bram Moolenaar63b92542007-03-27 14:57:09 +00008362 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008363 if (errormsg != NULL)
8364 {
8365 if (*errormsg)
8366 emsg(errormsg);
8367 vim_free(result);
8368 return NULL;
8369 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008370 if (repl == NULL) // no match (cannot happen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008371 {
8372 p += srclen;
8373 continue;
8374 }
8375 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
8376 newres = alloc(len);
8377 if (newres == NULL)
8378 {
8379 vim_free(repl);
8380 vim_free(result);
8381 return NULL;
8382 }
8383 mch_memmove(newres, result, (size_t)(p - result));
8384 STRCPY(newres + (p - result), repl);
8385 len = (int)STRLEN(newres);
8386 STRCAT(newres, p + srclen);
8387 vim_free(repl);
8388 vim_free(result);
8389 result = newres;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008390 p = newres + len; // continue after the match
Bram Moolenaar071d4272004-06-13 20:20:40 +00008391 }
8392 }
8393
8394 return result;
8395}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008396
Bram Moolenaar071d4272004-06-13 20:20:40 +00008397#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00008398/*
Bram Moolenaard9462e32011-04-11 21:35:11 +02008399 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +00008400 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +00008401 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008402 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008403dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008404{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008405 if (fname == NULL)
8406 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +02008407 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008408}
8409#endif
8410
8411/*
8412 * ":behave {mswin,xterm}"
8413 */
8414 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008415ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008416{
8417 if (STRCMP(eap->arg, "mswin") == 0)
8418 {
8419 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
8420 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
8421 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
8422 set_option_value((char_u *)"keymodel", 0L,
8423 (char_u *)"startsel,stopsel", 0);
8424 }
8425 else if (STRCMP(eap->arg, "xterm") == 0)
8426 {
8427 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
8428 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
8429 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
8430 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
8431 }
8432 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008433 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008434}
8435
Bram Moolenaar071d4272004-06-13 20:20:40 +00008436static int filetype_detect = FALSE;
8437static int filetype_plugin = FALSE;
8438static int filetype_indent = FALSE;
8439
8440/*
8441 * ":filetype [plugin] [indent] {on,off,detect}"
8442 * on: Load the filetype.vim file to install autocommands for file types.
8443 * off: Load the ftoff.vim file to remove all autocommands for file types.
8444 * plugin on: load filetype.vim and ftplugin.vim
8445 * plugin off: load ftplugof.vim
8446 * indent on: load filetype.vim and indent.vim
8447 * indent off: load indoff.vim
8448 */
8449 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008450ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008451{
8452 char_u *arg = eap->arg;
8453 int plugin = FALSE;
8454 int indent = FALSE;
8455
8456 if (*eap->arg == NUL)
8457 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008458 // Print current status.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008459 smsg("filetype detection:%s plugin:%s indent:%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00008460 filetype_detect ? "ON" : "OFF",
8461 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
8462 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
8463 return;
8464 }
8465
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008466 // Accept "plugin" and "indent" in any order.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008467 for (;;)
8468 {
8469 if (STRNCMP(arg, "plugin", 6) == 0)
8470 {
8471 plugin = TRUE;
8472 arg = skipwhite(arg + 6);
8473 continue;
8474 }
8475 if (STRNCMP(arg, "indent", 6) == 0)
8476 {
8477 indent = TRUE;
8478 arg = skipwhite(arg + 6);
8479 continue;
8480 }
8481 break;
8482 }
8483 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
8484 {
8485 if (*arg == 'o' || !filetype_detect)
8486 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008487 source_runtime((char_u *)FILETYPE_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008488 filetype_detect = TRUE;
8489 if (plugin)
8490 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008491 source_runtime((char_u *)FTPLUGIN_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008492 filetype_plugin = TRUE;
8493 }
8494 if (indent)
8495 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008496 source_runtime((char_u *)INDENT_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008497 filetype_indent = TRUE;
8498 }
8499 }
8500 if (*arg == 'd')
8501 {
Bram Moolenaar1610d052016-06-09 22:53:01 +02008502 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +00008503 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008504 }
8505 }
8506 else if (STRCMP(arg, "off") == 0)
8507 {
8508 if (plugin || indent)
8509 {
8510 if (plugin)
8511 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008512 source_runtime((char_u *)FTPLUGOF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008513 filetype_plugin = FALSE;
8514 }
8515 if (indent)
8516 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008517 source_runtime((char_u *)INDOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008518 filetype_indent = FALSE;
8519 }
8520 }
8521 else
8522 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008523 source_runtime((char_u *)FTOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008524 filetype_detect = FALSE;
8525 }
8526 }
8527 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008528 semsg(_(e_invarg2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008529}
8530
8531/*
Bram Moolenaar3e545692017-06-04 19:00:32 +02008532 * ":setfiletype [FALLBACK] {name}"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008533 */
8534 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008535ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008536{
8537 if (!did_filetype)
Bram Moolenaar3e545692017-06-04 19:00:32 +02008538 {
8539 char_u *arg = eap->arg;
8540
8541 if (STRNCMP(arg, "FALLBACK ", 9) == 0)
8542 arg += 9;
8543
8544 set_option_value((char_u *)"filetype", 0L, arg, OPT_LOCAL);
8545 if (arg != eap->arg)
8546 did_filetype = FALSE;
8547 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008548}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008549
Bram Moolenaar071d4272004-06-13 20:20:40 +00008550 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008551ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008552{
8553#ifdef FEAT_DIGRAPHS
8554 if (*eap->arg != NUL)
8555 putdigraph(eap->arg);
8556 else
Bram Moolenaareae8ae12018-12-14 18:53:02 +01008557 listdigraphs(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008558#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008559 emsg(_("E196: No digraphs in this version"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008560#endif
8561}
8562
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02008563#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
8564 void
8565set_no_hlsearch(int flag)
8566{
8567 no_hlsearch = flag;
8568# ifdef FEAT_EVAL
8569 set_vim_var_nr(VV_HLSEARCH, !no_hlsearch && p_hls);
8570# endif
8571}
8572
Bram Moolenaar071d4272004-06-13 20:20:40 +00008573/*
8574 * ":nohlsearch"
8575 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008576 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008577ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008578{
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02008579 set_no_hlsearch(TRUE);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00008580 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008581}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008582#endif
8583
8584#ifdef FEAT_CRYPT
8585/*
8586 * ":X": Get crypt key
8587 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008588 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008589ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008590{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +01008591 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02008592 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008593}
8594#endif
8595
8596#ifdef FEAT_FOLDING
8597 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008598ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008599{
8600 if (foldManualAllowed(TRUE))
8601 foldCreate(eap->line1, eap->line2);
8602}
8603
8604 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008605ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008606{
8607 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
8608 eap->forceit, FALSE);
8609}
8610
8611 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008612ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008613{
8614 linenr_T lnum;
8615
Bram Moolenaar4facea32019-10-12 20:17:40 +02008616# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02008617 start_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +02008618# endif
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02008619
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008620 // First set the marks for all lines closed/open.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008621 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
8622 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
8623 ml_setmarked(lnum);
8624
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008625 // Execute the command on the marked lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008626 global_exe(eap->arg);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008627 ml_clearmarked(); // clear rest of the marks
Bram Moolenaar4facea32019-10-12 20:17:40 +02008628# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02008629 end_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +02008630# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008631}
8632#endif
Bram Moolenaarf5291f32017-09-14 22:55:37 +02008633
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008634#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar39665952018-08-15 20:59:48 +02008635/*
8636 * Returns TRUE if the supplied Ex cmdidx is for a location list command
8637 * instead of a quickfix command.
8638 */
8639 int
8640is_loclist_cmd(int cmdidx)
8641{
Bram Moolenaar74c8be22018-08-23 22:51:40 +02008642 if (cmdidx < 0 || cmdidx >= CMD_SIZE)
Bram Moolenaar39665952018-08-15 20:59:48 +02008643 return FALSE;
8644 return cmdnames[cmdidx].cmd_name[0] == 'l';
8645}
8646#endif
8647
Bram Moolenaar4facea32019-10-12 20:17:40 +02008648#if defined(FEAT_TIMERS) || defined(PROTO)
Bram Moolenaarf5291f32017-09-14 22:55:37 +02008649 int
8650get_pressedreturn(void)
8651{
8652 return ex_pressedreturn;
8653}
8654
8655 void
8656set_pressedreturn(int val)
8657{
8658 ex_pressedreturn = val;
8659}
8660#endif