blob: 3d590e557758e5540d54c4ba1ac3e3054259046d [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
Bram Moolenaar822ba242020-05-24 23:00:18 +0200276# define ex_def ex_ni
277# define ex_defcompile ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200278# define ex_delfunction ex_ni
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100279# define ex_disassemble ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000280# define ex_echo ex_ni
281# define ex_echohl ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000282# define ex_else ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200283# define ex_endfunction ex_ni
284# define ex_endif ex_ni
285# define ex_endtry ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000286# define ex_endwhile ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200287# define ex_eval ex_ni
288# define ex_execute ex_ni
289# define ex_finally ex_ni
290# define ex_finish ex_ni
291# define ex_function ex_ni
292# define ex_if ex_ni
293# define ex_let ex_ni
294# define ex_lockvar ex_ni
295# define ex_oldfiles ex_ni
296# define ex_options ex_ni
297# define ex_packadd ex_ni
298# define ex_packloadall ex_ni
299# define ex_return ex_ni
300# define ex_scriptnames ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000301# define ex_throw ex_ni
302# define ex_try ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000303# define ex_unlet ex_ni
Bram Moolenaar65c1b012005-01-31 19:02:28 +0000304# define ex_unlockvar ex_ni
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100305# define ex_vim9script ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200306# define ex_while ex_ni
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100307# define ex_import ex_ni
308# define ex_export ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000309#endif
Bram Moolenaar84538072019-07-28 14:15:42 +0200310#ifndef FEAT_SESSION
Bram Moolenaar071d4272004-06-13 20:20:40 +0000311# define ex_loadview ex_ni
312#endif
Bram Moolenaardefa0672019-07-21 19:25:37 +0200313#ifndef FEAT_VIMINFO
Bram Moolenaar071d4272004-06-13 20:20:40 +0000314# define ex_viminfo ex_ni
315#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100316static void ex_behave(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100317static void ex_filetype(exarg_T *eap);
318static void ex_setfiletype(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000319#ifndef FEAT_DIFF
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000320# define ex_diffoff ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000321# define ex_diffpatch ex_ni
322# define ex_diffgetput ex_ni
323# define ex_diffsplit ex_ni
324# define ex_diffthis ex_ni
325# define ex_diffupdate ex_ni
326#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100327static void ex_digraphs(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000328#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100329static void ex_nohlsearch(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000330#else
331# define ex_nohlsearch ex_ni
332# define ex_match ex_ni
333#endif
334#ifdef FEAT_CRYPT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100335static void ex_X(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000336#else
337# define ex_X ex_ni
338#endif
339#ifdef FEAT_FOLDING
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100340static void ex_fold(exarg_T *eap);
341static void ex_foldopen(exarg_T *eap);
342static void ex_folddo(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000343#else
344# define ex_fold ex_ni
345# define ex_foldopen ex_ni
346# define ex_folddo ex_ni
347#endif
Bram Moolenaar13505972019-01-24 15:04:48 +0100348#if !(defined(HAVE_LOCALE_H) || defined(X_LOCALE))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000349# define ex_language ex_ni
350#endif
351#ifndef FEAT_SIGNS
352# define ex_sign ex_ni
353#endif
Bram Moolenaar009b2592004-10-24 19:18:58 +0000354#ifndef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200355# define ex_nbclose ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000356# define ex_nbkey ex_ni
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200357# define ex_nbstart ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000358#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000359
Bram Moolenaar071d4272004-06-13 20:20:40 +0000360#ifndef FEAT_JUMPLIST
361# define ex_jumps ex_ni
Bram Moolenaar2d358992016-06-12 21:20:54 +0200362# define ex_clearjumps ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000363# define ex_changes ex_ni
364#endif
365
Bram Moolenaar05159a02005-02-26 23:04:13 +0000366#ifndef FEAT_PROFILE
367# define ex_profile ex_ni
368#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200369#ifndef FEAT_TERMINAL
370# define ex_terminal ex_ni
371#endif
Bram Moolenaard4aa83a2019-05-09 18:59:31 +0200372#if !defined(FEAT_X11) || !defined(FEAT_XCLIPBOARD)
373# define ex_xrestore ex_ni
374#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100375#if !defined(FEAT_PROP_POPUP)
Bram Moolenaar682725c2019-05-25 20:10:37 +0200376# define ex_popupclear ex_ni
377#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +0000378
Bram Moolenaar071d4272004-06-13 20:20:40 +0000379/*
380 * Declare cmdnames[].
381 */
382#define DO_DECLARE_EXCMD
383#include "ex_cmds.h"
Bram Moolenaar6de5e122017-04-20 21:55:44 +0200384#include "ex_cmdidxs.h"
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +0100385
Bram Moolenaar071d4272004-06-13 20:20:40 +0000386static char_u dollar_command[2] = {'$', 0};
387
388
389#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100390// Struct for storing a line inside a while/for loop
Bram Moolenaar071d4272004-06-13 20:20:40 +0000391typedef struct
392{
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100393 char_u *line; // command line
394 linenr_T lnum; // sourcing_lnum of the line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000395} wcmd_T;
396
397/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000398 * Structure used to store info for line position in a while or for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000399 * This is required, because do_one_cmd() may invoke ex_function(), which
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000400 * reads more lines that may come from the while/for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000401 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000402struct loop_cookie
Bram Moolenaar071d4272004-06-13 20:20:40 +0000403{
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100404 garray_T *lines_gap; // growarray with line info
405 int current_line; // last read line from growarray
406 int repeating; // TRUE when looping a second time
407 // When "repeating" is FALSE use "getline" and "cookie" to get lines
Bram Moolenaare96a2492019-06-25 04:12:16 +0200408 char_u *(*getline)(int, void *, int, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000409 void *cookie;
410};
411
Bram Moolenaare96a2492019-06-25 04:12:16 +0200412static char_u *get_loop_line(int c, void *cookie, int indent, int do_concat);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100413static int store_loop_line(garray_T *gap, char_u *line);
414static void free_cmdlines(garray_T *gap);
Bram Moolenaared203462004-06-16 11:19:22 +0000415
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100416// Struct to save a few things while debugging. Used in do_cmdline() only.
Bram Moolenaared203462004-06-16 11:19:22 +0000417struct dbg_stuff
418{
419 int trylevel;
420 int force_abort;
421 except_T *caught_stack;
422 char_u *vv_exception;
423 char_u *vv_throwpoint;
424 int did_emsg;
425 int got_int;
426 int did_throw;
427 int need_rethrow;
428 int check_cstack;
429 except_T *current_exception;
430};
431
Bram Moolenaared203462004-06-16 11:19:22 +0000432 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100433save_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000434{
435 dsp->trylevel = trylevel; trylevel = 0;
436 dsp->force_abort = force_abort; force_abort = FALSE;
437 dsp->caught_stack = caught_stack; caught_stack = NULL;
438 dsp->vv_exception = v_exception(NULL);
439 dsp->vv_throwpoint = v_throwpoint(NULL);
440
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100441 // Necessary for debugging an inactive ":catch", ":finally", ":endtry"
Bram Moolenaared203462004-06-16 11:19:22 +0000442 dsp->did_emsg = did_emsg; did_emsg = FALSE;
443 dsp->got_int = got_int; got_int = FALSE;
444 dsp->did_throw = did_throw; did_throw = FALSE;
445 dsp->need_rethrow = need_rethrow; need_rethrow = FALSE;
446 dsp->check_cstack = check_cstack; check_cstack = FALSE;
447 dsp->current_exception = current_exception; current_exception = NULL;
448}
449
450 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100451restore_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000452{
453 suppress_errthrow = FALSE;
454 trylevel = dsp->trylevel;
455 force_abort = dsp->force_abort;
456 caught_stack = dsp->caught_stack;
457 (void)v_exception(dsp->vv_exception);
458 (void)v_throwpoint(dsp->vv_throwpoint);
459 did_emsg = dsp->did_emsg;
460 got_int = dsp->got_int;
461 did_throw = dsp->did_throw;
462 need_rethrow = dsp->need_rethrow;
463 check_cstack = dsp->check_cstack;
464 current_exception = dsp->current_exception;
465}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000466#endif
467
Bram Moolenaar071d4272004-06-13 20:20:40 +0000468/*
469 * do_exmode(): Repeatedly get commands for the "Ex" mode, until the ":vi"
470 * command is given.
471 */
472 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100473do_exmode(
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100474 int improved) // TRUE for "improved Ex" mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000475{
476 int save_msg_scroll;
477 int prev_msg_row;
478 linenr_T prev_line;
Bram Moolenaar79518e22017-02-17 16:31:35 +0100479 varnumber_T changedtick;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000480
481 if (improved)
482 exmode_active = EXMODE_VIM;
483 else
484 exmode_active = EXMODE_NORMAL;
485 State = NORMAL;
486
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100487 // When using ":global /pat/ visual" and then "Q" we return to continue
488 // the :global command.
Bram Moolenaardf177f62005-02-22 08:39:57 +0000489 if (global_busy)
490 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000491
492 save_msg_scroll = msg_scroll;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100493 ++RedrawingDisabled; // don't redisplay the window
494 ++no_wait_return; // don't wait for return
Bram Moolenaar071d4272004-06-13 20:20:40 +0000495#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100496 // Ignore scrollbar and mouse events in Ex mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000497 ++hold_gui_events;
498#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000499
Bram Moolenaar32526b32019-01-19 17:43:09 +0100500 msg(_("Entering Ex mode. Type \"visual\" to go to Normal mode."));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000501 while (exmode_active)
502 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100503 // Check for a ":normal" command and no more characters left.
Bram Moolenaar7c626922005-02-07 22:01:03 +0000504 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
505 {
506 exmode_active = FALSE;
507 break;
508 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000509 msg_scroll = TRUE;
510 need_wait_return = FALSE;
511 ex_pressedreturn = FALSE;
512 ex_no_reprint = FALSE;
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100513 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000514 prev_msg_row = msg_row;
515 prev_line = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000516 if (improved)
517 {
518 cmdline_row = msg_row;
519 do_cmdline(NULL, getexline, NULL, 0);
520 }
521 else
522 do_cmdline(NULL, getexmodeline, NULL, DOCMD_NOWAIT);
523 lines_left = Rows - 1;
524
Bram Moolenaardf177f62005-02-22 08:39:57 +0000525 if ((prev_line != curwin->w_cursor.lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100526 || changedtick != CHANGEDTICK(curbuf)) && !ex_no_reprint)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000527 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000528 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100529 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000530 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000531 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000532 if (ex_pressedreturn)
533 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100534 // go up one line, to overwrite the ":<CR>" line, so the
535 // output doesn't contain empty lines.
Bram Moolenaardf177f62005-02-22 08:39:57 +0000536 msg_row = prev_msg_row;
537 if (prev_msg_row == Rows - 1)
538 msg_row--;
539 }
540 msg_col = 0;
541 print_line_no_prefix(curwin->w_cursor.lnum, FALSE, FALSE);
542 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000543 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000544 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100545 else if (ex_pressedreturn && !ex_no_reprint) // must be at EOF
Bram Moolenaardf177f62005-02-22 08:39:57 +0000546 {
547 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100548 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000549 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100550 emsg(_("E501: At end-of-file"));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000551 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000552 }
553
554#ifdef FEAT_GUI
555 --hold_gui_events;
556#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000557 --RedrawingDisabled;
558 --no_wait_return;
559 update_screen(CLEAR);
560 need_wait_return = FALSE;
561 msg_scroll = save_msg_scroll;
562}
563
564/*
Bram Moolenaar4facea32019-10-12 20:17:40 +0200565 * Print the executed command for when 'verbose' is set.
566 * When "lnum" is 0 only print the command.
567 */
568 static void
569msg_verbose_cmd(linenr_T lnum, char_u *cmd)
570{
571 ++no_wait_return;
572 verbose_enter_scroll();
573
574 if (lnum == 0)
575 smsg(_("Executing: %s"), cmd);
576 else
577 smsg(_("line %ld: %s"), (long)lnum, cmd);
578 if (msg_silent == 0)
579 msg_puts("\n"); // don't overwrite this
580
581 verbose_leave_scroll();
582 --no_wait_return;
583}
584
585/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000586 * Execute a simple command line. Used for translated commands like "*".
587 */
588 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100589do_cmdline_cmd(char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000590{
591 return do_cmdline(cmd, NULL, NULL,
592 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
593}
594
595/*
596 * do_cmdline(): execute one Ex command line
597 *
598 * 1. Execute "cmdline" when it is not NULL.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100599 * If "cmdline" is NULL, or more lines are needed, fgetline() is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000600 * 2. Split up in parts separated with '|'.
601 *
602 * This function can be called recursively!
603 *
604 * flags:
605 * DOCMD_VERBOSE - The command will be included in the error message.
606 * DOCMD_NOWAIT - Don't call wait_return() and friends.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100607 * DOCMD_REPEAT - Repeat execution until fgetline() returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000608 * DOCMD_KEYTYPED - Don't reset KeyTyped.
609 * DOCMD_EXCRESET - Reset the exception environment (used for debugging).
610 * DOCMD_KEEPLINE - Store first typed line (for repeating with ".").
611 *
612 * return FAIL if cmdline could not be executed, OK otherwise
613 */
614 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100615do_cmdline(
616 char_u *cmdline,
Bram Moolenaare96a2492019-06-25 04:12:16 +0200617 char_u *(*fgetline)(int, void *, int, int),
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100618 void *cookie, // argument for fgetline()
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100619 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620{
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100621 char_u *next_cmdline; // next cmd to execute
622 char_u *cmdline_copy = NULL; // copy of cmd line
623 int used_getline = FALSE; // used "fgetline" to obtain command
624 static int recursive = 0; // recursive depth
Bram Moolenaar071d4272004-06-13 20:20:40 +0000625 int msg_didout_before_start = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100626 int count = 0; // line number count
627 int did_inc = FALSE; // incremented RedrawingDisabled
Bram Moolenaar071d4272004-06-13 20:20:40 +0000628 int retval = OK;
629#ifdef FEAT_EVAL
Bram Moolenaarddef1292019-12-16 17:10:33 +0100630 cstack_T cstack; // conditional stack
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100631 garray_T lines_ga; // keep lines for ":while"/":for"
632 int current_line = 0; // active line in lines_ga
633 char_u *fname = NULL; // function or script name
634 linenr_T *breakpoint = NULL; // ptr to breakpoint field in cookie
635 int *dbg_tick = NULL; // ptr to dbg_tick field in cookie
636 struct dbg_stuff debug_saved; // saved things for debug mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000637 int initial_trylevel;
638 struct msglist **saved_msg_list = NULL;
639 struct msglist *private_msg_list;
640
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100641 // "fgetline" and "cookie" passed to do_one_cmd()
Bram Moolenaare96a2492019-06-25 04:12:16 +0200642 char_u *(*cmd_getline)(int, void *, int, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000643 void *cmd_cookie;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000644 struct loop_cookie cmd_loop_cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000645 void *real_cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000646 int getline_is_func;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000647#else
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100648# define cmd_getline fgetline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649# define cmd_cookie cookie
650#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100651 static int call_depth = 0; // recursiveness
Bram Moolenaar2196bce2020-04-12 20:01:11 +0200652#ifdef FEAT_EVAL
Bram Moolenaare31ee862020-01-07 20:59:34 +0100653 ESTACK_CHECK_DECLARATION
Bram Moolenaar071d4272004-06-13 20:20:40 +0000654
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100655 // For every pair of do_cmdline()/do_one_cmd() calls, use an extra memory
656 // location for storing error messages to be converted to an exception.
657 // This ensures that the do_errthrow() call in do_one_cmd() does not
658 // combine the messages stored by an earlier invocation of do_one_cmd()
659 // with the command name of the later one. This would happen when
660 // BufWritePost autocommands are executed after a write error.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000661 saved_msg_list = msg_list;
662 msg_list = &private_msg_list;
663 private_msg_list = NULL;
664#endif
665
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100666 // It's possible to create an endless loop with ":execute", catch that
667 // here. The value of 200 allows nested function calls, ":source", etc.
668 // Allow 200 or 'maxfuncdepth', whatever is larger.
Bram Moolenaarb094ff42017-01-02 16:16:39 +0100669 if (call_depth >= 200
670#ifdef FEAT_EVAL
671 && call_depth >= p_mfd
672#endif
673 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000674 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100675 emsg(_("E169: Command too recursive"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000676#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100677 // When converting to an exception, we do not include the command name
678 // since this is not an error of the specific command.
Bram Moolenaarddef1292019-12-16 17:10:33 +0100679 do_errthrow((cstack_T *)NULL, (char_u *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000680 msg_list = saved_msg_list;
681#endif
682 return FAIL;
683 }
684 ++call_depth;
685
686#ifdef FEAT_EVAL
687 cstack.cs_idx = -1;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000688 cstack.cs_looplevel = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000689 cstack.cs_trylevel = 0;
690 cstack.cs_emsg_silent_list = NULL;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000691 cstack.cs_lflags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692 ga_init2(&lines_ga, (int)sizeof(wcmd_T), 10);
693
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100694 real_cookie = getline_cookie(fgetline, cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000695
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100696 // Inside a function use a higher nesting level.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100697 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000698 if (getline_is_func && ex_nesting_level == func_level(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000699 ++ex_nesting_level;
700
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100701 // Get the function or script name and the address where the next breakpoint
702 // line and the debug tick for a function or script are stored.
Bram Moolenaar05159a02005-02-26 23:04:13 +0000703 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000704 {
705 fname = func_name(real_cookie);
706 breakpoint = func_breakpoint(real_cookie);
707 dbg_tick = func_dbg_tick(real_cookie);
708 }
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100709 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000710 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100711 fname = SOURCING_NAME;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000712 breakpoint = source_breakpoint(real_cookie);
713 dbg_tick = source_dbg_tick(real_cookie);
714 }
715
716 /*
717 * Initialize "force_abort" and "suppress_errthrow" at the top level.
718 */
719 if (!recursive)
720 {
721 force_abort = FALSE;
722 suppress_errthrow = FALSE;
723 }
724
725 /*
726 * If requested, store and reset the global values controlling the
Bram Moolenaar89d40322006-08-29 15:30:07 +0000727 * exception handling (used when debugging). Otherwise clear it to avoid
728 * a bogus compiler warning when the optimizer uses inline functions...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000729 */
Bram Moolenaarb4872942006-05-13 10:32:52 +0000730 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +0000731 save_dbg_stuff(&debug_saved);
Bram Moolenaar89d40322006-08-29 15:30:07 +0000732 else
Bram Moolenaara80faa82020-04-12 19:37:17 +0200733 CLEAR_FIELD(debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000734
735 initial_trylevel = trylevel;
736
737 /*
738 * "did_throw" will be set to TRUE when an exception is being thrown.
739 */
740 did_throw = FALSE;
741#endif
742 /*
743 * "did_emsg" will be set to TRUE when emsg() is used, in which case we
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000744 * cancel the whole command line, and any if/endif or loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000745 * If force_abort is set, we cancel everything.
746 */
747 did_emsg = FALSE;
748
749 /*
750 * KeyTyped is only set when calling vgetc(). Reset it here when not
751 * calling vgetc() (sourced command lines).
752 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100753 if (!(flags & DOCMD_KEYTYPED)
754 && !getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755 KeyTyped = FALSE;
756
757 /*
758 * Continue executing command lines:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000759 * - when inside an ":if", ":while" or ":for"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000760 * - for multiple commands on one line, separated with '|'
761 * - when repeating until there are no more lines (for ":source")
762 */
763 next_cmdline = cmdline;
764 do
765 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000766#ifdef FEAT_EVAL
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100767 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000768#endif
769
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100770 // stop skipping cmds for an error msg after all endif/while/for
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771 if (next_cmdline == NULL
772#ifdef FEAT_EVAL
773 && !force_abort
774 && cstack.cs_idx < 0
Bram Moolenaar05159a02005-02-26 23:04:13 +0000775 && !(getline_is_func && func_has_abort(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000776#endif
777 )
778 did_emsg = FALSE;
779
780 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000781 * 1. If repeating a line in a loop, get a line from lines_ga.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100782 * 2. If no line given: Get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783 * 3. If a line is given: Make a copy, so we can mess with it.
784 */
785
786#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100787 // 1. If repeating, get a previous line from lines_ga.
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000788 if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000789 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100790 // Each '|' separated command is stored separately in lines_ga, to
791 // be able to jump to it. Don't use next_cmdline now.
Bram Moolenaard23a8232018-02-10 18:45:26 +0100792 VIM_CLEAR(cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100794 // Check if a function has returned or, unless it has an unclosed
795 // try conditional, aborted.
Bram Moolenaar05159a02005-02-26 23:04:13 +0000796 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000797 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000798# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000799 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000800 func_line_end(real_cookie);
801# endif
802 if (func_has_ended(real_cookie))
803 {
804 retval = FAIL;
805 break;
806 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000807 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000808#ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000809 else if (do_profiling == PROF_YES
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100810 && getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000811 script_line_end();
812#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100814 // Check if a sourced file hit a ":finish" command.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100815 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816 {
817 retval = FAIL;
818 break;
819 }
820
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100821 // If breakpoints have been added/deleted need to check for it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822 if (breakpoint != NULL && dbg_tick != NULL
823 && *dbg_tick != debug_tick)
824 {
825 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100826 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100827 fname, SOURCING_LNUM);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000828 *dbg_tick = debug_tick;
829 }
830
831 next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100832 SOURCING_LNUM = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000833
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100834 // Did we encounter a breakpoint?
Bram Moolenaar071d4272004-06-13 20:20:40 +0000835 if (breakpoint != NULL && *breakpoint != 0
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100836 && *breakpoint <= SOURCING_LNUM)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100838 dbg_breakpoint(fname, SOURCING_LNUM);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100839 // Find next breakpoint.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100841 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100842 fname, SOURCING_LNUM);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000843 *dbg_tick = debug_tick;
844 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000845# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000846 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000847 {
848 if (getline_is_func)
849 func_line_start(real_cookie);
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100850 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000851 script_line_start();
852 }
853# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854 }
855
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000856 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000857 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100858 // Inside a while/for loop we need to store the lines and use them
859 // again. Pass a different "fgetline" function to do_one_cmd()
860 // below, so that it stores lines in or reads them from
861 // "lines_ga". Makes it possible to define a function inside a
862 // while/for loop.
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000863 cmd_getline = get_loop_line;
864 cmd_cookie = (void *)&cmd_loop_cookie;
865 cmd_loop_cookie.lines_gap = &lines_ga;
866 cmd_loop_cookie.current_line = current_line;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100867 cmd_loop_cookie.getline = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000868 cmd_loop_cookie.cookie = cookie;
869 cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000870 }
871 else
872 {
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100873 cmd_getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874 cmd_cookie = cookie;
875 }
876#endif
877
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100878 // 2. If no line given, get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879 if (next_cmdline == NULL)
880 {
881 /*
882 * Need to set msg_didout for the first line after an ":if",
883 * otherwise the ":if" will be overwritten.
884 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100885 if (count == 1 && getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000886 msg_didout = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100887 if (fgetline == NULL || (next_cmdline = fgetline(':', cookie,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000888#ifdef FEAT_EVAL
889 cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2
890#else
891 0
892#endif
Bram Moolenaare96a2492019-06-25 04:12:16 +0200893 , TRUE)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100895 // Don't call wait_return for aborted command line. The NULL
896 // returned for the end of a sourced file or executed function
897 // doesn't do this.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000898 if (KeyTyped && !(flags & DOCMD_REPEAT))
899 need_wait_return = FALSE;
900 retval = FAIL;
901 break;
902 }
903 used_getline = TRUE;
904
905 /*
906 * Keep the first typed line. Clear it when more lines are typed.
907 */
908 if (flags & DOCMD_KEEPLINE)
909 {
910 vim_free(repeat_cmdline);
911 if (count == 0)
912 repeat_cmdline = vim_strsave(next_cmdline);
913 else
914 repeat_cmdline = NULL;
915 }
916 }
917
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100918 // 3. Make a copy of the command so we can mess with it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000919 else if (cmdline_copy == NULL)
920 {
921 next_cmdline = vim_strsave(next_cmdline);
922 if (next_cmdline == NULL)
923 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100924 emsg(_(e_outofmem));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000925 retval = FAIL;
926 break;
927 }
928 }
929 cmdline_copy = next_cmdline;
930
931#ifdef FEAT_EVAL
932 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000933 * Save the current line when inside a ":while" or ":for", and when
934 * the command looks like a ":while" or ":for", because we may need it
935 * later. When there is a '|' and another command, it is stored
936 * separately, because we need to be able to jump back to it from an
937 * :endwhile/:endfor.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000938 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000939 if (current_line == lines_ga.ga_len
940 && (cstack.cs_looplevel || has_loop_cmd(next_cmdline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000941 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000942 if (store_loop_line(&lines_ga, next_cmdline) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000943 {
944 retval = FAIL;
945 break;
946 }
947 }
948 did_endif = FALSE;
949#endif
950
951 if (count++ == 0)
952 {
953 /*
954 * All output from the commands is put below each other, without
955 * waiting for a return. Don't do this when executing commands
956 * from a script or when being called recursive (e.g. for ":e
957 * +command file").
958 */
959 if (!(flags & DOCMD_NOWAIT) && !recursive)
960 {
961 msg_didout_before_start = msg_didout;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100962 msg_didany = FALSE; // no output yet
Bram Moolenaar071d4272004-06-13 20:20:40 +0000963 msg_start();
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100964 msg_scroll = TRUE; // put messages below each other
965 ++no_wait_return; // don't wait for return until finished
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966 ++RedrawingDisabled;
967 did_inc = TRUE;
968 }
969 }
970
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100971 if (p_verbose >= 15 && SOURCING_NAME != NULL)
972 msg_verbose_cmd(SOURCING_LNUM, cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973
974 /*
975 * 2. Execute one '|' separated command.
976 * do_one_cmd() will return NULL if there is no trailing '|'.
977 * "cmdline_copy" can change, e.g. for '%' and '#' expansion.
978 */
979 ++recursive;
980 next_cmdline = do_one_cmd(&cmdline_copy, flags & DOCMD_VERBOSE,
981#ifdef FEAT_EVAL
982 &cstack,
983#endif
984 cmd_getline, cmd_cookie);
985 --recursive;
986
987#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000988 if (cmd_cookie == (void *)&cmd_loop_cookie)
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100989 // Use "current_line" from "cmd_loop_cookie", it may have been
990 // incremented when defining a function.
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000991 current_line = cmd_loop_cookie.current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992#endif
993
994 if (next_cmdline == NULL)
995 {
Bram Moolenaard23a8232018-02-10 18:45:26 +0100996 VIM_CLEAR(cmdline_copy);
Bram Moolenaard7663c22019-08-06 21:59:57 +0200997
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998 /*
999 * If the command was typed, remember it for the ':' register.
1000 * Do this AFTER executing the command to make :@: work.
1001 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001002 if (getline_equal(fgetline, cookie, getexline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003 && new_last_cmdline != NULL)
1004 {
1005 vim_free(last_cmdline);
1006 last_cmdline = new_last_cmdline;
1007 new_last_cmdline = NULL;
1008 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009 }
1010 else
1011 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001012 // need to copy the command after the '|' to cmdline_copy, for the
1013 // next do_one_cmd()
Bram Moolenaara7241f52008-06-24 20:39:31 +00001014 STRMOVE(cmdline_copy, next_cmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015 next_cmdline = cmdline_copy;
1016 }
1017
1018
1019#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001020 // reset did_emsg for a function that is not aborted by an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021 if (did_emsg && !force_abort
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001022 && getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023 && !func_has_abort(real_cookie))
1024 did_emsg = FALSE;
1025
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001026 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027 {
1028 ++current_line;
1029
1030 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001031 * An ":endwhile", ":endfor" and ":continue" is handled here.
1032 * If we were executing commands, jump back to the ":while" or
1033 * ":for".
1034 * If we were not executing commands, decrement cs_looplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001036 if (cstack.cs_lflags & (CSL_HAD_CONT | CSL_HAD_ENDLOOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001037 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001038 cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001039
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001040 // Jump back to the matching ":while" or ":for". Be careful
1041 // not to use a cs_line[] from an entry that isn't a ":while"
1042 // or ":for": It would make "current_line" invalid and can
1043 // cause a crash.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001044 if (!did_emsg && !got_int && !did_throw
1045 && cstack.cs_idx >= 0
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001046 && (cstack.cs_flags[cstack.cs_idx]
1047 & (CSF_WHILE | CSF_FOR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048 && cstack.cs_line[cstack.cs_idx] >= 0
1049 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE))
1050 {
1051 current_line = cstack.cs_line[cstack.cs_idx];
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001052 // remember we jumped there
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001053 cstack.cs_lflags |= CSL_HAD_LOOP;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001054 line_breakcheck(); // check if CTRL-C typed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001055
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001056 // Check for the next breakpoint at or after the ":while"
1057 // or ":for".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058 if (breakpoint != NULL)
1059 {
1060 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001061 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062 fname,
1063 ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1);
1064 *dbg_tick = debug_tick;
1065 }
1066 }
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001067 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001069 // can only get here with ":endwhile" or ":endfor"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001070 if (cstack.cs_idx >= 0)
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001071 rewind_conditionals(&cstack, cstack.cs_idx - 1,
1072 CSF_WHILE | CSF_FOR, &cstack.cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001073 }
1074 }
1075
1076 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001077 * For a ":while" or ":for" we need to remember the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001079 else if (cstack.cs_lflags & CSL_HAD_LOOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001081 cstack.cs_lflags &= ~CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082 cstack.cs_line[cstack.cs_idx] = current_line - 1;
1083 }
1084 }
1085
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001086 // Check for the next breakpoint after a watchexpression
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01001087 if (breakpoint != NULL && has_watchexpr())
1088 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001089 *breakpoint = dbg_find_breakpoint(FALSE, fname, SOURCING_LNUM);
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01001090 *dbg_tick = debug_tick;
1091 }
1092
Bram Moolenaar071d4272004-06-13 20:20:40 +00001093 /*
1094 * When not inside any ":while" loop, clear remembered lines.
1095 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001096 if (cstack.cs_looplevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001097 {
1098 if (lines_ga.ga_len > 0)
1099 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001100 SOURCING_LNUM =
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101 ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum;
1102 free_cmdlines(&lines_ga);
1103 }
1104 current_line = 0;
1105 }
1106
1107 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001108 * A ":finally" makes did_emsg, got_int, and did_throw pending for
1109 * being restored at the ":endtry". Reset them here and set the
1110 * ACTIVE and FINALLY flags, so that the finally clause gets executed.
1111 * This includes the case where a missing ":endif", ":endwhile" or
1112 * ":endfor" was detected by the ":finally" itself.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001114 if (cstack.cs_lflags & CSL_HAD_FINA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001116 cstack.cs_lflags &= ~CSL_HAD_FINA;
1117 report_make_pending(cstack.cs_pending[cstack.cs_idx]
1118 & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001119 did_throw ? (void *)current_exception : NULL);
1120 did_emsg = got_int = did_throw = FALSE;
1121 cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY;
1122 }
1123
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001124 // Update global "trylevel" for recursive calls to do_cmdline() from
1125 // within this loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126 trylevel = initial_trylevel + cstack.cs_trylevel;
1127
1128 /*
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001129 * If the outermost try conditional (across function calls and sourced
Bram Moolenaar071d4272004-06-13 20:20:40 +00001130 * files) is aborted because of an error, an interrupt, or an uncaught
1131 * exception, cancel everything. If it is left normally, reset
1132 * force_abort to get the non-EH compatible abortion behavior for
1133 * the rest of the script.
1134 */
1135 if (trylevel == 0 && !did_emsg && !got_int && !did_throw)
1136 force_abort = FALSE;
1137
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001138 // Convert an interrupt to an exception if appropriate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139 (void)do_intthrow(&cstack);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001140#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001141
1142 }
1143 /*
1144 * Continue executing command lines when:
1145 * - no CTRL-C typed, no aborting error, no exception thrown or try
1146 * conditionals need to be checked for executing finally clauses or
1147 * catching an interrupt exception
1148 * - didn't get an error message or lines are not typed
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001149 * - there is a command after '|', inside a :if, :while, :for or :try, or
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150 * looping for ":source" command or function call.
1151 */
1152 while (!((got_int
1153#ifdef FEAT_EVAL
1154 || (did_emsg && force_abort) || did_throw
1155#endif
1156 )
1157#ifdef FEAT_EVAL
1158 && cstack.cs_trylevel == 0
1159#endif
1160 )
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001161 && !(did_emsg
1162#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001163 // Keep going when inside try/catch, so that the error can be
1164 // deal with, except when it is a syntax error, it may cause
1165 // the :endtry to be missed.
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001166 && (cstack.cs_trylevel == 0 || did_emsg_syntax)
1167#endif
1168 && used_getline
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001169 && (getline_equal(fgetline, cookie, getexmodeline)
1170 || getline_equal(fgetline, cookie, getexline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001171 && (next_cmdline != NULL
1172#ifdef FEAT_EVAL
1173 || cstack.cs_idx >= 0
1174#endif
1175 || (flags & DOCMD_REPEAT)));
1176
1177 vim_free(cmdline_copy);
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001178 did_emsg_syntax = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179#ifdef FEAT_EVAL
1180 free_cmdlines(&lines_ga);
1181 ga_clear(&lines_ga);
1182
1183 if (cstack.cs_idx >= 0)
1184 {
1185 /*
1186 * If a sourced file or executed function ran to its end, report the
1187 * unclosed conditional.
1188 */
1189 if (!got_int && !did_throw
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001190 && ((getline_equal(fgetline, cookie, getsourceline)
1191 && !source_finished(fgetline, cookie))
1192 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193 && !func_has_ended(real_cookie))))
1194 {
1195 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001196 emsg(_(e_endtry));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001198 emsg(_(e_endwhile));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001199 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001200 emsg(_(e_endfor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001202 emsg(_(e_endif));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203 }
1204
1205 /*
1206 * Reset "trylevel" in case of a ":finish" or ":return" or a missing
1207 * ":endtry" in a sourced file or executed function. If the try
1208 * conditional is in its finally clause, ignore anything pending.
1209 * If it is in a catch clause, finish the caught exception.
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001210 * Also cleanup any "cs_forinfo" structures.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211 */
1212 do
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001213 {
1214 int idx = cleanup_conditionals(&cstack, 0, TRUE);
1215
Bram Moolenaar89e5d682005-01-17 22:06:23 +00001216 if (idx >= 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001217 --idx; // remove try block not in its finally clause
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001218 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
1219 &cstack.cs_looplevel);
1220 }
1221 while (cstack.cs_idx >= 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222 trylevel = initial_trylevel;
1223 }
1224
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001225 // If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
1226 // lack was reported above and the error message is to be converted to an
1227 // exception, do this now after rewinding the cstack.
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001228 do_errthrow(&cstack, getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001229 ? (char_u *)"endfunction" : (char_u *)NULL);
1230
1231 if (trylevel == 0)
1232 {
1233 /*
1234 * When an exception is being thrown out of the outermost try
1235 * conditional, discard the uncaught exception, disable the conversion
1236 * of interrupts or errors to exceptions, and ensure that no more
1237 * commands are executed.
1238 */
1239 if (did_throw)
1240 {
1241 void *p = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242 struct msglist *messages = NULL, *next;
1243
1244 /*
1245 * If the uncaught exception is a user exception, report it as an
1246 * error. If it is an error exception, display the saved error
1247 * message now. For an interrupt exception, do nothing; the
1248 * interrupt message is given elsewhere.
1249 */
1250 switch (current_exception->type)
1251 {
1252 case ET_USER:
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001253 vim_snprintf((char *)IObuff, IOSIZE,
1254 _("E605: Exception not caught: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001255 current_exception->value);
1256 p = vim_strsave(IObuff);
1257 break;
1258 case ET_ERROR:
1259 messages = current_exception->messages;
1260 current_exception->messages = NULL;
1261 break;
1262 case ET_INTERRUPT:
1263 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264 }
1265
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001266 estack_push(ETYPE_EXCEPT, current_exception->throw_name,
1267 current_exception->throw_lnum);
Bram Moolenaare31ee862020-01-07 20:59:34 +01001268 ESTACK_CHECK_SETUP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269 current_exception->throw_name = NULL;
1270
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001271 discard_current_exception(); // uses IObuff if 'verbose'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001272 suppress_errthrow = TRUE;
1273 force_abort = TRUE;
1274
1275 if (messages != NULL)
1276 {
1277 do
1278 {
1279 next = messages->next;
1280 emsg(messages->msg);
1281 vim_free(messages->msg);
1282 vim_free(messages);
1283 messages = next;
1284 }
1285 while (messages != NULL);
1286 }
1287 else if (p != NULL)
1288 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01001289 emsg(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001290 vim_free(p);
1291 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001292 vim_free(SOURCING_NAME);
Bram Moolenaare31ee862020-01-07 20:59:34 +01001293 ESTACK_CHECK_NOW
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001294 estack_pop();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001295 }
1296
1297 /*
1298 * On an interrupt or an aborting error not converted to an exception,
1299 * disable the conversion of errors to exceptions. (Interrupts are not
Bram Moolenaar2196bce2020-04-12 20:01:11 +02001300 * converted anymore, here.) This enables also the interrupt message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001301 * when force_abort is set and did_emsg unset in case of an interrupt
1302 * from a finally clause after an error.
1303 */
1304 else if (got_int || (did_emsg && force_abort))
1305 suppress_errthrow = TRUE;
1306 }
1307
1308 /*
1309 * The current cstack will be freed when do_cmdline() returns. An uncaught
1310 * exception will have to be rethrown in the previous cstack. If a function
1311 * has just returned or a script file was just finished and the previous
1312 * cstack belongs to the same function or, respectively, script file, it
1313 * will have to be checked for finally clauses to be executed due to the
1314 * ":return" or ":finish". This is done in do_one_cmd().
1315 */
1316 if (did_throw)
1317 need_rethrow = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001318 if ((getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001319 && ex_nesting_level > source_level(real_cookie))
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001320 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321 && ex_nesting_level > func_level(real_cookie) + 1))
1322 {
1323 if (!did_throw)
1324 check_cstack = TRUE;
1325 }
1326 else
1327 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001328 // When leaving a function, reduce nesting level.
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001329 if (getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330 --ex_nesting_level;
1331 /*
1332 * Go to debug mode when returning from a function in which we are
1333 * single-stepping.
1334 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001335 if ((getline_equal(fgetline, cookie, getsourceline)
1336 || getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337 && ex_nesting_level + 1 <= debug_break_level)
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001338 do_debug(getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339 ? (char_u *)_("End of sourced file")
1340 : (char_u *)_("End of function"));
1341 }
1342
1343 /*
1344 * Restore the exception environment (done after returning from the
1345 * debugger).
1346 */
1347 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001348 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349
1350 msg_list = saved_msg_list;
Bram Moolenaar292b90d2020-03-18 15:23:16 +01001351
1352 // Cleanup if "cs_emsg_silent_list" remains.
1353 if (cstack.cs_emsg_silent_list != NULL)
1354 {
1355 eslist_T *elem, *temp;
1356
1357 for (elem = cstack.cs_emsg_silent_list; elem != NULL; elem = temp)
1358 {
1359 temp = elem->next;
1360 vim_free(elem);
1361 }
1362 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001363#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364
1365 /*
1366 * If there was too much output to fit on the command line, ask the user to
1367 * hit return before redrawing the screen. With the ":global" command we do
1368 * this only once after the command is finished.
1369 */
1370 if (did_inc)
1371 {
1372 --RedrawingDisabled;
1373 --no_wait_return;
1374 msg_scroll = FALSE;
1375
1376 /*
1377 * When just finished an ":if"-":else" which was typed, no need to
1378 * wait for hit-return. Also for an error situation.
1379 */
1380 if (retval == FAIL
1381#ifdef FEAT_EVAL
1382 || (did_endif && KeyTyped && !did_emsg)
1383#endif
1384 )
1385 {
1386 need_wait_return = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001387 msg_didany = FALSE; // don't wait when restarting edit
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388 }
1389 else if (need_wait_return)
1390 {
1391 /*
1392 * The msg_start() above clears msg_didout. The wait_return we do
1393 * here should not overwrite the command that may be shown before
1394 * doing that.
1395 */
1396 msg_didout |= msg_didout_before_start;
1397 wait_return(FALSE);
1398 }
1399 }
1400
Bram Moolenaar2a942252012-11-28 23:03:07 +01001401#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001402 did_endif = FALSE; // in case do_cmdline used recursively
Bram Moolenaar2a942252012-11-28 23:03:07 +01001403#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404 /*
1405 * Reset if_level, in case a sourced script file contains more ":if" than
1406 * ":endif" (could be ":if x | foo | endif").
1407 */
1408 if_level = 0;
Bram Moolenaar2e18a122012-11-28 19:10:54 +01001409#endif
Bram Moolenaard4ad0d42012-11-28 17:34:48 +01001410
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411 --call_depth;
1412 return retval;
1413}
1414
1415#ifdef FEAT_EVAL
1416/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001417 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418 */
1419 static char_u *
Bram Moolenaare96a2492019-06-25 04:12:16 +02001420get_loop_line(int c, void *cookie, int indent, int do_concat)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001422 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423 wcmd_T *wp;
1424 char_u *line;
1425
1426 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1427 {
1428 if (cp->repeating)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001429 return NULL; // trying to read past ":endwhile"/":endfor"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001430
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001431 // First time inside the ":while"/":for": get line normally.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432 if (cp->getline == NULL)
Bram Moolenaare96a2492019-06-25 04:12:16 +02001433 line = getcmdline(c, 0L, indent, do_concat);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001434 else
Bram Moolenaare96a2492019-06-25 04:12:16 +02001435 line = cp->getline(c, cp->cookie, indent, do_concat);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001436 if (line != NULL && store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001437 ++cp->current_line;
1438
1439 return line;
1440 }
1441
1442 KeyTyped = FALSE;
1443 ++cp->current_line;
1444 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001445 SOURCING_LNUM = wp->lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001446 return vim_strsave(wp->line);
1447}
1448
1449/*
1450 * Store a line in "gap" so that a ":while" loop can execute it again.
1451 */
1452 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001453store_loop_line(garray_T *gap, char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001454{
1455 if (ga_grow(gap, 1) == FAIL)
1456 return FAIL;
1457 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001458 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = SOURCING_LNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001459 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001460 return OK;
1461}
1462
1463/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001464 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001465 */
1466 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001467free_cmdlines(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001468{
1469 while (gap->ga_len > 0)
1470 {
1471 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1472 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001473 }
1474}
1475#endif
1476
1477/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001478 * If "fgetline" is get_loop_line(), return TRUE if the getline it uses equals
1479 * "func". * Otherwise return TRUE when "fgetline" equals "func".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001480 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001481 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001482getline_equal(
Bram Moolenaare96a2492019-06-25 04:12:16 +02001483 char_u *(*fgetline)(int, void *, int, int),
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001484 void *cookie UNUSED, // argument for fgetline()
Bram Moolenaare96a2492019-06-25 04:12:16 +02001485 char_u *(*func)(int, void *, int, int))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001486{
1487#ifdef FEAT_EVAL
Bram Moolenaare96a2492019-06-25 04:12:16 +02001488 char_u *(*gp)(int, void *, int, int);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001489 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001491 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1492 // function that's originally used to obtain the lines. This may be
1493 // nested several levels.
Bram Moolenaar89d40322006-08-29 15:30:07 +00001494 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001495 cp = (struct loop_cookie *)cookie;
1496 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001497 {
1498 gp = cp->getline;
1499 cp = cp->cookie;
1500 }
1501 return gp == func;
1502#else
Bram Moolenaar89d40322006-08-29 15:30:07 +00001503 return fgetline == func;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001504#endif
1505}
1506
Bram Moolenaar071d4272004-06-13 20:20:40 +00001507/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001508 * If "fgetline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001509 * getline function. Otherwise return "cookie".
1510 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511 void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001512getline_cookie(
Bram Moolenaare96a2492019-06-25 04:12:16 +02001513 char_u *(*fgetline)(int, void *, int, int) UNUSED,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001514 void *cookie) // argument for fgetline()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001515{
Bram Moolenaar13505972019-01-24 15:04:48 +01001516#ifdef FEAT_EVAL
Bram Moolenaare96a2492019-06-25 04:12:16 +02001517 char_u *(*gp)(int, void *, int, int);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001518 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001519
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001520 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1521 // cookie that's originally used to obtain the lines. This may be nested
1522 // several levels.
Bram Moolenaar89d40322006-08-29 15:30:07 +00001523 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001524 cp = (struct loop_cookie *)cookie;
1525 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001526 {
1527 gp = cp->getline;
1528 cp = cp->cookie;
1529 }
1530 return cp;
Bram Moolenaar13505972019-01-24 15:04:48 +01001531#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001532 return cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001533#endif
Bram Moolenaar13505972019-01-24 15:04:48 +01001534}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001535
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001536
1537/*
1538 * Helper function to apply an offset for buffer commands, i.e. ":bdelete",
1539 * ":bwipeout", etc.
1540 * Returns the buffer number.
1541 */
1542 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001543compute_buffer_local_count(int addr_type, int lnum, int offset)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001544{
1545 buf_T *buf;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001546 buf_T *nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001547 int count = offset;
1548
1549 buf = firstbuf;
1550 while (buf->b_next != NULL && buf->b_fnum < lnum)
1551 buf = buf->b_next;
1552 while (count != 0)
1553 {
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001554 count += (offset < 0) ? 1 : -1;
1555 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1556 if (nextbuf == NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001557 break;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001558 buf = nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001559 if (addr_type == ADDR_LOADED_BUFFERS)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001560 // skip over unloaded buffers
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001561 while (buf->b_ml.ml_mfp == NULL)
1562 {
1563 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1564 if (nextbuf == NULL)
1565 break;
1566 buf = nextbuf;
1567 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001568 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001569 // we might have gone too far, last buffer is not loadedd
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001570 if (addr_type == ADDR_LOADED_BUFFERS)
1571 while (buf->b_ml.ml_mfp == NULL)
1572 {
1573 nextbuf = (offset >= 0) ? buf->b_prev : buf->b_next;
1574 if (nextbuf == NULL)
1575 break;
1576 buf = nextbuf;
1577 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001578 return buf->b_fnum;
1579}
1580
Bram Moolenaarb7316892019-05-01 18:08:42 +02001581/*
1582 * Return the window number of "win".
1583 * When "win" is NULL return the number of windows.
1584 */
Bram Moolenaarf240e182014-11-27 18:33:02 +01001585 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001586current_win_nr(win_T *win)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001587{
1588 win_T *wp;
1589 int nr = 0;
1590
Bram Moolenaar29323592016-07-24 22:04:11 +02001591 FOR_ALL_WINDOWS(wp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001592 {
1593 ++nr;
1594 if (wp == win)
1595 break;
1596 }
1597 return nr;
1598}
1599
1600 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001601current_tab_nr(tabpage_T *tab)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001602{
1603 tabpage_T *tp;
1604 int nr = 0;
1605
Bram Moolenaar29323592016-07-24 22:04:11 +02001606 FOR_ALL_TABPAGES(tp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001607 {
1608 ++nr;
1609 if (tp == tab)
1610 break;
1611 }
1612 return nr;
1613}
1614
1615# define CURRENT_WIN_NR current_win_nr(curwin)
1616# define LAST_WIN_NR current_win_nr(NULL)
1617# define CURRENT_TAB_NR current_tab_nr(curtab)
1618# define LAST_TAB_NR current_tab_nr(NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001619
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620/*
1621 * Execute one Ex command.
1622 *
1623 * If 'sourcing' is TRUE, the command will be included in the error message.
1624 *
1625 * 1. skip comment lines and leading space
1626 * 2. handle command modifiers
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001627 * 3. find the command
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001628 * 4. parse range
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001629 * 5. Parse the command.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001630 * 6. parse arguments
1631 * 7. switch on command name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001632 *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001633 * Note: "fgetline" can be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001634 *
1635 * This function may be called recursively!
1636 */
1637#if (_MSC_VER == 1200)
1638/*
Bram Moolenaared203462004-06-16 11:19:22 +00001639 * Avoid optimisation bug in VC++ version 6.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001640 */
Bram Moolenaar281bdce2005-01-25 21:53:18 +00001641 #pragma optimize( "g", off )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001642#endif
1643 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001644do_one_cmd(
Bram Moolenaarddef1292019-12-16 17:10:33 +01001645 char_u **cmdlinep,
1646 int sourcing,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001647#ifdef FEAT_EVAL
Bram Moolenaarddef1292019-12-16 17:10:33 +01001648 cstack_T *cstack,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001649#endif
Bram Moolenaarddef1292019-12-16 17:10:33 +01001650 char_u *(*fgetline)(int, void *, int, int),
1651 void *cookie) // argument for fgetline()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001652{
Bram Moolenaarddef1292019-12-16 17:10:33 +01001653 char_u *p;
1654 linenr_T lnum;
1655 long n;
1656 char *errormsg = NULL; // error message
1657 char_u *after_modifier = NULL;
1658 exarg_T ea; // Ex command arguments
1659 int save_msg_scroll = msg_scroll;
1660 cmdmod_T save_cmdmod;
1661 int save_reg_executing = reg_executing;
1662 int ni; // set when Not Implemented
1663 char_u *cmd;
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001664#ifdef FEAT_EVAL
1665 int starts_with_colon;
1666#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667
Bram Moolenaara80faa82020-04-12 19:37:17 +02001668 CLEAR_FIELD(ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669 ea.line1 = 1;
1670 ea.line2 = 1;
1671#ifdef FEAT_EVAL
1672 ++ex_nesting_level;
1673#endif
1674
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001675 // When the last file has not been edited :q has to be typed twice.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001676 if (quitmore
1677#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001678 // avoid that a function call in 'statusline' does this
Bram Moolenaar89d40322006-08-29 15:30:07 +00001679 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001680#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001681 // avoid that an autocommand, e.g. QuitPre, does this
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001682 && !getline_equal(fgetline, cookie, getnextac))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001683 --quitmore;
1684
1685 /*
1686 * Reset browse, confirm, etc.. They are restored when returning, for
1687 * recursive calls.
1688 */
1689 save_cmdmod = cmdmod;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001690
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001691 // "#!anything" is handled like a comment.
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001692 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1693 goto doend;
1694
Bram Moolenaar4facea32019-10-12 20:17:40 +02001695 if (p_verbose >= 16)
1696 msg_verbose_cmd(0, *cmdlinep);
1697
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02001698/*
1699 * 1. Skip comment lines and leading white space and colons.
1700 * 2. Handle command modifiers.
1701 */
1702 // The "ea" structure holds the arguments that can be used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001703 ea.cmd = *cmdlinep;
Bram Moolenaareffed932018-08-14 13:38:17 +02001704 ea.cmdlinep = cmdlinep;
1705 ea.getline = fgetline;
1706 ea.cookie = cookie;
1707#ifdef FEAT_EVAL
1708 ea.cstack = cstack;
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001709 starts_with_colon = *skipwhite(ea.cmd) == ':';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710#endif
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02001711 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaareffed932018-08-14 13:38:17 +02001712 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001714 after_modifier = ea.cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001715
1716#ifdef FEAT_EVAL
1717 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
1718 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
1719#else
1720 ea.skip = (if_level > 0);
1721#endif
1722
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001724 * 3. Skip over the range to find the command. Let "p" point to after it.
1725 *
1726 * We need the command to know what kind of range it uses.
1727 */
1728 cmd = ea.cmd;
1729 ea.cmd = skip_range(ea.cmd, NULL);
1730 if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
1731 ea.cmd = skipwhite(ea.cmd + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001732
1733#ifdef FEAT_EVAL
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001734 if (current_sctx.sc_version == SCRIPT_VERSION_VIM9 && !starts_with_colon)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001735 p = find_ex_command(&ea, NULL, lookup_scriptvar, NULL);
1736 else
1737#endif
1738 p = find_ex_command(&ea, NULL, NULL, NULL);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001739
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001740#ifdef FEAT_EVAL
1741# ifdef FEAT_PROFILE
1742 // Count this line for profiling if skip is TRUE.
1743 if (do_profiling == PROF_YES
1744 && (!ea.skip || cstack->cs_idx == 0 || (cstack->cs_idx > 0
1745 && (cstack->cs_flags[cstack->cs_idx - 1] & CSF_ACTIVE))))
1746 {
1747 int skip = did_emsg || got_int || did_throw;
1748
1749 if (ea.cmdidx == CMD_catch)
1750 skip = !skip && !(cstack->cs_idx >= 0
1751 && (cstack->cs_flags[cstack->cs_idx] & CSF_THROWN)
1752 && !(cstack->cs_flags[cstack->cs_idx] & CSF_CAUGHT));
1753 else if (ea.cmdidx == CMD_else || ea.cmdidx == CMD_elseif)
1754 skip = skip || !(cstack->cs_idx >= 0
1755 && !(cstack->cs_flags[cstack->cs_idx]
1756 & (CSF_ACTIVE | CSF_TRUE)));
1757 else if (ea.cmdidx == CMD_finally)
1758 skip = FALSE;
1759 else if (ea.cmdidx != CMD_endif
1760 && ea.cmdidx != CMD_endfor
1761 && ea.cmdidx != CMD_endtry
1762 && ea.cmdidx != CMD_endwhile)
1763 skip = ea.skip;
1764
1765 if (!skip)
1766 {
1767 if (getline_equal(fgetline, cookie, get_func_line))
1768 func_line_exec(getline_cookie(fgetline, cookie));
1769 else if (getline_equal(fgetline, cookie, getsourceline))
1770 script_line_exec();
1771 }
1772 }
1773# endif
1774
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001775 // May go to debug mode. If this happens and the ">quit" debug command is
1776 // used, throw an interrupt exception and skip the next command.
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001777 dbg_check_breakpoint(&ea);
1778 if (!ea.skip && got_int)
1779 {
1780 ea.skip = TRUE;
1781 (void)do_intthrow(cstack);
1782 }
1783#endif
1784
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001785/*
1786 * 4. parse a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787 *
1788 * where 'addr' is:
1789 *
1790 * % (entire file)
1791 * $ [+-NUM]
1792 * 'x [+-NUM] (where x denotes a currently defined mark)
1793 * . [+-NUM]
1794 * [+-NUM]..
1795 * NUM
1796 *
1797 * The ea.cmd pointer is updated to point to the first character following the
1798 * range spec. If an initial address is found, but no second, the upper bound
1799 * is equal to the lower.
1800 */
1801
Bram Moolenaar25190db2019-05-04 15:05:28 +02001802 // ea.addr_type for user commands is set by find_ucmd
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001803 if (!IS_USER_CMDIDX(ea.cmdidx))
1804 {
1805 if (ea.cmdidx != CMD_SIZE)
1806 ea.addr_type = cmdnames[(int)ea.cmdidx].cmd_addr_type;
1807 else
1808 ea.addr_type = ADDR_LINES;
1809
Bram Moolenaar25190db2019-05-04 15:05:28 +02001810 // :wincmd range depends on the argument.
Bram Moolenaar164f3262015-01-14 21:22:01 +01001811 if (ea.cmdidx == CMD_wincmd && p != NULL)
1812 get_wincmd_addr_type(skipwhite(p), &ea);
Bram Moolenaar25190db2019-05-04 15:05:28 +02001813#ifdef FEAT_QUICKFIX
1814 // :.cc in quickfix window uses line number
1815 if ((ea.cmdidx == CMD_cc || ea.cmdidx == CMD_ll) && bt_quickfix(curbuf))
1816 ea.addr_type = ADDR_OTHER;
1817#endif
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001818 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001819
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001820 ea.cmd = cmd;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02001821 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaaree8415b2018-08-10 23:13:12 +02001822 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823
Bram Moolenaar071d4272004-06-13 20:20:40 +00001824/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001825 * 5. Parse the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826 */
1827
1828 /*
1829 * Skip ':' and any white space
1830 */
1831 ea.cmd = skipwhite(ea.cmd);
1832 while (*ea.cmd == ':')
1833 ea.cmd = skipwhite(ea.cmd + 1);
1834
1835 /*
1836 * If we got a line, but no command, then go to the line.
1837 * If we find a '|' or '\n' we set ea.nextcmd.
1838 */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001839 if (*ea.cmd == NUL || *ea.cmd == '"'
Bram Moolenaar7a092242020-04-16 22:10:49 +02001840#ifdef FEAT_EVAL
Bram Moolenaara26b9702020-04-18 19:53:28 +02001841 || (*ea.cmd == '#' && ea.cmd[1] != '{'
1842 && !starts_with_colon && in_vim9script())
Bram Moolenaar7a092242020-04-16 22:10:49 +02001843#endif
1844 || (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845 {
1846 /*
1847 * strange vi behaviour:
1848 * ":3" jumps to line 3
1849 * ":3|..." prints line 3
1850 * ":|" prints current line
1851 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001852 if (ea.skip) // skip this if inside :if
Bram Moolenaar071d4272004-06-13 20:20:40 +00001853 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001854 if (*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855 {
1856 ea.cmdidx = CMD_print;
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001857 ea.argt = EX_RANGE+EX_COUNT+EX_TRLBAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001858 if ((errormsg = invalid_range(&ea)) == NULL)
1859 {
1860 correct_range(&ea);
1861 ex_print(&ea);
1862 }
1863 }
1864 else if (ea.addr_count != 0)
1865 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001866 if (ea.line2 > curbuf->b_ml.ml_line_count)
1867 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001868 // With '-' in 'cpoptions' a line number past the file is an
1869 // error, otherwise put it at the end of the file.
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001870 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
1871 ea.line2 = -1;
1872 else
1873 ea.line2 = curbuf->b_ml.ml_line_count;
1874 }
1875
1876 if (ea.line2 < 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001877 errormsg = _(e_invrange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001878 else
1879 {
1880 if (ea.line2 == 0)
1881 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001882 else
1883 curwin->w_cursor.lnum = ea.line2;
1884 beginline(BL_SOL | BL_FIX);
1885 }
1886 }
1887 goto doend;
1888 }
1889
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001890 // If this looks like an undefined user command and there are CmdUndefined
1891 // autocommands defined, trigger the matching autocommands.
Bram Moolenaard5005162014-08-22 23:05:54 +02001892 if (p != NULL && ea.cmdidx == CMD_SIZE && !ea.skip
1893 && ASCII_ISUPPER(*ea.cmd)
1894 && has_cmdundefined())
1895 {
Bram Moolenaard5005162014-08-22 23:05:54 +02001896 int ret;
1897
Bram Moolenaar5a31b462014-08-23 14:16:20 +02001898 p = ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02001899 while (ASCII_ISALNUM(*p))
1900 ++p;
Bram Moolenaar120f4a82014-09-09 12:22:06 +02001901 p = vim_strnsave(ea.cmd, (int)(p - ea.cmd));
Bram Moolenaard5005162014-08-22 23:05:54 +02001902 ret = apply_autocmds(EVENT_CMDUNDEFINED, p, p, TRUE, NULL);
1903 vim_free(p);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001904 // If the autocommands did something and didn't cause an error, try
1905 // finding the command again.
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001906 p = (ret
1907#ifdef FEAT_EVAL
1908 && !aborting()
Bram Moolenaard5005162014-08-22 23:05:54 +02001909#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001910 ) ? find_ex_command(&ea, NULL, NULL, NULL) : ea.cmd;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001911 }
Bram Moolenaard5005162014-08-22 23:05:54 +02001912
Bram Moolenaar071d4272004-06-13 20:20:40 +00001913 if (p == NULL)
1914 {
1915 if (!ea.skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001916 errormsg = _("E464: Ambiguous use of user-defined command");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917 goto doend;
1918 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001919 // Check for wrong commands.
Bram Moolenaar6f9a4762017-06-22 20:39:17 +02001920 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78
1921 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001922 {
1923 errormsg = uc_fun_cmd();
1924 goto doend;
1925 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001926
Bram Moolenaar071d4272004-06-13 20:20:40 +00001927 if (ea.cmdidx == CMD_SIZE)
1928 {
1929 if (!ea.skip)
1930 {
1931 STRCPY(IObuff, _("E492: Not an editor command"));
1932 if (!sourcing)
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001933 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001934 // If the modifier was parsed OK the error must be in the
1935 // following command
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001936 if (after_modifier != NULL)
1937 append_command(after_modifier);
1938 else
1939 append_command(*cmdlinep);
1940 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001941 errormsg = (char *)IObuff;
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001942 did_emsg_syntax = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001943 }
1944 goto doend;
1945 }
1946
Bram Moolenaar958636c2014-10-21 20:01:58 +02001947 ni = (!IS_USER_CMDIDX(ea.cmdidx)
1948 && (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00001949#ifdef HAVE_EX_SCRIPT_NI
1950 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
1951#endif
1952 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001953
1954#ifndef FEAT_EVAL
1955 /*
1956 * When the expression evaluation is disabled, recognize the ":if" and
1957 * ":endif" commands and ignore everything in between it.
1958 */
1959 if (ea.cmdidx == CMD_if)
1960 ++if_level;
1961 if (if_level)
1962 {
1963 if (ea.cmdidx == CMD_endif)
1964 --if_level;
1965 goto doend;
1966 }
1967
1968#endif
1969
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001970 // forced commands
Bram Moolenaar196b3b02008-06-20 16:51:41 +00001971 if (*p == '!' && ea.cmdidx != CMD_substitute
1972 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001973 {
1974 ++p;
1975 ea.forceit = TRUE;
1976 }
1977 else
1978 ea.forceit = FALSE;
1979
1980/*
Bram Moolenaarb7316892019-05-01 18:08:42 +02001981 * 6. Parse arguments. Then check for errors.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02001983 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001984 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001985
1986 if (!ea.skip)
1987 {
1988#ifdef HAVE_SANDBOX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001989 if (sandbox != 0 && !(ea.argt & EX_SBOXOK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990 {
Bram Moolenaar8c62a082019-02-08 14:34:10 +01001991 // Command not allowed in sandbox.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001992 errormsg = _(e_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001993 goto doend;
1994 }
1995#endif
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001996 if (restricted != 0 && (ea.argt & EX_RESTRICT))
Bram Moolenaar8c62a082019-02-08 14:34:10 +01001997 {
1998 errormsg = _("E981: Command not allowed in rvim");
1999 goto doend;
2000 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002001 if (!curbuf->b_p_ma && (ea.argt & EX_MODIFY))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002002 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002003 // Command not allowed in non-'modifiable' buffer
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002004 errormsg = _(e_modifiable);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002005 goto doend;
2006 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002007
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002008 if (text_locked() && !(ea.argt & EX_CMDWIN)
Bram Moolenaar958636c2014-10-21 20:01:58 +02002009 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002010 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002011 // Command not allowed when editing the command line.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002012 errormsg = _(get_text_locked_msg());
Bram Moolenaar071d4272004-06-13 20:20:40 +00002013 goto doend;
2014 }
Bram Moolenaar379fb762018-08-30 15:58:28 +02002015
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002016 // Disallow editing another buffer when "curbuf_lock" is set.
2017 // Do allow ":checktime" (it is postponed).
2018 // Do allow ":edit" (check for an argument later).
2019 // Do allow ":file" with no arguments (check for an argument later).
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002020 if (!(ea.argt & EX_CMDWIN)
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002021 && ea.cmdidx != CMD_checktime
Bram Moolenaar379fb762018-08-30 15:58:28 +02002022 && ea.cmdidx != CMD_edit
2023 && ea.cmdidx != CMD_file
Bram Moolenaar958636c2014-10-21 20:01:58 +02002024 && !IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002025 && curbuf_locked())
2026 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002027
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002028 if (!ni && !(ea.argt & EX_RANGE) && ea.addr_count > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002029 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002030 // no range allowed
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002031 errormsg = _(e_norange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002032 goto doend;
2033 }
2034 }
2035
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002036 if (!ni && !(ea.argt & EX_BANG) && ea.forceit) // no <!> allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002037 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002038 errormsg = _(e_nobang);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002039 goto doend;
2040 }
2041
2042 /*
2043 * Don't complain about the range if it is not used
2044 * (could happen if line_count is accidentally set to 0).
2045 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002046 if (!ea.skip && !ni && (ea.argt & EX_RANGE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002047 {
2048 /*
2049 * If the range is backwards, ask for confirmation and, if given, swap
2050 * ea.line1 & ea.line2 so it's forwards again.
2051 * When global command is busy, don't ask, will fail below.
2052 */
2053 if (!global_busy && ea.line1 > ea.line2)
2054 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002055 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002056 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002057 if (sourcing || exmode_active)
2058 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002059 errormsg = _("E493: Backwards range given");
Bram Moolenaara5792f52005-11-23 21:25:05 +00002060 goto doend;
2061 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002062 if (ask_yesno((char_u *)
2063 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002064 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002065 }
2066 lnum = ea.line1;
2067 ea.line1 = ea.line2;
2068 ea.line2 = lnum;
2069 }
2070 if ((errormsg = invalid_range(&ea)) != NULL)
2071 goto doend;
2072 }
2073
Bram Moolenaarb7316892019-05-01 18:08:42 +02002074 if ((ea.addr_type == ADDR_OTHER) && ea.addr_count == 0)
2075 // default is 1, not cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002076 ea.line2 = 1;
2077
2078 correct_range(&ea);
2079
2080#ifdef FEAT_FOLDING
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002081 if (((ea.argt & EX_WHOLEFOLD) || ea.addr_count >= 2) && !global_busy
Bram Moolenaara3306952016-01-02 21:41:06 +01002082 && ea.addr_type == ADDR_LINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002083 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002084 // Put the first line at the start of a closed fold, put the last line
2085 // at the end of a closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002086 (void)hasFolding(ea.line1, &ea.line1, NULL);
2087 (void)hasFolding(ea.line2, NULL, &ea.line2);
2088 }
2089#endif
2090
2091#ifdef FEAT_QUICKFIX
2092 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002093 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002094 * option here, so things like % get expanded.
2095 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002096 p = replace_makeprg(&ea, p, cmdlinep);
2097 if (p == NULL)
2098 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002099#endif
2100
2101 /*
2102 * Skip to start of argument.
2103 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2104 */
2105 if (ea.cmdidx == CMD_bang)
2106 ea.arg = p;
2107 else
2108 ea.arg = skipwhite(p);
2109
Bram Moolenaar379fb762018-08-30 15:58:28 +02002110 // ":file" cannot be run with an argument when "curbuf_lock" is set
2111 if (ea.cmdidx == CMD_file && *ea.arg != NUL && curbuf_locked())
2112 goto doend;
2113
Bram Moolenaar071d4272004-06-13 20:20:40 +00002114 /*
2115 * Check for "++opt=val" argument.
2116 * Must be first, allow ":w ++enc=utf8 !cmd"
2117 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002118 if (ea.argt & EX_ARGOPT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002119 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2120 if (getargopt(&ea) == FAIL && !ni)
2121 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002122 errormsg = _(e_invarg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002123 goto doend;
2124 }
2125
2126 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2127 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002128 if (*ea.arg == '>') // append
Bram Moolenaar071d4272004-06-13 20:20:40 +00002129 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002130 if (*++ea.arg != '>') // typed wrong
Bram Moolenaar071d4272004-06-13 20:20:40 +00002131 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002132 errormsg = _("E494: Use w or w>>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002133 goto doend;
2134 }
2135 ea.arg = skipwhite(ea.arg + 1);
2136 ea.append = TRUE;
2137 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002138 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) // :w !filter
Bram Moolenaar071d4272004-06-13 20:20:40 +00002139 {
2140 ++ea.arg;
2141 ea.usefilter = TRUE;
2142 }
2143 }
2144
2145 if (ea.cmdidx == CMD_read)
2146 {
2147 if (ea.forceit)
2148 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002149 ea.usefilter = TRUE; // :r! filter if ea.forceit
Bram Moolenaar071d4272004-06-13 20:20:40 +00002150 ea.forceit = FALSE;
2151 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002152 else if (*ea.arg == '!') // :r !filter
Bram Moolenaar071d4272004-06-13 20:20:40 +00002153 {
2154 ++ea.arg;
2155 ea.usefilter = TRUE;
2156 }
2157 }
2158
2159 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2160 {
2161 ea.amount = 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002162 while (*ea.arg == *ea.cmd) // count number of '>' or '<'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163 {
2164 ++ea.arg;
2165 ++ea.amount;
2166 }
2167 ea.arg = skipwhite(ea.arg);
2168 }
2169
2170 /*
2171 * Check for "+command" argument, before checking for next command.
2172 * Don't do this for ":read !cmd" and ":write !cmd".
2173 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002174 if ((ea.argt & EX_CMDARG) && !ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2176
2177 /*
2178 * Check for '|' to separate commands and '"' to start comments.
2179 * Don't do this for ":read !cmd" and ":write !cmd".
2180 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002181 if ((ea.argt & EX_TRLBAR) && !ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002182 separate_nextcmd(&ea);
2183
2184 /*
2185 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002186 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2187 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002188 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002189 else if (ea.cmdidx == CMD_bang
Bram Moolenaar67883b42017-07-27 22:57:00 +02002190 || ea.cmdidx == CMD_terminal
Bram Moolenaardf177f62005-02-22 08:39:57 +00002191 || ea.cmdidx == CMD_global
2192 || ea.cmdidx == CMD_vglobal
2193 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002194 {
2195 for (p = ea.arg; *p; ++p)
2196 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002197 // Remove one backslash before a newline, so that it's possible to
2198 // pass a newline to the shell and also a newline that is preceded
2199 // with a backslash. This makes it impossible to end a shell
2200 // command in a backslash, but that doesn't appear useful.
2201 // Halving the number of backslashes is incompatible with previous
2202 // versions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002203 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002204 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002205 else if (*p == '\n')
2206 {
2207 ea.nextcmd = p + 1;
2208 *p = NUL;
2209 break;
2210 }
2211 }
2212 }
2213
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002214 if ((ea.argt & EX_DFLALL) && ea.addr_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002215 {
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002216 buf_T *buf;
2217
Bram Moolenaar071d4272004-06-13 20:20:40 +00002218 ea.line1 = 1;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002219 switch (ea.addr_type)
2220 {
2221 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02002222 case ADDR_OTHER:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002223 ea.line2 = curbuf->b_ml.ml_line_count;
2224 break;
2225 case ADDR_LOADED_BUFFERS:
2226 buf = firstbuf;
2227 while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL)
2228 buf = buf->b_next;
2229 ea.line1 = buf->b_fnum;
2230 buf = lastbuf;
2231 while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL)
2232 buf = buf->b_prev;
2233 ea.line2 = buf->b_fnum;
2234 break;
2235 case ADDR_BUFFERS:
2236 ea.line1 = firstbuf->b_fnum;
2237 ea.line2 = lastbuf->b_fnum;
2238 break;
2239 case ADDR_WINDOWS:
2240 ea.line2 = LAST_WIN_NR;
2241 break;
2242 case ADDR_TABS:
2243 ea.line2 = LAST_TAB_NR;
2244 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01002245 case ADDR_TABS_RELATIVE:
2246 ea.line2 = 1;
2247 break;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002248 case ADDR_ARGUMENTS:
2249 if (ARGCOUNT == 0)
2250 ea.line1 = ea.line2 = 0;
2251 else
2252 ea.line2 = ARGCOUNT;
2253 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02002254 case ADDR_QUICKFIX_VALID:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002255#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02002256 ea.line2 = qf_get_valid_size(&ea);
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002257 if (ea.line2 == 0)
2258 ea.line2 = 1;
Bram Moolenaare906c502015-09-09 21:10:39 +02002259#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002260 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02002261 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02002262 case ADDR_UNSIGNED:
2263 case ADDR_QUICKFIX:
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002264 iemsg(_("INTERNAL: Cannot use EX_DFLALL with ADDR_NONE, ADDR_UNSIGNED or ADDR_QUICKFIX"));
Bram Moolenaarb7316892019-05-01 18:08:42 +02002265 break;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002266 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002267 }
2268
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002269 // accept numbered register only when no count allowed (:put)
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002270 if ( (ea.argt & EX_REGSTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271 && *ea.arg != NUL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002272 // Do not allow register = for user commands
Bram Moolenaar958636c2014-10-21 20:01:58 +02002273 && (!IS_USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002274 && !((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002275 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002276#ifndef FEAT_CLIPBOARD
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002277 // check these explicitly for a more specific error message
Bram Moolenaar85de2062011-05-05 14:26:41 +02002278 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002279 {
Bram Moolenaar99b12722019-01-14 20:16:40 +01002280 errormsg = _(e_invalidreg);
Bram Moolenaar85de2062011-05-05 14:26:41 +02002281 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282 }
2283#endif
Bram Moolenaar958636c2014-10-21 20:01:58 +02002284 if (valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2285 && !IS_USER_CMDIDX(ea.cmdidx))))
Bram Moolenaar85de2062011-05-05 14:26:41 +02002286 {
2287 ea.regname = *ea.arg++;
2288#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002289 // for '=' register: accept the rest of the line as an expression
Bram Moolenaar85de2062011-05-05 14:26:41 +02002290 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2291 {
2292 set_expr_line(vim_strsave(ea.arg));
2293 ea.arg += STRLEN(ea.arg);
2294 }
2295#endif
2296 ea.arg = skipwhite(ea.arg);
2297 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002298 }
2299
2300 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002301 * Check for a count. When accepting a EX_BUFNAME, don't use "123foo" as a
Bram Moolenaar071d4272004-06-13 20:20:40 +00002302 * count, it's a buffer name.
2303 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002304 if ((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)
2305 && (!(ea.argt & EX_BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01002306 || VIM_ISWHITE(*p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307 {
2308 n = getdigits(&ea.arg);
2309 ea.arg = skipwhite(ea.arg);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002310 if (n <= 0 && !ni && (ea.argt & EX_ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002311 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002312 errormsg = _(e_zerocount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313 goto doend;
2314 }
Bram Moolenaarb7316892019-05-01 18:08:42 +02002315 if (ea.addr_type != ADDR_LINES) // e.g. :buffer 2, :sleep 3
Bram Moolenaar071d4272004-06-13 20:20:40 +00002316 {
2317 ea.line2 = n;
2318 if (ea.addr_count == 0)
2319 ea.addr_count = 1;
2320 }
2321 else
2322 {
2323 ea.line1 = ea.line2;
2324 ea.line2 += n - 1;
2325 ++ea.addr_count;
2326 /*
2327 * Be vi compatible: no error message for out of range.
2328 */
Bram Moolenaarb7316892019-05-01 18:08:42 +02002329 if (ea.line2 > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002330 ea.line2 = curbuf->b_ml.ml_line_count;
2331 }
2332 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002333
2334 /*
2335 * Check for flags: 'l', 'p' and '#'.
2336 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002337 if (ea.argt & EX_FLAGS)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002338 get_flags(&ea);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002339 if (!ni && !(ea.argt & EX_EXTRA) && *ea.arg != NUL
2340 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & EX_TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002342 // no arguments allowed but there is something
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002343 errormsg = _(e_trailing);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344 goto doend;
2345 }
2346
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002347 if (!ni && (ea.argt & EX_NEEDARG) && *ea.arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002349 errormsg = _(e_argreq);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002350 goto doend;
2351 }
2352
2353#ifdef FEAT_EVAL
2354 /*
2355 * Skip the command when it's not going to be executed.
2356 * The commands like :if, :endif, etc. always need to be executed.
2357 * Also make an exception for commands that handle a trailing command
2358 * themselves.
2359 */
2360 if (ea.skip)
2361 {
2362 switch (ea.cmdidx)
2363 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002364 // commands that need evaluation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365 case CMD_while:
2366 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002367 case CMD_for:
2368 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369 case CMD_if:
2370 case CMD_elseif:
2371 case CMD_else:
2372 case CMD_endif:
2373 case CMD_try:
2374 case CMD_catch:
2375 case CMD_finally:
2376 case CMD_endtry:
2377 case CMD_function:
Bram Moolenaarab55c682020-03-01 19:41:43 +01002378 case CMD_def:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002379 break;
2380
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002381 // Commands that handle '|' themselves. Check: A command should
2382 // either have the EX_TRLBAR flag, appear in this list or appear in
2383 // the list at ":help :bar".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002384 case CMD_aboveleft:
2385 case CMD_and:
2386 case CMD_belowright:
2387 case CMD_botright:
2388 case CMD_browse:
2389 case CMD_call:
2390 case CMD_confirm:
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01002391 case CMD_const:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392 case CMD_delfunction:
2393 case CMD_djump:
2394 case CMD_dlist:
2395 case CMD_dsearch:
2396 case CMD_dsplit:
2397 case CMD_echo:
2398 case CMD_echoerr:
2399 case CMD_echomsg:
2400 case CMD_echon:
Bram Moolenaara76b3152020-02-16 16:20:21 +01002401 case CMD_eval:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402 case CMD_execute:
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002403 case CMD_filter:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002404 case CMD_help:
2405 case CMD_hide:
2406 case CMD_ijump:
2407 case CMD_ilist:
2408 case CMD_isearch:
2409 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002410 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411 case CMD_keepjumps:
2412 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01002413 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414 case CMD_leftabove:
2415 case CMD_let:
2416 case CMD_lockmarks:
Bram Moolenaarcc4423a2019-11-26 17:05:00 +01002417 case CMD_lockvar:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002418 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002420 case CMD_mzscheme:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01002421 case CMD_noautocmd:
2422 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002423 case CMD_perl:
2424 case CMD_psearch:
2425 case CMD_python:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002426 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002427 case CMD_python3:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002428 case CMD_return:
2429 case CMD_rightbelow:
2430 case CMD_ruby:
2431 case CMD_silent:
2432 case CMD_smagic:
2433 case CMD_snomagic:
2434 case CMD_substitute:
2435 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002436 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437 case CMD_tcl:
2438 case CMD_throw:
2439 case CMD_tilde:
2440 case CMD_topleft:
2441 case CMD_unlet:
Bram Moolenaarcc4423a2019-11-26 17:05:00 +01002442 case CMD_unlockvar:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443 case CMD_verbose:
2444 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002445 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002446 break;
2447
2448 default: goto doend;
2449 }
2450 }
2451#endif
2452
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002453 if (ea.argt & EX_XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454 {
2455 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2456 goto doend;
2457 }
2458
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459 /*
2460 * Accept buffer name. Cannot be used at the same time with a buffer
2461 * number. Don't do this for a user command.
2462 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002463 if ((ea.argt & EX_BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
Bram Moolenaar958636c2014-10-21 20:01:58 +02002464 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465 {
2466 /*
2467 * :bdelete, :bwipeout and :bunload take several arguments, separated
2468 * by spaces: find next space (skipping over escaped characters).
2469 * The others take one argument: ignore trailing spaces.
2470 */
2471 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2472 || ea.cmdidx == CMD_bunload)
2473 p = skiptowhite_esc(ea.arg);
2474 else
2475 {
2476 p = ea.arg + STRLEN(ea.arg);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002477 while (p > ea.arg && VIM_ISWHITE(p[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002478 --p;
2479 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002480 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & EX_BUFUNL) != 0,
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002481 FALSE, FALSE);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002482 if (ea.line2 < 0) // failed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483 goto doend;
2484 ea.addr_count = 1;
2485 ea.arg = skipwhite(p);
2486 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002488 // The :try command saves the emsg_silent flag, reset it here when
2489 // ":silent! try" was used, it should only apply to :try itself.
Bram Moolenaareffed932018-08-14 13:38:17 +02002490 if (ea.cmdidx == CMD_try && ea.did_esilent > 0)
Bram Moolenaar2be57332018-02-13 18:05:18 +01002491 {
Bram Moolenaareffed932018-08-14 13:38:17 +02002492 emsg_silent -= ea.did_esilent;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002493 if (emsg_silent < 0)
2494 emsg_silent = 0;
Bram Moolenaareffed932018-08-14 13:38:17 +02002495 ea.did_esilent = 0;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002496 }
2497
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498/*
Bram Moolenaar2be57332018-02-13 18:05:18 +01002499 * 7. Execute the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002500 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501
Bram Moolenaar958636c2014-10-21 20:01:58 +02002502 if (IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503 {
2504 /*
2505 * Execute a user-defined command.
2506 */
2507 do_ucmd(&ea);
2508 }
2509 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002510 {
2511 /*
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002512 * Call the function to execute the builtin command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002513 */
2514 ea.errmsg = NULL;
2515 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2516 if (ea.errmsg != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002517 errormsg = _(ea.errmsg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002518 }
2519
2520#ifdef FEAT_EVAL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002521 // Set flag that any command was executed, used by ex_vim9script().
Bram Moolenaar16531552020-02-08 16:00:46 +01002522 if (getline_equal(ea.getline, ea.cookie, getsourceline)
2523 && current_sctx.sc_sid > 0)
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002524 SCRIPT_ITEM(current_sctx.sc_sid)->sn_had_command = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002525
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526 /*
2527 * If the command just executed called do_cmdline(), any throw or ":return"
2528 * or ":finish" encountered there must also check the cstack of the still
2529 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2530 * exception, or reanimate a returned function or finished script file and
2531 * return or finish it again.
2532 */
2533 if (need_rethrow)
2534 do_throw(cstack);
2535 else if (check_cstack)
2536 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002537 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002539 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540 && current_func_returned())
2541 do_return(&ea, TRUE, FALSE, NULL);
2542 }
2543 need_rethrow = check_cstack = FALSE;
2544#endif
2545
2546doend:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002547 if (curwin->w_cursor.lnum == 0) // can happen with zero line number
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002548 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002549 curwin->w_cursor.lnum = 1;
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002550 curwin->w_cursor.col = 0;
2551 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002552
2553 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2554 {
2555 if (sourcing)
2556 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002557 if (errormsg != (char *)IObuff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002558 {
2559 STRCPY(IObuff, errormsg);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002560 errormsg = (char *)IObuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002562 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002563 }
2564 emsg(errormsg);
2565 }
2566#ifdef FEAT_EVAL
2567 do_errthrow(cstack,
Bram Moolenaar958636c2014-10-21 20:01:58 +02002568 (ea.cmdidx != CMD_SIZE && !IS_USER_CMDIDX(ea.cmdidx))
2569 ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002570#endif
2571
Bram Moolenaareffed932018-08-14 13:38:17 +02002572 if (ea.verbose_save >= 0)
2573 p_verbose = ea.verbose_save;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002574
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002575 free_cmdmod();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576 cmdmod = save_cmdmod;
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01002577 reg_executing = save_reg_executing;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578
Bram Moolenaareffed932018-08-14 13:38:17 +02002579 if (ea.save_msg_silent != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002581 // messages could be enabled for a serious error, need to check if the
2582 // counters don't become negative
Bram Moolenaareffed932018-08-14 13:38:17 +02002583 if (!did_emsg || msg_silent > ea.save_msg_silent)
2584 msg_silent = ea.save_msg_silent;
2585 emsg_silent -= ea.did_esilent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002586 if (emsg_silent < 0)
2587 emsg_silent = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002588 // Restore msg_scroll, it's set by file I/O commands, even when no
2589 // message is actually displayed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002590 msg_scroll = save_msg_scroll;
Bram Moolenaar77ab2802009-04-22 12:44:48 +00002591
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002592 // "silent reg" or "silent echo x" inside "redir" leaves msg_col
2593 // somewhere in the line. Put it back in the first column.
Bram Moolenaar77ab2802009-04-22 12:44:48 +00002594 if (redirecting())
2595 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002596 }
2597
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002598#ifdef HAVE_SANDBOX
Bram Moolenaareffed932018-08-14 13:38:17 +02002599 if (ea.did_sandbox)
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002600 --sandbox;
2601#endif
2602
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002603 if (ea.nextcmd && *ea.nextcmd == NUL) // not really a next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604 ea.nextcmd = NULL;
2605
2606#ifdef FEAT_EVAL
2607 --ex_nesting_level;
2608#endif
2609
2610 return ea.nextcmd;
2611}
2612#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00002613 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002614#endif
2615
2616/*
Bram Moolenaareffed932018-08-14 13:38:17 +02002617 * Parse and skip over command modifiers:
2618 * - update eap->cmd
2619 * - store flags in "cmdmod".
2620 * - Set ex_pressedreturn for an empty command line.
2621 * - set msg_silent for ":silent"
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002622 * - set 'eventignore' to "all" for ":noautocmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002623 * - set p_verbose for ":verbose"
2624 * - Increment "sandbox" for ":sandbox"
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002625 * When "skip_only" is TRUE the global variables are not changed, except for
2626 * "cmdmod".
Bram Moolenaareffed932018-08-14 13:38:17 +02002627 * Return FAIL when the command is not to be executed.
2628 * May set "errormsg" to an error message.
2629 */
2630 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002631parse_command_modifiers(exarg_T *eap, char **errormsg, int skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002632{
2633 char_u *p;
2634
Bram Moolenaara80faa82020-04-12 19:37:17 +02002635 CLEAR_FIELD(cmdmod);
Bram Moolenaareffed932018-08-14 13:38:17 +02002636 eap->verbose_save = -1;
2637 eap->save_msg_silent = -1;
2638
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002639 // Repeat until no more command modifiers are found.
Bram Moolenaareffed932018-08-14 13:38:17 +02002640 for (;;)
2641 {
Bram Moolenaareffed932018-08-14 13:38:17 +02002642 while (*eap->cmd == ' ' || *eap->cmd == '\t' || *eap->cmd == ':')
2643 ++eap->cmd;
2644
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002645 // in ex mode, an empty line works like :+
Bram Moolenaareffed932018-08-14 13:38:17 +02002646 if (*eap->cmd == NUL && exmode_active
2647 && (getline_equal(eap->getline, eap->cookie, getexmodeline)
2648 || getline_equal(eap->getline, eap->cookie, getexline))
2649 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2650 {
2651 eap->cmd = (char_u *)"+";
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002652 if (!skip_only)
2653 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002654 }
2655
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002656 // ignore comment and empty lines
Bram Moolenaareffed932018-08-14 13:38:17 +02002657 if (*eap->cmd == '"')
2658 return FAIL;
2659 if (*eap->cmd == NUL)
2660 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002661 if (!skip_only)
2662 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002663 return FAIL;
2664 }
2665
Bram Moolenaareffed932018-08-14 13:38:17 +02002666 p = skip_range(eap->cmd, NULL);
2667 switch (*p)
2668 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002669 // When adding an entry, also modify cmd_exists().
Bram Moolenaareffed932018-08-14 13:38:17 +02002670 case 'a': if (!checkforcmd(&eap->cmd, "aboveleft", 3))
2671 break;
2672 cmdmod.split |= WSP_ABOVE;
2673 continue;
2674
2675 case 'b': if (checkforcmd(&eap->cmd, "belowright", 3))
2676 {
2677 cmdmod.split |= WSP_BELOW;
2678 continue;
2679 }
2680 if (checkforcmd(&eap->cmd, "browse", 3))
2681 {
2682#ifdef FEAT_BROWSE_CMD
2683 cmdmod.browse = TRUE;
2684#endif
2685 continue;
2686 }
2687 if (!checkforcmd(&eap->cmd, "botright", 2))
2688 break;
2689 cmdmod.split |= WSP_BOT;
2690 continue;
2691
2692 case 'c': if (!checkforcmd(&eap->cmd, "confirm", 4))
2693 break;
2694#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2695 cmdmod.confirm = TRUE;
2696#endif
2697 continue;
2698
2699 case 'k': if (checkforcmd(&eap->cmd, "keepmarks", 3))
2700 {
2701 cmdmod.keepmarks = TRUE;
2702 continue;
2703 }
2704 if (checkforcmd(&eap->cmd, "keepalt", 5))
2705 {
2706 cmdmod.keepalt = TRUE;
2707 continue;
2708 }
2709 if (checkforcmd(&eap->cmd, "keeppatterns", 5))
2710 {
2711 cmdmod.keeppatterns = TRUE;
2712 continue;
2713 }
2714 if (!checkforcmd(&eap->cmd, "keepjumps", 5))
2715 break;
2716 cmdmod.keepjumps = TRUE;
2717 continue;
2718
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002719 case 'f': // only accept ":filter {pat} cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002720 {
2721 char_u *reg_pat;
2722
2723 if (!checkforcmd(&p, "filter", 4)
2724 || *p == NUL || ends_excmd(*p))
2725 break;
2726 if (*p == '!')
2727 {
2728 cmdmod.filter_force = TRUE;
2729 p = skipwhite(p + 1);
2730 if (*p == NUL || ends_excmd(*p))
2731 break;
2732 }
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002733 if (skip_only)
2734 p = skip_vimgrep_pat(p, NULL, NULL);
2735 else
2736 // NOTE: This puts a NUL after the pattern.
2737 p = skip_vimgrep_pat(p, &reg_pat, NULL);
Bram Moolenaareffed932018-08-14 13:38:17 +02002738 if (p == NULL || *p == NUL)
2739 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002740 if (!skip_only)
2741 {
2742 cmdmod.filter_regmatch.regprog =
Bram Moolenaareffed932018-08-14 13:38:17 +02002743 vim_regcomp(reg_pat, RE_MAGIC);
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002744 if (cmdmod.filter_regmatch.regprog == NULL)
2745 break;
2746 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002747 eap->cmd = p;
2748 continue;
2749 }
2750
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002751 // ":hide" and ":hide | cmd" are not modifiers
Bram Moolenaareffed932018-08-14 13:38:17 +02002752 case 'h': if (p != eap->cmd || !checkforcmd(&p, "hide", 3)
2753 || *p == NUL || ends_excmd(*p))
2754 break;
2755 eap->cmd = p;
2756 cmdmod.hide = TRUE;
2757 continue;
2758
2759 case 'l': if (checkforcmd(&eap->cmd, "lockmarks", 3))
2760 {
2761 cmdmod.lockmarks = TRUE;
2762 continue;
2763 }
2764
2765 if (!checkforcmd(&eap->cmd, "leftabove", 5))
2766 break;
2767 cmdmod.split |= WSP_ABOVE;
2768 continue;
2769
2770 case 'n': if (checkforcmd(&eap->cmd, "noautocmd", 3))
2771 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002772 if (cmdmod.save_ei == NULL && !skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002773 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002774 // Set 'eventignore' to "all". Restore the
2775 // existing option value later.
Bram Moolenaareffed932018-08-14 13:38:17 +02002776 cmdmod.save_ei = vim_strsave(p_ei);
2777 set_string_option_direct((char_u *)"ei", -1,
2778 (char_u *)"all", OPT_FREE, SID_NONE);
2779 }
2780 continue;
2781 }
2782 if (!checkforcmd(&eap->cmd, "noswapfile", 3))
2783 break;
2784 cmdmod.noswapfile = TRUE;
2785 continue;
2786
2787 case 'r': if (!checkforcmd(&eap->cmd, "rightbelow", 6))
2788 break;
2789 cmdmod.split |= WSP_BELOW;
2790 continue;
2791
2792 case 's': if (checkforcmd(&eap->cmd, "sandbox", 3))
2793 {
2794#ifdef HAVE_SANDBOX
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002795 if (!skip_only)
2796 {
2797 if (!eap->did_sandbox)
2798 ++sandbox;
2799 eap->did_sandbox = TRUE;
2800 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002801#endif
2802 continue;
2803 }
2804 if (!checkforcmd(&eap->cmd, "silent", 3))
2805 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002806 if (!skip_only)
2807 {
2808 if (eap->save_msg_silent == -1)
2809 eap->save_msg_silent = msg_silent;
2810 ++msg_silent;
2811 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002812 if (*eap->cmd == '!' && !VIM_ISWHITE(eap->cmd[-1]))
2813 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002814 // ":silent!", but not "silent !cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002815 eap->cmd = skipwhite(eap->cmd + 1);
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002816 if (!skip_only)
2817 {
2818 ++emsg_silent;
2819 ++eap->did_esilent;
2820 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002821 }
2822 continue;
2823
2824 case 't': if (checkforcmd(&p, "tab", 3))
2825 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002826 if (!skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002827 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002828 long tabnr = get_address(eap, &eap->cmd,
2829 ADDR_TABS, eap->skip,
2830 skip_only, FALSE, 1);
2831 if (tabnr == MAXLNUM)
2832 cmdmod.tab = tabpage_index(curtab) + 1;
2833 else
Bram Moolenaareffed932018-08-14 13:38:17 +02002834 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002835 if (tabnr < 0 || tabnr > LAST_TAB_NR)
2836 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002837 *errormsg = _(e_invrange);
Bram Moolenaar548e5982018-12-26 21:45:00 +01002838 return FAIL;
2839 }
2840 cmdmod.tab = tabnr + 1;
Bram Moolenaareffed932018-08-14 13:38:17 +02002841 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002842 }
2843 eap->cmd = p;
2844 continue;
2845 }
2846 if (!checkforcmd(&eap->cmd, "topleft", 2))
2847 break;
2848 cmdmod.split |= WSP_TOP;
2849 continue;
2850
2851 case 'u': if (!checkforcmd(&eap->cmd, "unsilent", 3))
2852 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002853 if (!skip_only)
2854 {
2855 if (eap->save_msg_silent == -1)
2856 eap->save_msg_silent = msg_silent;
2857 msg_silent = 0;
2858 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002859 continue;
2860
2861 case 'v': if (checkforcmd(&eap->cmd, "vertical", 4))
2862 {
2863 cmdmod.split |= WSP_VERT;
2864 continue;
2865 }
2866 if (!checkforcmd(&p, "verbose", 4))
2867 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002868 if (!skip_only)
2869 {
2870 if (eap->verbose_save < 0)
2871 eap->verbose_save = p_verbose;
2872 if (vim_isdigit(*eap->cmd))
2873 p_verbose = atoi((char *)eap->cmd);
2874 else
2875 p_verbose = 1;
2876 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002877 eap->cmd = p;
2878 continue;
2879 }
2880 break;
2881 }
2882
2883 return OK;
2884}
2885
2886/*
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002887 * Free contents of "cmdmod".
2888 */
2889 static void
2890free_cmdmod(void)
2891{
2892 if (cmdmod.save_ei != NULL)
2893 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002894 // Restore 'eventignore' to the value before ":noautocmd".
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002895 set_string_option_direct((char_u *)"ei", -1, cmdmod.save_ei,
2896 OPT_FREE, SID_NONE);
2897 free_string_option(cmdmod.save_ei);
2898 }
2899
2900 if (cmdmod.filter_regmatch.regprog != NULL)
2901 vim_regfree(cmdmod.filter_regmatch.regprog);
2902}
2903
2904/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002905 * Parse the address range, if any, in "eap".
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02002906 * May set the last search pattern, unless "silent" is TRUE.
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002907 * Return FAIL and set "errormsg" or return OK.
2908 */
2909 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002910parse_cmd_address(exarg_T *eap, char **errormsg, int silent)
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002911{
2912 int address_count = 1;
2913 linenr_T lnum;
2914
2915 // Repeat for all ',' or ';' separated addresses.
2916 for (;;)
2917 {
2918 eap->line1 = eap->line2;
2919 switch (eap->addr_type)
2920 {
2921 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02002922 case ADDR_OTHER:
Bram Moolenaarb5383b12020-05-18 19:46:48 +02002923 // Default is the cursor line number. Avoid using an invalid
2924 // line number though.
2925 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
2926 eap->line2 = curbuf->b_ml.ml_line_count;
2927 else
2928 eap->line2 = curwin->w_cursor.lnum;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002929 break;
2930 case ADDR_WINDOWS:
2931 eap->line2 = CURRENT_WIN_NR;
2932 break;
2933 case ADDR_ARGUMENTS:
2934 eap->line2 = curwin->w_arg_idx + 1;
2935 if (eap->line2 > ARGCOUNT)
2936 eap->line2 = ARGCOUNT;
2937 break;
2938 case ADDR_LOADED_BUFFERS:
2939 case ADDR_BUFFERS:
2940 eap->line2 = curbuf->b_fnum;
2941 break;
2942 case ADDR_TABS:
2943 eap->line2 = CURRENT_TAB_NR;
2944 break;
2945 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02002946 case ADDR_UNSIGNED:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002947 eap->line2 = 1;
2948 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002949 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002950#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02002951 eap->line2 = qf_get_cur_idx(eap);
2952#endif
2953 break;
2954 case ADDR_QUICKFIX_VALID:
2955#ifdef FEAT_QUICKFIX
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002956 eap->line2 = qf_get_cur_valid_idx(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002957#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002958 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02002959 case ADDR_NONE:
2960 // Will give an error later if a range is found.
2961 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002962 }
2963 eap->cmd = skipwhite(eap->cmd);
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02002964 lnum = get_address(eap, &eap->cmd, eap->addr_type, eap->skip, silent,
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002965 eap->addr_count == 0, address_count++);
2966 if (eap->cmd == NULL) // error detected
2967 return FAIL;
2968 if (lnum == MAXLNUM)
2969 {
2970 if (*eap->cmd == '%') // '%' - all lines
2971 {
2972 ++eap->cmd;
2973 switch (eap->addr_type)
2974 {
2975 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02002976 case ADDR_OTHER:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002977 eap->line1 = 1;
2978 eap->line2 = curbuf->b_ml.ml_line_count;
2979 break;
2980 case ADDR_LOADED_BUFFERS:
2981 {
2982 buf_T *buf = firstbuf;
2983
2984 while (buf->b_next != NULL
2985 && buf->b_ml.ml_mfp == NULL)
2986 buf = buf->b_next;
2987 eap->line1 = buf->b_fnum;
2988 buf = lastbuf;
2989 while (buf->b_prev != NULL
2990 && buf->b_ml.ml_mfp == NULL)
2991 buf = buf->b_prev;
2992 eap->line2 = buf->b_fnum;
2993 break;
2994 }
2995 case ADDR_BUFFERS:
2996 eap->line1 = firstbuf->b_fnum;
2997 eap->line2 = lastbuf->b_fnum;
2998 break;
2999 case ADDR_WINDOWS:
3000 case ADDR_TABS:
3001 if (IS_USER_CMDIDX(eap->cmdidx))
3002 {
3003 eap->line1 = 1;
3004 eap->line2 = eap->addr_type == ADDR_WINDOWS
3005 ? LAST_WIN_NR : LAST_TAB_NR;
3006 }
3007 else
3008 {
3009 // there is no Vim command which uses '%' and
3010 // ADDR_WINDOWS or ADDR_TABS
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003011 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003012 return FAIL;
3013 }
3014 break;
3015 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003016 case ADDR_UNSIGNED:
3017 case ADDR_QUICKFIX:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003018 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003019 return FAIL;
3020 case ADDR_ARGUMENTS:
3021 if (ARGCOUNT == 0)
3022 eap->line1 = eap->line2 = 0;
3023 else
3024 {
3025 eap->line1 = 1;
3026 eap->line2 = ARGCOUNT;
3027 }
3028 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003029 case ADDR_QUICKFIX_VALID:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003030#ifdef FEAT_QUICKFIX
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003031 eap->line1 = 1;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003032 eap->line2 = qf_get_valid_size(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003033 if (eap->line2 == 0)
3034 eap->line2 = 1;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003035#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003036 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003037 case ADDR_NONE:
3038 // Will give an error later if a range is found.
3039 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003040 }
3041 ++eap->addr_count;
3042 }
3043 else if (*eap->cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
3044 {
3045 pos_T *fp;
3046
3047 // '*' - visual area
3048 if (eap->addr_type != ADDR_LINES)
3049 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003050 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003051 return FAIL;
3052 }
3053
3054 ++eap->cmd;
3055 if (!eap->skip)
3056 {
3057 fp = getmark('<', FALSE);
3058 if (check_mark(fp) == FAIL)
3059 return FAIL;
3060 eap->line1 = fp->lnum;
3061 fp = getmark('>', FALSE);
3062 if (check_mark(fp) == FAIL)
3063 return FAIL;
3064 eap->line2 = fp->lnum;
3065 ++eap->addr_count;
3066 }
3067 }
3068 }
3069 else
3070 eap->line2 = lnum;
3071 eap->addr_count++;
3072
3073 if (*eap->cmd == ';')
3074 {
3075 if (!eap->skip)
3076 {
3077 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar0e717042020-04-27 19:29:01 +02003078 // Don't leave the cursor on an illegal line or column, but do
3079 // accept zero as address, so 0;/PATTERN/ works correctly.
3080 if (eap->line2 > 0)
3081 check_cursor();
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003082 }
3083 }
3084 else if (*eap->cmd != ',')
3085 break;
3086 ++eap->cmd;
3087 }
3088
3089 // One address given: set start and end lines.
3090 if (eap->addr_count == 1)
3091 {
3092 eap->line1 = eap->line2;
3093 // ... but only implicit: really no address given
3094 if (lnum == MAXLNUM)
3095 eap->addr_count = 0;
3096 }
3097 return OK;
3098}
3099
3100/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003101 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003102 * If there is a match advance "pp" to the argument and return TRUE.
3103 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003104 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003105checkforcmd(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003106 char_u **pp, // start of command
3107 char *cmd, // name of command
3108 int len) // required length
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109{
3110 int i;
3111
3112 for (i = 0; cmd[i] != NUL; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003113 if (((char_u *)cmd)[i] != (*pp)[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114 break;
3115 if (i >= len && !isalpha((*pp)[i]))
3116 {
3117 *pp = skipwhite(*pp + i);
3118 return TRUE;
3119 }
3120 return FALSE;
3121}
3122
3123/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003124 * Append "cmd" to the error message in IObuff.
3125 * Takes care of limiting the length and handling 0xa0, which would be
3126 * invisible otherwise.
3127 */
3128 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003129append_command(char_u *cmd)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003130{
3131 char_u *s = cmd;
3132 char_u *d;
3133
3134 STRCAT(IObuff, ": ");
3135 d = IObuff + STRLEN(IObuff);
3136 while (*s != NUL && d - IObuff < IOSIZE - 7)
3137 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003138 if (enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) : *s == 0xa0)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003139 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003140 s += enc_utf8 ? 2 : 1;
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003141 STRCPY(d, "<a0>");
3142 d += 4;
3143 }
3144 else
3145 MB_COPY_CHAR(s, d);
3146 }
3147 *d = NUL;
3148}
3149
3150/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003151 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003152 * Start of the name can be found at eap->cmd.
Bram Moolenaar25190db2019-05-04 15:05:28 +02003153 * Sets eap->cmdidx and returns a pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003154 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003155 *
3156 * If "lookup" is not NULL recognize expression without "eval" or "call" and
3157 * assignment without "let". Sets eap->cmdidx to the command while returning
3158 * "eap->cmd".
3159 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003160 * Returns NULL for an ambiguous user command.
3161 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003162 char_u *
3163find_ex_command(
3164 exarg_T *eap,
Bram Moolenaara494f562020-04-18 17:45:38 +02003165 int *full UNUSED,
Bram Moolenaarb84a3812020-05-01 15:44:29 +02003166 void *(*lookup)(char_u *, size_t, cctx_T *) UNUSED,
Bram Moolenaara494f562020-04-18 17:45:38 +02003167 cctx_T *cctx UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003168{
3169 int len;
3170 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003171 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003172
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003173#ifdef FEAT_EVAL
3174 /*
3175 * Recognize a Vim9 script function/method call and assignment:
3176 * "lvar = value", "lvar(arg)", "[1, 2 3]->Func()"
3177 */
Bram Moolenaar0c6ceaf2020-02-22 18:36:32 +01003178 p = eap->cmd;
3179 if (lookup != NULL && (*p == '('
3180 || ((p = to_name_const_end(eap->cmd)) > eap->cmd && *p != NUL)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003181 {
3182 int oplen;
3183 int heredoc;
3184
3185 // "funcname(" is always a function call.
3186 // "varname[]" is an expression.
3187 // "g:varname" is an expression.
3188 // "varname->expr" is an expression.
Bram Moolenaar0c6ceaf2020-02-22 18:36:32 +01003189 // "(..." is an expression.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003190 if (*p == '('
3191 || *p == '['
3192 || p[1] == ':'
3193 || (*p == '-' && p[1] == '>'))
3194 {
3195 eap->cmdidx = CMD_eval;
3196 return eap->cmd;
3197 }
3198
3199 oplen = assignment_len(skipwhite(p), &heredoc);
3200 if (oplen > 0)
3201 {
3202 // Recognize an assignment if we recognize the variable name:
3203 // "g:var = expr"
3204 // "var = expr" where "var" is a local var name.
3205 if (((p - eap->cmd) > 2 && eap->cmd[1] == ':')
Bram Moolenaarb84a3812020-05-01 15:44:29 +02003206 || lookup(eap->cmd, p - eap->cmd, cctx) != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003207 {
3208 eap->cmdidx = CMD_let;
3209 return eap->cmd;
3210 }
3211 }
3212 }
3213#endif
3214
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215 /*
3216 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00003217 * Exceptions:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218 * - the 'k' command can directly be followed by any character.
3219 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003220 * but :sre[wind] is another command, as are :scr[iptnames],
Bram Moolenaar071d4272004-06-13 20:20:40 +00003221 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00003222 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223 */
3224 p = eap->cmd;
3225 if (*p == 'k')
3226 {
3227 eap->cmdidx = CMD_k;
3228 ++p;
3229 }
3230 else if (p[0] == 's'
Bram Moolenaar204b93f2015-08-04 22:02:51 +02003231 && ((p[1] == 'c' && (p[2] == NUL || (p[2] != 's' && p[2] != 'r'
3232 && (p[3] == NUL || (p[3] != 'i' && p[4] != 'p')))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003233 || p[1] == 'g'
3234 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
3235 || p[1] == 'I'
3236 || (p[1] == 'r' && p[2] != 'e')))
3237 {
3238 eap->cmdidx = CMD_substitute;
3239 ++p;
3240 }
3241 else
3242 {
3243 while (ASCII_ISALPHA(*p))
3244 ++p;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003245 // for python 3.x support ":py3", ":python3", ":py3file", etc.
Bram Moolenaar55d5c032010-07-17 23:52:29 +02003246 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003247 {
Bram Moolenaarb6590522010-07-21 16:00:43 +02003248 while (ASCII_ISALNUM(*p))
3249 ++p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003250 }
3251 else if (*p == '9' && STRNCMP("vim9", eap->cmd, 4) == 0)
3252 {
3253 // include "9" for "vim9script"
3254 ++p;
3255 while (ASCII_ISALPHA(*p))
3256 ++p;
3257 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02003258
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003259 // check for non-alpha command
Bram Moolenaar071d4272004-06-13 20:20:40 +00003260 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3261 ++p;
3262 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003263 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
3264 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003265 // Check for ":dl", ":dell", etc. to ":deletel": that's
3266 // :delete with the 'l' flag. Same for 'p'.
Bram Moolenaardf177f62005-02-22 08:39:57 +00003267 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003268 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00003269 break;
3270 if (i == len - 1)
3271 {
3272 --len;
3273 if (p[-1] == 'l')
3274 eap->flags |= EXFLAG_LIST;
3275 else
3276 eap->flags |= EXFLAG_PRINT;
3277 }
3278 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003280 if (ASCII_ISLOWER(eap->cmd[0]))
3281 {
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003282 int c1 = eap->cmd[0];
Bram Moolenaar94f82cb2019-07-24 22:30:27 +02003283 int c2 = len == 1 ? NUL : eap->cmd[1];
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003284
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003285 if (command_count != (int)CMD_SIZE)
3286 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003287 iemsg(_("E943: Command table needs to be updated, run 'make cmdidxs'"));
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003288 getout(1);
3289 }
3290
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003291 // Use a precomputed index for fast look-up in cmdnames[]
3292 // taking into account the first 2 letters of eap->cmd.
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003293 eap->cmdidx = cmdidxs1[CharOrdLow(c1)];
3294 if (ASCII_ISLOWER(c2))
3295 eap->cmdidx += cmdidxs2[CharOrdLow(c1)][CharOrdLow(c2)];
3296 }
Bram Moolenaara494f562020-04-18 17:45:38 +02003297 else if (ASCII_ISUPPER(eap->cmd[0]))
3298 eap->cmdidx = CMD_Next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003299 else
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003300 eap->cmdidx = CMD_bang;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003301
3302 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
3303 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
3304 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
3305 (size_t)len) == 0)
3306 {
3307#ifdef FEAT_EVAL
3308 if (full != NULL
3309 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
3310 *full = TRUE;
3311#endif
3312 break;
3313 }
3314
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003315 // Look for a user defined command as a last resort. Let ":Print" be
3316 // overruled by a user defined command.
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01003317 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
3318 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003319 {
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003320 // User defined commands may contain digits.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321 while (ASCII_ISALNUM(*p))
3322 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003323 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003325 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326 eap->cmdidx = CMD_SIZE;
3327 }
3328
3329 return p;
3330}
3331
3332#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003333static struct cmdmod
3334{
3335 char *name;
3336 int minlen;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003337 int has_count; // :123verbose :3tab
Bram Moolenaared53fb92007-11-24 20:50:24 +00003338} cmdmods[] = {
3339 {"aboveleft", 3, FALSE},
3340 {"belowright", 3, FALSE},
3341 {"botright", 2, FALSE},
3342 {"browse", 3, FALSE},
3343 {"confirm", 4, FALSE},
Bram Moolenaar7b668e82016-08-23 23:51:21 +02003344 {"filter", 4, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003345 {"hide", 3, FALSE},
3346 {"keepalt", 5, FALSE},
3347 {"keepjumps", 5, FALSE},
3348 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01003349 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003350 {"leftabove", 5, FALSE},
3351 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003352 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003353 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003354 {"rightbelow", 6, FALSE},
3355 {"sandbox", 3, FALSE},
3356 {"silent", 3, FALSE},
3357 {"tab", 3, TRUE},
3358 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003359 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003360 {"verbose", 4, TRUE},
3361 {"vertical", 4, FALSE},
3362};
3363
3364/*
3365 * Return length of a command modifier (including optional count).
3366 * Return zero when it's not a modifier.
3367 */
3368 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003369modifier_len(char_u *cmd)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003370{
3371 int i, j;
3372 char_u *p = cmd;
3373
3374 if (VIM_ISDIGIT(*cmd))
3375 p = skipwhite(skipdigits(cmd));
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003376 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003377 {
3378 for (j = 0; p[j] != NUL; ++j)
3379 if (p[j] != cmdmods[i].name[j])
3380 break;
Bram Moolenaar2d473ab2013-06-12 17:12:24 +02003381 if (!ASCII_ISALPHA(p[j]) && j >= cmdmods[i].minlen
Bram Moolenaared53fb92007-11-24 20:50:24 +00003382 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003383 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003384 }
3385 return 0;
3386}
3387
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388/*
3389 * Return > 0 if an Ex command "name" exists.
3390 * Return 2 if there is an exact match.
3391 * Return 3 if there is an ambiguous match.
3392 */
3393 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003394cmd_exists(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003395{
3396 exarg_T ea;
3397 int full = FALSE;
3398 int i;
3399 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003400 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003402 // Check command modifiers.
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003403 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404 {
3405 for (j = 0; name[j] != NUL; ++j)
3406 if (name[j] != cmdmods[i].name[j])
3407 break;
3408 if (name[j] == NUL && j >= cmdmods[i].minlen)
3409 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3410 }
3411
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003412 // Check built-in commands and user defined commands.
3413 // For ":2match" and ":3match" we need to skip the number.
Bram Moolenaara9587612006-05-04 21:47:50 +00003414 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003416 p = find_ex_command(&ea, &full, NULL, NULL);
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003417 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003419 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3420 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003421 if (*skipwhite(p) != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003422 return 0; // trailing garbage
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3424}
3425#endif
3426
Bram Moolenaard0190392019-08-23 21:17:35 +02003427 cmdidx_T
3428excmd_get_cmdidx(char_u *cmd, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429{
Bram Moolenaard0190392019-08-23 21:17:35 +02003430 cmdidx_T idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431
Bram Moolenaard0190392019-08-23 21:17:35 +02003432 for (idx = (cmdidx_T)0; (int)idx < (int)CMD_SIZE;
3433 idx = (cmdidx_T)((int)idx + 1))
3434 if (STRNCMP(cmdnames[(int)idx].cmd_name, cmd, (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435 break;
3436
Bram Moolenaard0190392019-08-23 21:17:35 +02003437 return idx;
3438}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439
Bram Moolenaard0190392019-08-23 21:17:35 +02003440 long
3441excmd_get_argt(cmdidx_T idx)
3442{
3443 return (long)cmdnames[(int)idx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444}
3445
3446/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003447 * Skip a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448 *
3449 * Backslashed delimiters after / or ? will be skipped, and commands will
3450 * not be expanded between /'s and ?'s or after "'".
3451 *
Bram Moolenaardf177f62005-02-22 08:39:57 +00003452 * Also skip white space and ":" characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453 * Returns the "cmd" pointer advanced to beyond the range.
3454 */
3455 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003456skip_range(
3457 char_u *cmd,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003458 int *ctx) // pointer to xp_context or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003460 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01003462 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;\\", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463 {
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01003464 if (*cmd == '\\')
3465 {
3466 if (cmd[1] == '?' || cmd[1] == '/' || cmd[1] == '&')
3467 ++cmd;
3468 else
3469 break;
3470 }
3471 else if (*cmd == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472 {
3473 if (*++cmd == NUL && ctx != NULL)
3474 *ctx = EXPAND_NOTHING;
3475 }
3476 else if (*cmd == '/' || *cmd == '?')
3477 {
3478 delim = *cmd++;
3479 while (*cmd != NUL && *cmd != delim)
3480 if (*cmd++ == '\\' && *cmd != NUL)
3481 ++cmd;
3482 if (*cmd == NUL && ctx != NULL)
3483 *ctx = EXPAND_NOTHING;
3484 }
3485 if (*cmd != NUL)
3486 ++cmd;
3487 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00003488
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003489 // Skip ":" and white space.
Bram Moolenaardf177f62005-02-22 08:39:57 +00003490 while (*cmd == ':')
3491 cmd = skipwhite(cmd + 1);
3492
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493 return cmd;
3494}
3495
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003496 static void
3497addr_error(cmd_addr_T addr_type)
3498{
3499 if (addr_type == ADDR_NONE)
3500 emsg(_(e_norange));
3501 else
3502 emsg(_(e_invrange));
3503}
3504
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505/*
Bram Moolenaar198cb662018-09-06 21:44:17 +02003506 * Get a single EX address.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507 *
3508 * Set ptr to the next character after the part that was interpreted.
3509 * Set ptr to NULL when an error is encountered.
Bram Moolenaar198cb662018-09-06 21:44:17 +02003510 * This may set the last used search pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511 *
3512 * Return MAXLNUM when no Ex address was found.
3513 */
3514 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003515get_address(
3516 exarg_T *eap UNUSED,
3517 char_u **ptr,
Bram Moolenaar1b03a192019-12-08 17:08:29 +01003518 cmd_addr_T addr_type,
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003519 int skip, // only skip the address, don't use it
3520 int silent, // no errors or side effects
3521 int to_other_file, // flag: may jump to other file
3522 int address_count UNUSED) // 1 for first address, >1 after comma
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523{
3524 int c;
3525 int i;
3526 long n;
3527 char_u *cmd;
3528 pos_T pos;
3529 pos_T *fp;
3530 linenr_T lnum;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003531 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003532
3533 cmd = skipwhite(*ptr);
3534 lnum = MAXLNUM;
3535 do
3536 {
3537 switch (*cmd)
3538 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003539 case '.': // '.' - Cursor position
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003540 ++cmd;
3541 switch (addr_type)
3542 {
3543 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003544 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545 lnum = curwin->w_cursor.lnum;
3546 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003547 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003548 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003549 break;
3550 case ADDR_ARGUMENTS:
3551 lnum = curwin->w_arg_idx + 1;
3552 break;
3553 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003554 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003555 lnum = curbuf->b_fnum;
3556 break;
3557 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003558 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003559 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003560 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003561 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003562 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003563 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003564 cmd = NULL;
3565 goto error;
3566 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003567 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003568#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02003569 lnum = qf_get_cur_idx(eap);
3570#endif
3571 break;
3572 case ADDR_QUICKFIX_VALID:
3573#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003574 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02003575#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003576 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003577 }
3578 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003580 case '$': // '$' - last line
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003581 ++cmd;
3582 switch (addr_type)
3583 {
3584 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003585 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586 lnum = curbuf->b_ml.ml_line_count;
3587 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003588 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003589 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003590 break;
3591 case ADDR_ARGUMENTS:
3592 lnum = ARGCOUNT;
3593 break;
3594 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003595 buf = lastbuf;
3596 while (buf->b_ml.ml_mfp == NULL)
3597 {
3598 if (buf->b_prev == NULL)
3599 break;
3600 buf = buf->b_prev;
3601 }
3602 lnum = buf->b_fnum;
3603 break;
3604 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003605 lnum = lastbuf->b_fnum;
3606 break;
3607 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003608 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003609 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003610 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003611 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003612 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003613 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003614 cmd = NULL;
3615 goto error;
3616 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003617 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003618#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003619 lnum = qf_get_size(eap);
3620 if (lnum == 0)
3621 lnum = 1;
Bram Moolenaare906c502015-09-09 21:10:39 +02003622#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003623 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003624 case ADDR_QUICKFIX_VALID:
3625#ifdef FEAT_QUICKFIX
3626 lnum = qf_get_valid_size(eap);
3627 if (lnum == 0)
3628 lnum = 1;
3629#endif
3630 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003631 }
3632 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003634 case '\'': // ''' - mark
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003635 if (*++cmd == NUL)
3636 {
3637 cmd = NULL;
3638 goto error;
3639 }
3640 if (addr_type != ADDR_LINES)
3641 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003642 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003643 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003644 goto error;
3645 }
3646 if (skip)
3647 ++cmd;
3648 else
3649 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003650 // Only accept a mark in another file when it is
3651 // used by itself: ":'M".
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003652 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
3653 ++cmd;
3654 if (fp == (pos_T *)-1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003655 // Jumped to another file.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003656 lnum = curwin->w_cursor.lnum;
3657 else
3658 {
3659 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003660 {
3661 cmd = NULL;
3662 goto error;
3663 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003664 lnum = fp->lnum;
3665 }
3666 }
3667 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668
3669 case '/':
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003670 case '?': // '/' or '?' - search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003671 c = *cmd++;
3672 if (addr_type != ADDR_LINES)
3673 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003674 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003675 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003676 goto error;
3677 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003678 if (skip) // skip "/pat/"
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003679 {
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02003680 cmd = skip_regexp(cmd, c, (int)p_magic);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003681 if (*cmd == c)
3682 ++cmd;
3683 }
3684 else
3685 {
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003686 int flags;
3687
3688 pos = curwin->w_cursor; // save curwin->w_cursor
3689
3690 // When '/' or '?' follows another address, start from
3691 // there.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003692 if (lnum != MAXLNUM)
3693 curwin->w_cursor.lnum = lnum;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003694
3695 // Start a forward search at the end of the line (unless
3696 // before the first line).
3697 // Start a backward search at the start of the line.
3698 // This makes sure we never match in the current
3699 // line, and can match anywhere in the
3700 // next/previous line.
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01003701 if (c == '/' && curwin->w_cursor.lnum > 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003702 curwin->w_cursor.col = MAXCOL;
3703 else
3704 curwin->w_cursor.col = 0;
3705 searchcmdlen = 0;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003706 flags = silent ? 0 : SEARCH_HIS | SEARCH_MSG;
Bram Moolenaarc036e872020-02-21 21:30:52 +01003707 if (!do_search(NULL, c, c, cmd, 1L, flags, NULL))
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003708 {
3709 curwin->w_cursor = pos;
3710 cmd = NULL;
3711 goto error;
3712 }
3713 lnum = curwin->w_cursor.lnum;
3714 curwin->w_cursor = pos;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003715 // adjust command string pointer
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003716 cmd += searchcmdlen;
3717 }
3718 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003720 case '\\': // "\?", "\/" or "\&", repeat search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003721 ++cmd;
3722 if (addr_type != ADDR_LINES)
3723 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003724 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003725 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003726 goto error;
3727 }
3728 if (*cmd == '&')
3729 i = RE_SUBST;
3730 else if (*cmd == '?' || *cmd == '/')
3731 i = RE_SEARCH;
3732 else
3733 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003734 emsg(_(e_backslash));
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003735 cmd = NULL;
3736 goto error;
3737 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003738
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003739 if (!skip)
3740 {
3741 /*
3742 * When search follows another address, start from
3743 * there.
3744 */
3745 if (lnum != MAXLNUM)
3746 pos.lnum = lnum;
3747 else
3748 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003749
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003750 /*
3751 * Start the search just like for the above
3752 * do_search().
3753 */
3754 if (*cmd != '?')
3755 pos.col = MAXCOL;
3756 else
3757 pos.col = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02003758 pos.coladd = 0;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01003759 if (searchit(curwin, curbuf, &pos, NULL,
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003760 *cmd == '?' ? BACKWARD : FORWARD,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02003761 (char_u *)"", 1L, SEARCH_MSG, i, NULL) != FAIL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003762 lnum = pos.lnum;
3763 else
3764 {
3765 cmd = NULL;
3766 goto error;
3767 }
3768 }
3769 ++cmd;
3770 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003771
3772 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003773 if (VIM_ISDIGIT(*cmd)) // absolute line number
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003774 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003775 }
3776
3777 for (;;)
3778 {
3779 cmd = skipwhite(cmd);
3780 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
3781 break;
3782
3783 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003784 {
3785 switch (addr_type)
3786 {
3787 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003788 case ADDR_OTHER:
3789 // "+1" is same as ".+1"
Bram Moolenaarf240e182014-11-27 18:33:02 +01003790 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003791 break;
3792 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003793 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003794 break;
3795 case ADDR_ARGUMENTS:
3796 lnum = curwin->w_arg_idx + 1;
3797 break;
3798 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003799 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003800 lnum = curbuf->b_fnum;
3801 break;
3802 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003803 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003804 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003805 case ADDR_TABS_RELATIVE:
3806 lnum = 1;
3807 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003808 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003809#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02003810 lnum = qf_get_cur_idx(eap);
3811#endif
3812 break;
3813 case ADDR_QUICKFIX_VALID:
3814#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003815 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02003816#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003817 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003818 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003819 case ADDR_UNSIGNED:
3820 lnum = 0;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003821 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003822 }
3823 }
3824
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825 if (VIM_ISDIGIT(*cmd))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003826 i = '+'; // "number" is same as "+number"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003827 else
3828 i = *cmd++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003829 if (!VIM_ISDIGIT(*cmd)) // '+' is '+1', but '+0' is not '+1'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830 n = 1;
3831 else
3832 n = getdigits(&cmd);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003833
3834 if (addr_type == ADDR_TABS_RELATIVE)
3835 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003836 emsg(_(e_invrange));
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003837 cmd = NULL;
3838 goto error;
3839 }
3840 else if (addr_type == ADDR_LOADED_BUFFERS
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003841 || addr_type == ADDR_BUFFERS)
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003842 lnum = compute_buffer_local_count(
3843 addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003844 else
Bram Moolenaarded27822017-01-02 14:27:34 +01003845 {
3846#ifdef FEAT_FOLDING
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003847 // Relative line addressing, need to adjust for folded lines
3848 // now, but only do it after the first address.
Bram Moolenaarded27822017-01-02 14:27:34 +01003849 if (addr_type == ADDR_LINES && (i == '-' || i == '+')
3850 && address_count >= 2)
3851 (void)hasFolding(lnum, NULL, &lnum);
3852#endif
3853 if (i == '-')
3854 lnum -= n;
3855 else
3856 lnum += n;
3857 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003858 }
3859 } while (*cmd == '/' || *cmd == '?');
3860
3861error:
3862 *ptr = cmd;
3863 return lnum;
3864}
3865
3866/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00003867 * Get flags from an Ex command argument.
3868 */
3869 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003870get_flags(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00003871{
3872 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
3873 {
3874 if (*eap->arg == 'l')
3875 eap->flags |= EXFLAG_LIST;
3876 else if (*eap->arg == 'p')
3877 eap->flags |= EXFLAG_PRINT;
3878 else
3879 eap->flags |= EXFLAG_NR;
3880 eap->arg = skipwhite(eap->arg + 1);
3881 }
3882}
3883
3884/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003885 * Function called for command which is Not Implemented. NI!
3886 */
3887 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003888ex_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889{
3890 if (!eap->skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003891 eap->errmsg = N_("E319: Sorry, the command is not available in this version");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892}
3893
Bram Moolenaar7bb75552007-07-16 18:39:49 +00003894#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003895/*
3896 * Function called for script command which is Not Implemented. NI!
3897 * Skips over ":perl <<EOF" constructs.
3898 */
3899 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003900ex_script_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003901{
3902 if (!eap->skip)
3903 ex_ni(eap);
3904 else
3905 vim_free(script_get(eap, eap->arg));
3906}
3907#endif
3908
3909/*
3910 * Check range in Ex command for validity.
3911 * Return NULL when valid, error message when invalid.
3912 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003913 static char *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003914invalid_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003915{
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003916 buf_T *buf;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003917
Bram Moolenaar071d4272004-06-13 20:20:40 +00003918 if ( eap->line1 < 0
3919 || eap->line2 < 0
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003920 || eap->line1 > eap->line2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003921 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003922
Bram Moolenaar8071cb22019-07-12 17:58:01 +02003923 if (eap->argt & EX_RANGE)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003924 {
Bram Moolenaarb7316892019-05-01 18:08:42 +02003925 switch (eap->addr_type)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003926 {
3927 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003928 if (eap->line2 > curbuf->b_ml.ml_line_count
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003929#ifdef FEAT_DIFF
3930 + (eap->cmdidx == CMD_diffget)
3931#endif
3932 )
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003933 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003934 break;
3935 case ADDR_ARGUMENTS:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003936 // add 1 if ARGCOUNT is 0
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003937 if (eap->line2 > ARGCOUNT + (!ARGCOUNT))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003938 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003939 break;
3940 case ADDR_BUFFERS:
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02003941 // Only a boundary check, not whether the buffers actually
3942 // exist.
3943 if (eap->line1 < 1 || eap->line2 > get_highest_fnum())
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003944 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003945 break;
3946 case ADDR_LOADED_BUFFERS:
3947 buf = firstbuf;
3948 while (buf->b_ml.ml_mfp == NULL)
3949 {
3950 if (buf->b_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003951 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003952 buf = buf->b_next;
3953 }
3954 if (eap->line1 < buf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003955 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003956 buf = lastbuf;
3957 while (buf->b_ml.ml_mfp == NULL)
3958 {
3959 if (buf->b_prev == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003960 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003961 buf = buf->b_prev;
3962 }
3963 if (eap->line2 > buf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003964 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003965 break;
3966 case ADDR_WINDOWS:
Bram Moolenaar8be63882015-01-14 11:25:05 +01003967 if (eap->line2 > LAST_WIN_NR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003968 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003969 break;
3970 case ADDR_TABS:
3971 if (eap->line2 > LAST_TAB_NR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003972 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003973 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003974 case ADDR_TABS_RELATIVE:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003975 case ADDR_OTHER:
3976 // Any range is OK.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003977 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003978 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003979#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02003980 // No error for value that is too big, will use the last entry.
3981 if (eap->line2 <= 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003982 return _(e_invrange);
Bram Moolenaare906c502015-09-09 21:10:39 +02003983#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003984 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003985 case ADDR_QUICKFIX_VALID:
3986#ifdef FEAT_QUICKFIX
3987 if ((eap->line2 != 1 && eap->line2 > qf_get_valid_size(eap))
3988 || eap->line2 < 0)
3989 return _(e_invrange);
3990#endif
3991 break;
3992 case ADDR_UNSIGNED:
3993 if (eap->line2 < 0)
3994 return _(e_invrange);
3995 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003996 case ADDR_NONE:
3997 // Will give an error elsewhere.
3998 break;
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003999 }
4000 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004001 return NULL;
4002}
4003
4004/*
4005 * Correct the range for zero line number, if required.
4006 */
4007 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004008correct_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009{
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004010 if (!(eap->argt & EX_ZEROR)) // zero in range not allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004011 {
4012 if (eap->line1 == 0)
4013 eap->line1 = 1;
4014 if (eap->line2 == 0)
4015 eap->line2 = 1;
4016 }
4017}
4018
Bram Moolenaar748bf032005-02-02 23:04:36 +00004019#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004020/*
4021 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4022 * pattern. Otherwise return eap->arg.
4023 */
4024 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004025skip_grep_pat(exarg_T *eap)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004026{
4027 char_u *p = eap->arg;
4028
Bram Moolenaara37420f2006-02-04 22:37:47 +00004029 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4030 || eap->cmdidx == CMD_vimgrepadd
4031 || eap->cmdidx == CMD_lvimgrepadd
4032 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004033 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004034 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004035 if (p == NULL)
4036 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004037 }
4038 return p;
4039}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004040
4041/*
4042 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4043 * in the command line, so that things like % get expanded.
4044 */
4045 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004046replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004047{
4048 char_u *new_cmdline;
4049 char_u *program;
4050 char_u *pos;
4051 char_u *ptr;
4052 int len;
4053 int i;
4054
4055 /*
4056 * Don't do it when ":vimgrep" is used for ":grep".
4057 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004058 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4059 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4060 || eap->cmdidx == CMD_grepadd
4061 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004062 && !grep_internal(eap->cmdidx))
4063 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004064 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4065 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004066 {
4067 if (*curbuf->b_p_gp == NUL)
4068 program = p_gp;
4069 else
4070 program = curbuf->b_p_gp;
4071 }
4072 else
4073 {
4074 if (*curbuf->b_p_mp == NUL)
4075 program = p_mp;
4076 else
4077 program = curbuf->b_p_mp;
4078 }
4079
4080 p = skipwhite(p);
4081
4082 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4083 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004084 // replace $* by given arguments
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004085 i = 1;
4086 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4087 ++i;
4088 len = (int)STRLEN(p);
Bram Moolenaar51e14382019-05-25 20:21:28 +02004089 new_cmdline = alloc(STRLEN(program) + i * (len - 2) + 1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004090 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004091 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004092 ptr = new_cmdline;
4093 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4094 {
4095 i = (int)(pos - program);
4096 STRNCPY(ptr, program, i);
4097 STRCPY(ptr += i, p);
4098 ptr += len;
4099 program = pos + 2;
4100 }
4101 STRCPY(ptr, program);
4102 }
4103 else
4104 {
Bram Moolenaar51e14382019-05-25 20:21:28 +02004105 new_cmdline = alloc(STRLEN(program) + STRLEN(p) + 2);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004106 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004107 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004108 STRCPY(new_cmdline, program);
4109 STRCAT(new_cmdline, " ");
4110 STRCAT(new_cmdline, p);
4111 }
4112 msg_make(p);
4113
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004114 // 'eap->cmd' is not set here, because it is not used at CMD_make
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004115 vim_free(*cmdlinep);
4116 *cmdlinep = new_cmdline;
4117 p = new_cmdline;
4118 }
4119 return p;
4120}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004121#endif
4122
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123/*
4124 * Expand file name in Ex command argument.
Bram Moolenaar0abb4272019-06-15 16:06:00 +02004125 * When an error is detected, "errormsgp" is set to a non-NULL pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126 * Return FAIL for failure, OK otherwise.
4127 */
4128 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004129expand_filename(
4130 exarg_T *eap,
4131 char_u **cmdlinep,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004132 char **errormsgp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004133{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004134 int has_wildcards; // need to expand wildcards
Bram Moolenaar071d4272004-06-13 20:20:40 +00004135 char_u *repl;
4136 int srclen;
4137 char_u *p;
4138 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004139 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004140
Bram Moolenaar748bf032005-02-02 23:04:36 +00004141#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004142 // Skip a regexp pattern for ":vimgrep[add] pat file..."
Bram Moolenaar748bf032005-02-02 23:04:36 +00004143 p = skip_grep_pat(eap);
4144#else
4145 p = eap->arg;
4146#endif
4147
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148 /*
4149 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4150 * the file name contains a wildcard it should not cause expanding.
4151 * (it will be expanded anyway if there is a wildcard before replacing).
4152 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004153 has_wildcards = mch_has_wildcard(p);
4154 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004155 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004156#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004157 // Skip over `=expr`, wildcards in it are not expanded.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004158 if (p[0] == '`' && p[1] == '=')
4159 {
4160 p += 2;
4161 (void)skip_expr(&p);
4162 if (*p == '`')
4163 ++p;
4164 continue;
4165 }
4166#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167 /*
4168 * Quick check if this cannot be the start of a special string.
4169 * Also removes backslash before '%', '#' and '<'.
4170 */
4171 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4172 {
4173 ++p;
4174 continue;
4175 }
4176
4177 /*
4178 * Try to find a match at this position.
4179 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00004180 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
4181 errormsgp, &escaped);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004182 if (*errormsgp != NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00004183 return FAIL;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004184 if (repl == NULL) // no match found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004185 {
4186 p += srclen;
4187 continue;
4188 }
4189
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004190 // Wildcards won't be expanded below, the replacement is taken
4191 // literally. But do expand "~/file", "~user/file" and "$HOME/file".
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004192 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4193 {
4194 char_u *l = repl;
4195
4196 repl = expand_env_save(repl);
4197 vim_free(l);
4198 }
4199
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004200 // Need to escape white space et al. with a backslash.
4201 // Don't do this for:
4202 // - replacement that already has been escaped: "##"
4203 // - shell commands (may have to use quotes instead).
4204 // - non-unix systems when there is a single argument (spaces don't
4205 // separate arguments then).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004206 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00004207 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00004208 && eap->cmdidx != CMD_bang
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209 && eap->cmdidx != CMD_grep
4210 && eap->cmdidx != CMD_grepadd
Bram Moolenaarbf15b8d2017-06-04 20:43:48 +02004211 && eap->cmdidx != CMD_hardcopy
Bram Moolenaar67883b42017-07-27 22:57:00 +02004212 && eap->cmdidx != CMD_lgrep
4213 && eap->cmdidx != CMD_lgrepadd
4214 && eap->cmdidx != CMD_lmake
4215 && eap->cmdidx != CMD_make
4216 && eap->cmdidx != CMD_terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00004217#ifndef UNIX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004218 && !(eap->argt & EX_NOSPC)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219#endif
4220 )
4221 {
4222 char_u *l;
4223#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004224 // Don't escape a backslash here, because rem_backslash() doesn't
4225 // remove it later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004226 static char_u *nobslash = (char_u *)" \t\"|";
4227# define ESCAPE_CHARS nobslash
4228#else
4229# define ESCAPE_CHARS escape_chars
4230#endif
4231
4232 for (l = repl; *l; ++l)
4233 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
4234 {
4235 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
4236 if (l != NULL)
4237 {
4238 vim_free(repl);
4239 repl = l;
4240 }
4241 break;
4242 }
4243 }
4244
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004245 // For a shell command a '!' must be escaped.
Bram Moolenaar67883b42017-07-27 22:57:00 +02004246 if ((eap->usefilter || eap->cmdidx == CMD_bang
4247 || eap->cmdidx == CMD_terminal)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004248 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249 {
4250 char_u *l;
4251
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004252 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253 if (l != NULL)
4254 {
4255 vim_free(repl);
4256 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004257 }
4258 }
4259
4260 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
4261 vim_free(repl);
4262 if (p == NULL)
4263 return FAIL;
4264 }
4265
4266 /*
4267 * One file argument: Expand wildcards.
4268 * Don't do this with ":r !command" or ":w !command".
4269 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004270 if ((eap->argt & EX_NOSPC) && !eap->usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271 {
4272 /*
4273 * May do this twice:
4274 * 1. Replace environment variables.
4275 * 2. Replace any other wildcards, remove backslashes.
4276 */
4277 for (n = 1; n <= 2; ++n)
4278 {
4279 if (n == 2)
4280 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281 /*
4282 * Halve the number of backslashes (this is Vi compatible).
4283 * For Unix and OS/2, when wildcards are expanded, this is
4284 * done by ExpandOne() below.
4285 */
Bram Moolenaare7fedb62015-12-31 19:07:19 +01004286#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004287 if (!has_wildcards)
4288#endif
4289 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004290 }
4291
4292 if (has_wildcards)
4293 {
4294 if (n == 1)
4295 {
4296 /*
4297 * First loop: May expand environment variables. This
4298 * can be done much faster with expand_env() than with
4299 * something else (e.g., calling a shell).
4300 * After expanding environment variables, check again
4301 * if there are still wildcards present.
4302 */
4303 if (vim_strchr(eap->arg, '$') != NULL
4304 || vim_strchr(eap->arg, '~') != NULL)
4305 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00004306 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004307 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004308 has_wildcards = mch_has_wildcard(NameBuff);
4309 p = NameBuff;
4310 }
4311 else
4312 p = NULL;
4313 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004314 else // n == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004315 {
4316 expand_T xpc;
Bram Moolenaarb40c2572019-10-19 21:01:05 +02004317 int options = WILD_LIST_NOTFOUND
4318 | WILD_NOERROR | WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319
4320 ExpandInit(&xpc);
4321 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004322 if (p_wic)
4323 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004324 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01004325 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004326 if (p == NULL)
4327 return FAIL;
4328 }
4329 if (p != NULL)
4330 {
4331 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
4332 p, cmdlinep);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004333 if (n == 2) // p came from ExpandOne()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004334 vim_free(p);
4335 }
4336 }
4337 }
4338 }
4339 return OK;
4340}
4341
4342/*
4343 * Replace part of the command line, keeping eap->cmd, eap->arg and
4344 * eap->nextcmd correct.
4345 * "src" points to the part that is to be replaced, of length "srclen".
4346 * "repl" is the replacement string.
4347 * Returns a pointer to the character after the replaced string.
4348 * Returns NULL for failure.
4349 */
4350 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004351repl_cmdline(
4352 exarg_T *eap,
4353 char_u *src,
4354 int srclen,
4355 char_u *repl,
4356 char_u **cmdlinep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004357{
4358 int len;
4359 int i;
4360 char_u *new_cmdline;
4361
4362 /*
4363 * The new command line is build in new_cmdline[].
4364 * First allocate it.
4365 * Careful: a "+cmd" argument may have been NUL terminated.
4366 */
4367 len = (int)STRLEN(repl);
4368 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004369 if (eap->nextcmd != NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004370 i += (int)STRLEN(eap->nextcmd);// add space for next command
Bram Moolenaar964b3742019-05-24 18:54:09 +02004371 if ((new_cmdline = alloc(i)) == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004372 return NULL; // out of memory!
Bram Moolenaar071d4272004-06-13 20:20:40 +00004373
4374 /*
4375 * Copy the stuff before the expanded part.
4376 * Copy the expanded stuff.
4377 * Copy what came after the expanded part.
4378 * Copy the next commands, if there are any.
4379 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004380 i = (int)(src - *cmdlinep); // length of part before match
Bram Moolenaar071d4272004-06-13 20:20:40 +00004381 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00004382
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383 mch_memmove(new_cmdline + i, repl, (size_t)len);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004384 i += len; // remember the end of the string
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385 STRCPY(new_cmdline + i, src + srclen);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004386 src = new_cmdline + i; // remember where to continue
Bram Moolenaar071d4272004-06-13 20:20:40 +00004387
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004388 if (eap->nextcmd != NULL) // append next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00004389 {
4390 i = (int)STRLEN(new_cmdline) + 1;
4391 STRCPY(new_cmdline + i, eap->nextcmd);
4392 eap->nextcmd = new_cmdline + i;
4393 }
4394 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
4395 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
4396 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
4397 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
4398 vim_free(*cmdlinep);
4399 *cmdlinep = new_cmdline;
4400
4401 return src;
4402}
4403
4404/*
4405 * Check for '|' to separate commands and '"' to start comments.
4406 */
4407 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004408separate_nextcmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004409{
4410 char_u *p;
4411
Bram Moolenaar86b68352004-12-27 21:59:20 +00004412#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004413 p = skip_grep_pat(eap);
4414#else
4415 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004416#endif
4417
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004418 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004419 {
4420 if (*p == Ctrl_V)
4421 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004422 if (eap->argt & (EX_CTRLV | EX_XFILE))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004423 ++p; // skip CTRL-V and next char
Bram Moolenaar071d4272004-06-13 20:20:40 +00004424 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004425 // remove CTRL-V and skip next char
Bram Moolenaara7241f52008-06-24 20:39:31 +00004426 STRMOVE(p, p + 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004427 if (*p == NUL) // stop at NUL after CTRL-V
Bram Moolenaar071d4272004-06-13 20:20:40 +00004428 break;
4429 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004430
4431#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004432 // Skip over `=expr` when wildcards are expanded.
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004433 else if (p[0] == '`' && p[1] == '=' && (eap->argt & EX_XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004434 {
4435 p += 2;
4436 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004437 }
4438#endif
4439
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004440 // Check for '"': start of comment or '|': next command
4441 // :@" and :*" do not start a comment!
4442 // :redir @" doesn't either.
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004443 else if ((*p == '"' && !(eap->argt & EX_NOTRLCOM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004444 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
4445 || p != eap->arg)
4446 && (eap->cmdidx != CMD_redir
4447 || p != eap->arg + 1 || p[-1] != '@'))
Bram Moolenaara26b9702020-04-18 19:53:28 +02004448#ifdef FEAT_EVAL
4449 || (*p == '#' && in_vim9script()
4450 && p[1] != '{' && p > eap->cmd && VIM_ISWHITE(p[-1]))
4451#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004452 || *p == '|' || *p == '\n')
4453 {
4454 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004455 * We remove the '\' before the '|', unless EX_CTRLV is used
Bram Moolenaar071d4272004-06-13 20:20:40 +00004456 * AND 'b' is present in 'cpoptions'.
4457 */
4458 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004459 || !(eap->argt & EX_CTRLV)) && *(p - 1) == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004460 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004461 STRMOVE(p - 1, p); // remove the '\'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462 --p;
4463 }
4464 else
4465 {
4466 eap->nextcmd = check_nextcmd(p);
4467 *p = NUL;
4468 break;
4469 }
4470 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004471 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004472
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004473 if (!(eap->argt & EX_NOTRLCOM)) // remove trailing spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00004474 del_trailing_spaces(eap->arg);
4475}
4476
4477/*
4478 * get + command from ex argument
4479 */
4480 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004481getargcmd(char_u **argp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004482{
4483 char_u *arg = *argp;
4484 char_u *command = NULL;
4485
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004486 if (*arg == '+') // +[command]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004487 {
4488 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02004489 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004490 command = dollar_command;
4491 else
4492 {
4493 command = arg;
4494 arg = skip_cmd_arg(command, TRUE);
4495 if (*arg != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004496 *arg++ = NUL; // terminate command with NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004497 }
4498
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004499 arg = skipwhite(arg); // skip over spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00004500 *argp = arg;
4501 }
4502 return command;
4503}
4504
4505/*
4506 * Find end of "+command" argument. Skip over "\ " and "\\".
4507 */
Bram Moolenaard0190392019-08-23 21:17:35 +02004508 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004509skip_cmd_arg(
4510 char_u *p,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004511 int rembs) // TRUE to halve the number of backslashes
Bram Moolenaar071d4272004-06-13 20:20:40 +00004512{
4513 while (*p && !vim_isspace(*p))
4514 {
4515 if (*p == '\\' && p[1] != NUL)
4516 {
4517 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00004518 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004519 else
4520 ++p;
4521 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004522 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004523 }
4524 return p;
4525}
4526
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004527 int
4528get_bad_opt(char_u *p, exarg_T *eap)
4529{
4530 if (STRICMP(p, "keep") == 0)
4531 eap->bad_char = BAD_KEEP;
4532 else if (STRICMP(p, "drop") == 0)
4533 eap->bad_char = BAD_DROP;
4534 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
4535 eap->bad_char = *p;
Bram Moolenaar75808492018-06-12 12:39:41 +02004536 else
4537 return FAIL;
4538 return OK;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004539}
4540
Bram Moolenaar071d4272004-06-13 20:20:40 +00004541/*
4542 * Get "++opt=arg" argument.
4543 * Return FAIL or OK.
4544 */
4545 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004546getargopt(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004547{
4548 char_u *arg = eap->arg + 2;
4549 int *pp = NULL;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004550 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004551 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004552
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004553 // ":edit ++[no]bin[ary] file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004554 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
4555 {
4556 if (*arg == 'n')
4557 {
4558 arg += 2;
4559 eap->force_bin = FORCE_NOBIN;
4560 }
4561 else
4562 eap->force_bin = FORCE_BIN;
4563 if (!checkforcmd(&arg, "binary", 3))
4564 return FAIL;
4565 eap->arg = skipwhite(arg);
4566 return OK;
4567 }
4568
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004569 // ":read ++edit file"
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004570 if (STRNCMP(arg, "edit", 4) == 0)
4571 {
4572 eap->read_edit = TRUE;
4573 eap->arg = skipwhite(arg + 4);
4574 return OK;
4575 }
4576
Bram Moolenaar071d4272004-06-13 20:20:40 +00004577 if (STRNCMP(arg, "ff", 2) == 0)
4578 {
4579 arg += 2;
4580 pp = &eap->force_ff;
4581 }
4582 else if (STRNCMP(arg, "fileformat", 10) == 0)
4583 {
4584 arg += 10;
4585 pp = &eap->force_ff;
4586 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004587 else if (STRNCMP(arg, "enc", 3) == 0)
4588 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01004589 if (STRNCMP(arg, "encoding", 8) == 0)
4590 arg += 8;
4591 else
4592 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004593 pp = &eap->force_enc;
4594 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004595 else if (STRNCMP(arg, "bad", 3) == 0)
4596 {
4597 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004598 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004599 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004600
4601 if (pp == NULL || *arg != '=')
4602 return FAIL;
4603
4604 ++arg;
4605 *pp = (int)(arg - eap->cmd);
4606 arg = skip_cmd_arg(arg, FALSE);
4607 eap->arg = skipwhite(arg);
4608 *arg = NUL;
4609
Bram Moolenaar071d4272004-06-13 20:20:40 +00004610 if (pp == &eap->force_ff)
4611 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004612 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
4613 return FAIL;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004614 eap->force_ff = eap->cmd[eap->force_ff];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004615 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004616 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004618 // Make 'fileencoding' lower case.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004619 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
4620 *p = TOLOWER_ASC(*p);
4621 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004622 else
4623 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004624 // Check ++bad= argument. Must be a single-byte character, "keep" or
4625 // "drop".
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004626 if (get_bad_opt(eap->cmd + bad_char_idx, eap) == FAIL)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004627 return FAIL;
4628 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004629
4630 return OK;
4631}
4632
Bram Moolenaar071d4272004-06-13 20:20:40 +00004633 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004634ex_autocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004635{
4636 /*
Bram Moolenaar26d4b892018-07-04 22:26:28 +02004637 * Disallow autocommands from .exrc and .vimrc in current
Bram Moolenaar071d4272004-06-13 20:20:40 +00004638 * directory for security reasons.
4639 */
4640 if (secure)
4641 {
4642 secure = 2;
4643 eap->errmsg = e_curdir;
4644 }
4645 else if (eap->cmdidx == CMD_autocmd)
4646 do_autocmd(eap->arg, eap->forceit);
4647 else
4648 do_augroup(eap->arg, eap->forceit);
4649}
4650
4651/*
4652 * ":doautocmd": Apply the automatic commands to the current buffer.
4653 */
4654 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004655ex_doautocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004656{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01004657 char_u *arg = eap->arg;
4658 int call_do_modelines = check_nomodeline(&arg);
Bram Moolenaar1610d052016-06-09 22:53:01 +02004659 int did_aucmd;
Bram Moolenaar60542ac2012-02-12 20:14:01 +01004660
Bram Moolenaar1610d052016-06-09 22:53:01 +02004661 (void)do_doautocmd(arg, TRUE, &did_aucmd);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004662 // Only when there is no <nomodeline>.
Bram Moolenaar1610d052016-06-09 22:53:01 +02004663 if (call_do_modelines && did_aucmd)
Bram Moolenaar60542ac2012-02-12 20:14:01 +01004664 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004665}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004666
Bram Moolenaar071d4272004-06-13 20:20:40 +00004667/*
4668 * :[N]bunload[!] [N] [bufname] unload buffer
4669 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
4670 * :[N]bwipeout[!] [N] [bufname] delete buffer really
4671 */
4672 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004673ex_bunload(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004674{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004675 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02004676 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004677 eap->errmsg = do_bufdel(
4678 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
4679 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
4680 : DOBUF_UNLOAD, eap->arg,
4681 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
4682}
4683
4684/*
4685 * :[N]buffer [N] to buffer N
4686 * :[N]sbuffer [N] to buffer N
4687 */
4688 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004689ex_buffer(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004690{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004691 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02004692 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004693 if (*eap->arg)
4694 eap->errmsg = e_trailing;
4695 else
4696 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004697 if (eap->addr_count == 0) // default is current buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00004698 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
4699 else
4700 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004701 if (eap->do_ecmd_cmd != NULL)
4702 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004703 }
4704}
4705
4706/*
4707 * :[N]bmodified [N] to next mod. buffer
4708 * :[N]sbmodified [N] to next mod. buffer
4709 */
4710 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004711ex_bmodified(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004712{
4713 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004714 if (eap->do_ecmd_cmd != NULL)
4715 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004716}
4717
4718/*
4719 * :[N]bnext [N] to next buffer
4720 * :[N]sbnext [N] split and to next buffer
4721 */
4722 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004723ex_bnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004724{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004725 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01004726 return;
4727
Bram Moolenaar071d4272004-06-13 20:20:40 +00004728 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004729 if (eap->do_ecmd_cmd != NULL)
4730 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004731}
4732
4733/*
4734 * :[N]bNext [N] to previous buffer
4735 * :[N]bprevious [N] to previous buffer
4736 * :[N]sbNext [N] split and to previous buffer
4737 * :[N]sbprevious [N] split and to previous buffer
4738 */
4739 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004740ex_bprevious(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004741{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004742 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01004743 return;
4744
Bram Moolenaar071d4272004-06-13 20:20:40 +00004745 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004746 if (eap->do_ecmd_cmd != NULL)
4747 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004748}
4749
4750/*
4751 * :brewind to first buffer
4752 * :bfirst to first buffer
4753 * :sbrewind split and to first buffer
4754 * :sbfirst split and to first buffer
4755 */
4756 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004757ex_brewind(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004758{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004759 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01004760 return;
4761
Bram Moolenaar071d4272004-06-13 20:20:40 +00004762 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004763 if (eap->do_ecmd_cmd != NULL)
4764 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004765}
4766
4767/*
4768 * :blast to last buffer
4769 * :sblast split and to last buffer
4770 */
4771 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004772ex_blast(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004773{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004774 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01004775 return;
4776
Bram Moolenaar071d4272004-06-13 20:20:40 +00004777 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004778 if (eap->do_ecmd_cmd != NULL)
4779 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004780}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004781
Bram Moolenaar7a092242020-04-16 22:10:49 +02004782/*
4783 * Check if "c" ends an Ex command.
Bram Moolenaara494f562020-04-18 17:45:38 +02004784 * In Vim9 script does not check for white space before # or #{.
Bram Moolenaar7a092242020-04-16 22:10:49 +02004785 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004786 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004787ends_excmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004788{
Bram Moolenaarfbda69b2020-04-13 15:06:53 +02004789#ifdef FEAT_EVAL
Bram Moolenaar2c330432020-04-13 14:41:35 +02004790 if (c == '#')
Bram Moolenaar7a092242020-04-16 22:10:49 +02004791 return in_vim9script();
4792#endif
4793 return (c == NUL || c == '|' || c == '"' || c == '\n');
4794}
4795
4796/*
4797 * Like ends_excmd() but checks that a # in Vim9 script either has "cmd" equal
4798 * to "cmd_start" or has a white space character before it.
4799 */
4800 int
Bram Moolenaar21cfe502020-04-16 23:01:50 +02004801ends_excmd2(char_u *cmd_start UNUSED, char_u *cmd)
Bram Moolenaar7a092242020-04-16 22:10:49 +02004802{
4803 int c = *cmd;
4804
4805#ifdef FEAT_EVAL
Bram Moolenaara26b9702020-04-18 19:53:28 +02004806 if (c == '#' && cmd[1] != '{' && (cmd == cmd_start || VIM_ISWHITE(cmd[-1])))
Bram Moolenaar2c330432020-04-13 14:41:35 +02004807 return in_vim9script();
Bram Moolenaarfbda69b2020-04-13 15:06:53 +02004808#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004809 return (c == NUL || c == '|' || c == '"' || c == '\n');
4810}
4811
4812#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
4813 || defined(PROTO)
4814/*
4815 * Return the next command, after the first '|' or '\n'.
4816 * Return NULL if not found.
4817 */
4818 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004819find_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004820{
4821 while (*p != '|' && *p != '\n')
4822 {
4823 if (*p == NUL)
4824 return NULL;
4825 ++p;
4826 }
4827 return (p + 1);
4828}
4829#endif
4830
4831/*
Bram Moolenaar2256c992016-11-15 21:17:07 +01004832 * Check if *p is a separator between Ex commands, skipping over white space.
4833 * Return NULL if it isn't, the following character if it is.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004834 */
4835 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004836check_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004837{
Bram Moolenaar2256c992016-11-15 21:17:07 +01004838 char_u *s = skipwhite(p);
4839
4840 if (*s == '|' || *s == '\n')
4841 return (s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004842 else
4843 return NULL;
4844}
4845
4846/*
4847 * - if there are more files to edit
4848 * - and this is the last window
4849 * - and forceit not used
4850 * - and not repeated twice on a row
4851 * return FAIL and give error message if 'message' TRUE
4852 * return OK otherwise
4853 */
4854 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004855check_more(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004856 int message, // when FALSE check only, no messages
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004857 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004858{
4859 int n = ARGCOUNT - curwin->w_arg_idx - 1;
4860
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00004861 if (!forceit && only_one_window()
4862 && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004863 {
4864 if (message)
4865 {
4866#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
4867 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
4868 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02004869 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004870
Bram Moolenaarda6e8912018-08-21 15:12:14 +02004871 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
4872 NGETTEXT("%d more file to edit. Quit anyway?",
4873 "%d more files to edit. Quit anyway?", n), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004874 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
4875 return OK;
4876 return FAIL;
4877 }
4878#endif
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01004879 semsg(NGETTEXT("E173: %d more file to edit",
4880 "E173: %d more files to edit", n), n);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004881 quitmore = 2; // next try to quit is allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004882 }
4883 return FAIL;
4884 }
4885 return OK;
4886}
4887
Bram Moolenaar071d4272004-06-13 20:20:40 +00004888/*
4889 * Function given to ExpandGeneric() to obtain the list of command names.
4890 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004892get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004893{
4894 if (idx >= (int)CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004895 return get_user_command_name(idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004896 return cmdnames[idx].cmd_name;
4897}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004898
Bram Moolenaar071d4272004-06-13 20:20:40 +00004899 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004900ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004901{
Bram Moolenaare6850792010-05-14 15:28:44 +02004902 if (*eap->arg == NUL)
4903 {
4904#ifdef FEAT_EVAL
4905 char_u *expr = vim_strsave((char_u *)"g:colors_name");
4906 char_u *p = NULL;
4907
4908 if (expr != NULL)
4909 {
4910 ++emsg_off;
4911 p = eval_to_string(expr, NULL, FALSE);
4912 --emsg_off;
4913 vim_free(expr);
4914 }
4915 if (p != NULL)
4916 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004917 msg((char *)p);
Bram Moolenaare6850792010-05-14 15:28:44 +02004918 vim_free(p);
4919 }
4920 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01004921 msg("default");
Bram Moolenaare6850792010-05-14 15:28:44 +02004922#else
Bram Moolenaar32526b32019-01-19 17:43:09 +01004923 msg(_("unknown"));
Bram Moolenaare6850792010-05-14 15:28:44 +02004924#endif
4925 }
4926 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004927 semsg(_("E185: Cannot find color scheme '%s'"), eap->arg);
Bram Moolenaarf58d81a2019-01-28 20:19:05 +01004928
4929#ifdef FEAT_VTP
4930 else if (has_vtp_working())
4931 {
4932 // background color change requires clear + redraw
4933 update_screen(CLEAR);
4934 redrawcmd();
4935 }
4936#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004937}
4938
4939 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004940ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004941{
4942 if (*eap->arg == NUL && eap->cmd[2] == '!')
Bram Moolenaar32526b32019-01-19 17:43:09 +01004943 msg(_("Greetings, Vim user!"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944 do_highlight(eap->arg, eap->forceit, FALSE);
4945}
4946
4947
4948/*
4949 * Call this function if we thought we were going to exit, but we won't
4950 * (because of an error). May need to restore the terminal mode.
4951 */
4952 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004953not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954{
4955 exiting = FALSE;
4956 settmode(TMODE_RAW);
4957}
4958
Bram Moolenaar12a96de2018-03-11 14:44:18 +01004959 static int
4960before_quit_autocmds(win_T *wp, int quit_all, int forceit)
4961{
4962 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, wp->w_buffer);
4963
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02004964 // Bail out when autocommands closed the window.
4965 // Refuse to quit when the buffer in the last window is being closed (can
4966 // only happen in autocommands).
Bram Moolenaar12a96de2018-03-11 14:44:18 +01004967 if (!win_valid(wp)
4968 || curbuf_locked()
4969 || (wp->w_buffer->b_nwindows == 1 && wp->w_buffer->b_locked > 0))
4970 return TRUE;
4971
4972 if (quit_all || (check_more(FALSE, forceit) == OK && only_one_window()))
4973 {
4974 apply_autocmds(EVENT_EXITPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02004975 // Refuse to quit when locked or when the window was closed or the
4976 // buffer in the last window is being closed (can only happen in
4977 // autocommands).
4978 if (!win_valid(wp) || curbuf_locked()
Bram Moolenaar12a96de2018-03-11 14:44:18 +01004979 || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
4980 return TRUE;
4981 }
4982
4983 return FALSE;
4984}
4985
Bram Moolenaar071d4272004-06-13 20:20:40 +00004986/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01004987 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar12a96de2018-03-11 14:44:18 +01004988 * ":{nr}quit": quit window {nr}
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02004989 * Also used when closing a terminal window that's the last one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004990 */
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02004991 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004992ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004993{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004994 win_T *wp;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004995
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996#ifdef FEAT_CMDWIN
4997 if (cmdwin_type != 0)
4998 {
4999 cmdwin_result = Ctrl_C;
5000 return;
5001 }
5002#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005003 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005004 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005005 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005006 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005007 return;
5008 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005009 if (eap->addr_count > 0)
5010 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01005011 int wnr = eap->line2;
5012
5013 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
5014 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005015 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005016 }
5017 else
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005018 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01005019
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005020 // Refuse to quit when locked.
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02005021 if (curbuf_locked())
5022 return;
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005023
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005024 // Trigger QuitPre and maybe ExitPre
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005025 if (before_quit_autocmds(wp, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005026 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005027
5028#ifdef FEAT_NETBEANS_INTG
5029 netbeansForcedQuit = eap->forceit;
5030#endif
5031
5032 /*
5033 * If there are more files or windows we won't exit.
5034 */
5035 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
5036 exiting = TRUE;
Bram Moolenaarff930ca2017-10-19 17:12:10 +02005037 if ((!buf_hide(wp->w_buffer)
5038 && check_changed(wp->w_buffer, (p_awa ? CCGD_AW : 0)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01005039 | (eap->forceit ? CCGD_FORCEIT : 0)
5040 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005041 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01005042 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005043 {
5044 not_exiting();
5045 }
5046 else
5047 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005048 // quit last window
5049 // Note: only_one_window() returns true, even so a help window is
5050 // still open. In that case only quit, if no address has been
5051 // specified. Example:
5052 // :h|wincmd w|1q - don't quit
5053 // :h|wincmd w|q - quit
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01005054 if (only_one_window() && (ONE_WINDOW || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005055 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02005056 not_exiting();
Bram Moolenaar4033c552017-09-16 20:54:51 +02005057#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005058 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005059#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005060 // close window; may free buffer
Bram Moolenaareb44a682017-08-03 22:44:55 +02005061 win_close(wp, !buf_hide(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005062 }
5063}
5064
5065/*
5066 * ":cquit".
5067 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005068 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005069ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070{
Bram Moolenaar1860bde2020-01-06 21:47:21 +01005071 // this does not always pass on the exit code to the Manx compiler. why?
5072 getout(eap->addr_count > 0 ? (int)eap->line2 : EXIT_FAILURE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005073}
5074
5075/*
5076 * ":qall": try to quit all windows
5077 */
5078 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005079ex_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005080{
5081# ifdef FEAT_CMDWIN
5082 if (cmdwin_type != 0)
5083 {
5084 if (eap->forceit)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005085 cmdwin_result = K_XF1; // ex_window() takes care of this
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086 else
5087 cmdwin_result = K_XF2;
5088 return;
5089 }
5090# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005091
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005092 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005093 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005094 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005095 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005096 return;
5097 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005098
5099 if (before_quit_autocmds(curwin, TRUE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005100 return;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005101
Bram Moolenaar071d4272004-06-13 20:20:40 +00005102 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01005103 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005104 getout(0);
5105 not_exiting();
5106}
5107
Bram Moolenaar071d4272004-06-13 20:20:40 +00005108/*
5109 * ":close": close current window, unless it is the last one
5110 */
5111 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005112ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005113{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005114 win_T *win;
5115 int winnr = 0;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005116#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00005117 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02005118 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005119 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005120#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005121 if (!text_locked() && !curbuf_locked())
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005122 {
5123 if (eap->addr_count == 0)
5124 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar6d41c782018-06-06 09:11:12 +02005125 else
5126 {
Bram Moolenaar29323592016-07-24 22:04:11 +02005127 FOR_ALL_WINDOWS(win)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005128 {
5129 winnr++;
5130 if (winnr == eap->line2)
5131 break;
5132 }
5133 if (win == NULL)
5134 win = lastwin;
5135 ex_win_close(eap->forceit, win, NULL);
5136 }
5137 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005138}
5139
Bram Moolenaar4033c552017-09-16 20:54:51 +02005140#ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005141/*
5142 * ":pclose": Close any preview window.
5143 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005144 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005145ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005146{
5147 win_T *win;
5148
Bram Moolenaar79648732019-07-18 21:43:07 +02005149 // First close any normal window.
Bram Moolenaar29323592016-07-24 22:04:11 +02005150 FOR_ALL_WINDOWS(win)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005151 if (win->w_p_pvw)
5152 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005153 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar79648732019-07-18 21:43:07 +02005154 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005155 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005156# ifdef FEAT_PROP_POPUP
Bram Moolenaar79648732019-07-18 21:43:07 +02005157 // Also when 'previewpopup' is empty, it might have been cleared.
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02005158 popup_close_preview();
Bram Moolenaar79648732019-07-18 21:43:07 +02005159# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005160}
Bram Moolenaar4033c552017-09-16 20:54:51 +02005161#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005162
Bram Moolenaarf740b292006-02-16 22:11:02 +00005163/*
5164 * Close window "win" and take care of handling closing the last window for a
5165 * modified buffer.
5166 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005167 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005168ex_win_close(
5169 int forceit,
5170 win_T *win,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005171 tabpage_T *tp) // NULL or the tab page "win" is in
Bram Moolenaar071d4272004-06-13 20:20:40 +00005172{
5173 int need_hide;
5174 buf_T *buf = win->w_buffer;
5175
5176 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02005177 if (need_hide && !buf_hide(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005178 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02005179#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005180 if ((p_confirm || cmdmod.confirm) && p_write)
5181 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005182 bufref_T bufref;
5183
5184 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185 dialog_changed(buf, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005186 if (bufref_valid(&bufref) && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005187 return;
5188 need_hide = FALSE;
5189 }
5190 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005191#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005192 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02005193 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194 return;
5195 }
5196 }
5197
Bram Moolenaar4033c552017-09-16 20:54:51 +02005198#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005199 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005200#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005201
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005202 // free buffer when not hiding it or when it's a scratch buffer
Bram Moolenaarf740b292006-02-16 22:11:02 +00005203 if (tp == NULL)
Bram Moolenaareb44a682017-08-03 22:44:55 +02005204 win_close(win, !need_hide && !buf_hide(buf));
Bram Moolenaarf740b292006-02-16 22:11:02 +00005205 else
Bram Moolenaareb44a682017-08-03 22:44:55 +02005206 win_close_othertab(win, !need_hide && !buf_hide(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207}
5208
Bram Moolenaar071d4272004-06-13 20:20:40 +00005209/*
Bram Moolenaardea25702017-01-29 15:18:10 +01005210 * Handle the argument for a tabpage related ex command.
5211 * Returns a tabpage number.
5212 * When an error is encountered then eap->errmsg is set.
5213 */
5214 static int
5215get_tabpage_arg(exarg_T *eap)
5216{
5217 int tab_number;
5218 int unaccept_arg0 = (eap->cmdidx == CMD_tabmove) ? 0 : 1;
5219
5220 if (eap->arg && *eap->arg != NUL)
5221 {
5222 char_u *p = eap->arg;
5223 char_u *p_save;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005224 int relative = 0; // argument +N/-N means: go to N places to the
5225 // right/left relative to the current position.
Bram Moolenaardea25702017-01-29 15:18:10 +01005226
5227 if (*p == '-')
5228 {
5229 relative = -1;
5230 p++;
5231 }
5232 else if (*p == '+')
5233 {
5234 relative = 1;
5235 p++;
5236 }
5237
5238 p_save = p;
5239 tab_number = getdigits(&p);
5240
5241 if (relative == 0)
5242 {
5243 if (STRCMP(p, "$") == 0)
5244 tab_number = LAST_TAB_NR;
5245 else if (p == p_save || *p_save == '-' || *p != NUL
5246 || tab_number > LAST_TAB_NR)
5247 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005248 // No numbers as argument.
Bram Moolenaardea25702017-01-29 15:18:10 +01005249 eap->errmsg = e_invarg;
5250 goto theend;
5251 }
5252 }
5253 else
5254 {
5255 if (*p_save == NUL)
5256 tab_number = 1;
5257 else if (p == p_save || *p_save == '-' || *p != NUL
5258 || tab_number == 0)
5259 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005260 // No numbers as argument.
Bram Moolenaardea25702017-01-29 15:18:10 +01005261 eap->errmsg = e_invarg;
5262 goto theend;
5263 }
5264 tab_number = tab_number * relative + tabpage_index(curtab);
5265 if (!unaccept_arg0 && relative == -1)
5266 --tab_number;
5267 }
5268 if (tab_number < unaccept_arg0 || tab_number > LAST_TAB_NR)
5269 eap->errmsg = e_invarg;
5270 }
5271 else if (eap->addr_count > 0)
5272 {
5273 if (unaccept_arg0 && eap->line2 == 0)
Bram Moolenaarc6251552017-01-29 21:42:20 +01005274 {
Bram Moolenaardea25702017-01-29 15:18:10 +01005275 eap->errmsg = e_invrange;
Bram Moolenaarc6251552017-01-29 21:42:20 +01005276 tab_number = 0;
5277 }
Bram Moolenaardea25702017-01-29 15:18:10 +01005278 else
5279 {
5280 tab_number = eap->line2;
Bram Moolenaar82a12462019-01-22 22:41:42 +01005281 if (!unaccept_arg0 && *skipwhite(*eap->cmdlinep) == '-')
Bram Moolenaardea25702017-01-29 15:18:10 +01005282 {
5283 --tab_number;
5284 if (tab_number < unaccept_arg0)
5285 eap->errmsg = e_invarg;
5286 }
5287 }
5288 }
5289 else
5290 {
5291 switch (eap->cmdidx)
5292 {
5293 case CMD_tabnext:
5294 tab_number = tabpage_index(curtab) + 1;
5295 if (tab_number > LAST_TAB_NR)
5296 tab_number = 1;
5297 break;
5298 case CMD_tabmove:
5299 tab_number = LAST_TAB_NR;
5300 break;
5301 default:
5302 tab_number = tabpage_index(curtab);
5303 }
5304 }
5305
5306theend:
5307 return tab_number;
5308}
5309
5310/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00005311 * ":tabclose": close current tab page, unless it is the last one.
5312 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005313 */
5314 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005315ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005316{
Bram Moolenaarf740b292006-02-16 22:11:02 +00005317 tabpage_T *tp;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005318 int tab_number;
Bram Moolenaarf740b292006-02-16 22:11:02 +00005319
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005320# ifdef FEAT_CMDWIN
5321 if (cmdwin_type != 0)
5322 cmdwin_result = K_IGNORE;
5323 else
5324# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005325 if (first_tabpage->tp_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005326 emsg(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00005327 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005328 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005329 tab_number = get_tabpage_arg(eap);
5330 if (eap->errmsg == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005331 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005332 tp = find_tabpage(tab_number);
Bram Moolenaarf740b292006-02-16 22:11:02 +00005333 if (tp == NULL)
5334 {
5335 beep_flush();
5336 return;
5337 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005338 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005339 {
5340 tabpage_close_other(tp, eap->forceit);
5341 return;
5342 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005343 else if (!text_locked() && !curbuf_locked())
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005344 tabpage_close(eap->forceit);
5345 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005346 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005347}
5348
5349/*
5350 * ":tabonly": close all tab pages except the current one
5351 */
5352 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005353ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005354{
5355 tabpage_T *tp;
5356 int done;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005357 int tab_number;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005358
5359# ifdef FEAT_CMDWIN
5360 if (cmdwin_type != 0)
5361 cmdwin_result = K_IGNORE;
5362 else
5363# endif
5364 if (first_tabpage->tp_next == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005365 msg(_("Already only one tab page"));
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005366 else
5367 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005368 tab_number = get_tabpage_arg(eap);
5369 if (eap->errmsg == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005370 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005371 goto_tabpage(tab_number);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005372 // Repeat this up to a 1000 times, because autocommands may
5373 // mess up the lists.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005374 for (done = 0; done < 1000; ++done)
5375 {
5376 FOR_ALL_TABPAGES(tp)
5377 if (tp->tp_topframe != topframe)
5378 {
5379 tabpage_close_other(tp, eap->forceit);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005380 // if we failed to close it quit
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005381 if (valid_tabpage(tp))
5382 done = 1000;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005383 // start over, "tp" is now invalid
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005384 break;
5385 }
5386 if (first_tabpage->tp_next == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005387 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005388 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005389 }
5390 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005391}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005392
5393/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00005394 * Close the current tab page.
5395 */
5396 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005397tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005398{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005399 // First close all the windows but the current one. If that worked then
5400 // close the last window in this tab, that will close it.
Bram Moolenaar459ca562016-11-10 18:16:33 +01005401 if (!ONE_WINDOW)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005402 close_others(TRUE, forceit);
Bram Moolenaar459ca562016-11-10 18:16:33 +01005403 if (ONE_WINDOW)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005404 ex_win_close(forceit, curwin, NULL);
5405# ifdef FEAT_GUI
5406 need_mouse_correct = TRUE;
5407# endif
5408}
5409
5410/*
5411 * Close tab page "tp", which is not the current tab page.
5412 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005413 * Also takes care of the tab pages line disappearing when closing the
5414 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00005415 */
5416 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005417tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005418{
5419 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005420 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005421 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00005422
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005423 // Limit to 1000 windows, autocommands may add a window while we close
5424 // one. OK, so I'm paranoid...
Bram Moolenaarf740b292006-02-16 22:11:02 +00005425 while (++done < 1000)
5426 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005427 wp = tp->tp_firstwin;
5428 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00005429
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005430 // Autocommands may delete the tab page under our fingers and we may
5431 // fail to close a window with a modified buffer.
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005432 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005433 break;
5434 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005435
Bram Moolenaar29323592016-07-24 22:04:11 +02005436 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar29323592016-07-24 22:04:11 +02005437
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005438 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005439 if (h != tabline_height())
5440 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00005441}
5442
5443/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005444 * ":only".
5445 */
5446 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005447ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005448{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005449 win_T *wp;
5450 int wnr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005451# ifdef FEAT_GUI
5452 need_mouse_correct = TRUE;
5453# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005454 if (eap->addr_count > 0)
5455 {
5456 wnr = eap->line2;
5457 for (wp = firstwin; --wnr > 0; )
5458 {
5459 if (wp->w_next == NULL)
5460 break;
5461 else
5462 wp = wp->w_next;
5463 }
5464 win_goto(wp);
5465 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005466 close_others(TRUE, eap->forceit);
5467}
5468
Bram Moolenaar071d4272004-06-13 20:20:40 +00005469 static void
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01005470ex_hide(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005471{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005472 // ":hide" or ":hide | cmd": hide current window
Bram Moolenaar2256c992016-11-15 21:17:07 +01005473 if (!eap->skip)
5474 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02005475#ifdef FEAT_GUI
Bram Moolenaar2256c992016-11-15 21:17:07 +01005476 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005477#endif
Bram Moolenaar2256c992016-11-15 21:17:07 +01005478 if (eap->addr_count == 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005479 win_close(curwin, FALSE); // don't free buffer
Bram Moolenaar2256c992016-11-15 21:17:07 +01005480 else
5481 {
5482 int winnr = 0;
5483 win_T *win;
Bram Moolenaarf240e182014-11-27 18:33:02 +01005484
Bram Moolenaar2256c992016-11-15 21:17:07 +01005485 FOR_ALL_WINDOWS(win)
5486 {
5487 winnr++;
5488 if (winnr == eap->line2)
5489 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005490 }
Bram Moolenaar2256c992016-11-15 21:17:07 +01005491 if (win == NULL)
5492 win = lastwin;
5493 win_close(win, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005494 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005495 }
5496}
5497
5498/*
5499 * ":stop" and ":suspend": Suspend Vim.
5500 */
5501 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005502ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005503{
5504 /*
5505 * Disallow suspending for "rvim".
5506 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005507 if (!check_restricted())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005508 {
5509 if (!eap->forceit)
5510 autowrite_all();
5511 windgoto((int)Rows - 1, 0);
5512 out_char('\n');
5513 out_flush();
5514 stoptermcap();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005515 out_flush(); // needed for SUN to restore xterm buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00005516#ifdef FEAT_TITLE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005517 mch_restore_title(SAVE_RESTORE_BOTH); // restore window titles
Bram Moolenaar071d4272004-06-13 20:20:40 +00005518#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005519 ui_suspend(); // call machine specific function
Bram Moolenaar071d4272004-06-13 20:20:40 +00005520#ifdef FEAT_TITLE
5521 maketitle();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005522 resettitle(); // force updating the title
Bram Moolenaar071d4272004-06-13 20:20:40 +00005523#endif
5524 starttermcap();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005525 scroll_start(); // scroll screen before redrawing
Bram Moolenaar071d4272004-06-13 20:20:40 +00005526 redraw_later_clear();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005527 shell_resized(); // may have resized window
Bram Moolenaar071d4272004-06-13 20:20:40 +00005528 }
5529}
5530
5531/*
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005532 * ":exit", ":xit" and ":wq": Write file and quite the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005533 */
5534 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005535ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005536{
5537#ifdef FEAT_CMDWIN
5538 if (cmdwin_type != 0)
5539 {
5540 cmdwin_result = Ctrl_C;
5541 return;
5542 }
5543#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005544 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005545 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005546 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005547 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005548 return;
5549 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005550
5551 if (before_quit_autocmds(curwin, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005552 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005553
5554 /*
5555 * if more files or windows we won't exit
5556 */
5557 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
5558 exiting = TRUE;
5559 if ( ((eap->cmdidx == CMD_wq
5560 || curbufIsChanged())
5561 && do_write(eap) == FAIL)
5562 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01005563 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005564 {
5565 not_exiting();
5566 }
5567 else
5568 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005569 if (only_one_window()) // quit last window, exit Vim
Bram Moolenaar071d4272004-06-13 20:20:40 +00005570 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02005571 not_exiting();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005572# ifdef FEAT_GUI
5573 need_mouse_correct = TRUE;
5574# endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005575 // Quit current window, may free the buffer.
Bram Moolenaareb44a682017-08-03 22:44:55 +02005576 win_close(curwin, !buf_hide(curwin->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005577 }
5578}
5579
5580/*
5581 * ":print", ":list", ":number".
5582 */
5583 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005584ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005585{
Bram Moolenaardf177f62005-02-22 08:39:57 +00005586 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005587 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +00005588 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005589 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00005590 for ( ;!got_int; ui_breakcheck())
5591 {
5592 print_line(eap->line1,
5593 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
5594 || (eap->flags & EXFLAG_NR)),
5595 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
5596 if (++eap->line1 > eap->line2)
5597 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005598 out_flush(); // show one line at a time
Bram Moolenaardf177f62005-02-22 08:39:57 +00005599 }
5600 setpcmark();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005601 // put cursor at last line
Bram Moolenaardf177f62005-02-22 08:39:57 +00005602 curwin->w_cursor.lnum = eap->line2;
5603 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005604 }
5605
Bram Moolenaar071d4272004-06-13 20:20:40 +00005606 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005607}
5608
5609#ifdef FEAT_BYTEOFF
5610 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005611ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005612{
5613 goto_byte(eap->line2);
5614}
5615#endif
5616
5617/*
5618 * ":shell".
5619 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005620 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005621ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005622{
5623 do_shell(NULL, 0);
5624}
5625
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005626#if defined(HAVE_DROP_FILE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005627
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005628static int drop_busy = FALSE;
5629static int drop_filec;
5630static char_u **drop_filev = NULL;
5631static int drop_split;
5632static void (*drop_callback)(void *);
5633static void *drop_cookie;
5634
5635 static void
5636handle_drop_internal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005637{
5638 exarg_T ea;
5639 int save_msg_scroll = msg_scroll;
5640
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005641 // Setting the argument list may cause screen updates and being called
5642 // recursively. Avoid that by setting drop_busy.
5643 drop_busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005644
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005645 // Check whether the current buffer is changed. If so, we will need
5646 // to split the current window or data could be lost.
5647 // We don't need to check if the 'hidden' option is set, as in this
5648 // case the buffer won't be lost.
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005649 if (!buf_hide(curbuf) && !drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005650 {
5651 ++emsg_off;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005652 drop_split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005653 --emsg_off;
5654 }
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005655 if (drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005656 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005657 if (win_split(0, 0) == FAIL)
5658 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02005659 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005660
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005661 // When splitting the window, create a new alist. Otherwise the
5662 // existing one is overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005663 alist_unlink(curwin->w_alist);
5664 alist_new();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005665 }
5666
5667 /*
5668 * Set up the new argument list.
5669 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005670 alist_set(ALIST(curwin), drop_filec, drop_filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005671
5672 /*
5673 * Move to the first file.
5674 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005675 // Fake up a minimal "next" command for do_argfile()
Bram Moolenaara80faa82020-04-12 19:37:17 +02005676 CLEAR_FIELD(ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005677 ea.cmd = (char_u *)"next";
5678 do_argfile(&ea, 0);
5679
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005680 // do_ecmd() may set need_start_insertmode, but since we never left Insert
5681 // mode that is not needed here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005682 need_start_insertmode = FALSE;
5683
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005684 // Restore msg_scroll, otherwise a following command may cause scrolling
5685 // unexpectedly. The screen will be redrawn by the caller, thus
5686 // msg_scroll being set by displaying a message is irrelevant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005687 msg_scroll = save_msg_scroll;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005688
5689 if (drop_callback != NULL)
5690 drop_callback(drop_cookie);
5691
5692 drop_filev = NULL;
5693 drop_busy = FALSE;
5694}
5695
5696/*
5697 * Handle a file drop. The code is here because a drop is *nearly* like an
5698 * :args command, but not quite (we have a list of exact filenames, so we
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01005699 * don't want to (a) parse a command line, or (b) expand wildcards). So the
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005700 * code is very similar to :args and hence needs access to a lot of the static
5701 * functions in this file.
5702 *
5703 * The "filev" list must have been allocated using alloc(), as should each item
5704 * in the list. This function takes over responsibility for freeing the "filev"
5705 * list.
5706 */
5707 void
5708handle_drop(
5709 int filec, // the number of files dropped
5710 char_u **filev, // the list of files dropped
5711 int split, // force splitting the window
5712 void (*callback)(void *), // to be called after setting the argument
5713 // list
5714 void *cookie) // argument for "callback" (allocated)
5715{
5716 // Cannot handle recursive drops, finish the pending one.
5717 if (drop_busy)
5718 {
5719 FreeWild(filec, filev);
5720 vim_free(cookie);
5721 return;
5722 }
5723
5724 // When calling handle_drop() more than once in a row we only use the last
5725 // one.
5726 if (drop_filev != NULL)
5727 {
5728 FreeWild(drop_filec, drop_filev);
5729 vim_free(drop_cookie);
5730 }
5731
5732 drop_filec = filec;
5733 drop_filev = filev;
5734 drop_split = split;
5735 drop_callback = callback;
5736 drop_cookie = cookie;
5737
5738 // Postpone this when:
5739 // - editing the command line
5740 // - not possible to change the current buffer
5741 // - updating the screen
5742 // As it may change buffers and window structures that are in use and cause
5743 // freed memory to be used.
5744 if (text_locked() || curbuf_locked() || updating_screen)
5745 return;
5746
5747 handle_drop_internal();
5748}
5749
5750/*
5751 * To be called when text is unlocked, curbuf is unlocked or updating_screen is
5752 * reset: Handle a postponed drop.
5753 */
5754 void
5755handle_any_postponed_drop(void)
5756{
5757 if (!drop_busy && drop_filev != NULL
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02005758 && !text_locked() && !curbuf_locked() && !updating_screen)
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005759 handle_drop_internal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005760}
5761#endif
5762
Bram Moolenaar071d4272004-06-13 20:20:40 +00005763/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005764 * ":preserve".
5765 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005766 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005767ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005768{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005769 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005770 ml_preserve(curbuf, TRUE);
5771}
5772
5773/*
5774 * ":recover".
5775 */
5776 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005777ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005778{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005779 // Set recoverymode right away to avoid the ATTENTION prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005780 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01005781 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
5782 | CCGD_MULTWIN
5783 | (eap->forceit ? CCGD_FORCEIT : 0)
5784 | CCGD_EXCMD)
5785
Bram Moolenaar071d4272004-06-13 20:20:40 +00005786 && (*eap->arg == NUL
5787 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
Bram Moolenaar99499b12019-05-23 21:35:48 +02005788 ml_recover(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005789 recoverymode = FALSE;
5790}
5791
5792/*
5793 * Command modifier used in a wrong way.
5794 */
5795 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005796ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005797{
5798 eap->errmsg = e_invcmd;
5799}
5800
Bram Moolenaar071d4272004-06-13 20:20:40 +00005801/*
5802 * :sview [+command] file split window with new file, read-only
5803 * :split [[+command] file] split window with current or new file
5804 * :vsplit [[+command] file] split window vertically with current or new file
5805 * :new [[+command] file] split window with no or new file
5806 * :vnew [[+command] file] split vertically window with no or new file
5807 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005808 *
5809 * :tabedit open new Tab page with empty window
5810 * :tabedit [+command] file open new Tab page and edit "file"
5811 * :tabnew [[+command] file] just like :tabedit
5812 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005813 */
5814 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005815ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005816{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005817 win_T *old_curwin = curwin;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005818#if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005819 char_u *fname = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005820#endif
5821#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005822 int browse_flag = cmdmod.browse;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005823#endif
Bram Moolenaar39902a02018-06-21 22:10:08 +02005824 int use_tab = eap->cmdidx == CMD_tabedit
5825 || eap->cmdidx == CMD_tabfind
5826 || eap->cmdidx == CMD_tabnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005827
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005828 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005829 return;
5830
Bram Moolenaar4033c552017-09-16 20:54:51 +02005831#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005832 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005833#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005834
Bram Moolenaar4033c552017-09-16 20:54:51 +02005835#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005836 // A ":split" in the quickfix window works like ":new". Don't want two
5837 // quickfix windows. But it's OK when doing ":tab split".
Bram Moolenaar05bb9532008-07-04 09:44:11 +00005838 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005839 {
5840 if (eap->cmdidx == CMD_split)
5841 eap->cmdidx = CMD_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005842 if (eap->cmdidx == CMD_vsplit)
5843 eap->cmdidx = CMD_vnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005844 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02005845#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005846
Bram Moolenaar4033c552017-09-16 20:54:51 +02005847#ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005848 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005849 {
5850 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
5851 FNAME_MESS, TRUE, curbuf->b_ffname);
5852 if (fname == NULL)
5853 goto theend;
5854 eap->arg = fname;
5855 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005856# ifdef FEAT_BROWSE
Bram Moolenaar4033c552017-09-16 20:54:51 +02005857 else
5858# endif
5859#endif
5860#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005861 if (cmdmod.browse
Bram Moolenaar071d4272004-06-13 20:20:40 +00005862 && eap->cmdidx != CMD_vnew
Bram Moolenaar071d4272004-06-13 20:20:40 +00005863 && eap->cmdidx != CMD_new)
5864 {
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005865 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005866# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005867 !gui.in_use &&
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005868# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005869 au_has_group((char_u *)"FileExplorer"))
5870 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005871 // No browsing supported but we do have the file explorer:
5872 // Edit the directory.
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005873 if (*eap->arg == NUL || !mch_isdir(eap->arg))
5874 eap->arg = (char_u *)".";
5875 }
5876 else
5877 {
Bram Moolenaar39902a02018-06-21 22:10:08 +02005878 fname = do_browse(0, (char_u *)(use_tab
5879 ? _("Edit File in new tab page")
5880 : _("Edit File in new window")),
Bram Moolenaar071d4272004-06-13 20:20:40 +00005881 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005882 if (fname == NULL)
5883 goto theend;
5884 eap->arg = fname;
5885 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005886 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005887 cmdmod.browse = FALSE; // Don't browse again in do_ecmd().
Bram Moolenaar4033c552017-09-16 20:54:51 +02005888#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005889
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005890 /*
5891 * Either open new tab page or split the window.
5892 */
Bram Moolenaar39902a02018-06-21 22:10:08 +02005893 if (use_tab)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005894 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005895 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
5896 : eap->addr_count == 0 ? 0
5897 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005898 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00005899 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005900
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005901 // set the alternate buffer for the window we came from
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005902 if (curwin != old_curwin
5903 && win_valid(old_curwin)
5904 && old_curwin->w_buffer != curbuf
5905 && !cmdmod.keepalt)
5906 old_curwin->w_alt_fnum = curbuf->b_fnum;
5907 }
5908 }
5909 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005910 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
5911 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005912 // Reset 'scrollbind' when editing another file, but keep it when
5913 // doing ":split" without arguments.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005914 if (*eap->arg != NUL
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01005915# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005916 || cmdmod.browse
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01005917# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005918 )
Bram Moolenaar3368ea22010-09-21 16:56:35 +02005919 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005920 else
5921 do_check_scrollbind(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005922 do_exedit(eap, old_curwin);
5923 }
5924
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005925# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005926 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005927# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005928
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005929# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005930theend:
5931 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005932# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005933}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005934
5935/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00005936 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005937 */
5938 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005939tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00005940{
5941 exarg_T ea;
5942
Bram Moolenaara80faa82020-04-12 19:37:17 +02005943 CLEAR_FIELD(ea);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00005944 ea.cmdidx = CMD_tabnew;
5945 ea.cmd = (char_u *)"tabn";
5946 ea.arg = (char_u *)"";
5947 ex_splitview(&ea);
5948}
5949
5950/*
5951 * :tabnext command
5952 */
5953 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005954ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005955{
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005956 int tab_number;
5957
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02005958 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005959 return;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005960 switch (eap->cmdidx)
5961 {
5962 case CMD_tabfirst:
5963 case CMD_tabrewind:
5964 goto_tabpage(1);
5965 break;
5966 case CMD_tablast:
5967 goto_tabpage(9999);
5968 break;
5969 case CMD_tabprevious:
5970 case CMD_tabNext:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005971 if (eap->arg && *eap->arg != NUL)
5972 {
5973 char_u *p = eap->arg;
5974 char_u *p_save = p;
5975
5976 tab_number = getdigits(&p);
5977 if (p == p_save || *p_save == '-' || *p != NUL
5978 || tab_number == 0)
5979 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005980 // No numbers as argument.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005981 eap->errmsg = e_invarg;
5982 return;
5983 }
5984 }
5985 else
5986 {
5987 if (eap->addr_count == 0)
5988 tab_number = 1;
5989 else
5990 {
5991 tab_number = eap->line2;
5992 if (tab_number < 1)
5993 {
5994 eap->errmsg = e_invrange;
5995 return;
5996 }
5997 }
5998 }
5999 goto_tabpage(-tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006000 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006001 default: // CMD_tabnext
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006002 tab_number = get_tabpage_arg(eap);
6003 if (eap->errmsg == NULL)
6004 goto_tabpage(tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006005 break;
6006 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006007}
6008
6009/*
6010 * :tabmove command
6011 */
6012 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006013ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006014{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02006015 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02006016
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006017 tab_number = get_tabpage_arg(eap);
6018 if (eap->errmsg == NULL)
6019 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006020}
6021
6022/*
6023 * :tabs command: List tabs and their contents.
6024 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006025 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006026ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006027{
6028 tabpage_T *tp;
6029 win_T *wp;
6030 int tabcount = 1;
6031
6032 msg_start();
6033 msg_scroll = TRUE;
6034 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
6035 {
6036 msg_putchar('\n');
6037 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
Bram Moolenaar8820b482017-03-16 17:23:31 +01006038 msg_outtrans_attr(IObuff, HL_ATTR(HLF_T));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006039 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006040 ui_breakcheck();
6041
Bram Moolenaar030f0df2006-02-21 22:02:53 +00006042 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006043 wp = firstwin;
6044 else
6045 wp = tp->tp_firstwin;
6046 for ( ; wp != NULL && !got_int; wp = wp->w_next)
6047 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006048 msg_putchar('\n');
6049 msg_putchar(wp == curwin ? '>' : ' ');
6050 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006051 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
6052 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006053 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02006054 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006055 else
6056 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
6057 IObuff, IOSIZE, TRUE);
6058 msg_outtrans(IObuff);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006059 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006060 ui_breakcheck();
6061 }
6062 }
6063}
6064
Bram Moolenaar071d4272004-06-13 20:20:40 +00006065/*
6066 * ":mode": Set screen mode.
6067 * If no argument given, just get the screen size and redraw.
6068 */
6069 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006070ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006071{
6072 if (*eap->arg == NUL)
6073 shell_resized();
6074 else
Bram Moolenaar3c71aec2020-01-17 19:32:20 +01006075 emsg(_(e_screenmode));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006076}
6077
Bram Moolenaar071d4272004-06-13 20:20:40 +00006078/*
6079 * ":resize".
6080 * set, increment or decrement current window height
6081 */
6082 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006083ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006084{
6085 int n;
6086 win_T *wp = curwin;
6087
6088 if (eap->addr_count > 0)
6089 {
6090 n = eap->line2;
6091 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
6092 ;
6093 }
6094
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006095# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006096 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006097# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006098 n = atol((char *)eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006099 if (cmdmod.split & WSP_VERT)
6100 {
6101 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar02631462017-09-22 15:20:32 +02006102 n += curwin->w_width;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006103 else if (n == 0 && eap->arg[0] == NUL) // default is very wide
Bram Moolenaar071d4272004-06-13 20:20:40 +00006104 n = 9999;
6105 win_setwidth_win((int)n, wp);
6106 }
6107 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006108 {
6109 if (*eap->arg == '-' || *eap->arg == '+')
6110 n += curwin->w_height;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006111 else if (n == 0 && eap->arg[0] == NUL) // default is very high
Bram Moolenaar071d4272004-06-13 20:20:40 +00006112 n = 9999;
6113 win_setheight_win((int)n, wp);
6114 }
6115}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006116
6117/*
6118 * ":find [+command] <file>" command.
6119 */
6120 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006121ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006122{
6123#ifdef FEAT_SEARCHPATH
6124 char_u *fname;
6125 int count;
6126
6127 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
6128 TRUE, curbuf->b_ffname);
6129 if (eap->addr_count > 0)
6130 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006131 // Repeat finding the file "count" times. This matters when it
6132 // appears several times in the path.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006133 count = eap->line2;
6134 while (fname != NULL && --count > 0)
6135 {
6136 vim_free(fname);
6137 fname = find_file_in_path(NULL, 0, FNAME_MESS,
6138 FALSE, curbuf->b_ffname);
6139 }
6140 }
6141
6142 if (fname != NULL)
6143 {
6144 eap->arg = fname;
6145#endif
6146 do_exedit(eap, NULL);
6147#ifdef FEAT_SEARCHPATH
6148 vim_free(fname);
6149 }
6150#endif
6151}
6152
6153/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00006154 * ":open" simulation: for now just work like ":visual".
6155 */
6156 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006157ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006158{
6159 regmatch_T regmatch;
6160 char_u *p;
6161
6162 curwin->w_cursor.lnum = eap->line2;
6163 beginline(BL_SOL | BL_FIX);
6164 if (*eap->arg == '/')
6165 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006166 // ":open /pattern/": put cursor in column found with pattern
Bram Moolenaardf177f62005-02-22 08:39:57 +00006167 ++eap->arg;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006168 p = skip_regexp(eap->arg, '/', p_magic);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006169 *p = NUL;
6170 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
6171 if (regmatch.regprog != NULL)
6172 {
6173 regmatch.rm_ic = p_ic;
6174 p = ml_get_curline();
6175 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006176 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006177 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006178 emsg(_(e_nomatch));
Bram Moolenaar473de612013-06-08 18:19:48 +02006179 vim_regfree(regmatch.regprog);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006180 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006181 // Move to the NUL, ignore any other arguments.
Bram Moolenaardf177f62005-02-22 08:39:57 +00006182 eap->arg += STRLEN(eap->arg);
6183 }
6184 check_cursor();
6185
6186 eap->cmdidx = CMD_visual;
6187 do_exedit(eap, NULL);
6188}
6189
6190/*
6191 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006192 */
6193 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006194ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006195{
6196 do_exedit(eap, NULL);
6197}
6198
6199/*
Bram Moolenaar4b96df52020-01-26 22:00:26 +01006200 * ":edit <file>" command and alike.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006201 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006202 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006203do_exedit(
6204 exarg_T *eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006205 win_T *old_curwin) // curwin before doing a split or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006206{
6207 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006208 int need_hide;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006209 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006210
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01006211 if ((eap->cmdidx != CMD_pedit && ERROR_IF_POPUP_WINDOW)
6212 || ERROR_IF_TERM_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006213 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006214 /*
6215 * ":vi" command ends Ex mode.
6216 */
6217 if (exmode_active && (eap->cmdidx == CMD_visual
6218 || eap->cmdidx == CMD_view))
6219 {
6220 exmode_active = FALSE;
6221 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006222 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006223 // Special case: ":global/pat/visual\NLvi-commands"
Bram Moolenaardf177f62005-02-22 08:39:57 +00006224 if (global_busy)
6225 {
6226 int rd = RedrawingDisabled;
6227 int nwr = no_wait_return;
6228 int ms = msg_scroll;
6229#ifdef FEAT_GUI
6230 int he = hold_gui_events;
6231#endif
6232
6233 if (eap->nextcmd != NULL)
6234 {
6235 stuffReadbuff(eap->nextcmd);
6236 eap->nextcmd = NULL;
6237 }
6238
6239 if (exmode_was != EXMODE_VIM)
6240 settmode(TMODE_RAW);
6241 RedrawingDisabled = 0;
6242 no_wait_return = 0;
6243 need_wait_return = FALSE;
6244 msg_scroll = 0;
6245#ifdef FEAT_GUI
6246 hold_gui_events = 0;
6247#endif
6248 must_redraw = CLEAR;
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01006249 pending_exmode_active = TRUE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006250
6251 main_loop(FALSE, TRUE);
6252
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01006253 pending_exmode_active = FALSE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006254 RedrawingDisabled = rd;
6255 no_wait_return = nwr;
6256 msg_scroll = ms;
6257#ifdef FEAT_GUI
6258 hold_gui_events = he;
6259#endif
6260 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006261 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006262 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006263 }
6264
6265 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006266 || eap->cmdidx == CMD_tabnew
6267 || eap->cmdidx == CMD_tabedit
Bram Moolenaar4033c552017-09-16 20:54:51 +02006268 || eap->cmdidx == CMD_vnew) && *eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006269 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006270 // ":new" or ":tabnew" without argument: edit an new empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006271 setpcmark();
6272 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006273 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
6274 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006275 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006276 else if ((eap->cmdidx != CMD_split && eap->cmdidx != CMD_vsplit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006277 || *eap->arg != NUL
6278#ifdef FEAT_BROWSE
6279 || cmdmod.browse
6280#endif
6281 )
6282 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006283 // Can't edit another file when "curbuf_lock" is set. Only ":edit"
6284 // can bring us here, others are stopped earlier.
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006285 if (*eap->arg != NUL && curbuf_locked())
6286 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006287
Bram Moolenaar071d4272004-06-13 20:20:40 +00006288 n = readonlymode;
6289 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
6290 readonlymode = TRUE;
6291 else if (eap->cmdidx == CMD_enew)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006292 readonlymode = FALSE; // 'readonly' doesn't make sense in an
6293 // empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006294 setpcmark();
6295 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
6296 NULL, eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006297 // ":edit" goes to first line if Vi compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00006298 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
6299 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
6300 ? ECMD_ONE : eap->do_ecmd_lnum,
Bram Moolenaareb44a682017-08-03 22:44:55 +02006301 (buf_hide(curbuf) ? ECMD_HIDE : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006302 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006303 // after a split we can use an existing buffer
Bram Moolenaar7b449342014-03-25 13:03:48 +01006304 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006305 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006306 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006307 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006308 // Editing the file failed. If the window was split, close it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006309 if (old_curwin != NULL)
6310 {
6311 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02006312 if (!need_hide || buf_hide(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006313 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006314#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006315 cleanup_T cs;
6316
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006317 // Reset the error/interrupt/exception state here so that
6318 // aborting() returns FALSE when closing a window.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006319 enter_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02006320#endif
6321#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006322 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006323#endif
Bram Moolenaareb44a682017-08-03 22:44:55 +02006324 win_close(curwin, !need_hide && !buf_hide(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006325
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006326#if defined(FEAT_EVAL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006327 // Restore the error/interrupt/exception state if not
6328 // discarded by a new aborting error, interrupt, or
6329 // uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006330 leave_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02006331#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006332 }
6333 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006334 }
6335 else if (readonlymode && curbuf->b_nwindows == 1)
6336 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006337 // When editing an already visited buffer, 'readonly' won't be set
6338 // but the previous value is kept. With ":view" and ":sview" we
6339 // want the file to be readonly, except when another window is
6340 // editing the same buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006341 curbuf->b_p_ro = TRUE;
6342 }
6343 readonlymode = n;
6344 }
6345 else
6346 {
6347 if (eap->do_ecmd_cmd != NULL)
6348 do_cmdline_cmd(eap->do_ecmd_cmd);
6349#ifdef FEAT_TITLE
6350 n = curwin->w_arg_idx_invalid;
6351#endif
6352 check_arg_idx(curwin);
6353#ifdef FEAT_TITLE
6354 if (n != curwin->w_arg_idx_invalid)
6355 maketitle();
6356#endif
6357 }
6358
Bram Moolenaar071d4272004-06-13 20:20:40 +00006359 /*
6360 * if ":split file" worked, set alternate file name in old window to new
6361 * file
6362 */
6363 if (old_curwin != NULL
6364 && *eap->arg != NUL
6365 && curwin != old_curwin
6366 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00006367 && old_curwin->w_buffer != curbuf
6368 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006369 old_curwin->w_alt_fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006370
6371 ex_no_reprint = TRUE;
6372}
6373
6374#ifndef FEAT_GUI
6375/*
6376 * ":gui" and ":gvim" when there is no GUI.
6377 */
6378 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006379ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006380{
6381 eap->errmsg = e_nogvim;
6382}
6383#endif
6384
Bram Moolenaar4f974752019-02-17 17:44:42 +01006385#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006386 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006387ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006388{
6389 gui_make_tearoff(eap->arg);
6390}
6391#endif
6392
Bram Moolenaar29a2c082018-03-05 21:06:23 +01006393#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
6394 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006395 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006396ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006397{
Bram Moolenaar29a2c082018-03-05 21:06:23 +01006398# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
6399 if (gui.in_use)
6400 gui_make_popup(eap->arg, eap->forceit);
6401# ifdef FEAT_TERM_POPUP_MENU
6402 else
6403# endif
6404# endif
6405# ifdef FEAT_TERM_POPUP_MENU
6406 pum_make_popup(eap->arg, eap->forceit);
6407# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006408}
6409#endif
6410
Bram Moolenaar071d4272004-06-13 20:20:40 +00006411 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006412ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006413{
6414 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006415 msg(_("No swap file"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006416 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01006417 msg((char *)curbuf->b_ml.ml_mfp->mf_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006418}
6419
6420/*
6421 * ":syncbind" forces all 'scrollbind' windows to have the same relative
6422 * offset.
6423 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
6424 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006425 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006426ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006427{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006428 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006429 win_T *save_curwin = curwin;
6430 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006431 long topline;
6432 long y;
6433 linenr_T old_linenr = curwin->w_cursor.lnum;
6434
6435 setpcmark();
6436
6437 /*
6438 * determine max topline
6439 */
6440 if (curwin->w_p_scb)
6441 {
6442 topline = curwin->w_topline;
Bram Moolenaar29323592016-07-24 22:04:11 +02006443 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006444 {
6445 if (wp->w_p_scb && wp->w_buffer)
6446 {
Bram Moolenaar375e3392019-01-31 18:26:10 +01006447 y = wp->w_buffer->b_ml.ml_line_count - get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006448 if (topline > y)
6449 topline = y;
6450 }
6451 }
6452 if (topline < 1)
6453 topline = 1;
6454 }
6455 else
6456 {
6457 topline = 1;
6458 }
6459
6460
6461 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006462 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006463 */
Bram Moolenaar29323592016-07-24 22:04:11 +02006464 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006465 {
6466 if (curwin->w_p_scb)
6467 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006468 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006469 y = topline - curwin->w_topline;
6470 if (y > 0)
6471 scrollup(y, TRUE);
6472 else
6473 scrolldown(-y, TRUE);
6474 curwin->w_scbind_pos = topline;
6475 redraw_later(VALID);
6476 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006477 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006478 }
6479 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006480 curwin = save_curwin;
6481 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006482 if (curwin->w_p_scb)
6483 {
6484 did_syncbind = TRUE;
6485 checkpcmark();
6486 if (old_linenr != curwin->w_cursor.lnum)
6487 {
6488 char_u ctrl_o[2];
6489
6490 ctrl_o[0] = Ctrl_O;
6491 ctrl_o[1] = 0;
6492 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
6493 }
6494 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006495}
6496
6497
6498 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006499ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006500{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006501 int i;
6502 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
6503 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006504
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006505 if (eap->usefilter) // :r!cmd
Bram Moolenaar071d4272004-06-13 20:20:40 +00006506 do_bang(1, eap, FALSE, FALSE, TRUE);
6507 else
6508 {
6509 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
6510 return;
6511
6512#ifdef FEAT_BROWSE
6513 if (cmdmod.browse)
6514 {
6515 char_u *browseFile;
6516
Bram Moolenaar7171abe2004-10-11 10:06:20 +00006517 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006518 NULL, NULL, NULL, curbuf);
6519 if (browseFile != NULL)
6520 {
6521 i = readfile(browseFile, NULL,
6522 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
6523 vim_free(browseFile);
6524 }
6525 else
6526 i = OK;
6527 }
6528 else
6529#endif
6530 if (*eap->arg == NUL)
6531 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006532 if (check_fname() == FAIL) // check for no file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00006533 return;
6534 i = readfile(curbuf->b_ffname, curbuf->b_fname,
6535 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
6536 }
6537 else
6538 {
6539 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
6540 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
6541 i = readfile(eap->arg, NULL,
6542 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
6543
6544 }
Bram Moolenaare13b9af2017-01-13 22:01:02 +01006545 if (i != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006546 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006547#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006548 if (!aborting())
6549#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006550 semsg(_(e_notopen), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006551 }
6552 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00006553 {
6554 if (empty && exmode_active)
6555 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006556 // Delete the empty line that remains. Historically ex does
6557 // this but vi doesn't.
Bram Moolenaardf177f62005-02-22 08:39:57 +00006558 if (eap->line2 == 0)
6559 lnum = curbuf->b_ml.ml_line_count;
6560 else
6561 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00006562 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006563 {
6564 ml_delete(lnum, FALSE);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00006565 if (curwin->w_cursor.lnum > 1
6566 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006567 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00006568 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006569 }
6570 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006571 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006572 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006573 }
6574}
6575
Bram Moolenaarea408852005-06-25 22:49:46 +00006576static char_u *prev_dir = NULL;
6577
6578#if defined(EXITFREE) || defined(PROTO)
6579 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006580free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00006581{
Bram Moolenaard23a8232018-02-10 18:45:26 +01006582 VIM_CLEAR(prev_dir);
6583 VIM_CLEAR(globaldir);
Bram Moolenaarea408852005-06-25 22:49:46 +00006584}
6585#endif
6586
Bram Moolenaarf4258302013-06-02 18:20:17 +02006587/*
6588 * Deal with the side effects of changing the current directory.
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006589 * When 'scope' is CDSCOPE_TABPAGE then this was after an ":tcd" command.
6590 * When 'scope' is CDSCOPE_WINDOW then this was after an ":lcd" command.
Bram Moolenaarf4258302013-06-02 18:20:17 +02006591 */
6592 void
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006593post_chdir(cdscope_T scope)
Bram Moolenaarf4258302013-06-02 18:20:17 +02006594{
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006595 if (scope != CDSCOPE_WINDOW)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006596 // Clear tab local directory for both :cd and :tcd
6597 VIM_CLEAR(curtab->tp_localdir);
Bram Moolenaard23a8232018-02-10 18:45:26 +01006598 VIM_CLEAR(curwin->w_localdir);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006599 if (scope != CDSCOPE_GLOBAL)
Bram Moolenaarf4258302013-06-02 18:20:17 +02006600 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006601 // If still in global directory, need to remember current
6602 // directory as global directory.
Bram Moolenaarf4258302013-06-02 18:20:17 +02006603 if (globaldir == NULL && prev_dir != NULL)
6604 globaldir = vim_strsave(prev_dir);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006605 // Remember this local directory for the window.
Bram Moolenaarf4258302013-06-02 18:20:17 +02006606 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006607 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006608 if (scope == CDSCOPE_TABPAGE)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006609 curtab->tp_localdir = vim_strsave(NameBuff);
6610 else
6611 curwin->w_localdir = vim_strsave(NameBuff);
6612 }
Bram Moolenaarf4258302013-06-02 18:20:17 +02006613 }
6614 else
6615 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006616 // We are now in the global directory, no need to remember its
6617 // name.
Bram Moolenaard23a8232018-02-10 18:45:26 +01006618 VIM_CLEAR(globaldir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006619 }
6620
6621 shorten_fnames(TRUE);
6622}
6623
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006624/*
6625 * Change directory function used by :cd/:tcd/:lcd Ex commands and the
6626 * chdir() function. If 'winlocaldir' is TRUE, then changes the window-local
6627 * directory. If 'tablocaldir' is TRUE, then changes the tab-local directory.
6628 * Otherwise changes the global directory.
6629 * Returns TRUE if the directory is successfully changed.
6630 */
6631 int
6632changedir_func(
6633 char_u *new_dir,
6634 int forceit,
6635 cdscope_T scope)
6636{
6637 char_u *tofree;
6638 int dir_differs;
6639 int retval = FALSE;
6640
Bram Moolenaar7cc96922020-01-31 22:41:38 +01006641 if (new_dir == NULL || allbuf_locked())
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006642 return FALSE;
6643
6644 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged() && !forceit)
6645 {
6646 emsg(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
6647 return FALSE;
6648 }
6649
6650 // ":cd -": Change to previous directory
6651 if (STRCMP(new_dir, "-") == 0)
6652 {
6653 if (prev_dir == NULL)
6654 {
6655 emsg(_("E186: No previous directory"));
6656 return FALSE;
6657 }
6658 new_dir = prev_dir;
6659 }
6660
6661 // Save current directory for next ":cd -"
6662 tofree = prev_dir;
6663 if (mch_dirname(NameBuff, MAXPATHL) == OK)
6664 prev_dir = vim_strsave(NameBuff);
6665 else
6666 prev_dir = NULL;
6667
6668#if defined(UNIX) || defined(VMS)
6669 // for UNIX ":cd" means: go to home directory
6670 if (*new_dir == NUL)
6671 {
6672 // use NameBuff for home directory name
6673# ifdef VMS
6674 char_u *p;
6675
6676 p = mch_getenv((char_u *)"SYS$LOGIN");
6677 if (p == NULL || *p == NUL) // empty is the same as not set
6678 NameBuff[0] = NUL;
6679 else
6680 vim_strncpy(NameBuff, p, MAXPATHL - 1);
6681# else
6682 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
6683# endif
6684 new_dir = NameBuff;
6685 }
6686#endif
6687 dir_differs = new_dir == NULL || prev_dir == NULL
6688 || pathcmp((char *)prev_dir, (char *)new_dir, -1) != 0;
6689 if (new_dir == NULL || (dir_differs && vim_chdir(new_dir)))
6690 emsg(_(e_failed));
6691 else
6692 {
6693 char_u *acmd_fname;
6694
6695 post_chdir(scope);
6696
6697 if (dir_differs)
6698 {
6699 if (scope == CDSCOPE_WINDOW)
6700 acmd_fname = (char_u *)"window";
6701 else if (scope == CDSCOPE_TABPAGE)
6702 acmd_fname = (char_u *)"tabpage";
6703 else
6704 acmd_fname = (char_u *)"global";
6705 apply_autocmds(EVENT_DIRCHANGED, acmd_fname, new_dir, FALSE,
6706 curbuf);
6707 }
6708 retval = TRUE;
6709 }
6710 vim_free(tofree);
6711
6712 return retval;
6713}
Bram Moolenaarea408852005-06-25 22:49:46 +00006714
Bram Moolenaar071d4272004-06-13 20:20:40 +00006715/*
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006716 * ":cd", ":tcd", ":lcd", ":chdir" ":tchdir" and ":lchdir".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006717 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00006718 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006719ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006720{
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01006721 char_u *new_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006722
6723 new_dir = eap->arg;
6724#if !defined(UNIX) && !defined(VMS)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006725 // for non-UNIX ":cd" means: print current directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006726 if (*new_dir == NUL)
6727 ex_pwd(NULL);
6728 else
6729#endif
6730 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006731 cdscope_T scope = CDSCOPE_GLOBAL;
6732
6733 if (eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir)
6734 scope = CDSCOPE_WINDOW;
6735 else if (eap->cmdidx == CMD_tcd || eap->cmdidx == CMD_tchdir)
6736 scope = CDSCOPE_TABPAGE;
6737
6738 if (changedir_func(new_dir, eap->forceit, scope))
Bram Moolenaardf177f62005-02-22 08:39:57 +00006739 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006740 // Echo the new current directory if the command was typed.
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00006741 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006742 ex_pwd(eap);
6743 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006744 }
6745}
6746
6747/*
6748 * ":pwd".
6749 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006750 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006751ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006752{
6753 if (mch_dirname(NameBuff, MAXPATHL) == OK)
6754 {
6755#ifdef BACKSLASH_IN_FILENAME
6756 slash_adjust(NameBuff);
6757#endif
Bram Moolenaar32526b32019-01-19 17:43:09 +01006758 msg((char *)NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006759 }
6760 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006761 emsg(_("E187: Unknown"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006762}
6763
6764/*
6765 * ":=".
6766 */
6767 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006768ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006769{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006770 smsg("%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006771 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006772}
6773
6774 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006775ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006776{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006777 int n;
6778 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006779
6780 if (cursor_valid())
6781 {
6782 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
6783 if (n >= 0)
Bram Moolenaar53f81742017-09-22 14:35:51 +02006784 windgoto((int)n, curwin->w_wincol + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006785 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006786
6787 len = eap->line2;
6788 switch (*eap->arg)
6789 {
6790 case 'm': break;
6791 case NUL: len *= 1000L; break;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006792 default: semsg(_(e_invarg2), eap->arg); return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006793 }
6794 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006795}
6796
6797/*
6798 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
6799 */
6800 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006801do_sleep(long msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006802{
Bram Moolenaar52953192019-08-16 10:27:13 +02006803 long done = 0;
Bram Moolenaar975b5272016-03-15 23:10:59 +01006804 long wait_now;
Bram Moolenaar52953192019-08-16 10:27:13 +02006805# ifdef ELAPSED_FUNC
6806 elapsed_T start_tv;
6807
6808 // Remember at what time we started, so that we know how much longer we
6809 // should wait after waiting for a bit.
6810 ELAPSED_INIT(start_tv);
6811# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006812
6813 cursor_on();
Bram Moolenaarf45d7472018-09-30 18:22:26 +02006814 out_flush_cursor(FALSE, FALSE);
Bram Moolenaar52953192019-08-16 10:27:13 +02006815 while (!got_int && done < msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006816 {
Bram Moolenaar975b5272016-03-15 23:10:59 +01006817 wait_now = msec - done > 1000L ? 1000L : msec - done;
6818#ifdef FEAT_TIMERS
6819 {
6820 long due_time = check_due_timer();
6821
6822 if (due_time > 0 && due_time < wait_now)
6823 wait_now = due_time;
6824 }
6825#endif
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02006826#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar28e67e02019-08-15 23:05:49 +02006827 if (has_any_channel() && wait_now > 20L)
6828 wait_now = 20L;
6829#endif
6830#ifdef FEAT_SOUND
6831 if (has_any_sound_callback() && wait_now > 20L)
6832 wait_now = 20L;
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02006833#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +01006834 ui_delay(wait_now, TRUE);
Bram Moolenaar52953192019-08-16 10:27:13 +02006835
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02006836#ifdef FEAT_JOB_CHANNEL
6837 if (has_any_channel())
6838 ui_breakcheck_force(TRUE);
6839 else
6840#endif
6841 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006842#ifdef MESSAGE_QUEUE
Bram Moolenaar52953192019-08-16 10:27:13 +02006843 // Process the netbeans and clientserver messages that may have been
6844 // received in the call to ui_breakcheck() when the GUI is in use. This
6845 // may occur when running a test case.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006846 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02006847#endif
Bram Moolenaar52953192019-08-16 10:27:13 +02006848
6849# ifdef ELAPSED_FUNC
6850 // actual time passed
6851 done = ELAPSED_FUNC(start_tv);
6852# else
6853 // guestimate time passed (will actually be more)
6854 done += wait_now;
6855# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006856 }
Bram Moolenaar1ebff3d2018-07-07 16:18:13 +02006857
6858 // If CTRL-C was typed to interrupt the sleep, drop the CTRL-C from the
6859 // input buffer, otherwise a following call to input() fails.
6860 if (got_int)
6861 (void)vpeekc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006862}
6863
Bram Moolenaar071d4272004-06-13 20:20:40 +00006864/*
6865 * ":winsize" command (obsolete).
6866 */
6867 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006868ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006869{
6870 int w, h;
6871 char_u *arg = eap->arg;
6872 char_u *p;
6873
6874 w = getdigits(&arg);
6875 arg = skipwhite(arg);
6876 p = arg;
6877 h = getdigits(&arg);
6878 if (*p != NUL && *arg == NUL)
6879 set_shellsize(w, h, TRUE);
6880 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006881 emsg(_("E465: :winsize requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006882}
6883
Bram Moolenaar071d4272004-06-13 20:20:40 +00006884 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006885ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006886{
6887 int xchar = NUL;
6888 char_u *p;
6889
6890 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
6891 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006892 // CTRL-W g and CTRL-W CTRL-G have an extra command character
Bram Moolenaar071d4272004-06-13 20:20:40 +00006893 if (eap->arg[1] == NUL)
6894 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006895 emsg(_(e_invarg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006896 return;
6897 }
6898 xchar = eap->arg[1];
6899 p = eap->arg + 2;
6900 }
6901 else
6902 p = eap->arg + 1;
6903
6904 eap->nextcmd = check_nextcmd(p);
6905 p = skipwhite(p);
6906 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006907 emsg(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02006908 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006909 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006910 // Pass flags on for ":vertical wincmd ]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006911 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00006912 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006913 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
6914 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00006915 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006916 }
6917}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006918
Bram Moolenaar843ee412004-06-30 16:16:41 +00006919#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006920/*
6921 * ":winpos".
6922 */
6923 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006924ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006925{
6926 int x, y;
6927 char_u *arg = eap->arg;
6928 char_u *p;
6929
6930 if (*arg == NUL)
6931 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00006932# if defined(FEAT_GUI) || defined(MSWIN)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006933# ifdef VIMDLL
6934 if (gui.in_use ? gui_mch_get_winpos(&x, &y) != FAIL :
6935 mch_get_winpos(&x, &y) != FAIL)
6936# elif defined(FEAT_GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006937 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00006938# else
6939 if (mch_get_winpos(&x, &y) != FAIL)
6940# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006941 {
6942 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
Bram Moolenaar32526b32019-01-19 17:43:09 +01006943 msg((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006944 }
6945 else
6946# endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006947 emsg(_("E188: Obtaining window position not implemented for this platform"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006948 }
6949 else
6950 {
6951 x = getdigits(&arg);
6952 arg = skipwhite(arg);
6953 p = arg;
6954 y = getdigits(&arg);
6955 if (*p == NUL || *arg != NUL)
6956 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006957 emsg(_("E466: :winpos requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006958 return;
6959 }
6960# ifdef FEAT_GUI
6961 if (gui.in_use)
6962 gui_mch_set_winpos(x, y);
6963 else if (gui.starting)
6964 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006965 // Remember the coordinates for when the window is opened.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006966 gui_win_x = x;
6967 gui_win_y = y;
6968 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006969# if defined(HAVE_TGETENT) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006970 else
6971# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006972# endif
6973# if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar843ee412004-06-30 16:16:41 +00006974 mch_set_winpos(x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006975# endif
6976# ifdef HAVE_TGETENT
6977 if (*T_CWP)
6978 term_set_winpos(x, y);
6979# endif
6980 }
6981}
6982#endif
6983
6984/*
6985 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
6986 */
6987 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006988ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006989{
6990 oparg_T oa;
6991
6992 clear_oparg(&oa);
6993 oa.regname = eap->regname;
6994 oa.start.lnum = eap->line1;
6995 oa.end.lnum = eap->line2;
6996 oa.line_count = eap->line2 - eap->line1 + 1;
6997 oa.motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006998 virtual_op = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006999 if (eap->cmdidx != CMD_yank) // position cursor for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00007000 {
7001 setpcmark();
7002 curwin->w_cursor.lnum = eap->line1;
7003 beginline(BL_SOL | BL_FIX);
7004 }
7005
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007006 if (VIsual_active)
7007 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007008
Bram Moolenaar071d4272004-06-13 20:20:40 +00007009 switch (eap->cmdidx)
7010 {
7011 case CMD_delete:
7012 oa.op_type = OP_DELETE;
7013 op_delete(&oa);
7014 break;
7015
7016 case CMD_yank:
7017 oa.op_type = OP_YANK;
7018 (void)op_yank(&oa, FALSE, TRUE);
7019 break;
7020
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007021 default: // CMD_rshift or CMD_lshift
Bram Moolenaar6e707362013-06-14 19:15:58 +02007022 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007023#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02007024 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
7025#else
7026 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00007027#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02007028 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007029 oa.op_type = OP_RSHIFT;
7030 else
7031 oa.op_type = OP_LSHIFT;
7032 op_shift(&oa, FALSE, eap->amount);
7033 break;
7034 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007035 virtual_op = MAYBE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007036 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007037}
7038
7039/*
7040 * ":put".
7041 */
7042 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007043ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007044{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007045 // ":0put" works like ":1put!".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007046 if (eap->line2 == 0)
7047 {
7048 eap->line2 = 1;
7049 eap->forceit = TRUE;
7050 }
7051 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007052 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
7053 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007054}
7055
7056/*
7057 * Handle ":copy" and ":move".
7058 */
7059 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007060ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007061{
7062 long n;
7063
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02007064 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE, FALSE, 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007065 if (eap->arg == NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00007066 {
7067 eap->nextcmd = NULL;
7068 return;
7069 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00007070 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007071
7072 /*
7073 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
7074 */
7075 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
7076 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02007077 emsg(_(e_invrange));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007078 return;
7079 }
7080
7081 if (eap->cmdidx == CMD_move)
7082 {
7083 if (do_move(eap->line1, eap->line2, n) == FAIL)
7084 return;
7085 }
7086 else
7087 ex_copy(eap->line1, eap->line2, n);
7088 u_clearline();
7089 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007090 ex_may_print(eap);
7091}
7092
7093/*
7094 * Print the current line if flags were given to the Ex command.
7095 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02007096 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007097ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007098{
7099 if (eap->flags != 0)
7100 {
7101 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
7102 (eap->flags & EXFLAG_LIST));
7103 ex_no_reprint = TRUE;
7104 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007105}
7106
7107/*
7108 * ":smagic" and ":snomagic".
7109 */
7110 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007111ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007112{
7113 int magic_save = p_magic;
7114
7115 p_magic = (eap->cmdidx == CMD_smagic);
7116 do_sub(eap);
7117 p_magic = magic_save;
7118}
7119
7120/*
7121 * ":join".
7122 */
7123 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007124ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007125{
7126 curwin->w_cursor.lnum = eap->line1;
7127 if (eap->line1 == eap->line2)
7128 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007129 if (eap->addr_count >= 2) // :2,2join does nothing
Bram Moolenaar071d4272004-06-13 20:20:40 +00007130 return;
7131 if (eap->line2 == curbuf->b_ml.ml_line_count)
7132 {
7133 beep_flush();
7134 return;
7135 }
7136 ++eap->line2;
7137 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02007138 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007139 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007140 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007141}
7142
7143/*
7144 * ":[addr]@r" or ":[addr]*r": execute register
7145 */
7146 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007147ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007148{
7149 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00007150 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007151
7152 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4930a762016-09-11 14:39:53 +02007153 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007154
7155#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007156 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00007157#endif
7158
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007159 // get the register name. No name means to use the previous one
Bram Moolenaar071d4272004-06-13 20:20:40 +00007160 c = *eap->arg;
7161 if (c == NUL || (c == '*' && *eap->cmd == '*'))
7162 c = '@';
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007163 // Put the register in the typeahead buffer with the "silent" flag.
Bram Moolenaard333d1e2006-11-07 17:43:47 +00007164 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
7165 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007166 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007167 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00007168 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007169 else
7170 {
7171 int save_efr = exec_from_reg;
7172
7173 exec_from_reg = TRUE;
7174
7175 /*
7176 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00007177 * Continue until the stuff buffer is empty and all added characters
7178 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007179 */
Bram Moolenaar60462872009-11-03 11:40:19 +00007180 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007181 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
7182
7183 exec_from_reg = save_efr;
7184 }
7185}
7186
7187/*
7188 * ":!".
7189 */
7190 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007191ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007192{
7193 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
7194}
7195
7196/*
7197 * ":undo".
7198 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007199 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01007200ex_undo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007201{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007202 if (eap->addr_count == 1) // :undo 123
Bram Moolenaar730cde92010-06-27 05:18:54 +02007203 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00007204 else
7205 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007206}
7207
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007208#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02007209 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007210ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007211{
7212 char_u hash[UNDO_HASH_SIZE];
7213
7214 u_compute_hash(hash);
7215 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
7216}
7217
Bram Moolenaar6df6f472010-07-18 18:04:50 +02007218 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007219ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007220{
7221 char_u hash[UNDO_HASH_SIZE];
7222
7223 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02007224 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007225}
7226#endif
7227
Bram Moolenaar071d4272004-06-13 20:20:40 +00007228/*
7229 * ":redo".
7230 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007231 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007232ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007233{
7234 u_redo(1);
7235}
7236
7237/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007238 * ":earlier" and ":later".
7239 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007240 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007241ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007242{
7243 long count = 0;
7244 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02007245 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007246 char_u *p = eap->arg;
7247
7248 if (*p == NUL)
7249 count = 1;
7250 else if (isdigit(*p))
7251 {
7252 count = getdigits(&p);
7253 switch (*p)
7254 {
7255 case 's': ++p; sec = TRUE; break;
7256 case 'm': ++p; sec = TRUE; count *= 60; break;
7257 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02007258 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
7259 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007260 }
7261 }
7262
7263 if (*p != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007264 semsg(_(e_invarg2), eap->arg);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007265 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02007266 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
7267 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007268}
7269
7270/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007271 * ":redir": start/stop redirection.
7272 */
7273 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007274ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007275{
7276 char *mode;
7277 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00007278 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007279
Bram Moolenaarba768492016-07-08 15:32:54 +02007280#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02007281 if (redir_execute)
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007282 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007283 emsg(_("E930: Cannot use :redir inside execute()"));
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007284 return;
7285 }
Bram Moolenaarba768492016-07-08 15:32:54 +02007286#endif
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007287
Bram Moolenaar071d4272004-06-13 20:20:40 +00007288 if (STRICMP(eap->arg, "END") == 0)
7289 close_redir();
7290 else
7291 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007292 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007293 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007294 ++arg;
7295 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007296 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007297 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007298 mode = "a";
7299 }
7300 else
7301 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00007302 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007303
7304 close_redir();
7305
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007306 // Expand environment variables and "~/".
Bram Moolenaarca472992005-01-21 11:46:23 +00007307 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007308 if (fname == NULL)
7309 return;
7310#ifdef FEAT_BROWSE
7311 if (cmdmod.browse)
7312 {
7313 char_u *browseFile;
7314
Bram Moolenaar7171abe2004-10-11 10:06:20 +00007315 browseFile = do_browse(BROWSE_SAVE,
7316 (char_u *)_("Save Redirection"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +02007317 fname, NULL, NULL,
7318 (char_u *)_(BROWSE_FILTER_ALL_FILES), curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007319 if (browseFile == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007320 return; // operation cancelled
Bram Moolenaar071d4272004-06-13 20:20:40 +00007321 vim_free(fname);
7322 fname = browseFile;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007323 eap->forceit = TRUE; // since dialog already asked
Bram Moolenaar071d4272004-06-13 20:20:40 +00007324 }
7325#endif
7326
7327 redir_fd = open_exfile(fname, eap->forceit, mode);
7328 vim_free(fname);
7329 }
7330#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00007331 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007332 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007333 // redirect to a register a-z (resp. A-Z for appending)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007334 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00007335 ++arg;
7336 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007337# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00007338 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00007339 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00007340# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00007341 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007342 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007343 redir_reg = *arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007344 if (*arg == '>' && arg[1] == '>') // append
Bram Moolenaard9d30582005-05-18 22:10:28 +00007345 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00007346 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007347 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007348 // Can use both "@a" and "@a>".
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00007349 if (*arg == '>')
7350 arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007351 // Make register empty when not using @A-@Z and the
7352 // command is valid.
Bram Moolenaarc188b882007-10-19 14:20:54 +00007353 if (*arg == NUL && !isupper(redir_reg))
7354 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007355 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00007356 }
7357 if (*arg != NUL)
7358 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007359 redir_reg = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007360 semsg(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007361 }
7362 }
7363 else if (*arg == '=' && arg[1] == '>')
7364 {
7365 int append;
7366
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007367 // redirect to a variable
Bram Moolenaardf177f62005-02-22 08:39:57 +00007368 close_redir();
7369 arg += 2;
7370
7371 if (*arg == '>')
7372 {
7373 ++arg;
7374 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007375 }
7376 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00007377 append = FALSE;
7378
7379 if (var_redir_start(skipwhite(arg), append) == OK)
7380 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007381 }
7382#endif
7383
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007384 // TODO: redirect to a buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00007385
Bram Moolenaar071d4272004-06-13 20:20:40 +00007386 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007387 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007388 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00007389
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007390 // Make sure redirection is not off. Can happen for cmdline completion
7391 // that indirectly invokes a command to catch its output.
Bram Moolenaar29b2d262006-09-10 19:07:28 +00007392 if (redir_fd != NULL
7393#ifdef FEAT_EVAL
7394 || redir_reg || redir_vname
7395#endif
7396 )
7397 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007398}
7399
7400/*
7401 * ":redraw": force redraw
7402 */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01007403 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007404ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007405{
7406 int r = RedrawingDisabled;
7407 int p = p_lz;
7408
7409 RedrawingDisabled = 0;
7410 p_lz = FALSE;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01007411 validate_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007412 update_topline();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007413 update_screen(eap->forceit ? CLEAR : VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007414#ifdef FEAT_TITLE
7415 if (need_maketitle)
7416 maketitle();
7417#endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007418#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
7419# ifdef VIMDLL
7420 if (!gui.in_use)
7421# endif
7422 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01007423#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007424 RedrawingDisabled = r;
7425 p_lz = p;
7426
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007427 // Reset msg_didout, so that a message that's there is overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007428 msg_didout = FALSE;
7429 msg_col = 0;
7430
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007431 // No need to wait after an intentional redraw.
Bram Moolenaar56667a52013-07-17 11:54:28 +02007432 need_wait_return = FALSE;
7433
Bram Moolenaar071d4272004-06-13 20:20:40 +00007434 out_flush();
7435}
7436
7437/*
7438 * ":redrawstatus": force redraw of status line(s)
7439 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007440 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007441ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007442{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007443 int r = RedrawingDisabled;
7444 int p = p_lz;
7445
7446 RedrawingDisabled = 0;
7447 p_lz = FALSE;
7448 if (eap->forceit)
7449 status_redraw_all();
7450 else
7451 status_redraw_curbuf();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007452 update_screen(VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007453 RedrawingDisabled = r;
7454 p_lz = p;
7455 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007456}
7457
Bram Moolenaare12bab32019-01-08 22:02:56 +01007458/*
7459 * ":redrawtabline": force redraw of the tabline
7460 */
7461 static void
7462ex_redrawtabline(exarg_T *eap UNUSED)
7463{
7464 int r = RedrawingDisabled;
7465 int p = p_lz;
7466
7467 RedrawingDisabled = 0;
7468 p_lz = FALSE;
7469
7470 draw_tabline();
7471
7472 RedrawingDisabled = r;
7473 p_lz = p;
7474 out_flush();
7475}
7476
Bram Moolenaar071d4272004-06-13 20:20:40 +00007477 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007478close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007479{
7480 if (redir_fd != NULL)
7481 {
7482 fclose(redir_fd);
7483 redir_fd = NULL;
7484 }
7485#ifdef FEAT_EVAL
7486 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007487 if (redir_vname)
7488 {
7489 var_redir_stop();
7490 redir_vname = 0;
7491 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007492#endif
7493}
7494
Bram Moolenaarf96ae0b2019-07-28 15:21:55 +02007495#if (defined(FEAT_SESSION) || defined(FEAT_EVAL)) || defined(PROTO)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007496 int
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02007497vim_mkdir_emsg(char_u *name, int prot UNUSED)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007498{
7499 if (vim_mkdir(name, prot) != 0)
7500 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007501 semsg(_("E739: Cannot create directory: %s"), name);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007502 return FAIL;
7503 }
7504 return OK;
7505}
7506#endif
7507
Bram Moolenaar071d4272004-06-13 20:20:40 +00007508/*
7509 * Open a file for writing for an Ex command, with some checks.
7510 * Return file descriptor, or NULL on failure.
7511 */
7512 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007513open_exfile(
7514 char_u *fname,
7515 int forceit,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007516 char *mode) // "w" for create new file or "a" for append
Bram Moolenaar071d4272004-06-13 20:20:40 +00007517{
7518 FILE *fd;
7519
7520#ifdef UNIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007521 // with Unix it is possible to open a directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007522 if (mch_isdir(fname))
7523 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007524 semsg(_(e_isadir2), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007525 return NULL;
7526 }
7527#endif
7528 if (!forceit && *mode != 'a' && vim_fexists(fname))
7529 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007530 semsg(_("E189: \"%s\" exists (add ! to override)"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007531 return NULL;
7532 }
7533
7534 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007535 semsg(_("E190: Cannot open \"%s\" for writing"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007536
7537 return fd;
7538}
7539
7540/*
7541 * ":mark" and ":k".
7542 */
7543 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007544ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007545{
7546 pos_T pos;
7547
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007548 if (*eap->arg == NUL) // No argument?
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007549 emsg(_(e_argreq));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007550 else if (eap->arg[1] != NUL) // more than one character?
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007551 emsg(_(e_trailing));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007552 else
7553 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007554 pos = curwin->w_cursor; // save curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00007555 curwin->w_cursor.lnum = eap->line2;
7556 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007557 if (setmark(*eap->arg) == FAIL) // set mark
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007558 emsg(_("E191: Argument must be a letter or forward/backward quote"));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007559 curwin->w_cursor = pos; // restore curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00007560 }
7561}
7562
7563/*
7564 * Update w_topline, w_leftcol and the cursor position.
7565 */
7566 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007567update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007568{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007569 check_cursor(); // put cursor on valid line
Bram Moolenaar071d4272004-06-13 20:20:40 +00007570 update_topline();
7571 if (!curwin->w_p_wrap)
7572 validate_cursor();
7573 update_curswant();
7574}
7575
Bram Moolenaar071d4272004-06-13 20:20:40 +00007576/*
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007577 * Save the current State and go to Normal mode.
7578 * Return TRUE if the typeahead could be saved.
7579 */
7580 int
7581save_current_state(save_state_T *sst)
7582{
7583 sst->save_msg_scroll = msg_scroll;
7584 sst->save_restart_edit = restart_edit;
7585 sst->save_msg_didout = msg_didout;
7586 sst->save_State = State;
7587 sst->save_insertmode = p_im;
7588 sst->save_finish_op = finish_op;
7589 sst->save_opcount = opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01007590 sst->save_reg_executing = reg_executing;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007591
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007592 msg_scroll = FALSE; // no msg scrolling in Normal mode
7593 restart_edit = 0; // don't go to Insert mode
7594 p_im = FALSE; // don't use 'insertmode'
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007595
7596 /*
7597 * Save the current typeahead. This is required to allow using ":normal"
7598 * from an event handler and makes sure we don't hang when the argument
7599 * ends with half a command.
7600 */
7601 save_typeahead(&sst->tabuf);
7602 return sst->tabuf.typebuf_valid;
7603}
7604
7605 void
7606restore_current_state(save_state_T *sst)
7607{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007608 // Restore the previous typeahead.
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007609 restore_typeahead(&sst->tabuf);
7610
7611 msg_scroll = sst->save_msg_scroll;
7612 restart_edit = sst->save_restart_edit;
7613 p_im = sst->save_insertmode;
7614 finish_op = sst->save_finish_op;
7615 opcount = sst->save_opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01007616 reg_executing = sst->save_reg_executing;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007617 msg_didout |= sst->save_msg_didout; // don't reset msg_didout now
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007618
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007619 // Restore the state (needed when called from a function executed for
7620 // 'indentexpr'). Update the mouse and cursor, they may have changed.
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007621 State = sst->save_State;
7622#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007623 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007624#endif
7625}
7626
7627/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007628 * ":normal[!] {commands}": Execute normal mode commands.
7629 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01007630 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007631ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007632{
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007633 save_state_T save_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007634 char_u *arg = NULL;
7635 int l;
7636 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007637
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00007638 if (ex_normal_lock > 0)
7639 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007640 emsg(_(e_secure));
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00007641 return;
7642 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007643 if (ex_normal_busy >= p_mmd)
7644 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007645 emsg(_("E192: Recursive use of :normal too deep"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007646 return;
7647 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007648
Bram Moolenaar071d4272004-06-13 20:20:40 +00007649 /*
7650 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
7651 * this for the K_SPECIAL leading byte, otherwise special keys will not
7652 * work.
7653 */
7654 if (has_mbyte)
7655 {
7656 int len = 0;
7657
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007658 // Count the number of characters to be escaped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007659 for (p = eap->arg; *p != NUL; ++p)
7660 {
Bram Moolenaar13505972019-01-24 15:04:48 +01007661#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007662 if (*p == CSI) // leadbyte CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007663 len += 2;
Bram Moolenaar13505972019-01-24 15:04:48 +01007664#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007665 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007666 if (*++p == K_SPECIAL // trailbyte K_SPECIAL or CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01007667#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007668 || *p == CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01007669#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007670 )
7671 len += 2;
7672 }
7673 if (len > 0)
7674 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02007675 arg = alloc(STRLEN(eap->arg) + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007676 if (arg != NULL)
7677 {
7678 len = 0;
7679 for (p = eap->arg; *p != NUL; ++p)
7680 {
7681 arg[len++] = *p;
Bram Moolenaar13505972019-01-24 15:04:48 +01007682#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007683 if (*p == CSI)
7684 {
7685 arg[len++] = KS_EXTRA;
7686 arg[len++] = (int)KE_CSI;
7687 }
Bram Moolenaar13505972019-01-24 15:04:48 +01007688#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007689 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007690 {
7691 arg[len++] = *++p;
7692 if (*p == K_SPECIAL)
7693 {
7694 arg[len++] = KS_SPECIAL;
7695 arg[len++] = KE_FILLER;
7696 }
Bram Moolenaar13505972019-01-24 15:04:48 +01007697#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007698 else if (*p == CSI)
7699 {
7700 arg[len++] = KS_EXTRA;
7701 arg[len++] = (int)KE_CSI;
7702 }
Bram Moolenaar13505972019-01-24 15:04:48 +01007703#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007704 }
7705 arg[len] = NUL;
7706 }
7707 }
7708 }
7709 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007710
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007711 ++ex_normal_busy;
7712 if (save_current_state(&save_state))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007713 {
7714 /*
7715 * Repeat the :normal command for each line in the range. When no
7716 * range given, execute it just once, without positioning the cursor
7717 * first.
7718 */
7719 do
7720 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007721 if (eap->addr_count != 0)
7722 {
7723 curwin->w_cursor.lnum = eap->line1++;
7724 curwin->w_cursor.col = 0;
Bram Moolenaard5d37532017-03-27 23:02:07 +02007725 check_cursor_moved(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007726 }
7727
Bram Moolenaar13505972019-01-24 15:04:48 +01007728 exec_normal_cmd(arg != NULL
7729 ? arg
7730 : eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007731 }
7732 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
7733 }
7734
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007735 // Might not return to the main loop when in an event handler.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007736 update_topline_cursor();
7737
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007738 restore_current_state(&save_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007739 --ex_normal_busy;
Bram Moolenaareda73602014-11-05 09:53:23 +01007740 setmouse();
Bram Moolenaareda73602014-11-05 09:53:23 +01007741#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007742 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaareda73602014-11-05 09:53:23 +01007743#endif
7744
Bram Moolenaar071d4272004-06-13 20:20:40 +00007745 vim_free(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007746}
7747
7748/*
Bram Moolenaar64969662005-12-14 21:59:55 +00007749 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007750 */
7751 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007752ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007753{
Bram Moolenaarfd371682005-01-14 21:42:54 +00007754 if (eap->forceit)
7755 {
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007756 // cursor line can be zero on startup
Bram Moolenaar09ca9322017-09-26 17:40:45 +02007757 if (!curwin->w_cursor.lnum)
7758 curwin->w_cursor.lnum = 1;
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007759 set_cursor_for_append_to_line();
Bram Moolenaarfd371682005-01-14 21:42:54 +00007760 }
Bram Moolenaareb58a242020-04-19 18:13:19 +02007761#ifdef FEAT_TERMINAL
7762 // Ignore this when running in an active terminal.
7763 if (term_job_running(curbuf->b_term))
7764 return;
7765#endif
Bram Moolenaarfd371682005-01-14 21:42:54 +00007766
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007767 // Ignore the command when already in Insert mode. Inserting an
7768 // expression register that invokes a function can do this.
Bram Moolenaara40c5002005-01-09 21:16:21 +00007769 if (State & INSERT)
7770 return;
7771
Bram Moolenaar64969662005-12-14 21:59:55 +00007772 if (eap->cmdidx == CMD_startinsert)
7773 restart_edit = 'a';
7774 else if (eap->cmdidx == CMD_startreplace)
7775 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00007776 else
Bram Moolenaar64969662005-12-14 21:59:55 +00007777 restart_edit = 'V';
7778
7779 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007780 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00007781 if (eap->cmdidx == CMD_startinsert)
7782 restart_edit = 'i';
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007783 curwin->w_curswant = 0; // avoid MAXCOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007784 }
7785}
7786
7787/*
7788 * ":stopinsert"
7789 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007790 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007791ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007792{
7793 restart_edit = 0;
7794 stop_insert_mode = TRUE;
Bram Moolenaarfd773e92016-04-02 19:39:16 +02007795 clearmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007796}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007797
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007798/*
7799 * Execute normal mode command "cmd".
7800 * "remap" can be REMAP_NONE or REMAP_YES.
7801 */
7802 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007803exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007804{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007805 // Stuff the argument into the typeahead buffer.
Bram Moolenaar25281632016-01-21 23:32:32 +01007806 ins_typebuf(cmd, remap, 0, TRUE, silent);
Bram Moolenaar586c70c2018-10-02 16:23:58 +02007807 exec_normal(FALSE, FALSE, FALSE);
Bram Moolenaar25281632016-01-21 23:32:32 +01007808}
Bram Moolenaar25281632016-01-21 23:32:32 +01007809
Bram Moolenaar25281632016-01-21 23:32:32 +01007810/*
7811 * Execute normal_cmd() until there is no typeahead left.
Bram Moolenaar586c70c2018-10-02 16:23:58 +02007812 * When "use_vpeekc" is TRUE use vpeekc() to check for available chars.
Bram Moolenaar25281632016-01-21 23:32:32 +01007813 */
7814 void
Bram Moolenaar586c70c2018-10-02 16:23:58 +02007815exec_normal(int was_typed, int use_vpeekc, int may_use_terminal_loop UNUSED)
Bram Moolenaar25281632016-01-21 23:32:32 +01007816{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007817 oparg_T oa;
Bram Moolenaar905dd902019-04-07 14:21:47 +02007818 int c;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007819
Bram Moolenaar905dd902019-04-07 14:21:47 +02007820 // When calling vpeekc() from feedkeys() it will return Ctrl_C when there
7821 // is nothing to get, so also check for Ctrl_C.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007822 clear_oparg(&oa);
7823 finish_op = FALSE;
Bram Moolenaar586c70c2018-10-02 16:23:58 +02007824 while ((!stuff_empty()
7825 || ((was_typed || !typebuf_typed()) && typebuf.tb_len > 0)
Bram Moolenaar905dd902019-04-07 14:21:47 +02007826 || (use_vpeekc && (c = vpeekc()) != NUL && c != Ctrl_C))
Bram Moolenaar586c70c2018-10-02 16:23:58 +02007827 && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007828 {
7829 update_topline_cursor();
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02007830#ifdef FEAT_TERMINAL
Bram Moolenaar655a82a2018-05-08 22:01:07 +02007831 if (may_use_terminal_loop && term_use_loop()
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02007832 && oa.op_type == OP_NOP && oa.regname == NUL
7833 && !VIsual_active)
7834 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007835 // If terminal_loop() returns OK we got a key that is handled
7836 // in Normal model. With FAIL we first need to position the
7837 // cursor and the screen needs to be redrawn.
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02007838 if (terminal_loop(TRUE) == OK)
7839 normal_cmd(&oa, TRUE);
7840 }
7841 else
7842#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007843 // execute a Normal mode cmd
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02007844 normal_cmd(&oa, TRUE);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007845 }
7846}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007847
Bram Moolenaar071d4272004-06-13 20:20:40 +00007848#ifdef FEAT_FIND_ID
7849 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007850ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007851{
7852 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
7853 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
7854 (linenr_T)1, (linenr_T)MAXLNUM);
7855}
7856
Bram Moolenaar4033c552017-09-16 20:54:51 +02007857#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007858/*
7859 * ":psearch"
7860 */
7861 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007862ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007863{
7864 g_do_tagpreview = p_pvh;
7865 ex_findpat(eap);
7866 g_do_tagpreview = 0;
7867}
7868#endif
7869
7870 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007871ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007872{
7873 int whole = TRUE;
7874 long n;
7875 char_u *p;
7876 int action;
7877
7878 switch (cmdnames[eap->cmdidx].cmd_name[2])
7879 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007880 case 'e': // ":psearch", ":isearch" and ":dsearch"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007881 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
7882 action = ACTION_GOTO;
7883 else
7884 action = ACTION_SHOW;
7885 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007886 case 'i': // ":ilist" and ":dlist"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007887 action = ACTION_SHOW_ALL;
7888 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007889 case 'u': // ":ijump" and ":djump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007890 action = ACTION_GOTO;
7891 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007892 default: // ":isplit" and ":dsplit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007893 action = ACTION_SPLIT;
7894 break;
7895 }
7896
7897 n = 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007898 if (vim_isdigit(*eap->arg)) // get count
Bram Moolenaar071d4272004-06-13 20:20:40 +00007899 {
7900 n = getdigits(&eap->arg);
7901 eap->arg = skipwhite(eap->arg);
7902 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007903 if (*eap->arg == '/') // Match regexp, not just whole words
Bram Moolenaar071d4272004-06-13 20:20:40 +00007904 {
7905 whole = FALSE;
7906 ++eap->arg;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02007907 p = skip_regexp(eap->arg, '/', p_magic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007908 if (*p)
7909 {
7910 *p++ = NUL;
7911 p = skipwhite(p);
7912
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007913 // Check for trailing illegal characters
Bram Moolenaarfaac4102020-04-20 17:46:14 +02007914 if (!ends_excmd2(eap->arg, p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007915 eap->errmsg = e_trailing;
7916 else
7917 eap->nextcmd = check_nextcmd(p);
7918 }
7919 }
7920 if (!eap->skip)
7921 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
7922 whole, !eap->forceit,
7923 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
7924 n, action, eap->line1, eap->line2);
7925}
7926#endif
7927
Bram Moolenaar071d4272004-06-13 20:20:40 +00007928
Bram Moolenaar4033c552017-09-16 20:54:51 +02007929#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00007930/*
7931 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
7932 */
7933 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007934ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007935{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007936 g_do_tagpreview = p_pvh; // will be reset to 0 in ex_tag_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00007937 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
7938}
7939
7940/*
7941 * ":pedit"
7942 */
7943 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007944ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007945{
7946 win_T *curwin_save = curwin;
7947
Bram Moolenaar3a2505c2020-03-09 16:40:41 +01007948 if (ERROR_IF_ANY_POPUP_WINDOW)
7949 return;
7950
Bram Moolenaar026587b2019-08-17 15:08:00 +02007951 // Open the preview window or popup and make it the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007952 g_do_tagpreview = p_pvh;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02007953 prepare_tagpreview(TRUE, TRUE, FALSE);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02007954
Bram Moolenaar026587b2019-08-17 15:08:00 +02007955 // Edit the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007956 do_exedit(eap, NULL);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02007957
Bram Moolenaar071d4272004-06-13 20:20:40 +00007958 if (curwin != curwin_save && win_valid(curwin_save))
7959 {
Bram Moolenaar026587b2019-08-17 15:08:00 +02007960 // Return cursor to where we were
Bram Moolenaar071d4272004-06-13 20:20:40 +00007961 validate_cursor();
7962 redraw_later(VALID);
7963 win_enter(curwin_save, TRUE);
7964 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01007965# ifdef FEAT_PROP_POPUP
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02007966 else if (WIN_IS_POPUP(curwin))
7967 {
7968 // can't keep focus in popup window
7969 win_enter(firstwin, TRUE);
7970 }
7971# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007972 g_do_tagpreview = 0;
7973}
Bram Moolenaar4033c552017-09-16 20:54:51 +02007974#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007975
7976/*
7977 * ":stag", ":stselect" and ":stjump".
7978 */
7979 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007980ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007981{
7982 postponed_split = -1;
7983 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00007984 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007985 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
7986 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00007987 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007988}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007989
7990/*
7991 * ":tag", ":tselect", ":tjump", ":tnext", etc.
7992 */
7993 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007994ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007995{
7996 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
7997}
7998
7999 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008000ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008001{
8002 int cmd;
8003
8004 switch (name[1])
8005 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008006 case 'j': cmd = DT_JUMP; // ":tjump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008007 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008008 case 's': cmd = DT_SELECT; // ":tselect"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008009 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008010 case 'p': cmd = DT_PREV; // ":tprevious"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008011 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008012 case 'N': cmd = DT_PREV; // ":tNext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008013 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008014 case 'n': cmd = DT_NEXT; // ":tnext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008015 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008016 case 'o': cmd = DT_POP; // ":pop"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008017 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008018 case 'f': // ":tfirst"
8019 case 'r': cmd = DT_FIRST; // ":trewind"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008020 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008021 case 'l': cmd = DT_LAST; // ":tlast"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008022 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008023 default: // ":tag"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008024#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +00008025 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008026 {
Bram Moolenaard4db7712016-11-12 19:16:46 +01008027 ex_cstag(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008028 return;
8029 }
8030#endif
8031 cmd = DT_TAG;
8032 break;
8033 }
8034
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00008035 if (name[0] == 'l')
8036 {
8037#ifndef FEAT_QUICKFIX
8038 ex_ni(eap);
8039 return;
8040#else
8041 cmd = DT_LTAG;
8042#endif
8043 }
8044
Bram Moolenaar071d4272004-06-13 20:20:40 +00008045 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
8046 eap->forceit, TRUE);
8047}
8048
8049/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008050 * Check "str" for starting with a special cmdline variable.
8051 * If found return one of the SPEC_ values and set "*usedlen" to the length of
8052 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
8053 */
8054 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008055find_cmdline_var(char_u *src, int *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008056{
8057 int len;
8058 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +00008059 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008060 "%",
8061#define SPEC_PERC 0
8062 "#",
Bram Moolenaar65f08472017-09-10 18:16:20 +02008063#define SPEC_HASH (SPEC_PERC + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008064 "<cword>", // cursor word
Bram Moolenaar65f08472017-09-10 18:16:20 +02008065#define SPEC_CWORD (SPEC_HASH + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008066 "<cWORD>", // cursor WORD
Bram Moolenaar65f08472017-09-10 18:16:20 +02008067#define SPEC_CCWORD (SPEC_CWORD + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008068 "<cexpr>", // expr under cursor
Bram Moolenaar65f08472017-09-10 18:16:20 +02008069#define SPEC_CEXPR (SPEC_CCWORD + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008070 "<cfile>", // cursor path name
Bram Moolenaar65f08472017-09-10 18:16:20 +02008071#define SPEC_CFILE (SPEC_CEXPR + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008072 "<sfile>", // ":so" file name
Bram Moolenaar65f08472017-09-10 18:16:20 +02008073#define SPEC_SFILE (SPEC_CFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008074 "<slnum>", // ":so" file line number
Bram Moolenaar65f08472017-09-10 18:16:20 +02008075#define SPEC_SLNUM (SPEC_SFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008076 "<afile>", // autocommand file name
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008077#define SPEC_AFILE (SPEC_SLNUM + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008078 "<abuf>", // autocommand buffer number
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008079#define SPEC_ABUF (SPEC_AFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008080 "<amatch>", // autocommand match name
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008081#define SPEC_AMATCH (SPEC_ABUF + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008082 "<sflnum>", // script file line number
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008083#define SPEC_SFLNUM (SPEC_AMATCH + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008084#ifdef FEAT_CLIENTSERVER
8085 "<client>"
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008086# define SPEC_CLIENT (SPEC_SFLNUM + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008087#endif
8088 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008089
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008090 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008091 {
8092 len = (int)STRLEN(spec_str[i]);
8093 if (STRNCMP(src, spec_str[i], len) == 0)
8094 {
8095 *usedlen = len;
8096 return i;
8097 }
8098 }
8099 return -1;
8100}
8101
8102/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008103 * Evaluate cmdline variables.
8104 *
8105 * change '%' to curbuf->b_ffname
8106 * '#' to curwin->w_altfile
8107 * '<cword>' to word under the cursor
8108 * '<cWORD>' to WORD under the cursor
Bram Moolenaar8071cb22019-07-12 17:58:01 +02008109 * '<cexpr>' to C-expression under the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00008110 * '<cfile>' to path name under the cursor
8111 * '<sfile>' to sourced file name
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008112 * '<slnum>' to sourced file line number
Bram Moolenaar071d4272004-06-13 20:20:40 +00008113 * '<afile>' to file name for autocommand
8114 * '<abuf>' to buffer number for autocommand
8115 * '<amatch>' to matching name for autocommand
8116 *
8117 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
8118 * "" for error without a message) and NULL is returned.
8119 * Returns an allocated string if a valid match was found.
8120 * Returns NULL if no match was found. "usedlen" then still contains the
8121 * number of characters to skip.
8122 */
8123 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008124eval_vars(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008125 char_u *src, // pointer into commandline
8126 char_u *srcstart, // beginning of valid memory for src
8127 int *usedlen, // characters after src that are used
8128 linenr_T *lnump, // line number for :e command, or NULL
8129 char **errormsg, // pointer to error message
8130 int *escaped) // return value has escaped white space (can
8131 // be NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008132{
8133 int i;
8134 char_u *s;
8135 char_u *result;
8136 char_u *resultbuf = NULL;
8137 int resultlen;
8138 buf_T *buf;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008139 int valid = VALID_HEAD + VALID_PATH; // assume valid result
Bram Moolenaar071d4272004-06-13 20:20:40 +00008140 int spec_idx;
Bram Moolenaarfd249462018-07-28 16:14:30 +02008141 int tilde_file = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008142 int skip_mod = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008143 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008144
8145 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +00008146 if (escaped != NULL)
8147 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008148
8149 /*
8150 * Check if there is something to do.
8151 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008152 spec_idx = find_cmdline_var(src, usedlen);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008153 if (spec_idx < 0) // no match
Bram Moolenaar071d4272004-06-13 20:20:40 +00008154 {
8155 *usedlen = 1;
8156 return NULL;
8157 }
8158
8159 /*
8160 * Skip when preceded with a backslash "\%" and "\#".
8161 * Note: In "\\%" the % is also not recognized!
8162 */
8163 if (src > srcstart && src[-1] == '\\')
8164 {
8165 *usedlen = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008166 STRMOVE(src - 1, src); // remove backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00008167 return NULL;
8168 }
8169
8170 /*
8171 * word or WORD under cursor
8172 */
Bram Moolenaar65f08472017-09-10 18:16:20 +02008173 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD
8174 || spec_idx == SPEC_CEXPR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008175 {
Bram Moolenaar65f08472017-09-10 18:16:20 +02008176 resultlen = find_ident_under_cursor(&result,
8177 spec_idx == SPEC_CWORD ? (FIND_IDENT | FIND_STRING)
8178 : spec_idx == SPEC_CEXPR ? (FIND_IDENT | FIND_STRING | FIND_EVAL)
8179 : FIND_STRING);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008180 if (resultlen == 0)
8181 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008182 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008183 return NULL;
8184 }
8185 }
8186
8187 /*
8188 * '#': Alternate file name
8189 * '%': Current file name
8190 * File name under the cursor
8191 * File name for autocommand
8192 * and following modifiers
8193 */
8194 else
8195 {
8196 switch (spec_idx)
8197 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008198 case SPEC_PERC: // '%': current file
Bram Moolenaar071d4272004-06-13 20:20:40 +00008199 if (curbuf->b_fname == NULL)
8200 {
8201 result = (char_u *)"";
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008202 valid = 0; // Must have ":p:h" to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00008203 }
8204 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008205 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008206 result = curbuf->b_fname;
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008207 tilde_file = STRCMP(result, "~") == 0;
8208 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008209 break;
8210
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008211 case SPEC_HASH: // '#' or "#99": alternate file
8212 if (src[1] == '#') // "##": the argument list
Bram Moolenaar071d4272004-06-13 20:20:40 +00008213 {
8214 result = arg_all();
8215 resultbuf = result;
8216 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +00008217 if (escaped != NULL)
8218 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008219 skip_mod = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008220 break;
8221 }
8222 s = src + 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008223 if (*s == '<') // "#<99" uses v:oldfiles
Bram Moolenaard812df62008-11-09 12:46:09 +00008224 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008225 i = (int)getdigits(&s);
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008226 if (s == src + 2 && src[1] == '-')
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008227 // just a minus sign, don't skip over it
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008228 s--;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008229 *usedlen = (int)(s - src); // length of what we expand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008230
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008231 if (src[1] == '<' && i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008232 {
Bram Moolenaard812df62008-11-09 12:46:09 +00008233 if (*usedlen < 2)
8234 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008235 // Should we give an error message for #<text?
Bram Moolenaard812df62008-11-09 12:46:09 +00008236 *usedlen = 1;
8237 return NULL;
8238 }
8239#ifdef FEAT_EVAL
8240 result = list_find_str(get_vim_var_list(VV_OLDFILES),
8241 (long)i);
8242 if (result == NULL)
8243 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008244 *errormsg = "";
Bram Moolenaard812df62008-11-09 12:46:09 +00008245 return NULL;
8246 }
8247#else
Bram Moolenaar8e481e82019-01-15 20:07:48 +01008248 *errormsg = _("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008249 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00008250#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008251 }
8252 else
Bram Moolenaard812df62008-11-09 12:46:09 +00008253 {
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008254 if (i == 0 && src[1] == '<' && *usedlen > 1)
8255 *usedlen = 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00008256 buf = buflist_findnr(i);
8257 if (buf == NULL)
8258 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008259 *errormsg = _("E194: No alternate file name to substitute for '#'");
Bram Moolenaard812df62008-11-09 12:46:09 +00008260 return NULL;
8261 }
8262 if (lnump != NULL)
8263 *lnump = ECMD_LAST;
8264 if (buf->b_fname == NULL)
8265 {
8266 result = (char_u *)"";
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008267 valid = 0; // Must have ":p:h" to be valid
Bram Moolenaard812df62008-11-09 12:46:09 +00008268 }
8269 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008270 {
Bram Moolenaard812df62008-11-09 12:46:09 +00008271 result = buf->b_fname;
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008272 tilde_file = STRCMP(result, "~") == 0;
8273 }
Bram Moolenaard812df62008-11-09 12:46:09 +00008274 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008275 break;
8276
8277#ifdef FEAT_SEARCHPATH
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008278 case SPEC_CFILE: // file name under cursor
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008279 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008280 if (result == NULL)
8281 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008282 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008283 return NULL;
8284 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008285 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00008286 break;
8287#endif
8288
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008289 case SPEC_AFILE: // file name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008290 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +00008291 if (result != NULL && !autocmd_fname_full)
8292 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008293 // Still need to turn the fname into a full path. It is
8294 // postponed to avoid a delay when <afile> is not used.
Bram Moolenaarf6dad432008-09-18 19:29:58 +00008295 autocmd_fname_full = TRUE;
8296 result = FullName_save(autocmd_fname, FALSE);
8297 vim_free(autocmd_fname);
8298 autocmd_fname = result;
8299 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008300 if (result == NULL)
8301 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008302 *errormsg = _("E495: no autocommand file name to substitute for \"<afile>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008303 return NULL;
8304 }
Bram Moolenaara0174af2008-01-02 20:08:25 +00008305 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008306 break;
8307
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008308 case SPEC_ABUF: // buffer number for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008309 if (autocmd_bufnr <= 0)
8310 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008311 *errormsg = _("E496: no autocommand buffer number to substitute for \"<abuf>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008312 return NULL;
8313 }
8314 sprintf((char *)strbuf, "%d", autocmd_bufnr);
8315 result = strbuf;
8316 break;
8317
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008318 case SPEC_AMATCH: // match name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008319 result = autocmd_match;
8320 if (result == NULL)
8321 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008322 *errormsg = _("E497: no autocommand match name to substitute for \"<amatch>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008323 return NULL;
8324 }
8325 break;
8326
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008327 case SPEC_SFILE: // file name for ":so" command
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008328 result = estack_sfile();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008329 if (result == NULL)
8330 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008331 *errormsg = _("E498: no :source file name to substitute for \"<sfile>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008332 return NULL;
8333 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008334 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00008335 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008336
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008337 case SPEC_SLNUM: // line in file for ":so" command
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008338 if (SOURCING_NAME == NULL || SOURCING_LNUM == 0)
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008339 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008340 *errormsg = _("E842: no line number to use for \"<slnum>\"");
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008341 return NULL;
8342 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008343 sprintf((char *)strbuf, "%ld", SOURCING_LNUM);
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008344 result = strbuf;
8345 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008346
8347#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008348 case SPEC_SFLNUM: // line in script file
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008349 if (current_sctx.sc_lnum + SOURCING_LNUM == 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008350 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008351 *errormsg = _("E961: no line number to use for \"<sflnum>\"");
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008352 return NULL;
8353 }
8354 sprintf((char *)strbuf, "%ld",
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008355 (long)(current_sctx.sc_lnum + SOURCING_LNUM));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008356 result = strbuf;
8357 break;
8358#endif
8359
8360#ifdef FEAT_CLIENTSERVER
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008361 case SPEC_CLIENT: // Source of last submitted input
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008362 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
8363 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008364 result = strbuf;
8365 break;
8366#endif
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008367
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02008368 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008369 result = (char_u *)""; // avoid gcc warning
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02008370 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008371 }
8372
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008373 resultlen = (int)STRLEN(result); // length of new string
8374 if (src[*usedlen] == '<') // remove the file name extension
Bram Moolenaar071d4272004-06-13 20:20:40 +00008375 {
8376 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008377 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008378 resultlen = (int)(s - result);
8379 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008380 else if (!skip_mod)
8381 {
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008382 valid |= modify_fname(src, tilde_file, usedlen, &result, &resultbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008383 &resultlen);
8384 if (result == NULL)
8385 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008386 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008387 return NULL;
8388 }
8389 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008390 }
8391
8392 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
8393 {
8394 if (valid != VALID_HEAD + VALID_PATH)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008395 // xgettext:no-c-format
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008396 *errormsg = _("E499: Empty file name for '%' or '#', only works with \":p:h\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008397 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008398 *errormsg = _("E500: Evaluates to an empty string");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008399 result = NULL;
8400 }
8401 else
8402 result = vim_strnsave(result, resultlen);
8403 vim_free(resultbuf);
8404 return result;
8405}
8406
8407/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008408 * Expand the <sfile> string in "arg".
8409 *
8410 * Returns an allocated string, or NULL for any error.
8411 */
8412 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008413expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008414{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008415 char *errormsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008416 int len;
8417 char_u *result;
8418 char_u *newres;
8419 char_u *repl;
8420 int srclen;
8421 char_u *p;
8422
8423 result = vim_strsave(arg);
8424 if (result == NULL)
8425 return NULL;
8426
8427 for (p = result; *p; )
8428 {
8429 if (STRNCMP(p, "<sfile>", 7) != 0)
8430 ++p;
8431 else
8432 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008433 // replace "<sfile>" with the sourced file name, and do ":" stuff
Bram Moolenaar63b92542007-03-27 14:57:09 +00008434 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008435 if (errormsg != NULL)
8436 {
8437 if (*errormsg)
8438 emsg(errormsg);
8439 vim_free(result);
8440 return NULL;
8441 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008442 if (repl == NULL) // no match (cannot happen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008443 {
8444 p += srclen;
8445 continue;
8446 }
8447 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
8448 newres = alloc(len);
8449 if (newres == NULL)
8450 {
8451 vim_free(repl);
8452 vim_free(result);
8453 return NULL;
8454 }
8455 mch_memmove(newres, result, (size_t)(p - result));
8456 STRCPY(newres + (p - result), repl);
8457 len = (int)STRLEN(newres);
8458 STRCAT(newres, p + srclen);
8459 vim_free(repl);
8460 vim_free(result);
8461 result = newres;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008462 p = newres + len; // continue after the match
Bram Moolenaar071d4272004-06-13 20:20:40 +00008463 }
8464 }
8465
8466 return result;
8467}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008468
Bram Moolenaar071d4272004-06-13 20:20:40 +00008469#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00008470/*
Bram Moolenaard9462e32011-04-11 21:35:11 +02008471 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +00008472 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +00008473 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008474 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008475dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008476{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008477 if (fname == NULL)
8478 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +02008479 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008480}
8481#endif
8482
8483/*
8484 * ":behave {mswin,xterm}"
8485 */
8486 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008487ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008488{
8489 if (STRCMP(eap->arg, "mswin") == 0)
8490 {
8491 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
8492 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
8493 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
8494 set_option_value((char_u *)"keymodel", 0L,
8495 (char_u *)"startsel,stopsel", 0);
8496 }
8497 else if (STRCMP(eap->arg, "xterm") == 0)
8498 {
8499 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
8500 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
8501 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
8502 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
8503 }
8504 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008505 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008506}
8507
Bram Moolenaar071d4272004-06-13 20:20:40 +00008508static int filetype_detect = FALSE;
8509static int filetype_plugin = FALSE;
8510static int filetype_indent = FALSE;
8511
8512/*
8513 * ":filetype [plugin] [indent] {on,off,detect}"
8514 * on: Load the filetype.vim file to install autocommands for file types.
8515 * off: Load the ftoff.vim file to remove all autocommands for file types.
8516 * plugin on: load filetype.vim and ftplugin.vim
8517 * plugin off: load ftplugof.vim
8518 * indent on: load filetype.vim and indent.vim
8519 * indent off: load indoff.vim
8520 */
8521 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008522ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008523{
8524 char_u *arg = eap->arg;
8525 int plugin = FALSE;
8526 int indent = FALSE;
8527
8528 if (*eap->arg == NUL)
8529 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008530 // Print current status.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008531 smsg("filetype detection:%s plugin:%s indent:%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00008532 filetype_detect ? "ON" : "OFF",
8533 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
8534 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
8535 return;
8536 }
8537
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008538 // Accept "plugin" and "indent" in any order.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008539 for (;;)
8540 {
8541 if (STRNCMP(arg, "plugin", 6) == 0)
8542 {
8543 plugin = TRUE;
8544 arg = skipwhite(arg + 6);
8545 continue;
8546 }
8547 if (STRNCMP(arg, "indent", 6) == 0)
8548 {
8549 indent = TRUE;
8550 arg = skipwhite(arg + 6);
8551 continue;
8552 }
8553 break;
8554 }
8555 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
8556 {
8557 if (*arg == 'o' || !filetype_detect)
8558 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008559 source_runtime((char_u *)FILETYPE_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008560 filetype_detect = TRUE;
8561 if (plugin)
8562 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008563 source_runtime((char_u *)FTPLUGIN_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008564 filetype_plugin = TRUE;
8565 }
8566 if (indent)
8567 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008568 source_runtime((char_u *)INDENT_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008569 filetype_indent = TRUE;
8570 }
8571 }
8572 if (*arg == 'd')
8573 {
Bram Moolenaar1610d052016-06-09 22:53:01 +02008574 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +00008575 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008576 }
8577 }
8578 else if (STRCMP(arg, "off") == 0)
8579 {
8580 if (plugin || indent)
8581 {
8582 if (plugin)
8583 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008584 source_runtime((char_u *)FTPLUGOF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008585 filetype_plugin = FALSE;
8586 }
8587 if (indent)
8588 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008589 source_runtime((char_u *)INDOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008590 filetype_indent = FALSE;
8591 }
8592 }
8593 else
8594 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008595 source_runtime((char_u *)FTOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008596 filetype_detect = FALSE;
8597 }
8598 }
8599 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008600 semsg(_(e_invarg2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008601}
8602
8603/*
Bram Moolenaar3e545692017-06-04 19:00:32 +02008604 * ":setfiletype [FALLBACK] {name}"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008605 */
8606 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008607ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008608{
8609 if (!did_filetype)
Bram Moolenaar3e545692017-06-04 19:00:32 +02008610 {
8611 char_u *arg = eap->arg;
8612
8613 if (STRNCMP(arg, "FALLBACK ", 9) == 0)
8614 arg += 9;
8615
8616 set_option_value((char_u *)"filetype", 0L, arg, OPT_LOCAL);
8617 if (arg != eap->arg)
8618 did_filetype = FALSE;
8619 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008620}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008621
Bram Moolenaar071d4272004-06-13 20:20:40 +00008622 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008623ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008624{
8625#ifdef FEAT_DIGRAPHS
8626 if (*eap->arg != NUL)
8627 putdigraph(eap->arg);
8628 else
Bram Moolenaareae8ae12018-12-14 18:53:02 +01008629 listdigraphs(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008630#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008631 emsg(_("E196: No digraphs in this version"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008632#endif
8633}
8634
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02008635#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
8636 void
8637set_no_hlsearch(int flag)
8638{
8639 no_hlsearch = flag;
8640# ifdef FEAT_EVAL
8641 set_vim_var_nr(VV_HLSEARCH, !no_hlsearch && p_hls);
8642# endif
8643}
8644
Bram Moolenaar071d4272004-06-13 20:20:40 +00008645/*
8646 * ":nohlsearch"
8647 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008648 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008649ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008650{
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02008651 set_no_hlsearch(TRUE);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00008652 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008653}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008654#endif
8655
8656#ifdef FEAT_CRYPT
8657/*
8658 * ":X": Get crypt key
8659 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008660 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008661ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008662{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +01008663 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02008664 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008665}
8666#endif
8667
8668#ifdef FEAT_FOLDING
8669 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008670ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008671{
8672 if (foldManualAllowed(TRUE))
8673 foldCreate(eap->line1, eap->line2);
8674}
8675
8676 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008677ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008678{
8679 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
8680 eap->forceit, FALSE);
8681}
8682
8683 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008684ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008685{
8686 linenr_T lnum;
8687
Bram Moolenaar4facea32019-10-12 20:17:40 +02008688# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02008689 start_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +02008690# endif
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02008691
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008692 // First set the marks for all lines closed/open.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008693 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
8694 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
8695 ml_setmarked(lnum);
8696
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008697 // Execute the command on the marked lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008698 global_exe(eap->arg);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008699 ml_clearmarked(); // clear rest of the marks
Bram Moolenaar4facea32019-10-12 20:17:40 +02008700# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02008701 end_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +02008702# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008703}
8704#endif
Bram Moolenaarf5291f32017-09-14 22:55:37 +02008705
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008706#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar39665952018-08-15 20:59:48 +02008707/*
8708 * Returns TRUE if the supplied Ex cmdidx is for a location list command
8709 * instead of a quickfix command.
8710 */
8711 int
8712is_loclist_cmd(int cmdidx)
8713{
Bram Moolenaar74c8be22018-08-23 22:51:40 +02008714 if (cmdidx < 0 || cmdidx >= CMD_SIZE)
Bram Moolenaar39665952018-08-15 20:59:48 +02008715 return FALSE;
8716 return cmdnames[cmdidx].cmd_name[0] == 'l';
8717}
8718#endif
8719
Bram Moolenaar4facea32019-10-12 20:17:40 +02008720#if defined(FEAT_TIMERS) || defined(PROTO)
Bram Moolenaarf5291f32017-09-14 22:55:37 +02008721 int
8722get_pressedreturn(void)
8723{
8724 return ex_pressedreturn;
8725}
8726
8727 void
8728set_pressedreturn(int val)
8729{
8730 ex_pressedreturn = val;
8731}
8732#endif