blob: 59778eb981ab92d9b199fcd74087b98af305a933 [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 Moolenaarf28dbce2016-01-29 22:03:47 +010028static void append_command(char_u *cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000029
Bram Moolenaar071d4272004-06-13 20:20:40 +000030#ifndef FEAT_MENU
31# define ex_emenu ex_ni
32# define ex_menu ex_ni
33# define ex_menutranslate ex_ni
34#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010035static void ex_autocmd(exarg_T *eap);
36static void ex_doautocmd(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010037static void ex_bunload(exarg_T *eap);
38static void ex_buffer(exarg_T *eap);
39static void ex_bmodified(exarg_T *eap);
40static void ex_bnext(exarg_T *eap);
41static void ex_bprevious(exarg_T *eap);
42static void ex_brewind(exarg_T *eap);
43static void ex_blast(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010044static char_u *getargcmd(char_u **);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010045static int getargopt(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000046#ifndef FEAT_QUICKFIX
47# define ex_make ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +000048# define ex_cbuffer ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000049# define ex_cc ex_ni
50# define ex_cnext ex_ni
Bram Moolenaar3ff33112019-05-03 21:56:35 +020051# define ex_cbelow ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000052# define ex_cfile ex_ni
53# define qf_list ex_ni
54# define qf_age ex_ni
Bram Moolenaarf6acffb2016-07-16 16:54:24 +020055# define qf_history ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000056# define ex_helpgrep ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +000057# define ex_vimgrep ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000058#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +020059#if !defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +000060# define ex_cclose ex_ni
61# define ex_copen ex_ni
62# define ex_cwindow ex_ni
Bram Moolenaardcb17002016-07-07 18:58:59 +020063# define ex_cbottom ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000064#endif
Bram Moolenaar1e015462005-09-25 22:16:38 +000065#if !defined(FEAT_QUICKFIX) || !defined(FEAT_EVAL)
66# define ex_cexpr ex_ni
67#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000068
Bram Moolenaarb7316892019-05-01 18:08:42 +020069static 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 +010070static void get_flags(exarg_T *eap);
Bram Moolenaar85363ab2010-07-18 13:58:26 +020071#if !defined(FEAT_PERL) \
72 || !defined(FEAT_PYTHON) || !defined(FEAT_PYTHON3) \
73 || !defined(FEAT_TCL) \
74 || !defined(FEAT_RUBY) \
75 || !defined(FEAT_LUA) \
76 || !defined(FEAT_MZSCHEME)
Bram Moolenaar7bb75552007-07-16 18:39:49 +000077# define HAVE_EX_SCRIPT_NI
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010078static void ex_script_ni(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000079#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010080static char *invalid_range(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010081static void correct_range(exarg_T *eap);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000082#ifdef FEAT_QUICKFIX
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010083static char_u *replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000084#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010085static char_u *repl_cmdline(exarg_T *eap, char_u *src, int srclen, char_u *repl, char_u **cmdlinep);
86static void ex_highlight(exarg_T *eap);
87static void ex_colorscheme(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010088static void ex_cquit(exarg_T *eap);
89static void ex_quit_all(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010090static void ex_close(exarg_T *eap);
91static void ex_win_close(int forceit, win_T *win, tabpage_T *tp);
92static void ex_only(exarg_T *eap);
93static void ex_resize(exarg_T *eap);
94static void ex_stag(exarg_T *eap);
95static void ex_tabclose(exarg_T *eap);
96static void ex_tabonly(exarg_T *eap);
97static void ex_tabnext(exarg_T *eap);
98static void ex_tabmove(exarg_T *eap);
99static void ex_tabs(exarg_T *eap);
Bram Moolenaar4033c552017-09-16 20:54:51 +0200100#if defined(FEAT_QUICKFIX)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100101static void ex_pclose(exarg_T *eap);
102static void ex_ptag(exarg_T *eap);
103static void ex_pedit(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000104#else
105# define ex_pclose ex_ni
106# define ex_ptag ex_ni
107# define ex_pedit ex_ni
108#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100109static void ex_hide(exarg_T *eap);
110static void ex_stop(exarg_T *eap);
111static void ex_exit(exarg_T *eap);
112static void ex_print(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000113#ifdef FEAT_BYTEOFF
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100114static void ex_goto(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000115#else
116# define ex_goto ex_ni
117#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100118static void ex_shell(exarg_T *eap);
119static void ex_preserve(exarg_T *eap);
120static void ex_recover(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100121static void ex_mode(exarg_T *eap);
122static void ex_wrongmodifier(exarg_T *eap);
123static void ex_find(exarg_T *eap);
124static void ex_open(exarg_T *eap);
125static void ex_edit(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000126#ifndef FEAT_GUI
127# define ex_gui ex_nogui
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100128static void ex_nogui(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000129#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +0100130#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100131static void ex_tearoff(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000132#else
133# define ex_tearoff ex_ni
134#endif
Bram Moolenaar29a2c082018-03-05 21:06:23 +0100135#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
136 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100137static void ex_popup(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000138#else
139# define ex_popup ex_ni
140#endif
141#ifndef FEAT_GUI_MSWIN
142# define ex_simalt ex_ni
143#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000144#if !defined(FEAT_GUI_MSWIN) && !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000145# define gui_mch_find_dialog ex_ni
146# define gui_mch_replace_dialog ex_ni
147#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000148#if !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000149# define ex_helpfind ex_ni
150#endif
151#ifndef FEAT_CSCOPE
Bram Moolenaard4db7712016-11-12 19:16:46 +0100152# define ex_cscope ex_ni
153# define ex_scscope ex_ni
154# define ex_cstag ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000155#endif
156#ifndef FEAT_SYN_HL
157# define ex_syntax ex_ni
Bram Moolenaar860cae12010-06-05 23:22:07 +0200158# define ex_ownsyntax ex_ni
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000159#endif
Bram Moolenaarf7512552013-06-06 14:55:19 +0200160#if !defined(FEAT_SYN_HL) || !defined(FEAT_PROFILE)
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +0200161# define ex_syntime ex_ni
162#endif
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000163#ifndef FEAT_SPELL
Bram Moolenaarb765d632005-06-07 21:00:02 +0000164# define ex_spell ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000165# define ex_mkspell ex_ni
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000166# define ex_spelldump ex_ni
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000167# define ex_spellinfo ex_ni
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000168# define ex_spellrepall ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000169#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200170#ifndef FEAT_PERSISTENT_UNDO
171# define ex_rundo ex_ni
172# define ex_wundo ex_ni
173#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200174#ifndef FEAT_LUA
175# define ex_lua ex_script_ni
176# define ex_luado ex_ni
177# define ex_luafile ex_ni
178#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000179#ifndef FEAT_MZSCHEME
180# define ex_mzscheme ex_script_ni
181# define ex_mzfile ex_ni
182#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000183#ifndef FEAT_PERL
184# define ex_perl ex_script_ni
185# define ex_perldo ex_ni
186#endif
187#ifndef FEAT_PYTHON
188# define ex_python ex_script_ni
Bram Moolenaard620aa92013-05-17 16:40:06 +0200189# define ex_pydo ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000190# define ex_pyfile ex_ni
191#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200192#ifndef FEAT_PYTHON3
Bram Moolenaar368373e2010-07-19 20:46:22 +0200193# define ex_py3 ex_script_ni
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200194# define ex_py3do ex_ni
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200195# define ex_py3file ex_ni
196#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100197#if !defined(FEAT_PYTHON) && !defined(FEAT_PYTHON3)
198# define ex_pyx ex_script_ni
199# define ex_pyxdo ex_ni
200# define ex_pyxfile ex_ni
201#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000202#ifndef FEAT_TCL
203# define ex_tcl ex_script_ni
204# define ex_tcldo ex_ni
205# define ex_tclfile ex_ni
206#endif
207#ifndef FEAT_RUBY
208# define ex_ruby ex_script_ni
209# define ex_rubydo ex_ni
210# define ex_rubyfile ex_ni
211#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000212#ifndef FEAT_KEYMAP
213# define ex_loadkeymap ex_ni
214#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100215static void ex_swapname(exarg_T *eap);
216static void ex_syncbind(exarg_T *eap);
217static void ex_read(exarg_T *eap);
218static void ex_pwd(exarg_T *eap);
219static void ex_equal(exarg_T *eap);
220static void ex_sleep(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100221static void ex_winsize(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100222static void ex_wincmd(exarg_T *eap);
Bram Moolenaar843ee412004-06-30 16:16:41 +0000223#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100224static void ex_winpos(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000225#else
226# define ex_winpos ex_ni
227#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100228static void ex_operators(exarg_T *eap);
229static void ex_put(exarg_T *eap);
230static void ex_copymove(exarg_T *eap);
231static void ex_submagic(exarg_T *eap);
232static void ex_join(exarg_T *eap);
233static void ex_at(exarg_T *eap);
234static void ex_bang(exarg_T *eap);
235static void ex_undo(exarg_T *eap);
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200236#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100237static void ex_wundo(exarg_T *eap);
238static void ex_rundo(exarg_T *eap);
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200239#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100240static void ex_redo(exarg_T *eap);
241static void ex_later(exarg_T *eap);
242static void ex_redir(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100243static void ex_redrawstatus(exarg_T *eap);
Bram Moolenaare12bab32019-01-08 22:02:56 +0100244static void ex_redrawtabline(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100245static void close_redir(void);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100246static void ex_mark(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100247static void ex_startinsert(exarg_T *eap);
248static void ex_stopinsert(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000249#ifdef FEAT_FIND_ID
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100250static void ex_checkpath(exarg_T *eap);
251static void ex_findpat(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000252#else
253# define ex_findpat ex_ni
254# define ex_checkpath ex_ni
255#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +0200256#if defined(FEAT_FIND_ID) && defined(FEAT_QUICKFIX)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100257static void ex_psearch(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000258#else
259# define ex_psearch ex_ni
260#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100261static void ex_tag(exarg_T *eap);
262static void ex_tag_cmd(exarg_T *eap, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000263#ifndef FEAT_EVAL
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200264# define ex_break ex_ni
265# define ex_breakadd ex_ni
266# define ex_breakdel ex_ni
267# define ex_breaklist ex_ni
268# define ex_call ex_ni
269# define ex_catch ex_ni
270# define ex_compiler ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200271# define ex_continue ex_ni
272# define ex_debug ex_ni
273# define ex_debuggreedy ex_ni
Bram Moolenaar822ba242020-05-24 23:00:18 +0200274# define ex_def ex_ni
275# define ex_defcompile ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200276# define ex_delfunction ex_ni
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100277# define ex_disassemble ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000278# define ex_echo ex_ni
279# define ex_echohl ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000280# define ex_else ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200281# define ex_endfunction ex_ni
282# define ex_endif ex_ni
283# define ex_endtry ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000284# define ex_endwhile ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200285# define ex_eval ex_ni
286# define ex_execute ex_ni
287# define ex_finally ex_ni
288# define ex_finish ex_ni
289# define ex_function ex_ni
290# define ex_if ex_ni
291# define ex_let ex_ni
292# define ex_lockvar ex_ni
293# define ex_oldfiles ex_ni
294# define ex_options ex_ni
295# define ex_packadd ex_ni
296# define ex_packloadall ex_ni
297# define ex_return ex_ni
298# define ex_scriptnames ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000299# define ex_throw ex_ni
300# define ex_try ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000301# define ex_unlet ex_ni
Bram Moolenaar65c1b012005-01-31 19:02:28 +0000302# define ex_unlockvar ex_ni
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100303# define ex_vim9script ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200304# define ex_while ex_ni
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100305# define ex_import ex_ni
306# define ex_export ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000307#endif
Bram Moolenaar84538072019-07-28 14:15:42 +0200308#ifndef FEAT_SESSION
Bram Moolenaar071d4272004-06-13 20:20:40 +0000309# define ex_loadview ex_ni
310#endif
Bram Moolenaardefa0672019-07-21 19:25:37 +0200311#ifndef FEAT_VIMINFO
Bram Moolenaar071d4272004-06-13 20:20:40 +0000312# define ex_viminfo ex_ni
313#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100314static void ex_behave(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100315static void ex_filetype(exarg_T *eap);
316static void ex_setfiletype(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000317#ifndef FEAT_DIFF
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000318# define ex_diffoff ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000319# define ex_diffpatch ex_ni
320# define ex_diffgetput ex_ni
321# define ex_diffsplit ex_ni
322# define ex_diffthis ex_ni
323# define ex_diffupdate ex_ni
324#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100325static void ex_digraphs(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000326#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100327static void ex_nohlsearch(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000328#else
329# define ex_nohlsearch ex_ni
330# define ex_match ex_ni
331#endif
332#ifdef FEAT_CRYPT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100333static void ex_X(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000334#else
335# define ex_X ex_ni
336#endif
337#ifdef FEAT_FOLDING
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100338static void ex_fold(exarg_T *eap);
339static void ex_foldopen(exarg_T *eap);
340static void ex_folddo(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000341#else
342# define ex_fold ex_ni
343# define ex_foldopen ex_ni
344# define ex_folddo ex_ni
345#endif
Bram Moolenaar13505972019-01-24 15:04:48 +0100346#if !(defined(HAVE_LOCALE_H) || defined(X_LOCALE))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000347# define ex_language ex_ni
348#endif
349#ifndef FEAT_SIGNS
350# define ex_sign ex_ni
351#endif
Bram Moolenaar009b2592004-10-24 19:18:58 +0000352#ifndef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200353# define ex_nbclose ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000354# define ex_nbkey ex_ni
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200355# define ex_nbstart ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000356#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000357
Bram Moolenaar071d4272004-06-13 20:20:40 +0000358#ifndef FEAT_JUMPLIST
359# define ex_jumps ex_ni
Bram Moolenaar2d358992016-06-12 21:20:54 +0200360# define ex_clearjumps ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000361# define ex_changes ex_ni
362#endif
363
Bram Moolenaar05159a02005-02-26 23:04:13 +0000364#ifndef FEAT_PROFILE
365# define ex_profile ex_ni
366#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200367#ifndef FEAT_TERMINAL
368# define ex_terminal ex_ni
369#endif
Bram Moolenaard4aa83a2019-05-09 18:59:31 +0200370#if !defined(FEAT_X11) || !defined(FEAT_XCLIPBOARD)
371# define ex_xrestore ex_ni
372#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100373#if !defined(FEAT_PROP_POPUP)
Bram Moolenaar682725c2019-05-25 20:10:37 +0200374# define ex_popupclear ex_ni
375#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +0000376
Bram Moolenaar071d4272004-06-13 20:20:40 +0000377/*
378 * Declare cmdnames[].
379 */
380#define DO_DECLARE_EXCMD
381#include "ex_cmds.h"
Bram Moolenaar6de5e122017-04-20 21:55:44 +0200382#include "ex_cmdidxs.h"
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +0100383
Bram Moolenaar071d4272004-06-13 20:20:40 +0000384static char_u dollar_command[2] = {'$', 0};
385
386
387#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100388// Struct for storing a line inside a while/for loop
Bram Moolenaar071d4272004-06-13 20:20:40 +0000389typedef struct
390{
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100391 char_u *line; // command line
392 linenr_T lnum; // sourcing_lnum of the line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000393} wcmd_T;
394
395/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000396 * Structure used to store info for line position in a while or for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000397 * This is required, because do_one_cmd() may invoke ex_function(), which
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000398 * reads more lines that may come from the while/for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000399 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000400struct loop_cookie
Bram Moolenaar071d4272004-06-13 20:20:40 +0000401{
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100402 garray_T *lines_gap; // growarray with line info
403 int current_line; // last read line from growarray
404 int repeating; // TRUE when looping a second time
405 // When "repeating" is FALSE use "getline" and "cookie" to get lines
Bram Moolenaare96a2492019-06-25 04:12:16 +0200406 char_u *(*getline)(int, void *, int, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000407 void *cookie;
408};
409
Bram Moolenaare96a2492019-06-25 04:12:16 +0200410static char_u *get_loop_line(int c, void *cookie, int indent, int do_concat);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100411static int store_loop_line(garray_T *gap, char_u *line);
412static void free_cmdlines(garray_T *gap);
Bram Moolenaared203462004-06-16 11:19:22 +0000413
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100414// Struct to save a few things while debugging. Used in do_cmdline() only.
Bram Moolenaared203462004-06-16 11:19:22 +0000415struct dbg_stuff
416{
417 int trylevel;
418 int force_abort;
419 except_T *caught_stack;
420 char_u *vv_exception;
421 char_u *vv_throwpoint;
422 int did_emsg;
423 int got_int;
424 int did_throw;
425 int need_rethrow;
426 int check_cstack;
427 except_T *current_exception;
428};
429
Bram Moolenaared203462004-06-16 11:19:22 +0000430 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100431save_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000432{
433 dsp->trylevel = trylevel; trylevel = 0;
434 dsp->force_abort = force_abort; force_abort = FALSE;
435 dsp->caught_stack = caught_stack; caught_stack = NULL;
436 dsp->vv_exception = v_exception(NULL);
437 dsp->vv_throwpoint = v_throwpoint(NULL);
438
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100439 // Necessary for debugging an inactive ":catch", ":finally", ":endtry"
Bram Moolenaared203462004-06-16 11:19:22 +0000440 dsp->did_emsg = did_emsg; did_emsg = FALSE;
441 dsp->got_int = got_int; got_int = FALSE;
442 dsp->did_throw = did_throw; did_throw = FALSE;
443 dsp->need_rethrow = need_rethrow; need_rethrow = FALSE;
444 dsp->check_cstack = check_cstack; check_cstack = FALSE;
445 dsp->current_exception = current_exception; current_exception = NULL;
446}
447
448 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100449restore_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000450{
451 suppress_errthrow = FALSE;
452 trylevel = dsp->trylevel;
453 force_abort = dsp->force_abort;
454 caught_stack = dsp->caught_stack;
455 (void)v_exception(dsp->vv_exception);
456 (void)v_throwpoint(dsp->vv_throwpoint);
457 did_emsg = dsp->did_emsg;
458 got_int = dsp->got_int;
459 did_throw = dsp->did_throw;
460 need_rethrow = dsp->need_rethrow;
461 check_cstack = dsp->check_cstack;
462 current_exception = dsp->current_exception;
463}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000464#endif
465
Bram Moolenaar071d4272004-06-13 20:20:40 +0000466/*
467 * do_exmode(): Repeatedly get commands for the "Ex" mode, until the ":vi"
468 * command is given.
469 */
470 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100471do_exmode(
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100472 int improved) // TRUE for "improved Ex" mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000473{
474 int save_msg_scroll;
475 int prev_msg_row;
476 linenr_T prev_line;
Bram Moolenaar79518e22017-02-17 16:31:35 +0100477 varnumber_T changedtick;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000478
479 if (improved)
480 exmode_active = EXMODE_VIM;
481 else
482 exmode_active = EXMODE_NORMAL;
483 State = NORMAL;
484
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100485 // When using ":global /pat/ visual" and then "Q" we return to continue
486 // the :global command.
Bram Moolenaardf177f62005-02-22 08:39:57 +0000487 if (global_busy)
488 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000489
490 save_msg_scroll = msg_scroll;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100491 ++RedrawingDisabled; // don't redisplay the window
492 ++no_wait_return; // don't wait for return
Bram Moolenaar071d4272004-06-13 20:20:40 +0000493#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100494 // Ignore scrollbar and mouse events in Ex mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000495 ++hold_gui_events;
496#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000497
Bram Moolenaar32526b32019-01-19 17:43:09 +0100498 msg(_("Entering Ex mode. Type \"visual\" to go to Normal mode."));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000499 while (exmode_active)
500 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100501 // Check for a ":normal" command and no more characters left.
Bram Moolenaar7c626922005-02-07 22:01:03 +0000502 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
503 {
504 exmode_active = FALSE;
505 break;
506 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000507 msg_scroll = TRUE;
508 need_wait_return = FALSE;
509 ex_pressedreturn = FALSE;
510 ex_no_reprint = FALSE;
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100511 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000512 prev_msg_row = msg_row;
513 prev_line = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000514 if (improved)
515 {
516 cmdline_row = msg_row;
517 do_cmdline(NULL, getexline, NULL, 0);
518 }
519 else
520 do_cmdline(NULL, getexmodeline, NULL, DOCMD_NOWAIT);
521 lines_left = Rows - 1;
522
Bram Moolenaardf177f62005-02-22 08:39:57 +0000523 if ((prev_line != curwin->w_cursor.lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100524 || changedtick != CHANGEDTICK(curbuf)) && !ex_no_reprint)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000525 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000526 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100527 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000528 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000529 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000530 if (ex_pressedreturn)
531 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100532 // go up one line, to overwrite the ":<CR>" line, so the
533 // output doesn't contain empty lines.
Bram Moolenaardf177f62005-02-22 08:39:57 +0000534 msg_row = prev_msg_row;
535 if (prev_msg_row == Rows - 1)
536 msg_row--;
537 }
538 msg_col = 0;
539 print_line_no_prefix(curwin->w_cursor.lnum, FALSE, FALSE);
540 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000541 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000542 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100543 else if (ex_pressedreturn && !ex_no_reprint) // must be at EOF
Bram Moolenaardf177f62005-02-22 08:39:57 +0000544 {
545 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100546 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000547 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100548 emsg(_("E501: At end-of-file"));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000549 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000550 }
551
552#ifdef FEAT_GUI
553 --hold_gui_events;
554#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000555 --RedrawingDisabled;
556 --no_wait_return;
557 update_screen(CLEAR);
558 need_wait_return = FALSE;
559 msg_scroll = save_msg_scroll;
560}
561
562/*
Bram Moolenaar4facea32019-10-12 20:17:40 +0200563 * Print the executed command for when 'verbose' is set.
564 * When "lnum" is 0 only print the command.
565 */
566 static void
567msg_verbose_cmd(linenr_T lnum, char_u *cmd)
568{
569 ++no_wait_return;
570 verbose_enter_scroll();
571
572 if (lnum == 0)
573 smsg(_("Executing: %s"), cmd);
574 else
575 smsg(_("line %ld: %s"), (long)lnum, cmd);
576 if (msg_silent == 0)
577 msg_puts("\n"); // don't overwrite this
578
579 verbose_leave_scroll();
580 --no_wait_return;
581}
582
583/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000584 * Execute a simple command line. Used for translated commands like "*".
585 */
586 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100587do_cmdline_cmd(char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000588{
589 return do_cmdline(cmd, NULL, NULL,
590 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
591}
592
593/*
594 * do_cmdline(): execute one Ex command line
595 *
596 * 1. Execute "cmdline" when it is not NULL.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100597 * If "cmdline" is NULL, or more lines are needed, fgetline() is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000598 * 2. Split up in parts separated with '|'.
599 *
600 * This function can be called recursively!
601 *
602 * flags:
603 * DOCMD_VERBOSE - The command will be included in the error message.
604 * DOCMD_NOWAIT - Don't call wait_return() and friends.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100605 * DOCMD_REPEAT - Repeat execution until fgetline() returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000606 * DOCMD_KEYTYPED - Don't reset KeyTyped.
607 * DOCMD_EXCRESET - Reset the exception environment (used for debugging).
608 * DOCMD_KEEPLINE - Store first typed line (for repeating with ".").
609 *
610 * return FAIL if cmdline could not be executed, OK otherwise
611 */
612 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100613do_cmdline(
614 char_u *cmdline,
Bram Moolenaare96a2492019-06-25 04:12:16 +0200615 char_u *(*fgetline)(int, void *, int, int),
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100616 void *cookie, // argument for fgetline()
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100617 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000618{
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100619 char_u *next_cmdline; // next cmd to execute
620 char_u *cmdline_copy = NULL; // copy of cmd line
621 int used_getline = FALSE; // used "fgetline" to obtain command
622 static int recursive = 0; // recursive depth
Bram Moolenaar071d4272004-06-13 20:20:40 +0000623 int msg_didout_before_start = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100624 int count = 0; // line number count
625 int did_inc = FALSE; // incremented RedrawingDisabled
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626 int retval = OK;
627#ifdef FEAT_EVAL
Bram Moolenaarddef1292019-12-16 17:10:33 +0100628 cstack_T cstack; // conditional stack
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100629 garray_T lines_ga; // keep lines for ":while"/":for"
630 int current_line = 0; // active line in lines_ga
Bram Moolenaard5053d02020-06-28 15:51:16 +0200631 int current_line_before = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100632 char_u *fname = NULL; // function or script name
633 linenr_T *breakpoint = NULL; // ptr to breakpoint field in cookie
634 int *dbg_tick = NULL; // ptr to dbg_tick field in cookie
635 struct dbg_stuff debug_saved; // saved things for debug mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000636 int initial_trylevel;
Bram Moolenaar25e0f582020-05-25 22:36:50 +0200637 msglist_T **saved_msg_list = NULL;
638 msglist_T *private_msg_list;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000639
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100640 // "fgetline" and "cookie" passed to do_one_cmd()
Bram Moolenaare96a2492019-06-25 04:12:16 +0200641 char_u *(*cmd_getline)(int, void *, int, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642 void *cmd_cookie;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000643 struct loop_cookie cmd_loop_cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000644 void *real_cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000645 int getline_is_func;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000646#else
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100647# define cmd_getline fgetline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000648# define cmd_cookie cookie
649#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100650 static int call_depth = 0; // recursiveness
Bram Moolenaar2196bce2020-04-12 20:01:11 +0200651#ifdef FEAT_EVAL
Bram Moolenaare31ee862020-01-07 20:59:34 +0100652 ESTACK_CHECK_DECLARATION
Bram Moolenaar071d4272004-06-13 20:20:40 +0000653
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100654 // For every pair of do_cmdline()/do_one_cmd() calls, use an extra memory
655 // location for storing error messages to be converted to an exception.
656 // This ensures that the do_errthrow() call in do_one_cmd() does not
657 // combine the messages stored by an earlier invocation of do_one_cmd()
658 // with the command name of the later one. This would happen when
659 // BufWritePost autocommands are executed after a write error.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000660 saved_msg_list = msg_list;
661 msg_list = &private_msg_list;
662 private_msg_list = NULL;
663#endif
664
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100665 // It's possible to create an endless loop with ":execute", catch that
666 // here. The value of 200 allows nested function calls, ":source", etc.
667 // Allow 200 or 'maxfuncdepth', whatever is larger.
Bram Moolenaarb094ff42017-01-02 16:16:39 +0100668 if (call_depth >= 200
669#ifdef FEAT_EVAL
670 && call_depth >= p_mfd
671#endif
672 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000673 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100674 emsg(_("E169: Command too recursive"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100676 // When converting to an exception, we do not include the command name
677 // since this is not an error of the specific command.
Bram Moolenaarddef1292019-12-16 17:10:33 +0100678 do_errthrow((cstack_T *)NULL, (char_u *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679 msg_list = saved_msg_list;
680#endif
681 return FAIL;
682 }
683 ++call_depth;
684
685#ifdef FEAT_EVAL
686 cstack.cs_idx = -1;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000687 cstack.cs_looplevel = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688 cstack.cs_trylevel = 0;
689 cstack.cs_emsg_silent_list = NULL;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000690 cstack.cs_lflags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691 ga_init2(&lines_ga, (int)sizeof(wcmd_T), 10);
692
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100693 real_cookie = getline_cookie(fgetline, cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100695 // Inside a function use a higher nesting level.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100696 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000697 if (getline_is_func && ex_nesting_level == func_level(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698 ++ex_nesting_level;
699
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100700 // Get the function or script name and the address where the next breakpoint
701 // line and the debug tick for a function or script are stored.
Bram Moolenaar05159a02005-02-26 23:04:13 +0000702 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000703 {
704 fname = func_name(real_cookie);
705 breakpoint = func_breakpoint(real_cookie);
706 dbg_tick = func_dbg_tick(real_cookie);
707 }
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100708 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100710 fname = SOURCING_NAME;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000711 breakpoint = source_breakpoint(real_cookie);
712 dbg_tick = source_dbg_tick(real_cookie);
713 }
714
715 /*
716 * Initialize "force_abort" and "suppress_errthrow" at the top level.
717 */
718 if (!recursive)
719 {
720 force_abort = FALSE;
721 suppress_errthrow = FALSE;
722 }
723
724 /*
725 * If requested, store and reset the global values controlling the
Bram Moolenaar89d40322006-08-29 15:30:07 +0000726 * exception handling (used when debugging). Otherwise clear it to avoid
727 * a bogus compiler warning when the optimizer uses inline functions...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000728 */
Bram Moolenaarb4872942006-05-13 10:32:52 +0000729 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +0000730 save_dbg_stuff(&debug_saved);
Bram Moolenaar89d40322006-08-29 15:30:07 +0000731 else
Bram Moolenaara80faa82020-04-12 19:37:17 +0200732 CLEAR_FIELD(debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000733
734 initial_trylevel = trylevel;
735
736 /*
737 * "did_throw" will be set to TRUE when an exception is being thrown.
738 */
739 did_throw = FALSE;
740#endif
741 /*
742 * "did_emsg" will be set to TRUE when emsg() is used, in which case we
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000743 * cancel the whole command line, and any if/endif or loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000744 * If force_abort is set, we cancel everything.
745 */
746 did_emsg = FALSE;
747
748 /*
749 * KeyTyped is only set when calling vgetc(). Reset it here when not
750 * calling vgetc() (sourced command lines).
751 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100752 if (!(flags & DOCMD_KEYTYPED)
753 && !getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754 KeyTyped = FALSE;
755
756 /*
757 * Continue executing command lines:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000758 * - when inside an ":if", ":while" or ":for"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000759 * - for multiple commands on one line, separated with '|'
760 * - when repeating until there are no more lines (for ":source")
761 */
762 next_cmdline = cmdline;
763 do
764 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000765#ifdef FEAT_EVAL
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100766 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000767#endif
768
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100769 // stop skipping cmds for an error msg after all endif/while/for
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770 if (next_cmdline == NULL
771#ifdef FEAT_EVAL
772 && !force_abort
773 && cstack.cs_idx < 0
Bram Moolenaar05159a02005-02-26 23:04:13 +0000774 && !(getline_is_func && func_has_abort(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775#endif
776 )
777 did_emsg = FALSE;
778
779 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000780 * 1. If repeating a line in a loop, get a line from lines_ga.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100781 * 2. If no line given: Get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782 * 3. If a line is given: Make a copy, so we can mess with it.
783 */
784
785#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100786 // 1. If repeating, get a previous line from lines_ga.
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000787 if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100789 // Each '|' separated command is stored separately in lines_ga, to
790 // be able to jump to it. Don't use next_cmdline now.
Bram Moolenaard23a8232018-02-10 18:45:26 +0100791 VIM_CLEAR(cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100793 // Check if a function has returned or, unless it has an unclosed
794 // try conditional, aborted.
Bram Moolenaar05159a02005-02-26 23:04:13 +0000795 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000797# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000798 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000799 func_line_end(real_cookie);
800# endif
801 if (func_has_ended(real_cookie))
802 {
803 retval = FAIL;
804 break;
805 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000807#ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000808 else if (do_profiling == PROF_YES
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100809 && getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000810 script_line_end();
811#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000812
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100813 // Check if a sourced file hit a ":finish" command.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100814 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815 {
816 retval = FAIL;
817 break;
818 }
819
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100820 // If breakpoints have been added/deleted need to check for it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000821 if (breakpoint != NULL && dbg_tick != NULL
822 && *dbg_tick != debug_tick)
823 {
824 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100825 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100826 fname, SOURCING_LNUM);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827 *dbg_tick = debug_tick;
828 }
829
830 next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100831 SOURCING_LNUM = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100833 // Did we encounter a breakpoint?
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834 if (breakpoint != NULL && *breakpoint != 0
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100835 && *breakpoint <= SOURCING_LNUM)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000836 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100837 dbg_breakpoint(fname, SOURCING_LNUM);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100838 // Find next breakpoint.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100840 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100841 fname, SOURCING_LNUM);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842 *dbg_tick = debug_tick;
843 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000844# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000845 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000846 {
847 if (getline_is_func)
848 func_line_start(real_cookie);
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100849 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000850 script_line_start();
851 }
852# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000853 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854#endif
855
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100856 // 2. If no line given, get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000857 if (next_cmdline == NULL)
858 {
859 /*
860 * Need to set msg_didout for the first line after an ":if",
861 * otherwise the ":if" will be overwritten.
862 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100863 if (count == 1 && getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000864 msg_didout = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100865 if (fgetline == NULL || (next_cmdline = fgetline(':', cookie,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000866#ifdef FEAT_EVAL
867 cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2
868#else
869 0
870#endif
Bram Moolenaare96a2492019-06-25 04:12:16 +0200871 , TRUE)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000872 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100873 // Don't call wait_return for aborted command line. The NULL
874 // returned for the end of a sourced file or executed function
875 // doesn't do this.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876 if (KeyTyped && !(flags & DOCMD_REPEAT))
877 need_wait_return = FALSE;
878 retval = FAIL;
879 break;
880 }
881 used_getline = TRUE;
882
883 /*
884 * Keep the first typed line. Clear it when more lines are typed.
885 */
886 if (flags & DOCMD_KEEPLINE)
887 {
888 vim_free(repeat_cmdline);
889 if (count == 0)
890 repeat_cmdline = vim_strsave(next_cmdline);
891 else
892 repeat_cmdline = NULL;
893 }
894 }
895
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100896 // 3. Make a copy of the command so we can mess with it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000897 else if (cmdline_copy == NULL)
898 {
899 next_cmdline = vim_strsave(next_cmdline);
900 if (next_cmdline == NULL)
901 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100902 emsg(_(e_outofmem));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000903 retval = FAIL;
904 break;
905 }
906 }
907 cmdline_copy = next_cmdline;
908
909#ifdef FEAT_EVAL
910 /*
Bram Moolenaard5053d02020-06-28 15:51:16 +0200911 * Inside a while/for loop, and when the command looks like a ":while"
912 * or ":for", the line is stored, because we may need it later when
913 * looping.
914 *
915 * When there is a '|' and another command, it is stored separately,
916 * because we need to be able to jump back to it from an
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000917 * :endwhile/:endfor.
Bram Moolenaard5053d02020-06-28 15:51:16 +0200918 *
919 * Pass a different "fgetline" function to do_one_cmd() below,
920 * that it stores lines in or reads them from "lines_ga". Makes it
921 * possible to define a function inside a while/for loop and handles
922 * line continuation.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000923 */
Bram Moolenaard5053d02020-06-28 15:51:16 +0200924 if ((cstack.cs_looplevel > 0 || has_loop_cmd(next_cmdline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000925 {
Bram Moolenaard5053d02020-06-28 15:51:16 +0200926 cmd_getline = get_loop_line;
927 cmd_cookie = (void *)&cmd_loop_cookie;
928 cmd_loop_cookie.lines_gap = &lines_ga;
929 cmd_loop_cookie.current_line = current_line;
930 cmd_loop_cookie.getline = fgetline;
931 cmd_loop_cookie.cookie = cookie;
932 cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len);
933
934 // Save the current line when encountering it the first time.
935 if (current_line == lines_ga.ga_len
936 && store_loop_line(&lines_ga, next_cmdline) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937 {
938 retval = FAIL;
939 break;
940 }
Bram Moolenaard5053d02020-06-28 15:51:16 +0200941 current_line_before = current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000942 }
Bram Moolenaard5053d02020-06-28 15:51:16 +0200943 else
944 {
945 cmd_getline = fgetline;
946 cmd_cookie = cookie;
947 }
948
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949 did_endif = FALSE;
950#endif
951
952 if (count++ == 0)
953 {
954 /*
955 * All output from the commands is put below each other, without
956 * waiting for a return. Don't do this when executing commands
957 * from a script or when being called recursive (e.g. for ":e
958 * +command file").
959 */
960 if (!(flags & DOCMD_NOWAIT) && !recursive)
961 {
962 msg_didout_before_start = msg_didout;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100963 msg_didany = FALSE; // no output yet
Bram Moolenaar071d4272004-06-13 20:20:40 +0000964 msg_start();
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100965 msg_scroll = TRUE; // put messages below each other
966 ++no_wait_return; // don't wait for return until finished
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967 ++RedrawingDisabled;
968 did_inc = TRUE;
969 }
970 }
971
Bram Moolenaar823654b2020-05-29 23:03:09 +0200972 if ((p_verbose >= 15 && SOURCING_NAME != NULL) || p_verbose >= 16)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100973 msg_verbose_cmd(SOURCING_LNUM, cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000974
975 /*
976 * 2. Execute one '|' separated command.
977 * do_one_cmd() will return NULL if there is no trailing '|'.
978 * "cmdline_copy" can change, e.g. for '%' and '#' expansion.
979 */
980 ++recursive;
981 next_cmdline = do_one_cmd(&cmdline_copy, flags & DOCMD_VERBOSE,
982#ifdef FEAT_EVAL
983 &cstack,
984#endif
985 cmd_getline, cmd_cookie);
986 --recursive;
987
988#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000989 if (cmd_cookie == (void *)&cmd_loop_cookie)
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100990 // Use "current_line" from "cmd_loop_cookie", it may have been
991 // incremented when defining a function.
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000992 current_line = cmd_loop_cookie.current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993#endif
994
995 if (next_cmdline == NULL)
996 {
Bram Moolenaard23a8232018-02-10 18:45:26 +0100997 VIM_CLEAR(cmdline_copy);
Bram Moolenaard7663c22019-08-06 21:59:57 +0200998
Bram Moolenaar071d4272004-06-13 20:20:40 +0000999 /*
1000 * If the command was typed, remember it for the ':' register.
1001 * Do this AFTER executing the command to make :@: work.
1002 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001003 if (getline_equal(fgetline, cookie, getexline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001004 && new_last_cmdline != NULL)
1005 {
1006 vim_free(last_cmdline);
1007 last_cmdline = new_last_cmdline;
1008 new_last_cmdline = NULL;
1009 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010 }
1011 else
1012 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001013 // need to copy the command after the '|' to cmdline_copy, for the
1014 // next do_one_cmd()
Bram Moolenaara7241f52008-06-24 20:39:31 +00001015 STRMOVE(cmdline_copy, next_cmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016 next_cmdline = cmdline_copy;
1017 }
1018
1019
1020#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001021 // reset did_emsg for a function that is not aborted by an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022 if (did_emsg && !force_abort
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001023 && getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001024 && !func_has_abort(real_cookie))
1025 did_emsg = FALSE;
1026
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001027 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028 {
1029 ++current_line;
1030
1031 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001032 * An ":endwhile", ":endfor" and ":continue" is handled here.
1033 * If we were executing commands, jump back to the ":while" or
1034 * ":for".
1035 * If we were not executing commands, decrement cs_looplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001037 if (cstack.cs_lflags & (CSL_HAD_CONT | CSL_HAD_ENDLOOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001039 cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001041 // Jump back to the matching ":while" or ":for". Be careful
1042 // not to use a cs_line[] from an entry that isn't a ":while"
1043 // or ":for": It would make "current_line" invalid and can
1044 // cause a crash.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045 if (!did_emsg && !got_int && !did_throw
1046 && cstack.cs_idx >= 0
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001047 && (cstack.cs_flags[cstack.cs_idx]
1048 & (CSF_WHILE | CSF_FOR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049 && cstack.cs_line[cstack.cs_idx] >= 0
1050 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE))
1051 {
1052 current_line = cstack.cs_line[cstack.cs_idx];
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001053 // remember we jumped there
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001054 cstack.cs_lflags |= CSL_HAD_LOOP;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001055 line_breakcheck(); // check if CTRL-C typed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001057 // Check for the next breakpoint at or after the ":while"
1058 // or ":for".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059 if (breakpoint != NULL)
1060 {
1061 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001062 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063 fname,
1064 ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1);
1065 *dbg_tick = debug_tick;
1066 }
1067 }
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001068 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001070 // can only get here with ":endwhile" or ":endfor"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071 if (cstack.cs_idx >= 0)
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001072 rewind_conditionals(&cstack, cstack.cs_idx - 1,
1073 CSF_WHILE | CSF_FOR, &cstack.cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074 }
1075 }
1076
1077 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001078 * For a ":while" or ":for" we need to remember the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001080 else if (cstack.cs_lflags & CSL_HAD_LOOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001082 cstack.cs_lflags &= ~CSL_HAD_LOOP;
Bram Moolenaard5053d02020-06-28 15:51:16 +02001083 cstack.cs_line[cstack.cs_idx] = current_line_before;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084 }
1085 }
1086
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001087 // Check for the next breakpoint after a watchexpression
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01001088 if (breakpoint != NULL && has_watchexpr())
1089 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001090 *breakpoint = dbg_find_breakpoint(FALSE, fname, SOURCING_LNUM);
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01001091 *dbg_tick = debug_tick;
1092 }
1093
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094 /*
1095 * When not inside any ":while" loop, clear remembered lines.
1096 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001097 if (cstack.cs_looplevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098 {
1099 if (lines_ga.ga_len > 0)
1100 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001101 SOURCING_LNUM =
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102 ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum;
1103 free_cmdlines(&lines_ga);
1104 }
1105 current_line = 0;
1106 }
1107
1108 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001109 * A ":finally" makes did_emsg, got_int, and did_throw pending for
1110 * being restored at the ":endtry". Reset them here and set the
1111 * ACTIVE and FINALLY flags, so that the finally clause gets executed.
1112 * This includes the case where a missing ":endif", ":endwhile" or
1113 * ":endfor" was detected by the ":finally" itself.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001115 if (cstack.cs_lflags & CSL_HAD_FINA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001116 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001117 cstack.cs_lflags &= ~CSL_HAD_FINA;
1118 report_make_pending(cstack.cs_pending[cstack.cs_idx]
1119 & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001120 did_throw ? (void *)current_exception : NULL);
1121 did_emsg = got_int = did_throw = FALSE;
1122 cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY;
1123 }
1124
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001125 // Update global "trylevel" for recursive calls to do_cmdline() from
1126 // within this loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001127 trylevel = initial_trylevel + cstack.cs_trylevel;
1128
1129 /*
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001130 * If the outermost try conditional (across function calls and sourced
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131 * files) is aborted because of an error, an interrupt, or an uncaught
1132 * exception, cancel everything. If it is left normally, reset
1133 * force_abort to get the non-EH compatible abortion behavior for
1134 * the rest of the script.
1135 */
1136 if (trylevel == 0 && !did_emsg && !got_int && !did_throw)
1137 force_abort = FALSE;
1138
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001139 // Convert an interrupt to an exception if appropriate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001140 (void)do_intthrow(&cstack);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001141#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142
1143 }
1144 /*
1145 * Continue executing command lines when:
1146 * - no CTRL-C typed, no aborting error, no exception thrown or try
1147 * conditionals need to be checked for executing finally clauses or
1148 * catching an interrupt exception
1149 * - didn't get an error message or lines are not typed
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001150 * - there is a command after '|', inside a :if, :while, :for or :try, or
Bram Moolenaar071d4272004-06-13 20:20:40 +00001151 * looping for ":source" command or function call.
1152 */
1153 while (!((got_int
1154#ifdef FEAT_EVAL
1155 || (did_emsg && force_abort) || did_throw
1156#endif
1157 )
1158#ifdef FEAT_EVAL
1159 && cstack.cs_trylevel == 0
1160#endif
1161 )
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001162 && !(did_emsg
1163#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001164 // Keep going when inside try/catch, so that the error can be
1165 // deal with, except when it is a syntax error, it may cause
1166 // the :endtry to be missed.
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001167 && (cstack.cs_trylevel == 0 || did_emsg_syntax)
1168#endif
1169 && used_getline
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001170 && (getline_equal(fgetline, cookie, getexmodeline)
1171 || getline_equal(fgetline, cookie, getexline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172 && (next_cmdline != NULL
1173#ifdef FEAT_EVAL
1174 || cstack.cs_idx >= 0
1175#endif
1176 || (flags & DOCMD_REPEAT)));
1177
1178 vim_free(cmdline_copy);
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001179 did_emsg_syntax = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180#ifdef FEAT_EVAL
1181 free_cmdlines(&lines_ga);
1182 ga_clear(&lines_ga);
1183
1184 if (cstack.cs_idx >= 0)
1185 {
1186 /*
1187 * If a sourced file or executed function ran to its end, report the
1188 * unclosed conditional.
1189 */
1190 if (!got_int && !did_throw
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001191 && ((getline_equal(fgetline, cookie, getsourceline)
1192 && !source_finished(fgetline, cookie))
1193 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194 && !func_has_ended(real_cookie))))
1195 {
1196 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001197 emsg(_(e_endtry));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001199 emsg(_(e_endwhile));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001200 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001201 emsg(_(e_endfor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001203 emsg(_(e_endif));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204 }
1205
1206 /*
1207 * Reset "trylevel" in case of a ":finish" or ":return" or a missing
1208 * ":endtry" in a sourced file or executed function. If the try
1209 * conditional is in its finally clause, ignore anything pending.
1210 * If it is in a catch clause, finish the caught exception.
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001211 * Also cleanup any "cs_forinfo" structures.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212 */
1213 do
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001214 {
1215 int idx = cleanup_conditionals(&cstack, 0, TRUE);
1216
Bram Moolenaar89e5d682005-01-17 22:06:23 +00001217 if (idx >= 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001218 --idx; // remove try block not in its finally clause
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001219 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
1220 &cstack.cs_looplevel);
1221 }
1222 while (cstack.cs_idx >= 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223 trylevel = initial_trylevel;
1224 }
1225
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001226 // If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
1227 // lack was reported above and the error message is to be converted to an
1228 // exception, do this now after rewinding the cstack.
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001229 do_errthrow(&cstack, getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001230 ? (char_u *)"endfunction" : (char_u *)NULL);
1231
1232 if (trylevel == 0)
1233 {
1234 /*
1235 * When an exception is being thrown out of the outermost try
1236 * conditional, discard the uncaught exception, disable the conversion
1237 * of interrupts or errors to exceptions, and ensure that no more
1238 * commands are executed.
1239 */
1240 if (did_throw)
1241 {
1242 void *p = NULL;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02001243 msglist_T *messages = NULL, *next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244
1245 /*
1246 * If the uncaught exception is a user exception, report it as an
1247 * error. If it is an error exception, display the saved error
1248 * message now. For an interrupt exception, do nothing; the
1249 * interrupt message is given elsewhere.
1250 */
1251 switch (current_exception->type)
1252 {
1253 case ET_USER:
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001254 vim_snprintf((char *)IObuff, IOSIZE,
1255 _("E605: Exception not caught: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256 current_exception->value);
1257 p = vim_strsave(IObuff);
1258 break;
1259 case ET_ERROR:
1260 messages = current_exception->messages;
1261 current_exception->messages = NULL;
1262 break;
1263 case ET_INTERRUPT:
1264 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265 }
1266
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001267 estack_push(ETYPE_EXCEPT, current_exception->throw_name,
1268 current_exception->throw_lnum);
Bram Moolenaare31ee862020-01-07 20:59:34 +01001269 ESTACK_CHECK_SETUP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270 current_exception->throw_name = NULL;
1271
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001272 discard_current_exception(); // uses IObuff if 'verbose'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273 suppress_errthrow = TRUE;
1274 force_abort = TRUE;
1275
1276 if (messages != NULL)
1277 {
1278 do
1279 {
1280 next = messages->next;
1281 emsg(messages->msg);
1282 vim_free(messages->msg);
Bram Moolenaar5fbf3bc2020-06-01 21:13:11 +02001283 vim_free(messages->sfile);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284 vim_free(messages);
1285 messages = next;
1286 }
1287 while (messages != NULL);
1288 }
1289 else if (p != NULL)
1290 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01001291 emsg(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292 vim_free(p);
1293 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001294 vim_free(SOURCING_NAME);
Bram Moolenaare31ee862020-01-07 20:59:34 +01001295 ESTACK_CHECK_NOW
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001296 estack_pop();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297 }
1298
1299 /*
1300 * On an interrupt or an aborting error not converted to an exception,
1301 * disable the conversion of errors to exceptions. (Interrupts are not
Bram Moolenaar2196bce2020-04-12 20:01:11 +02001302 * converted anymore, here.) This enables also the interrupt message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303 * when force_abort is set and did_emsg unset in case of an interrupt
1304 * from a finally clause after an error.
1305 */
1306 else if (got_int || (did_emsg && force_abort))
1307 suppress_errthrow = TRUE;
1308 }
1309
1310 /*
1311 * The current cstack will be freed when do_cmdline() returns. An uncaught
1312 * exception will have to be rethrown in the previous cstack. If a function
1313 * has just returned or a script file was just finished and the previous
1314 * cstack belongs to the same function or, respectively, script file, it
1315 * will have to be checked for finally clauses to be executed due to the
1316 * ":return" or ":finish". This is done in do_one_cmd().
1317 */
1318 if (did_throw)
1319 need_rethrow = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001320 if ((getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321 && ex_nesting_level > source_level(real_cookie))
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001322 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323 && ex_nesting_level > func_level(real_cookie) + 1))
1324 {
1325 if (!did_throw)
1326 check_cstack = TRUE;
1327 }
1328 else
1329 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001330 // When leaving a function, reduce nesting level.
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001331 if (getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332 --ex_nesting_level;
1333 /*
1334 * Go to debug mode when returning from a function in which we are
1335 * single-stepping.
1336 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001337 if ((getline_equal(fgetline, cookie, getsourceline)
1338 || getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339 && ex_nesting_level + 1 <= debug_break_level)
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001340 do_debug(getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341 ? (char_u *)_("End of sourced file")
1342 : (char_u *)_("End of function"));
1343 }
1344
1345 /*
1346 * Restore the exception environment (done after returning from the
1347 * debugger).
1348 */
1349 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001350 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351
1352 msg_list = saved_msg_list;
Bram Moolenaar292b90d2020-03-18 15:23:16 +01001353
1354 // Cleanup if "cs_emsg_silent_list" remains.
1355 if (cstack.cs_emsg_silent_list != NULL)
1356 {
1357 eslist_T *elem, *temp;
1358
1359 for (elem = cstack.cs_emsg_silent_list; elem != NULL; elem = temp)
1360 {
1361 temp = elem->next;
1362 vim_free(elem);
1363 }
1364 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001365#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366
1367 /*
1368 * If there was too much output to fit on the command line, ask the user to
1369 * hit return before redrawing the screen. With the ":global" command we do
1370 * this only once after the command is finished.
1371 */
1372 if (did_inc)
1373 {
1374 --RedrawingDisabled;
1375 --no_wait_return;
1376 msg_scroll = FALSE;
1377
1378 /*
1379 * When just finished an ":if"-":else" which was typed, no need to
1380 * wait for hit-return. Also for an error situation.
1381 */
1382 if (retval == FAIL
1383#ifdef FEAT_EVAL
1384 || (did_endif && KeyTyped && !did_emsg)
1385#endif
1386 )
1387 {
1388 need_wait_return = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001389 msg_didany = FALSE; // don't wait when restarting edit
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390 }
1391 else if (need_wait_return)
1392 {
1393 /*
1394 * The msg_start() above clears msg_didout. The wait_return we do
1395 * here should not overwrite the command that may be shown before
1396 * doing that.
1397 */
1398 msg_didout |= msg_didout_before_start;
1399 wait_return(FALSE);
1400 }
1401 }
1402
Bram Moolenaar2a942252012-11-28 23:03:07 +01001403#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001404 did_endif = FALSE; // in case do_cmdline used recursively
Bram Moolenaar2a942252012-11-28 23:03:07 +01001405#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406 /*
1407 * Reset if_level, in case a sourced script file contains more ":if" than
1408 * ":endif" (could be ":if x | foo | endif").
1409 */
1410 if_level = 0;
Bram Moolenaar2e18a122012-11-28 19:10:54 +01001411#endif
Bram Moolenaard4ad0d42012-11-28 17:34:48 +01001412
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413 --call_depth;
1414 return retval;
1415}
1416
1417#ifdef FEAT_EVAL
1418/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001419 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420 */
1421 static char_u *
Bram Moolenaare96a2492019-06-25 04:12:16 +02001422get_loop_line(int c, void *cookie, int indent, int do_concat)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001424 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425 wcmd_T *wp;
1426 char_u *line;
1427
1428 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1429 {
1430 if (cp->repeating)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001431 return NULL; // trying to read past ":endwhile"/":endfor"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001433 // First time inside the ":while"/":for": get line normally.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001434 if (cp->getline == NULL)
Bram Moolenaare96a2492019-06-25 04:12:16 +02001435 line = getcmdline(c, 0L, indent, do_concat);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001436 else
Bram Moolenaare96a2492019-06-25 04:12:16 +02001437 line = cp->getline(c, cp->cookie, indent, do_concat);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001438 if (line != NULL && store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001439 ++cp->current_line;
1440
1441 return line;
1442 }
1443
1444 KeyTyped = FALSE;
1445 ++cp->current_line;
1446 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001447 SOURCING_LNUM = wp->lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001448 return vim_strsave(wp->line);
1449}
1450
1451/*
1452 * Store a line in "gap" so that a ":while" loop can execute it again.
1453 */
1454 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001455store_loop_line(garray_T *gap, char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001456{
1457 if (ga_grow(gap, 1) == FAIL)
1458 return FAIL;
1459 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001460 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = SOURCING_LNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001461 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001462 return OK;
1463}
1464
1465/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001466 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001467 */
1468 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001469free_cmdlines(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470{
1471 while (gap->ga_len > 0)
1472 {
1473 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1474 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001475 }
1476}
1477#endif
1478
1479/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001480 * If "fgetline" is get_loop_line(), return TRUE if the getline it uses equals
1481 * "func". * Otherwise return TRUE when "fgetline" equals "func".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001484getline_equal(
Bram Moolenaare96a2492019-06-25 04:12:16 +02001485 char_u *(*fgetline)(int, void *, int, int),
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001486 void *cookie UNUSED, // argument for fgetline()
Bram Moolenaare96a2492019-06-25 04:12:16 +02001487 char_u *(*func)(int, void *, int, int))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488{
1489#ifdef FEAT_EVAL
Bram Moolenaare96a2492019-06-25 04:12:16 +02001490 char_u *(*gp)(int, void *, int, int);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001491 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001492
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001493 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1494 // function that's originally used to obtain the lines. This may be
1495 // nested several levels.
Bram Moolenaar89d40322006-08-29 15:30:07 +00001496 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001497 cp = (struct loop_cookie *)cookie;
1498 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001499 {
1500 gp = cp->getline;
1501 cp = cp->cookie;
1502 }
1503 return gp == func;
1504#else
Bram Moolenaar89d40322006-08-29 15:30:07 +00001505 return fgetline == func;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001506#endif
1507}
1508
Bram Moolenaar071d4272004-06-13 20:20:40 +00001509/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001510 * If "fgetline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511 * getline function. Otherwise return "cookie".
1512 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513 void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001514getline_cookie(
Bram Moolenaare96a2492019-06-25 04:12:16 +02001515 char_u *(*fgetline)(int, void *, int, int) UNUSED,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001516 void *cookie) // argument for fgetline()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517{
Bram Moolenaar13505972019-01-24 15:04:48 +01001518#ifdef FEAT_EVAL
Bram Moolenaare96a2492019-06-25 04:12:16 +02001519 char_u *(*gp)(int, void *, int, int);
Bram Moolenaard5053d02020-06-28 15:51:16 +02001520 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001521
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001522 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1523 // cookie that's originally used to obtain the lines. This may be nested
1524 // several levels.
Bram Moolenaar89d40322006-08-29 15:30:07 +00001525 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001526 cp = (struct loop_cookie *)cookie;
1527 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001528 {
1529 gp = cp->getline;
1530 cp = cp->cookie;
1531 }
1532 return cp;
Bram Moolenaar13505972019-01-24 15:04:48 +01001533#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001534 return cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001535#endif
Bram Moolenaar13505972019-01-24 15:04:48 +01001536}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537
Bram Moolenaard5053d02020-06-28 15:51:16 +02001538#if defined(FEAT_EVAL) || defined(PROT)
1539/*
1540 * Get the next line source line without advancing.
1541 */
1542 char_u *
1543getline_peek(
1544 char_u *(*fgetline)(int, void *, int, int) UNUSED,
1545 void *cookie) // argument for fgetline()
1546{
1547 char_u *(*gp)(int, void *, int, int);
1548 struct loop_cookie *cp;
1549 wcmd_T *wp;
1550
1551 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1552 // cookie that's originally used to obtain the lines. This may be nested
1553 // several levels.
1554 gp = fgetline;
1555 cp = (struct loop_cookie *)cookie;
1556 while (gp == get_loop_line)
1557 {
1558 if (cp->current_line + 1 < cp->lines_gap->ga_len)
1559 {
1560 // executing lines a second time, use the stored copy
1561 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line + 1;
1562 return wp->line;
1563 }
1564 gp = cp->getline;
1565 cp = cp->cookie;
1566 }
1567 if (gp == getsourceline)
1568 return source_nextline(cp);
1569 return NULL;
1570}
1571#endif
1572
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001573
1574/*
1575 * Helper function to apply an offset for buffer commands, i.e. ":bdelete",
1576 * ":bwipeout", etc.
1577 * Returns the buffer number.
1578 */
1579 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001580compute_buffer_local_count(int addr_type, int lnum, int offset)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001581{
1582 buf_T *buf;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001583 buf_T *nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001584 int count = offset;
1585
1586 buf = firstbuf;
1587 while (buf->b_next != NULL && buf->b_fnum < lnum)
1588 buf = buf->b_next;
1589 while (count != 0)
1590 {
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001591 count += (offset < 0) ? 1 : -1;
1592 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1593 if (nextbuf == NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001594 break;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001595 buf = nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001596 if (addr_type == ADDR_LOADED_BUFFERS)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001597 // skip over unloaded buffers
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001598 while (buf->b_ml.ml_mfp == NULL)
1599 {
1600 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1601 if (nextbuf == NULL)
1602 break;
1603 buf = nextbuf;
1604 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001605 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001606 // we might have gone too far, last buffer is not loadedd
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001607 if (addr_type == ADDR_LOADED_BUFFERS)
1608 while (buf->b_ml.ml_mfp == NULL)
1609 {
1610 nextbuf = (offset >= 0) ? buf->b_prev : buf->b_next;
1611 if (nextbuf == NULL)
1612 break;
1613 buf = nextbuf;
1614 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001615 return buf->b_fnum;
1616}
1617
Bram Moolenaarb7316892019-05-01 18:08:42 +02001618/*
1619 * Return the window number of "win".
1620 * When "win" is NULL return the number of windows.
1621 */
Bram Moolenaarf240e182014-11-27 18:33:02 +01001622 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001623current_win_nr(win_T *win)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001624{
1625 win_T *wp;
1626 int nr = 0;
1627
Bram Moolenaar29323592016-07-24 22:04:11 +02001628 FOR_ALL_WINDOWS(wp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001629 {
1630 ++nr;
1631 if (wp == win)
1632 break;
1633 }
1634 return nr;
1635}
1636
1637 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001638current_tab_nr(tabpage_T *tab)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001639{
1640 tabpage_T *tp;
1641 int nr = 0;
1642
Bram Moolenaar29323592016-07-24 22:04:11 +02001643 FOR_ALL_TABPAGES(tp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001644 {
1645 ++nr;
1646 if (tp == tab)
1647 break;
1648 }
1649 return nr;
1650}
1651
1652# define CURRENT_WIN_NR current_win_nr(curwin)
1653# define LAST_WIN_NR current_win_nr(NULL)
1654# define CURRENT_TAB_NR current_tab_nr(curtab)
1655# define LAST_TAB_NR current_tab_nr(NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001656
Bram Moolenaar071d4272004-06-13 20:20:40 +00001657/*
1658 * Execute one Ex command.
1659 *
1660 * If 'sourcing' is TRUE, the command will be included in the error message.
1661 *
1662 * 1. skip comment lines and leading space
1663 * 2. handle command modifiers
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001664 * 3. find the command
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001665 * 4. parse range
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001666 * 5. Parse the command.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001667 * 6. parse arguments
1668 * 7. switch on command name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669 *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001670 * Note: "fgetline" can be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001671 *
1672 * This function may be called recursively!
1673 */
1674#if (_MSC_VER == 1200)
1675/*
Bram Moolenaared203462004-06-16 11:19:22 +00001676 * Avoid optimisation bug in VC++ version 6.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001677 */
Bram Moolenaar281bdce2005-01-25 21:53:18 +00001678 #pragma optimize( "g", off )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001679#endif
1680 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001681do_one_cmd(
Bram Moolenaarddef1292019-12-16 17:10:33 +01001682 char_u **cmdlinep,
1683 int sourcing,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684#ifdef FEAT_EVAL
Bram Moolenaarddef1292019-12-16 17:10:33 +01001685 cstack_T *cstack,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001686#endif
Bram Moolenaarddef1292019-12-16 17:10:33 +01001687 char_u *(*fgetline)(int, void *, int, int),
1688 void *cookie) // argument for fgetline()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689{
Bram Moolenaarddef1292019-12-16 17:10:33 +01001690 char_u *p;
1691 linenr_T lnum;
1692 long n;
1693 char *errormsg = NULL; // error message
1694 char_u *after_modifier = NULL;
1695 exarg_T ea; // Ex command arguments
1696 int save_msg_scroll = msg_scroll;
1697 cmdmod_T save_cmdmod;
1698 int save_reg_executing = reg_executing;
1699 int ni; // set when Not Implemented
1700 char_u *cmd;
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001701#ifdef FEAT_EVAL
1702 int starts_with_colon;
1703#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001704
Bram Moolenaara80faa82020-04-12 19:37:17 +02001705 CLEAR_FIELD(ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001706 ea.line1 = 1;
1707 ea.line2 = 1;
1708#ifdef FEAT_EVAL
1709 ++ex_nesting_level;
1710#endif
1711
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001712 // When the last file has not been edited :q has to be typed twice.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713 if (quitmore
1714#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001715 // avoid that a function call in 'statusline' does this
Bram Moolenaar89d40322006-08-29 15:30:07 +00001716 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001717#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001718 // avoid that an autocommand, e.g. QuitPre, does this
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001719 && !getline_equal(fgetline, cookie, getnextac))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001720 --quitmore;
1721
1722 /*
1723 * Reset browse, confirm, etc.. They are restored when returning, for
1724 * recursive calls.
1725 */
1726 save_cmdmod = cmdmod;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001728 // "#!anything" is handled like a comment.
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001729 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1730 goto doend;
1731
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02001732/*
1733 * 1. Skip comment lines and leading white space and colons.
1734 * 2. Handle command modifiers.
1735 */
1736 // The "ea" structure holds the arguments that can be used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001737 ea.cmd = *cmdlinep;
Bram Moolenaareffed932018-08-14 13:38:17 +02001738 ea.cmdlinep = cmdlinep;
1739 ea.getline = fgetline;
1740 ea.cookie = cookie;
1741#ifdef FEAT_EVAL
1742 ea.cstack = cstack;
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001743 starts_with_colon = *skipwhite(ea.cmd) == ':';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001744#endif
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02001745 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaareffed932018-08-14 13:38:17 +02001746 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001747
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001748 after_modifier = ea.cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749
1750#ifdef FEAT_EVAL
1751 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
1752 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
1753#else
1754 ea.skip = (if_level > 0);
1755#endif
1756
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001758 * 3. Skip over the range to find the command. Let "p" point to after it.
1759 *
1760 * We need the command to know what kind of range it uses.
1761 */
1762 cmd = ea.cmd;
1763 ea.cmd = skip_range(ea.cmd, NULL);
1764 if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
1765 ea.cmd = skipwhite(ea.cmd + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001766
1767#ifdef FEAT_EVAL
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001768 if (current_sctx.sc_version == SCRIPT_VERSION_VIM9 && !starts_with_colon)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02001769 {
1770 if (ea.cmd > cmd)
1771 {
1772 emsg(_(e_colon_required));
1773 goto doend;
1774 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001775 p = find_ex_command(&ea, NULL, lookup_scriptvar, NULL);
Bram Moolenaardf069ee2020-06-22 23:02:51 +02001776 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001777 else
1778#endif
1779 p = find_ex_command(&ea, NULL, NULL, NULL);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001780
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001781#ifdef FEAT_EVAL
1782# ifdef FEAT_PROFILE
1783 // Count this line for profiling if skip is TRUE.
1784 if (do_profiling == PROF_YES
1785 && (!ea.skip || cstack->cs_idx == 0 || (cstack->cs_idx > 0
1786 && (cstack->cs_flags[cstack->cs_idx - 1] & CSF_ACTIVE))))
1787 {
1788 int skip = did_emsg || got_int || did_throw;
1789
1790 if (ea.cmdidx == CMD_catch)
1791 skip = !skip && !(cstack->cs_idx >= 0
1792 && (cstack->cs_flags[cstack->cs_idx] & CSF_THROWN)
1793 && !(cstack->cs_flags[cstack->cs_idx] & CSF_CAUGHT));
1794 else if (ea.cmdidx == CMD_else || ea.cmdidx == CMD_elseif)
1795 skip = skip || !(cstack->cs_idx >= 0
1796 && !(cstack->cs_flags[cstack->cs_idx]
1797 & (CSF_ACTIVE | CSF_TRUE)));
1798 else if (ea.cmdidx == CMD_finally)
1799 skip = FALSE;
1800 else if (ea.cmdidx != CMD_endif
1801 && ea.cmdidx != CMD_endfor
1802 && ea.cmdidx != CMD_endtry
1803 && ea.cmdidx != CMD_endwhile)
1804 skip = ea.skip;
1805
1806 if (!skip)
1807 {
1808 if (getline_equal(fgetline, cookie, get_func_line))
1809 func_line_exec(getline_cookie(fgetline, cookie));
1810 else if (getline_equal(fgetline, cookie, getsourceline))
1811 script_line_exec();
1812 }
1813 }
1814# endif
1815
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001816 // May go to debug mode. If this happens and the ">quit" debug command is
1817 // used, throw an interrupt exception and skip the next command.
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001818 dbg_check_breakpoint(&ea);
1819 if (!ea.skip && got_int)
1820 {
1821 ea.skip = TRUE;
1822 (void)do_intthrow(cstack);
1823 }
1824#endif
1825
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001826/*
1827 * 4. parse a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828 *
1829 * where 'addr' is:
1830 *
1831 * % (entire file)
1832 * $ [+-NUM]
1833 * 'x [+-NUM] (where x denotes a currently defined mark)
1834 * . [+-NUM]
1835 * [+-NUM]..
1836 * NUM
1837 *
1838 * The ea.cmd pointer is updated to point to the first character following the
1839 * range spec. If an initial address is found, but no second, the upper bound
1840 * is equal to the lower.
1841 */
1842
Bram Moolenaar25190db2019-05-04 15:05:28 +02001843 // ea.addr_type for user commands is set by find_ucmd
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001844 if (!IS_USER_CMDIDX(ea.cmdidx))
1845 {
1846 if (ea.cmdidx != CMD_SIZE)
1847 ea.addr_type = cmdnames[(int)ea.cmdidx].cmd_addr_type;
1848 else
1849 ea.addr_type = ADDR_LINES;
1850
Bram Moolenaar25190db2019-05-04 15:05:28 +02001851 // :wincmd range depends on the argument.
Bram Moolenaar164f3262015-01-14 21:22:01 +01001852 if (ea.cmdidx == CMD_wincmd && p != NULL)
1853 get_wincmd_addr_type(skipwhite(p), &ea);
Bram Moolenaar25190db2019-05-04 15:05:28 +02001854#ifdef FEAT_QUICKFIX
1855 // :.cc in quickfix window uses line number
1856 if ((ea.cmdidx == CMD_cc || ea.cmdidx == CMD_ll) && bt_quickfix(curbuf))
1857 ea.addr_type = ADDR_OTHER;
1858#endif
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001859 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001860
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001861 ea.cmd = cmd;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02001862 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaaree8415b2018-08-10 23:13:12 +02001863 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001864
Bram Moolenaar071d4272004-06-13 20:20:40 +00001865/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001866 * 5. Parse the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867 */
1868
1869 /*
1870 * Skip ':' and any white space
1871 */
1872 ea.cmd = skipwhite(ea.cmd);
1873 while (*ea.cmd == ':')
1874 ea.cmd = skipwhite(ea.cmd + 1);
1875
1876 /*
1877 * If we got a line, but no command, then go to the line.
1878 * If we find a '|' or '\n' we set ea.nextcmd.
1879 */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001880 if (*ea.cmd == NUL || *ea.cmd == '"'
Bram Moolenaar7a092242020-04-16 22:10:49 +02001881#ifdef FEAT_EVAL
Bram Moolenaara26b9702020-04-18 19:53:28 +02001882 || (*ea.cmd == '#' && ea.cmd[1] != '{'
1883 && !starts_with_colon && in_vim9script())
Bram Moolenaar7a092242020-04-16 22:10:49 +02001884#endif
1885 || (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886 {
1887 /*
1888 * strange vi behaviour:
1889 * ":3" jumps to line 3
1890 * ":3|..." prints line 3
1891 * ":|" prints current line
1892 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001893 if (ea.skip) // skip this if inside :if
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001895 if (*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896 {
1897 ea.cmdidx = CMD_print;
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001898 ea.argt = EX_RANGE+EX_COUNT+EX_TRLBAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001899 if ((errormsg = invalid_range(&ea)) == NULL)
1900 {
1901 correct_range(&ea);
1902 ex_print(&ea);
1903 }
1904 }
1905 else if (ea.addr_count != 0)
1906 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001907 if (ea.line2 > curbuf->b_ml.ml_line_count)
1908 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001909 // With '-' in 'cpoptions' a line number past the file is an
1910 // error, otherwise put it at the end of the file.
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001911 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
1912 ea.line2 = -1;
1913 else
1914 ea.line2 = curbuf->b_ml.ml_line_count;
1915 }
1916
1917 if (ea.line2 < 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001918 errormsg = _(e_invrange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001919 else
1920 {
1921 if (ea.line2 == 0)
1922 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923 else
1924 curwin->w_cursor.lnum = ea.line2;
1925 beginline(BL_SOL | BL_FIX);
1926 }
1927 }
1928 goto doend;
1929 }
1930
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001931 // If this looks like an undefined user command and there are CmdUndefined
1932 // autocommands defined, trigger the matching autocommands.
Bram Moolenaard5005162014-08-22 23:05:54 +02001933 if (p != NULL && ea.cmdidx == CMD_SIZE && !ea.skip
1934 && ASCII_ISUPPER(*ea.cmd)
1935 && has_cmdundefined())
1936 {
Bram Moolenaard5005162014-08-22 23:05:54 +02001937 int ret;
1938
Bram Moolenaar5a31b462014-08-23 14:16:20 +02001939 p = ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02001940 while (ASCII_ISALNUM(*p))
1941 ++p;
Bram Moolenaar158ea172020-06-18 17:28:39 +02001942 p = vim_strnsave(ea.cmd, p - ea.cmd);
Bram Moolenaard5005162014-08-22 23:05:54 +02001943 ret = apply_autocmds(EVENT_CMDUNDEFINED, p, p, TRUE, NULL);
1944 vim_free(p);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001945 // If the autocommands did something and didn't cause an error, try
1946 // finding the command again.
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001947 p = (ret
1948#ifdef FEAT_EVAL
1949 && !aborting()
Bram Moolenaard5005162014-08-22 23:05:54 +02001950#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001951 ) ? find_ex_command(&ea, NULL, NULL, NULL) : ea.cmd;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001952 }
Bram Moolenaard5005162014-08-22 23:05:54 +02001953
Bram Moolenaar071d4272004-06-13 20:20:40 +00001954 if (p == NULL)
1955 {
1956 if (!ea.skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001957 errormsg = _("E464: Ambiguous use of user-defined command");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001958 goto doend;
1959 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001960 // Check for wrong commands.
Bram Moolenaar6f9a4762017-06-22 20:39:17 +02001961 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78
1962 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001963 {
1964 errormsg = uc_fun_cmd();
1965 goto doend;
1966 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001967
Bram Moolenaar071d4272004-06-13 20:20:40 +00001968 if (ea.cmdidx == CMD_SIZE)
1969 {
1970 if (!ea.skip)
1971 {
1972 STRCPY(IObuff, _("E492: Not an editor command"));
1973 if (!sourcing)
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001974 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001975 // If the modifier was parsed OK the error must be in the
1976 // following command
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001977 if (after_modifier != NULL)
1978 append_command(after_modifier);
1979 else
1980 append_command(*cmdlinep);
1981 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001982 errormsg = (char *)IObuff;
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001983 did_emsg_syntax = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001984 }
1985 goto doend;
1986 }
1987
Bram Moolenaar958636c2014-10-21 20:01:58 +02001988 ni = (!IS_USER_CMDIDX(ea.cmdidx)
1989 && (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00001990#ifdef HAVE_EX_SCRIPT_NI
1991 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
1992#endif
1993 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001994
1995#ifndef FEAT_EVAL
1996 /*
1997 * When the expression evaluation is disabled, recognize the ":if" and
1998 * ":endif" commands and ignore everything in between it.
1999 */
2000 if (ea.cmdidx == CMD_if)
2001 ++if_level;
2002 if (if_level)
2003 {
2004 if (ea.cmdidx == CMD_endif)
2005 --if_level;
2006 goto doend;
2007 }
2008
2009#endif
2010
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002011 // forced commands
Bram Moolenaar196b3b02008-06-20 16:51:41 +00002012 if (*p == '!' && ea.cmdidx != CMD_substitute
2013 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002014 {
2015 ++p;
2016 ea.forceit = TRUE;
2017 }
2018 else
2019 ea.forceit = FALSE;
2020
2021/*
Bram Moolenaarb7316892019-05-01 18:08:42 +02002022 * 6. Parse arguments. Then check for errors.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002023 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02002024 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002025 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002026
2027 if (!ea.skip)
2028 {
2029#ifdef HAVE_SANDBOX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002030 if (sandbox != 0 && !(ea.argt & EX_SBOXOK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002031 {
Bram Moolenaar8c62a082019-02-08 14:34:10 +01002032 // Command not allowed in sandbox.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002033 errormsg = _(e_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002034 goto doend;
2035 }
2036#endif
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002037 if (restricted != 0 && (ea.argt & EX_RESTRICT))
Bram Moolenaar8c62a082019-02-08 14:34:10 +01002038 {
2039 errormsg = _("E981: Command not allowed in rvim");
2040 goto doend;
2041 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002042 if (!curbuf->b_p_ma && (ea.argt & EX_MODIFY))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002043 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002044 // Command not allowed in non-'modifiable' buffer
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002045 errormsg = _(e_modifiable);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046 goto doend;
2047 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002048
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002049 if (text_locked() && !(ea.argt & EX_CMDWIN)
Bram Moolenaar958636c2014-10-21 20:01:58 +02002050 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002051 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002052 // Command not allowed when editing the command line.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002053 errormsg = _(get_text_locked_msg());
Bram Moolenaar071d4272004-06-13 20:20:40 +00002054 goto doend;
2055 }
Bram Moolenaar379fb762018-08-30 15:58:28 +02002056
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002057 // Disallow editing another buffer when "curbuf_lock" is set.
2058 // Do allow ":checktime" (it is postponed).
2059 // Do allow ":edit" (check for an argument later).
2060 // Do allow ":file" with no arguments (check for an argument later).
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002061 if (!(ea.argt & EX_CMDWIN)
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002062 && ea.cmdidx != CMD_checktime
Bram Moolenaar379fb762018-08-30 15:58:28 +02002063 && ea.cmdidx != CMD_edit
2064 && ea.cmdidx != CMD_file
Bram Moolenaar958636c2014-10-21 20:01:58 +02002065 && !IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002066 && curbuf_locked())
2067 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002068
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002069 if (!ni && !(ea.argt & EX_RANGE) && ea.addr_count > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002070 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002071 // no range allowed
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002072 errormsg = _(e_norange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002073 goto doend;
2074 }
2075 }
2076
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002077 if (!ni && !(ea.argt & EX_BANG) && ea.forceit) // no <!> allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002078 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002079 errormsg = _(e_nobang);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002080 goto doend;
2081 }
2082
2083 /*
2084 * Don't complain about the range if it is not used
2085 * (could happen if line_count is accidentally set to 0).
2086 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002087 if (!ea.skip && !ni && (ea.argt & EX_RANGE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002088 {
2089 /*
2090 * If the range is backwards, ask for confirmation and, if given, swap
2091 * ea.line1 & ea.line2 so it's forwards again.
2092 * When global command is busy, don't ask, will fail below.
2093 */
2094 if (!global_busy && ea.line1 > ea.line2)
2095 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002096 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002097 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002098 if (sourcing || exmode_active)
2099 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002100 errormsg = _("E493: Backwards range given");
Bram Moolenaara5792f52005-11-23 21:25:05 +00002101 goto doend;
2102 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002103 if (ask_yesno((char_u *)
2104 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002105 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002106 }
2107 lnum = ea.line1;
2108 ea.line1 = ea.line2;
2109 ea.line2 = lnum;
2110 }
2111 if ((errormsg = invalid_range(&ea)) != NULL)
2112 goto doend;
2113 }
2114
Bram Moolenaarb7316892019-05-01 18:08:42 +02002115 if ((ea.addr_type == ADDR_OTHER) && ea.addr_count == 0)
2116 // default is 1, not cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002117 ea.line2 = 1;
2118
2119 correct_range(&ea);
2120
2121#ifdef FEAT_FOLDING
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002122 if (((ea.argt & EX_WHOLEFOLD) || ea.addr_count >= 2) && !global_busy
Bram Moolenaara3306952016-01-02 21:41:06 +01002123 && ea.addr_type == ADDR_LINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002125 // Put the first line at the start of a closed fold, put the last line
2126 // at the end of a closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002127 (void)hasFolding(ea.line1, &ea.line1, NULL);
2128 (void)hasFolding(ea.line2, NULL, &ea.line2);
2129 }
2130#endif
2131
2132#ifdef FEAT_QUICKFIX
2133 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002134 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135 * option here, so things like % get expanded.
2136 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002137 p = replace_makeprg(&ea, p, cmdlinep);
2138 if (p == NULL)
2139 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002140#endif
2141
2142 /*
2143 * Skip to start of argument.
2144 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2145 */
2146 if (ea.cmdidx == CMD_bang)
2147 ea.arg = p;
2148 else
2149 ea.arg = skipwhite(p);
2150
Bram Moolenaar379fb762018-08-30 15:58:28 +02002151 // ":file" cannot be run with an argument when "curbuf_lock" is set
2152 if (ea.cmdidx == CMD_file && *ea.arg != NUL && curbuf_locked())
2153 goto doend;
2154
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155 /*
2156 * Check for "++opt=val" argument.
2157 * Must be first, allow ":w ++enc=utf8 !cmd"
2158 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002159 if (ea.argt & EX_ARGOPT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2161 if (getargopt(&ea) == FAIL && !ni)
2162 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002163 errormsg = _(e_invarg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164 goto doend;
2165 }
2166
2167 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2168 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002169 if (*ea.arg == '>') // append
Bram Moolenaar071d4272004-06-13 20:20:40 +00002170 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002171 if (*++ea.arg != '>') // typed wrong
Bram Moolenaar071d4272004-06-13 20:20:40 +00002172 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002173 errormsg = _("E494: Use w or w>>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002174 goto doend;
2175 }
2176 ea.arg = skipwhite(ea.arg + 1);
2177 ea.append = TRUE;
2178 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002179 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) // :w !filter
Bram Moolenaar071d4272004-06-13 20:20:40 +00002180 {
2181 ++ea.arg;
2182 ea.usefilter = TRUE;
2183 }
2184 }
2185
2186 if (ea.cmdidx == CMD_read)
2187 {
2188 if (ea.forceit)
2189 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002190 ea.usefilter = TRUE; // :r! filter if ea.forceit
Bram Moolenaar071d4272004-06-13 20:20:40 +00002191 ea.forceit = FALSE;
2192 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002193 else if (*ea.arg == '!') // :r !filter
Bram Moolenaar071d4272004-06-13 20:20:40 +00002194 {
2195 ++ea.arg;
2196 ea.usefilter = TRUE;
2197 }
2198 }
2199
2200 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2201 {
2202 ea.amount = 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002203 while (*ea.arg == *ea.cmd) // count number of '>' or '<'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002204 {
2205 ++ea.arg;
2206 ++ea.amount;
2207 }
2208 ea.arg = skipwhite(ea.arg);
2209 }
2210
2211 /*
2212 * Check for "+command" argument, before checking for next command.
2213 * Don't do this for ":read !cmd" and ":write !cmd".
2214 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002215 if ((ea.argt & EX_CMDARG) && !ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002216 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2217
2218 /*
2219 * Check for '|' to separate commands and '"' to start comments.
2220 * Don't do this for ":read !cmd" and ":write !cmd".
2221 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002222 if ((ea.argt & EX_TRLBAR) && !ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223 separate_nextcmd(&ea);
2224
2225 /*
2226 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002227 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2228 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002229 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002230 else if (ea.cmdidx == CMD_bang
Bram Moolenaar67883b42017-07-27 22:57:00 +02002231 || ea.cmdidx == CMD_terminal
Bram Moolenaardf177f62005-02-22 08:39:57 +00002232 || ea.cmdidx == CMD_global
2233 || ea.cmdidx == CMD_vglobal
2234 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002235 {
2236 for (p = ea.arg; *p; ++p)
2237 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002238 // Remove one backslash before a newline, so that it's possible to
2239 // pass a newline to the shell and also a newline that is preceded
2240 // with a backslash. This makes it impossible to end a shell
2241 // command in a backslash, but that doesn't appear useful.
2242 // Halving the number of backslashes is incompatible with previous
2243 // versions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002244 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002245 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002246 else if (*p == '\n')
2247 {
2248 ea.nextcmd = p + 1;
2249 *p = NUL;
2250 break;
2251 }
2252 }
2253 }
2254
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002255 if ((ea.argt & EX_DFLALL) && ea.addr_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002256 {
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002257 buf_T *buf;
2258
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259 ea.line1 = 1;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002260 switch (ea.addr_type)
2261 {
2262 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02002263 case ADDR_OTHER:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002264 ea.line2 = curbuf->b_ml.ml_line_count;
2265 break;
2266 case ADDR_LOADED_BUFFERS:
2267 buf = firstbuf;
2268 while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL)
2269 buf = buf->b_next;
2270 ea.line1 = buf->b_fnum;
2271 buf = lastbuf;
2272 while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL)
2273 buf = buf->b_prev;
2274 ea.line2 = buf->b_fnum;
2275 break;
2276 case ADDR_BUFFERS:
2277 ea.line1 = firstbuf->b_fnum;
2278 ea.line2 = lastbuf->b_fnum;
2279 break;
2280 case ADDR_WINDOWS:
2281 ea.line2 = LAST_WIN_NR;
2282 break;
2283 case ADDR_TABS:
2284 ea.line2 = LAST_TAB_NR;
2285 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01002286 case ADDR_TABS_RELATIVE:
2287 ea.line2 = 1;
2288 break;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002289 case ADDR_ARGUMENTS:
2290 if (ARGCOUNT == 0)
2291 ea.line1 = ea.line2 = 0;
2292 else
2293 ea.line2 = ARGCOUNT;
2294 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02002295 case ADDR_QUICKFIX_VALID:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002296#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02002297 ea.line2 = qf_get_valid_size(&ea);
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002298 if (ea.line2 == 0)
2299 ea.line2 = 1;
Bram Moolenaare906c502015-09-09 21:10:39 +02002300#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002301 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02002302 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02002303 case ADDR_UNSIGNED:
2304 case ADDR_QUICKFIX:
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002305 iemsg(_("INTERNAL: Cannot use EX_DFLALL with ADDR_NONE, ADDR_UNSIGNED or ADDR_QUICKFIX"));
Bram Moolenaarb7316892019-05-01 18:08:42 +02002306 break;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002307 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308 }
2309
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002310 // accept numbered register only when no count allowed (:put)
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002311 if ( (ea.argt & EX_REGSTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312 && *ea.arg != NUL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002313 // Do not allow register = for user commands
Bram Moolenaar958636c2014-10-21 20:01:58 +02002314 && (!IS_USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002315 && !((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002316 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002317#ifndef FEAT_CLIPBOARD
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002318 // check these explicitly for a more specific error message
Bram Moolenaar85de2062011-05-05 14:26:41 +02002319 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002320 {
Bram Moolenaar99b12722019-01-14 20:16:40 +01002321 errormsg = _(e_invalidreg);
Bram Moolenaar85de2062011-05-05 14:26:41 +02002322 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323 }
2324#endif
Bram Moolenaar958636c2014-10-21 20:01:58 +02002325 if (valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2326 && !IS_USER_CMDIDX(ea.cmdidx))))
Bram Moolenaar85de2062011-05-05 14:26:41 +02002327 {
2328 ea.regname = *ea.arg++;
2329#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002330 // for '=' register: accept the rest of the line as an expression
Bram Moolenaar85de2062011-05-05 14:26:41 +02002331 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2332 {
2333 set_expr_line(vim_strsave(ea.arg));
2334 ea.arg += STRLEN(ea.arg);
2335 }
2336#endif
2337 ea.arg = skipwhite(ea.arg);
2338 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002339 }
2340
2341 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002342 * Check for a count. When accepting a EX_BUFNAME, don't use "123foo" as a
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343 * count, it's a buffer name.
2344 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002345 if ((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)
2346 && (!(ea.argt & EX_BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01002347 || VIM_ISWHITE(*p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348 {
2349 n = getdigits(&ea.arg);
2350 ea.arg = skipwhite(ea.arg);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002351 if (n <= 0 && !ni && (ea.argt & EX_ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002353 errormsg = _(e_zerocount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002354 goto doend;
2355 }
Bram Moolenaarb7316892019-05-01 18:08:42 +02002356 if (ea.addr_type != ADDR_LINES) // e.g. :buffer 2, :sleep 3
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357 {
2358 ea.line2 = n;
2359 if (ea.addr_count == 0)
2360 ea.addr_count = 1;
2361 }
2362 else
2363 {
2364 ea.line1 = ea.line2;
2365 ea.line2 += n - 1;
2366 ++ea.addr_count;
2367 /*
2368 * Be vi compatible: no error message for out of range.
2369 */
Bram Moolenaarb7316892019-05-01 18:08:42 +02002370 if (ea.line2 > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371 ea.line2 = curbuf->b_ml.ml_line_count;
2372 }
2373 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002374
2375 /*
2376 * Check for flags: 'l', 'p' and '#'.
2377 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002378 if (ea.argt & EX_FLAGS)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002379 get_flags(&ea);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002380 if (!ni && !(ea.argt & EX_EXTRA) && *ea.arg != NUL
2381 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & EX_TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002382 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002383 // no arguments allowed but there is something
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002384 errormsg = _(e_trailing);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385 goto doend;
2386 }
2387
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002388 if (!ni && (ea.argt & EX_NEEDARG) && *ea.arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002390 errormsg = _(e_argreq);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391 goto doend;
2392 }
2393
2394#ifdef FEAT_EVAL
2395 /*
2396 * Skip the command when it's not going to be executed.
2397 * The commands like :if, :endif, etc. always need to be executed.
2398 * Also make an exception for commands that handle a trailing command
2399 * themselves.
2400 */
2401 if (ea.skip)
2402 {
2403 switch (ea.cmdidx)
2404 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002405 // commands that need evaluation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406 case CMD_while:
2407 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002408 case CMD_for:
2409 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410 case CMD_if:
2411 case CMD_elseif:
2412 case CMD_else:
2413 case CMD_endif:
2414 case CMD_try:
2415 case CMD_catch:
2416 case CMD_finally:
2417 case CMD_endtry:
2418 case CMD_function:
Bram Moolenaarab55c682020-03-01 19:41:43 +01002419 case CMD_def:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420 break;
2421
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002422 // Commands that handle '|' themselves. Check: A command should
2423 // either have the EX_TRLBAR flag, appear in this list or appear in
2424 // the list at ":help :bar".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425 case CMD_aboveleft:
2426 case CMD_and:
2427 case CMD_belowright:
2428 case CMD_botright:
2429 case CMD_browse:
2430 case CMD_call:
2431 case CMD_confirm:
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01002432 case CMD_const:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433 case CMD_delfunction:
2434 case CMD_djump:
2435 case CMD_dlist:
2436 case CMD_dsearch:
2437 case CMD_dsplit:
2438 case CMD_echo:
2439 case CMD_echoerr:
2440 case CMD_echomsg:
2441 case CMD_echon:
Bram Moolenaara76b3152020-02-16 16:20:21 +01002442 case CMD_eval:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443 case CMD_execute:
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002444 case CMD_filter:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002445 case CMD_help:
2446 case CMD_hide:
2447 case CMD_ijump:
2448 case CMD_ilist:
2449 case CMD_isearch:
2450 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002451 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002452 case CMD_keepjumps:
2453 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01002454 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002455 case CMD_leftabove:
2456 case CMD_let:
2457 case CMD_lockmarks:
Bram Moolenaarcc4423a2019-11-26 17:05:00 +01002458 case CMD_lockvar:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002459 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002460 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002461 case CMD_mzscheme:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01002462 case CMD_noautocmd:
2463 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002464 case CMD_perl:
2465 case CMD_psearch:
2466 case CMD_python:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002467 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002468 case CMD_python3:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469 case CMD_return:
2470 case CMD_rightbelow:
2471 case CMD_ruby:
2472 case CMD_silent:
2473 case CMD_smagic:
2474 case CMD_snomagic:
2475 case CMD_substitute:
2476 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002477 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002478 case CMD_tcl:
2479 case CMD_throw:
2480 case CMD_tilde:
2481 case CMD_topleft:
2482 case CMD_unlet:
Bram Moolenaarcc4423a2019-11-26 17:05:00 +01002483 case CMD_unlockvar:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484 case CMD_verbose:
2485 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002486 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487 break;
2488
2489 default: goto doend;
2490 }
2491 }
2492#endif
2493
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002494 if (ea.argt & EX_XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495 {
2496 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2497 goto doend;
2498 }
2499
Bram Moolenaar071d4272004-06-13 20:20:40 +00002500 /*
2501 * Accept buffer name. Cannot be used at the same time with a buffer
2502 * number. Don't do this for a user command.
2503 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002504 if ((ea.argt & EX_BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
Bram Moolenaar958636c2014-10-21 20:01:58 +02002505 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002506 {
2507 /*
2508 * :bdelete, :bwipeout and :bunload take several arguments, separated
2509 * by spaces: find next space (skipping over escaped characters).
2510 * The others take one argument: ignore trailing spaces.
2511 */
2512 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2513 || ea.cmdidx == CMD_bunload)
2514 p = skiptowhite_esc(ea.arg);
2515 else
2516 {
2517 p = ea.arg + STRLEN(ea.arg);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002518 while (p > ea.arg && VIM_ISWHITE(p[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002519 --p;
2520 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002521 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & EX_BUFUNL) != 0,
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002522 FALSE, FALSE);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002523 if (ea.line2 < 0) // failed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524 goto doend;
2525 ea.addr_count = 1;
2526 ea.arg = skipwhite(p);
2527 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002528
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002529 // The :try command saves the emsg_silent flag, reset it here when
2530 // ":silent! try" was used, it should only apply to :try itself.
Bram Moolenaareffed932018-08-14 13:38:17 +02002531 if (ea.cmdidx == CMD_try && ea.did_esilent > 0)
Bram Moolenaar2be57332018-02-13 18:05:18 +01002532 {
Bram Moolenaareffed932018-08-14 13:38:17 +02002533 emsg_silent -= ea.did_esilent;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002534 if (emsg_silent < 0)
2535 emsg_silent = 0;
Bram Moolenaareffed932018-08-14 13:38:17 +02002536 ea.did_esilent = 0;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002537 }
2538
Bram Moolenaar071d4272004-06-13 20:20:40 +00002539/*
Bram Moolenaar2be57332018-02-13 18:05:18 +01002540 * 7. Execute the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002541 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542
Bram Moolenaar958636c2014-10-21 20:01:58 +02002543 if (IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544 {
2545 /*
2546 * Execute a user-defined command.
2547 */
2548 do_ucmd(&ea);
2549 }
2550 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002551 {
2552 /*
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002553 * Call the function to execute the builtin command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554 */
2555 ea.errmsg = NULL;
2556 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2557 if (ea.errmsg != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002558 errormsg = _(ea.errmsg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002559 }
2560
2561#ifdef FEAT_EVAL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002562 // Set flag that any command was executed, used by ex_vim9script().
Bram Moolenaar16531552020-02-08 16:00:46 +01002563 if (getline_equal(ea.getline, ea.cookie, getsourceline)
2564 && current_sctx.sc_sid > 0)
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002565 SCRIPT_ITEM(current_sctx.sc_sid)->sn_had_command = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002566
Bram Moolenaar071d4272004-06-13 20:20:40 +00002567 /*
2568 * If the command just executed called do_cmdline(), any throw or ":return"
2569 * or ":finish" encountered there must also check the cstack of the still
2570 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2571 * exception, or reanimate a returned function or finished script file and
2572 * return or finish it again.
2573 */
2574 if (need_rethrow)
2575 do_throw(cstack);
2576 else if (check_cstack)
2577 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002578 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002579 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002580 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002581 && current_func_returned())
2582 do_return(&ea, TRUE, FALSE, NULL);
2583 }
2584 need_rethrow = check_cstack = FALSE;
2585#endif
2586
2587doend:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002588 if (curwin->w_cursor.lnum == 0) // can happen with zero line number
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002589 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002590 curwin->w_cursor.lnum = 1;
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002591 curwin->w_cursor.col = 0;
2592 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593
2594 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2595 {
2596 if (sourcing)
2597 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002598 if (errormsg != (char *)IObuff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599 {
2600 STRCPY(IObuff, errormsg);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002601 errormsg = (char *)IObuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002603 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604 }
2605 emsg(errormsg);
2606 }
2607#ifdef FEAT_EVAL
2608 do_errthrow(cstack,
Bram Moolenaar958636c2014-10-21 20:01:58 +02002609 (ea.cmdidx != CMD_SIZE && !IS_USER_CMDIDX(ea.cmdidx))
2610 ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002611#endif
2612
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002613 undo_cmdmod(&ea, save_msg_scroll);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002614 cmdmod = save_cmdmod;
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01002615 reg_executing = save_reg_executing;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002616
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002617#ifdef HAVE_SANDBOX
Bram Moolenaareffed932018-08-14 13:38:17 +02002618 if (ea.did_sandbox)
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002619 --sandbox;
2620#endif
2621
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002622 if (ea.nextcmd && *ea.nextcmd == NUL) // not really a next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00002623 ea.nextcmd = NULL;
2624
2625#ifdef FEAT_EVAL
2626 --ex_nesting_level;
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002627 vim_free(ea.cmdline_tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002628#endif
2629
2630 return ea.nextcmd;
2631}
2632#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00002633 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002634#endif
2635
2636/*
Bram Moolenaareffed932018-08-14 13:38:17 +02002637 * Parse and skip over command modifiers:
2638 * - update eap->cmd
2639 * - store flags in "cmdmod".
2640 * - Set ex_pressedreturn for an empty command line.
2641 * - set msg_silent for ":silent"
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002642 * - set 'eventignore' to "all" for ":noautocmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002643 * - set p_verbose for ":verbose"
2644 * - Increment "sandbox" for ":sandbox"
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002645 * When "skip_only" is TRUE the global variables are not changed, except for
2646 * "cmdmod".
Bram Moolenaareffed932018-08-14 13:38:17 +02002647 * Return FAIL when the command is not to be executed.
2648 * May set "errormsg" to an error message.
2649 */
2650 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002651parse_command_modifiers(exarg_T *eap, char **errormsg, int skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002652{
2653 char_u *p;
2654
Bram Moolenaara80faa82020-04-12 19:37:17 +02002655 CLEAR_FIELD(cmdmod);
Bram Moolenaareffed932018-08-14 13:38:17 +02002656 eap->verbose_save = -1;
2657 eap->save_msg_silent = -1;
2658
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002659 // Repeat until no more command modifiers are found.
Bram Moolenaareffed932018-08-14 13:38:17 +02002660 for (;;)
2661 {
Bram Moolenaareffed932018-08-14 13:38:17 +02002662 while (*eap->cmd == ' ' || *eap->cmd == '\t' || *eap->cmd == ':')
2663 ++eap->cmd;
2664
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002665 // in ex mode, an empty line works like :+
Bram Moolenaareffed932018-08-14 13:38:17 +02002666 if (*eap->cmd == NUL && exmode_active
2667 && (getline_equal(eap->getline, eap->cookie, getexmodeline)
2668 || getline_equal(eap->getline, eap->cookie, getexline))
2669 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2670 {
2671 eap->cmd = (char_u *)"+";
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002672 if (!skip_only)
2673 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002674 }
2675
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002676 // ignore comment and empty lines
Bram Moolenaareffed932018-08-14 13:38:17 +02002677 if (*eap->cmd == '"')
2678 return FAIL;
2679 if (*eap->cmd == NUL)
2680 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002681 if (!skip_only)
2682 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002683 return FAIL;
2684 }
2685
Bram Moolenaareffed932018-08-14 13:38:17 +02002686 p = skip_range(eap->cmd, NULL);
2687 switch (*p)
2688 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002689 // When adding an entry, also modify cmd_exists().
Bram Moolenaareffed932018-08-14 13:38:17 +02002690 case 'a': if (!checkforcmd(&eap->cmd, "aboveleft", 3))
2691 break;
2692 cmdmod.split |= WSP_ABOVE;
2693 continue;
2694
2695 case 'b': if (checkforcmd(&eap->cmd, "belowright", 3))
2696 {
2697 cmdmod.split |= WSP_BELOW;
2698 continue;
2699 }
2700 if (checkforcmd(&eap->cmd, "browse", 3))
2701 {
2702#ifdef FEAT_BROWSE_CMD
2703 cmdmod.browse = TRUE;
2704#endif
2705 continue;
2706 }
2707 if (!checkforcmd(&eap->cmd, "botright", 2))
2708 break;
2709 cmdmod.split |= WSP_BOT;
2710 continue;
2711
2712 case 'c': if (!checkforcmd(&eap->cmd, "confirm", 4))
2713 break;
2714#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2715 cmdmod.confirm = TRUE;
2716#endif
2717 continue;
2718
2719 case 'k': if (checkforcmd(&eap->cmd, "keepmarks", 3))
2720 {
2721 cmdmod.keepmarks = TRUE;
2722 continue;
2723 }
2724 if (checkforcmd(&eap->cmd, "keepalt", 5))
2725 {
2726 cmdmod.keepalt = TRUE;
2727 continue;
2728 }
2729 if (checkforcmd(&eap->cmd, "keeppatterns", 5))
2730 {
2731 cmdmod.keeppatterns = TRUE;
2732 continue;
2733 }
2734 if (!checkforcmd(&eap->cmd, "keepjumps", 5))
2735 break;
2736 cmdmod.keepjumps = TRUE;
2737 continue;
2738
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002739 case 'f': // only accept ":filter {pat} cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002740 {
2741 char_u *reg_pat;
2742
2743 if (!checkforcmd(&p, "filter", 4)
2744 || *p == NUL || ends_excmd(*p))
2745 break;
2746 if (*p == '!')
2747 {
2748 cmdmod.filter_force = TRUE;
2749 p = skipwhite(p + 1);
2750 if (*p == NUL || ends_excmd(*p))
2751 break;
2752 }
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002753 if (skip_only)
2754 p = skip_vimgrep_pat(p, NULL, NULL);
2755 else
2756 // NOTE: This puts a NUL after the pattern.
2757 p = skip_vimgrep_pat(p, &reg_pat, NULL);
Bram Moolenaareffed932018-08-14 13:38:17 +02002758 if (p == NULL || *p == NUL)
2759 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002760 if (!skip_only)
2761 {
2762 cmdmod.filter_regmatch.regprog =
Bram Moolenaareffed932018-08-14 13:38:17 +02002763 vim_regcomp(reg_pat, RE_MAGIC);
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002764 if (cmdmod.filter_regmatch.regprog == NULL)
2765 break;
2766 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002767 eap->cmd = p;
2768 continue;
2769 }
2770
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002771 // ":hide" and ":hide | cmd" are not modifiers
Bram Moolenaareffed932018-08-14 13:38:17 +02002772 case 'h': if (p != eap->cmd || !checkforcmd(&p, "hide", 3)
2773 || *p == NUL || ends_excmd(*p))
2774 break;
2775 eap->cmd = p;
2776 cmdmod.hide = TRUE;
2777 continue;
2778
2779 case 'l': if (checkforcmd(&eap->cmd, "lockmarks", 3))
2780 {
2781 cmdmod.lockmarks = TRUE;
2782 continue;
2783 }
2784
2785 if (!checkforcmd(&eap->cmd, "leftabove", 5))
2786 break;
2787 cmdmod.split |= WSP_ABOVE;
2788 continue;
2789
2790 case 'n': if (checkforcmd(&eap->cmd, "noautocmd", 3))
2791 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002792 if (cmdmod.save_ei == NULL && !skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002793 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002794 // Set 'eventignore' to "all". Restore the
2795 // existing option value later.
Bram Moolenaareffed932018-08-14 13:38:17 +02002796 cmdmod.save_ei = vim_strsave(p_ei);
2797 set_string_option_direct((char_u *)"ei", -1,
2798 (char_u *)"all", OPT_FREE, SID_NONE);
2799 }
2800 continue;
2801 }
2802 if (!checkforcmd(&eap->cmd, "noswapfile", 3))
2803 break;
2804 cmdmod.noswapfile = TRUE;
2805 continue;
2806
2807 case 'r': if (!checkforcmd(&eap->cmd, "rightbelow", 6))
2808 break;
2809 cmdmod.split |= WSP_BELOW;
2810 continue;
2811
2812 case 's': if (checkforcmd(&eap->cmd, "sandbox", 3))
2813 {
2814#ifdef HAVE_SANDBOX
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002815 if (!skip_only)
2816 {
2817 if (!eap->did_sandbox)
2818 ++sandbox;
2819 eap->did_sandbox = TRUE;
2820 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002821#endif
2822 continue;
2823 }
2824 if (!checkforcmd(&eap->cmd, "silent", 3))
2825 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002826 if (!skip_only)
2827 {
2828 if (eap->save_msg_silent == -1)
2829 eap->save_msg_silent = msg_silent;
2830 ++msg_silent;
2831 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002832 if (*eap->cmd == '!' && !VIM_ISWHITE(eap->cmd[-1]))
2833 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002834 // ":silent!", but not "silent !cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002835 eap->cmd = skipwhite(eap->cmd + 1);
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002836 if (!skip_only)
2837 {
2838 ++emsg_silent;
2839 ++eap->did_esilent;
2840 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002841 }
2842 continue;
2843
2844 case 't': if (checkforcmd(&p, "tab", 3))
2845 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002846 if (!skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002847 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002848 long tabnr = get_address(eap, &eap->cmd,
2849 ADDR_TABS, eap->skip,
2850 skip_only, FALSE, 1);
2851 if (tabnr == MAXLNUM)
2852 cmdmod.tab = tabpage_index(curtab) + 1;
2853 else
Bram Moolenaareffed932018-08-14 13:38:17 +02002854 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002855 if (tabnr < 0 || tabnr > LAST_TAB_NR)
2856 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002857 *errormsg = _(e_invrange);
Bram Moolenaar548e5982018-12-26 21:45:00 +01002858 return FAIL;
2859 }
2860 cmdmod.tab = tabnr + 1;
Bram Moolenaareffed932018-08-14 13:38:17 +02002861 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002862 }
2863 eap->cmd = p;
2864 continue;
2865 }
2866 if (!checkforcmd(&eap->cmd, "topleft", 2))
2867 break;
2868 cmdmod.split |= WSP_TOP;
2869 continue;
2870
2871 case 'u': if (!checkforcmd(&eap->cmd, "unsilent", 3))
2872 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002873 if (!skip_only)
2874 {
2875 if (eap->save_msg_silent == -1)
2876 eap->save_msg_silent = msg_silent;
2877 msg_silent = 0;
2878 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002879 continue;
2880
2881 case 'v': if (checkforcmd(&eap->cmd, "vertical", 4))
2882 {
2883 cmdmod.split |= WSP_VERT;
2884 continue;
2885 }
2886 if (!checkforcmd(&p, "verbose", 4))
2887 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002888 if (!skip_only)
2889 {
2890 if (eap->verbose_save < 0)
2891 eap->verbose_save = p_verbose;
2892 if (vim_isdigit(*eap->cmd))
2893 p_verbose = atoi((char *)eap->cmd);
2894 else
2895 p_verbose = 1;
2896 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002897 eap->cmd = p;
2898 continue;
2899 }
2900 break;
2901 }
2902
2903 return OK;
2904}
2905
2906/*
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002907 * Unod and free contents of "cmdmod".
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002908 */
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002909 void
2910undo_cmdmod(exarg_T *eap, int save_msg_scroll)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002911{
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002912 if (eap->verbose_save >= 0)
2913 p_verbose = eap->verbose_save;
2914
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002915 if (cmdmod.save_ei != NULL)
2916 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002917 // Restore 'eventignore' to the value before ":noautocmd".
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002918 set_string_option_direct((char_u *)"ei", -1, cmdmod.save_ei,
2919 OPT_FREE, SID_NONE);
2920 free_string_option(cmdmod.save_ei);
2921 }
2922
2923 if (cmdmod.filter_regmatch.regprog != NULL)
2924 vim_regfree(cmdmod.filter_regmatch.regprog);
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002925
2926 if (eap->save_msg_silent != -1)
2927 {
2928 // messages could be enabled for a serious error, need to check if the
2929 // counters don't become negative
2930 if (!did_emsg || msg_silent > eap->save_msg_silent)
2931 msg_silent = eap->save_msg_silent;
2932 emsg_silent -= eap->did_esilent;
2933 if (emsg_silent < 0)
2934 emsg_silent = 0;
2935 // Restore msg_scroll, it's set by file I/O commands, even when no
2936 // message is actually displayed.
2937 msg_scroll = save_msg_scroll;
2938
2939 // "silent reg" or "silent echo x" inside "redir" leaves msg_col
2940 // somewhere in the line. Put it back in the first column.
2941 if (redirecting())
2942 msg_col = 0;
2943 }
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002944}
2945
2946/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002947 * Parse the address range, if any, in "eap".
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02002948 * May set the last search pattern, unless "silent" is TRUE.
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002949 * Return FAIL and set "errormsg" or return OK.
2950 */
2951 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002952parse_cmd_address(exarg_T *eap, char **errormsg, int silent)
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002953{
2954 int address_count = 1;
2955 linenr_T lnum;
2956
2957 // Repeat for all ',' or ';' separated addresses.
2958 for (;;)
2959 {
2960 eap->line1 = eap->line2;
2961 switch (eap->addr_type)
2962 {
2963 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02002964 case ADDR_OTHER:
Bram Moolenaarb5383b12020-05-18 19:46:48 +02002965 // Default is the cursor line number. Avoid using an invalid
2966 // line number though.
2967 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
2968 eap->line2 = curbuf->b_ml.ml_line_count;
2969 else
2970 eap->line2 = curwin->w_cursor.lnum;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002971 break;
2972 case ADDR_WINDOWS:
2973 eap->line2 = CURRENT_WIN_NR;
2974 break;
2975 case ADDR_ARGUMENTS:
2976 eap->line2 = curwin->w_arg_idx + 1;
2977 if (eap->line2 > ARGCOUNT)
2978 eap->line2 = ARGCOUNT;
2979 break;
2980 case ADDR_LOADED_BUFFERS:
2981 case ADDR_BUFFERS:
2982 eap->line2 = curbuf->b_fnum;
2983 break;
2984 case ADDR_TABS:
2985 eap->line2 = CURRENT_TAB_NR;
2986 break;
2987 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02002988 case ADDR_UNSIGNED:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002989 eap->line2 = 1;
2990 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002991 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002992#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02002993 eap->line2 = qf_get_cur_idx(eap);
2994#endif
2995 break;
2996 case ADDR_QUICKFIX_VALID:
2997#ifdef FEAT_QUICKFIX
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002998 eap->line2 = qf_get_cur_valid_idx(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002999#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003000 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003001 case ADDR_NONE:
3002 // Will give an error later if a range is found.
3003 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003004 }
3005 eap->cmd = skipwhite(eap->cmd);
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003006 lnum = get_address(eap, &eap->cmd, eap->addr_type, eap->skip, silent,
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003007 eap->addr_count == 0, address_count++);
3008 if (eap->cmd == NULL) // error detected
3009 return FAIL;
3010 if (lnum == MAXLNUM)
3011 {
3012 if (*eap->cmd == '%') // '%' - all lines
3013 {
3014 ++eap->cmd;
3015 switch (eap->addr_type)
3016 {
3017 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003018 case ADDR_OTHER:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003019 eap->line1 = 1;
3020 eap->line2 = curbuf->b_ml.ml_line_count;
3021 break;
3022 case ADDR_LOADED_BUFFERS:
3023 {
3024 buf_T *buf = firstbuf;
3025
3026 while (buf->b_next != NULL
3027 && buf->b_ml.ml_mfp == NULL)
3028 buf = buf->b_next;
3029 eap->line1 = buf->b_fnum;
3030 buf = lastbuf;
3031 while (buf->b_prev != NULL
3032 && buf->b_ml.ml_mfp == NULL)
3033 buf = buf->b_prev;
3034 eap->line2 = buf->b_fnum;
3035 break;
3036 }
3037 case ADDR_BUFFERS:
3038 eap->line1 = firstbuf->b_fnum;
3039 eap->line2 = lastbuf->b_fnum;
3040 break;
3041 case ADDR_WINDOWS:
3042 case ADDR_TABS:
3043 if (IS_USER_CMDIDX(eap->cmdidx))
3044 {
3045 eap->line1 = 1;
3046 eap->line2 = eap->addr_type == ADDR_WINDOWS
3047 ? LAST_WIN_NR : LAST_TAB_NR;
3048 }
3049 else
3050 {
3051 // there is no Vim command which uses '%' and
3052 // ADDR_WINDOWS or ADDR_TABS
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003053 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003054 return FAIL;
3055 }
3056 break;
3057 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003058 case ADDR_UNSIGNED:
3059 case ADDR_QUICKFIX:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003060 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003061 return FAIL;
3062 case ADDR_ARGUMENTS:
3063 if (ARGCOUNT == 0)
3064 eap->line1 = eap->line2 = 0;
3065 else
3066 {
3067 eap->line1 = 1;
3068 eap->line2 = ARGCOUNT;
3069 }
3070 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003071 case ADDR_QUICKFIX_VALID:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003072#ifdef FEAT_QUICKFIX
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003073 eap->line1 = 1;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003074 eap->line2 = qf_get_valid_size(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003075 if (eap->line2 == 0)
3076 eap->line2 = 1;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003077#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003078 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003079 case ADDR_NONE:
3080 // Will give an error later if a range is found.
3081 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003082 }
3083 ++eap->addr_count;
3084 }
3085 else if (*eap->cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
3086 {
3087 pos_T *fp;
3088
3089 // '*' - visual area
3090 if (eap->addr_type != ADDR_LINES)
3091 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003092 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003093 return FAIL;
3094 }
3095
3096 ++eap->cmd;
3097 if (!eap->skip)
3098 {
3099 fp = getmark('<', FALSE);
3100 if (check_mark(fp) == FAIL)
3101 return FAIL;
3102 eap->line1 = fp->lnum;
3103 fp = getmark('>', FALSE);
3104 if (check_mark(fp) == FAIL)
3105 return FAIL;
3106 eap->line2 = fp->lnum;
3107 ++eap->addr_count;
3108 }
3109 }
3110 }
3111 else
3112 eap->line2 = lnum;
3113 eap->addr_count++;
3114
3115 if (*eap->cmd == ';')
3116 {
3117 if (!eap->skip)
3118 {
3119 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar0e717042020-04-27 19:29:01 +02003120 // Don't leave the cursor on an illegal line or column, but do
3121 // accept zero as address, so 0;/PATTERN/ works correctly.
3122 if (eap->line2 > 0)
3123 check_cursor();
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003124 }
3125 }
3126 else if (*eap->cmd != ',')
3127 break;
3128 ++eap->cmd;
3129 }
3130
3131 // One address given: set start and end lines.
3132 if (eap->addr_count == 1)
3133 {
3134 eap->line1 = eap->line2;
3135 // ... but only implicit: really no address given
3136 if (lnum == MAXLNUM)
3137 eap->addr_count = 0;
3138 }
3139 return OK;
3140}
3141
3142/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003143 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003144 * If there is a match advance "pp" to the argument and return TRUE.
3145 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003146 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003147checkforcmd(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003148 char_u **pp, // start of command
3149 char *cmd, // name of command
3150 int len) // required length
Bram Moolenaar071d4272004-06-13 20:20:40 +00003151{
3152 int i;
3153
3154 for (i = 0; cmd[i] != NUL; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003155 if (((char_u *)cmd)[i] != (*pp)[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156 break;
3157 if (i >= len && !isalpha((*pp)[i]))
3158 {
3159 *pp = skipwhite(*pp + i);
3160 return TRUE;
3161 }
3162 return FALSE;
3163}
3164
3165/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003166 * Append "cmd" to the error message in IObuff.
3167 * Takes care of limiting the length and handling 0xa0, which would be
3168 * invisible otherwise.
3169 */
3170 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003171append_command(char_u *cmd)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003172{
3173 char_u *s = cmd;
3174 char_u *d;
3175
3176 STRCAT(IObuff, ": ");
3177 d = IObuff + STRLEN(IObuff);
3178 while (*s != NUL && d - IObuff < IOSIZE - 7)
3179 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003180 if (enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) : *s == 0xa0)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003181 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003182 s += enc_utf8 ? 2 : 1;
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003183 STRCPY(d, "<a0>");
3184 d += 4;
3185 }
3186 else
3187 MB_COPY_CHAR(s, d);
3188 }
3189 *d = NUL;
3190}
3191
3192/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003194 * Start of the name can be found at eap->cmd.
Bram Moolenaar25190db2019-05-04 15:05:28 +02003195 * Sets eap->cmdidx and returns a pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003196 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003197 *
3198 * If "lookup" is not NULL recognize expression without "eval" or "call" and
3199 * assignment without "let". Sets eap->cmdidx to the command while returning
3200 * "eap->cmd".
3201 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003202 * Returns NULL for an ambiguous user command.
3203 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003204 char_u *
3205find_ex_command(
3206 exarg_T *eap,
Bram Moolenaara494f562020-04-18 17:45:38 +02003207 int *full UNUSED,
Bram Moolenaarb84a3812020-05-01 15:44:29 +02003208 void *(*lookup)(char_u *, size_t, cctx_T *) UNUSED,
Bram Moolenaara494f562020-04-18 17:45:38 +02003209 cctx_T *cctx UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003210{
3211 int len;
3212 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003213 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003214
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003215#ifdef FEAT_EVAL
3216 /*
3217 * Recognize a Vim9 script function/method call and assignment:
3218 * "lvar = value", "lvar(arg)", "[1, 2 3]->Func()"
3219 */
Bram Moolenaar0c6ceaf2020-02-22 18:36:32 +01003220 p = eap->cmd;
Bram Moolenaard2ef6b32020-07-02 21:11:34 +02003221 if (lookup != NULL && (*p == '(' || *p == '{'
3222 || ((p = to_name_const_end(eap->cmd)) > eap->cmd && *p != NUL)
3223 || *p == '['))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003224 {
3225 int oplen;
3226 int heredoc;
3227
3228 // "funcname(" is always a function call.
3229 // "varname[]" is an expression.
3230 // "g:varname" is an expression.
3231 // "varname->expr" is an expression.
Bram Moolenaar0c6ceaf2020-02-22 18:36:32 +01003232 // "(..." is an expression.
Bram Moolenaar5f195932020-07-01 20:07:14 +02003233 // "{..." is an dict expression.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003234 if (*p == '('
Bram Moolenaar5f195932020-07-01 20:07:14 +02003235 || *p == '{'
Bram Moolenaard2ef6b32020-07-02 21:11:34 +02003236 || (*p == '[' && p > eap->cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003237 || p[1] == ':'
3238 || (*p == '-' && p[1] == '>'))
3239 {
3240 eap->cmdidx = CMD_eval;
3241 return eap->cmd;
3242 }
3243
Bram Moolenaard2ef6b32020-07-02 21:11:34 +02003244 // "[...]->Method()" is a list expression, but "[a, b] = Func()" is
3245 // an assignment.
3246 // If there is no line break inside the "[...]" then "p" is advanced to
3247 // after the "]" by to_name_const_end(): check if a "=" follows.
3248 // If "[...]" has a line break "p" still points at the "[" and it can't
3249 // be an assignment.
3250 if (*eap->cmd == '[' && (p == eap->cmd || *skipwhite(p) != '='))
3251 {
3252 eap->cmdidx = CMD_eval;
3253 return eap->cmd;
3254 }
3255
Bram Moolenaar5f195932020-07-01 20:07:14 +02003256 // Recognize an assignment if we recognize the variable name:
3257 // "g:var = expr"
3258 // "var = expr" where "var" is a local var name.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003259 oplen = assignment_len(skipwhite(p), &heredoc);
3260 if (oplen > 0)
3261 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003262 if (((p - eap->cmd) > 2 && eap->cmd[1] == ':')
Bram Moolenaarb84a3812020-05-01 15:44:29 +02003263 || lookup(eap->cmd, p - eap->cmd, cctx) != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003264 {
3265 eap->cmdidx = CMD_let;
3266 return eap->cmd;
3267 }
3268 }
3269 }
3270#endif
3271
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272 /*
3273 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00003274 * Exceptions:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275 * - the 'k' command can directly be followed by any character.
3276 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003277 * but :sre[wind] is another command, as are :scr[iptnames],
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00003279 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280 */
3281 p = eap->cmd;
3282 if (*p == 'k')
3283 {
3284 eap->cmdidx = CMD_k;
3285 ++p;
3286 }
3287 else if (p[0] == 's'
Bram Moolenaar204b93f2015-08-04 22:02:51 +02003288 && ((p[1] == 'c' && (p[2] == NUL || (p[2] != 's' && p[2] != 'r'
3289 && (p[3] == NUL || (p[3] != 'i' && p[4] != 'p')))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003290 || p[1] == 'g'
3291 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
3292 || p[1] == 'I'
3293 || (p[1] == 'r' && p[2] != 'e')))
3294 {
3295 eap->cmdidx = CMD_substitute;
3296 ++p;
3297 }
3298 else
3299 {
3300 while (ASCII_ISALPHA(*p))
3301 ++p;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003302 // for python 3.x support ":py3", ":python3", ":py3file", etc.
Bram Moolenaar55d5c032010-07-17 23:52:29 +02003303 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003304 {
Bram Moolenaarb6590522010-07-21 16:00:43 +02003305 while (ASCII_ISALNUM(*p))
3306 ++p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003307 }
3308 else if (*p == '9' && STRNCMP("vim9", eap->cmd, 4) == 0)
3309 {
3310 // include "9" for "vim9script"
3311 ++p;
3312 while (ASCII_ISALPHA(*p))
3313 ++p;
3314 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02003315
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003316 // check for non-alpha command
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3318 ++p;
3319 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003320 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
3321 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003322 // Check for ":dl", ":dell", etc. to ":deletel": that's
3323 // :delete with the 'l' flag. Same for 'p'.
Bram Moolenaardf177f62005-02-22 08:39:57 +00003324 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003325 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00003326 break;
3327 if (i == len - 1)
3328 {
3329 --len;
3330 if (p[-1] == 'l')
3331 eap->flags |= EXFLAG_LIST;
3332 else
3333 eap->flags |= EXFLAG_PRINT;
3334 }
3335 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003337 if (ASCII_ISLOWER(eap->cmd[0]))
3338 {
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003339 int c1 = eap->cmd[0];
Bram Moolenaar94f82cb2019-07-24 22:30:27 +02003340 int c2 = len == 1 ? NUL : eap->cmd[1];
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003341
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003342 if (command_count != (int)CMD_SIZE)
3343 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003344 iemsg(_("E943: Command table needs to be updated, run 'make cmdidxs'"));
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003345 getout(1);
3346 }
3347
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003348 // Use a precomputed index for fast look-up in cmdnames[]
3349 // taking into account the first 2 letters of eap->cmd.
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003350 eap->cmdidx = cmdidxs1[CharOrdLow(c1)];
3351 if (ASCII_ISLOWER(c2))
3352 eap->cmdidx += cmdidxs2[CharOrdLow(c1)][CharOrdLow(c2)];
3353 }
Bram Moolenaara494f562020-04-18 17:45:38 +02003354 else if (ASCII_ISUPPER(eap->cmd[0]))
3355 eap->cmdidx = CMD_Next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003356 else
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003357 eap->cmdidx = CMD_bang;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358
3359 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
3360 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
3361 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
3362 (size_t)len) == 0)
3363 {
3364#ifdef FEAT_EVAL
3365 if (full != NULL
3366 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
3367 *full = TRUE;
3368#endif
3369 break;
3370 }
3371
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003372 // Look for a user defined command as a last resort. Let ":Print" be
3373 // overruled by a user defined command.
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01003374 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
3375 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376 {
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003377 // User defined commands may contain digits.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378 while (ASCII_ISALNUM(*p))
3379 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003380 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003382 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383 eap->cmdidx = CMD_SIZE;
3384 }
3385
3386 return p;
3387}
3388
3389#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003390static struct cmdmod
3391{
3392 char *name;
3393 int minlen;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003394 int has_count; // :123verbose :3tab
Bram Moolenaared53fb92007-11-24 20:50:24 +00003395} cmdmods[] = {
3396 {"aboveleft", 3, FALSE},
3397 {"belowright", 3, FALSE},
3398 {"botright", 2, FALSE},
3399 {"browse", 3, FALSE},
3400 {"confirm", 4, FALSE},
Bram Moolenaar7b668e82016-08-23 23:51:21 +02003401 {"filter", 4, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003402 {"hide", 3, FALSE},
3403 {"keepalt", 5, FALSE},
3404 {"keepjumps", 5, FALSE},
3405 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01003406 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003407 {"leftabove", 5, FALSE},
3408 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003409 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003410 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003411 {"rightbelow", 6, FALSE},
3412 {"sandbox", 3, FALSE},
3413 {"silent", 3, FALSE},
3414 {"tab", 3, TRUE},
3415 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003416 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003417 {"verbose", 4, TRUE},
3418 {"vertical", 4, FALSE},
3419};
3420
3421/*
3422 * Return length of a command modifier (including optional count).
3423 * Return zero when it's not a modifier.
3424 */
3425 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003426modifier_len(char_u *cmd)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003427{
3428 int i, j;
3429 char_u *p = cmd;
3430
3431 if (VIM_ISDIGIT(*cmd))
3432 p = skipwhite(skipdigits(cmd));
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003433 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003434 {
3435 for (j = 0; p[j] != NUL; ++j)
3436 if (p[j] != cmdmods[i].name[j])
3437 break;
Bram Moolenaar2d473ab2013-06-12 17:12:24 +02003438 if (!ASCII_ISALPHA(p[j]) && j >= cmdmods[i].minlen
Bram Moolenaared53fb92007-11-24 20:50:24 +00003439 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003440 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003441 }
3442 return 0;
3443}
3444
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445/*
3446 * Return > 0 if an Ex command "name" exists.
3447 * Return 2 if there is an exact match.
3448 * Return 3 if there is an ambiguous match.
3449 */
3450 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003451cmd_exists(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452{
3453 exarg_T ea;
3454 int full = FALSE;
3455 int i;
3456 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003457 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003459 // Check command modifiers.
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003460 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461 {
3462 for (j = 0; name[j] != NUL; ++j)
3463 if (name[j] != cmdmods[i].name[j])
3464 break;
3465 if (name[j] == NUL && j >= cmdmods[i].minlen)
3466 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3467 }
3468
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003469 // Check built-in commands and user defined commands.
3470 // For ":2match" and ":3match" we need to skip the number.
Bram Moolenaara9587612006-05-04 21:47:50 +00003471 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003473 p = find_ex_command(&ea, &full, NULL, NULL);
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003474 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003476 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3477 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003478 if (*skipwhite(p) != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003479 return 0; // trailing garbage
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3481}
3482#endif
3483
Bram Moolenaard0190392019-08-23 21:17:35 +02003484 cmdidx_T
3485excmd_get_cmdidx(char_u *cmd, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486{
Bram Moolenaard0190392019-08-23 21:17:35 +02003487 cmdidx_T idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488
Bram Moolenaard0190392019-08-23 21:17:35 +02003489 for (idx = (cmdidx_T)0; (int)idx < (int)CMD_SIZE;
3490 idx = (cmdidx_T)((int)idx + 1))
3491 if (STRNCMP(cmdnames[(int)idx].cmd_name, cmd, (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492 break;
3493
Bram Moolenaard0190392019-08-23 21:17:35 +02003494 return idx;
3495}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496
Bram Moolenaard0190392019-08-23 21:17:35 +02003497 long
3498excmd_get_argt(cmdidx_T idx)
3499{
3500 return (long)cmdnames[(int)idx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501}
3502
3503/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003504 * Skip a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505 *
3506 * Backslashed delimiters after / or ? will be skipped, and commands will
3507 * not be expanded between /'s and ?'s or after "'".
3508 *
Bram Moolenaardf069ee2020-06-22 23:02:51 +02003509 * Also skip white space and ":" characters after the range.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510 * Returns the "cmd" pointer advanced to beyond the range.
3511 */
3512 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003513skip_range(
3514 char_u *cmd,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003515 int *ctx) // pointer to xp_context or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003517 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01003519 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;\\", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520 {
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01003521 if (*cmd == '\\')
3522 {
3523 if (cmd[1] == '?' || cmd[1] == '/' || cmd[1] == '&')
3524 ++cmd;
3525 else
3526 break;
3527 }
3528 else if (*cmd == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003529 {
3530 if (*++cmd == NUL && ctx != NULL)
3531 *ctx = EXPAND_NOTHING;
3532 }
3533 else if (*cmd == '/' || *cmd == '?')
3534 {
3535 delim = *cmd++;
3536 while (*cmd != NUL && *cmd != delim)
3537 if (*cmd++ == '\\' && *cmd != NUL)
3538 ++cmd;
3539 if (*cmd == NUL && ctx != NULL)
3540 *ctx = EXPAND_NOTHING;
3541 }
3542 if (*cmd != NUL)
3543 ++cmd;
3544 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00003545
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003546 // Skip ":" and white space.
Bram Moolenaardf177f62005-02-22 08:39:57 +00003547 while (*cmd == ':')
3548 cmd = skipwhite(cmd + 1);
3549
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550 return cmd;
3551}
3552
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003553 static void
3554addr_error(cmd_addr_T addr_type)
3555{
3556 if (addr_type == ADDR_NONE)
3557 emsg(_(e_norange));
3558 else
3559 emsg(_(e_invrange));
3560}
3561
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562/*
Bram Moolenaar198cb662018-09-06 21:44:17 +02003563 * Get a single EX address.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564 *
3565 * Set ptr to the next character after the part that was interpreted.
3566 * Set ptr to NULL when an error is encountered.
Bram Moolenaar198cb662018-09-06 21:44:17 +02003567 * This may set the last used search pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568 *
3569 * Return MAXLNUM when no Ex address was found.
3570 */
3571 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003572get_address(
3573 exarg_T *eap UNUSED,
3574 char_u **ptr,
Bram Moolenaar1b03a192019-12-08 17:08:29 +01003575 cmd_addr_T addr_type,
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003576 int skip, // only skip the address, don't use it
3577 int silent, // no errors or side effects
3578 int to_other_file, // flag: may jump to other file
3579 int address_count UNUSED) // 1 for first address, >1 after comma
Bram Moolenaar071d4272004-06-13 20:20:40 +00003580{
3581 int c;
3582 int i;
3583 long n;
3584 char_u *cmd;
3585 pos_T pos;
3586 pos_T *fp;
3587 linenr_T lnum;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003588 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589
3590 cmd = skipwhite(*ptr);
3591 lnum = MAXLNUM;
3592 do
3593 {
3594 switch (*cmd)
3595 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003596 case '.': // '.' - Cursor position
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003597 ++cmd;
3598 switch (addr_type)
3599 {
3600 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003601 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602 lnum = curwin->w_cursor.lnum;
3603 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003604 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003605 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003606 break;
3607 case ADDR_ARGUMENTS:
3608 lnum = curwin->w_arg_idx + 1;
3609 break;
3610 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003611 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003612 lnum = curbuf->b_fnum;
3613 break;
3614 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003615 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003616 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003617 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003618 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003619 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003620 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003621 cmd = NULL;
3622 goto error;
3623 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003624 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003625#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02003626 lnum = qf_get_cur_idx(eap);
3627#endif
3628 break;
3629 case ADDR_QUICKFIX_VALID:
3630#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003631 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02003632#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003633 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003634 }
3635 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003637 case '$': // '$' - last line
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003638 ++cmd;
3639 switch (addr_type)
3640 {
3641 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003642 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643 lnum = curbuf->b_ml.ml_line_count;
3644 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003645 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003646 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003647 break;
3648 case ADDR_ARGUMENTS:
3649 lnum = ARGCOUNT;
3650 break;
3651 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003652 buf = lastbuf;
3653 while (buf->b_ml.ml_mfp == NULL)
3654 {
3655 if (buf->b_prev == NULL)
3656 break;
3657 buf = buf->b_prev;
3658 }
3659 lnum = buf->b_fnum;
3660 break;
3661 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003662 lnum = lastbuf->b_fnum;
3663 break;
3664 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003665 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003666 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003667 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003668 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003669 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003670 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003671 cmd = NULL;
3672 goto error;
3673 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003674 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003675#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003676 lnum = qf_get_size(eap);
3677 if (lnum == 0)
3678 lnum = 1;
Bram Moolenaare906c502015-09-09 21:10:39 +02003679#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003680 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003681 case ADDR_QUICKFIX_VALID:
3682#ifdef FEAT_QUICKFIX
3683 lnum = qf_get_valid_size(eap);
3684 if (lnum == 0)
3685 lnum = 1;
3686#endif
3687 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003688 }
3689 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003690
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003691 case '\'': // ''' - mark
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003692 if (*++cmd == NUL)
3693 {
3694 cmd = NULL;
3695 goto error;
3696 }
3697 if (addr_type != ADDR_LINES)
3698 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003699 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003700 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003701 goto error;
3702 }
3703 if (skip)
3704 ++cmd;
3705 else
3706 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003707 // Only accept a mark in another file when it is
3708 // used by itself: ":'M".
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003709 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
3710 ++cmd;
3711 if (fp == (pos_T *)-1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003712 // Jumped to another file.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003713 lnum = curwin->w_cursor.lnum;
3714 else
3715 {
3716 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003717 {
3718 cmd = NULL;
3719 goto error;
3720 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003721 lnum = fp->lnum;
3722 }
3723 }
3724 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003725
3726 case '/':
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003727 case '?': // '/' or '?' - search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003728 c = *cmd++;
3729 if (addr_type != ADDR_LINES)
3730 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003731 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003732 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003733 goto error;
3734 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003735 if (skip) // skip "/pat/"
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003736 {
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02003737 cmd = skip_regexp(cmd, c, (int)p_magic);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003738 if (*cmd == c)
3739 ++cmd;
3740 }
3741 else
3742 {
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003743 int flags;
3744
3745 pos = curwin->w_cursor; // save curwin->w_cursor
3746
3747 // When '/' or '?' follows another address, start from
3748 // there.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003749 if (lnum != MAXLNUM)
3750 curwin->w_cursor.lnum = lnum;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003751
3752 // Start a forward search at the end of the line (unless
3753 // before the first line).
3754 // Start a backward search at the start of the line.
3755 // This makes sure we never match in the current
3756 // line, and can match anywhere in the
3757 // next/previous line.
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01003758 if (c == '/' && curwin->w_cursor.lnum > 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003759 curwin->w_cursor.col = MAXCOL;
3760 else
3761 curwin->w_cursor.col = 0;
3762 searchcmdlen = 0;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003763 flags = silent ? 0 : SEARCH_HIS | SEARCH_MSG;
Bram Moolenaarc036e872020-02-21 21:30:52 +01003764 if (!do_search(NULL, c, c, cmd, 1L, flags, NULL))
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003765 {
3766 curwin->w_cursor = pos;
3767 cmd = NULL;
3768 goto error;
3769 }
3770 lnum = curwin->w_cursor.lnum;
3771 curwin->w_cursor = pos;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003772 // adjust command string pointer
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003773 cmd += searchcmdlen;
3774 }
3775 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003776
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003777 case '\\': // "\?", "\/" or "\&", repeat search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003778 ++cmd;
3779 if (addr_type != ADDR_LINES)
3780 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003781 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003782 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003783 goto error;
3784 }
3785 if (*cmd == '&')
3786 i = RE_SUBST;
3787 else if (*cmd == '?' || *cmd == '/')
3788 i = RE_SEARCH;
3789 else
3790 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003791 emsg(_(e_backslash));
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003792 cmd = NULL;
3793 goto error;
3794 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003795
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003796 if (!skip)
3797 {
3798 /*
3799 * When search follows another address, start from
3800 * there.
3801 */
3802 if (lnum != MAXLNUM)
3803 pos.lnum = lnum;
3804 else
3805 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003806
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003807 /*
3808 * Start the search just like for the above
3809 * do_search().
3810 */
3811 if (*cmd != '?')
3812 pos.col = MAXCOL;
3813 else
3814 pos.col = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02003815 pos.coladd = 0;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01003816 if (searchit(curwin, curbuf, &pos, NULL,
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003817 *cmd == '?' ? BACKWARD : FORWARD,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02003818 (char_u *)"", 1L, SEARCH_MSG, i, NULL) != FAIL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003819 lnum = pos.lnum;
3820 else
3821 {
3822 cmd = NULL;
3823 goto error;
3824 }
3825 }
3826 ++cmd;
3827 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003828
3829 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003830 if (VIM_ISDIGIT(*cmd)) // absolute line number
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003831 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003832 }
3833
3834 for (;;)
3835 {
3836 cmd = skipwhite(cmd);
3837 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
3838 break;
3839
3840 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003841 {
3842 switch (addr_type)
3843 {
3844 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003845 case ADDR_OTHER:
3846 // "+1" is same as ".+1"
Bram Moolenaarf240e182014-11-27 18:33:02 +01003847 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003848 break;
3849 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003850 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003851 break;
3852 case ADDR_ARGUMENTS:
3853 lnum = curwin->w_arg_idx + 1;
3854 break;
3855 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003856 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003857 lnum = curbuf->b_fnum;
3858 break;
3859 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003860 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003861 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003862 case ADDR_TABS_RELATIVE:
3863 lnum = 1;
3864 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003865 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003866#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02003867 lnum = qf_get_cur_idx(eap);
3868#endif
3869 break;
3870 case ADDR_QUICKFIX_VALID:
3871#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003872 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02003873#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003874 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003875 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003876 case ADDR_UNSIGNED:
3877 lnum = 0;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003878 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003879 }
3880 }
3881
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882 if (VIM_ISDIGIT(*cmd))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003883 i = '+'; // "number" is same as "+number"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884 else
3885 i = *cmd++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003886 if (!VIM_ISDIGIT(*cmd)) // '+' is '+1', but '+0' is not '+1'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003887 n = 1;
3888 else
3889 n = getdigits(&cmd);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003890
3891 if (addr_type == ADDR_TABS_RELATIVE)
3892 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003893 emsg(_(e_invrange));
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003894 cmd = NULL;
3895 goto error;
3896 }
3897 else if (addr_type == ADDR_LOADED_BUFFERS
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003898 || addr_type == ADDR_BUFFERS)
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003899 lnum = compute_buffer_local_count(
3900 addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003901 else
Bram Moolenaarded27822017-01-02 14:27:34 +01003902 {
3903#ifdef FEAT_FOLDING
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003904 // Relative line addressing, need to adjust for folded lines
3905 // now, but only do it after the first address.
Bram Moolenaarded27822017-01-02 14:27:34 +01003906 if (addr_type == ADDR_LINES && (i == '-' || i == '+')
3907 && address_count >= 2)
3908 (void)hasFolding(lnum, NULL, &lnum);
3909#endif
3910 if (i == '-')
3911 lnum -= n;
3912 else
3913 lnum += n;
3914 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003915 }
3916 } while (*cmd == '/' || *cmd == '?');
3917
3918error:
3919 *ptr = cmd;
3920 return lnum;
3921}
3922
3923/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00003924 * Get flags from an Ex command argument.
3925 */
3926 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003927get_flags(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00003928{
3929 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
3930 {
3931 if (*eap->arg == 'l')
3932 eap->flags |= EXFLAG_LIST;
3933 else if (*eap->arg == 'p')
3934 eap->flags |= EXFLAG_PRINT;
3935 else
3936 eap->flags |= EXFLAG_NR;
3937 eap->arg = skipwhite(eap->arg + 1);
3938 }
3939}
3940
3941/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942 * Function called for command which is Not Implemented. NI!
3943 */
3944 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003945ex_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946{
3947 if (!eap->skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003948 eap->errmsg = N_("E319: Sorry, the command is not available in this version");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949}
3950
Bram Moolenaar7bb75552007-07-16 18:39:49 +00003951#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003952/*
3953 * Function called for script command which is Not Implemented. NI!
3954 * Skips over ":perl <<EOF" constructs.
3955 */
3956 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003957ex_script_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003958{
3959 if (!eap->skip)
3960 ex_ni(eap);
3961 else
3962 vim_free(script_get(eap, eap->arg));
3963}
3964#endif
3965
3966/*
3967 * Check range in Ex command for validity.
3968 * Return NULL when valid, error message when invalid.
3969 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003970 static char *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003971invalid_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003972{
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003973 buf_T *buf;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003974
Bram Moolenaar071d4272004-06-13 20:20:40 +00003975 if ( eap->line1 < 0
3976 || eap->line2 < 0
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003977 || eap->line1 > eap->line2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003978 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003979
Bram Moolenaar8071cb22019-07-12 17:58:01 +02003980 if (eap->argt & EX_RANGE)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003981 {
Bram Moolenaarb7316892019-05-01 18:08:42 +02003982 switch (eap->addr_type)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003983 {
3984 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003985 if (eap->line2 > curbuf->b_ml.ml_line_count
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003986#ifdef FEAT_DIFF
3987 + (eap->cmdidx == CMD_diffget)
3988#endif
3989 )
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003990 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003991 break;
3992 case ADDR_ARGUMENTS:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003993 // add 1 if ARGCOUNT is 0
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003994 if (eap->line2 > ARGCOUNT + (!ARGCOUNT))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003995 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003996 break;
3997 case ADDR_BUFFERS:
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02003998 // Only a boundary check, not whether the buffers actually
3999 // exist.
4000 if (eap->line1 < 1 || eap->line2 > get_highest_fnum())
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004001 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004002 break;
4003 case ADDR_LOADED_BUFFERS:
4004 buf = firstbuf;
4005 while (buf->b_ml.ml_mfp == NULL)
4006 {
4007 if (buf->b_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004008 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004009 buf = buf->b_next;
4010 }
4011 if (eap->line1 < buf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004012 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004013 buf = lastbuf;
4014 while (buf->b_ml.ml_mfp == NULL)
4015 {
4016 if (buf->b_prev == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004017 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004018 buf = buf->b_prev;
4019 }
4020 if (eap->line2 > buf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004021 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004022 break;
4023 case ADDR_WINDOWS:
Bram Moolenaar8be63882015-01-14 11:25:05 +01004024 if (eap->line2 > LAST_WIN_NR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004025 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004026 break;
4027 case ADDR_TABS:
4028 if (eap->line2 > LAST_TAB_NR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004029 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004030 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004031 case ADDR_TABS_RELATIVE:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004032 case ADDR_OTHER:
4033 // Any range is OK.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004034 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004035 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004036#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004037 // No error for value that is too big, will use the last entry.
4038 if (eap->line2 <= 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004039 return _(e_invrange);
Bram Moolenaare906c502015-09-09 21:10:39 +02004040#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004041 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004042 case ADDR_QUICKFIX_VALID:
4043#ifdef FEAT_QUICKFIX
4044 if ((eap->line2 != 1 && eap->line2 > qf_get_valid_size(eap))
4045 || eap->line2 < 0)
4046 return _(e_invrange);
4047#endif
4048 break;
4049 case ADDR_UNSIGNED:
4050 if (eap->line2 < 0)
4051 return _(e_invrange);
4052 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004053 case ADDR_NONE:
4054 // Will give an error elsewhere.
4055 break;
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004056 }
4057 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004058 return NULL;
4059}
4060
4061/*
4062 * Correct the range for zero line number, if required.
4063 */
4064 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004065correct_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004066{
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004067 if (!(eap->argt & EX_ZEROR)) // zero in range not allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068 {
4069 if (eap->line1 == 0)
4070 eap->line1 = 1;
4071 if (eap->line2 == 0)
4072 eap->line2 = 1;
4073 }
4074}
4075
Bram Moolenaar748bf032005-02-02 23:04:36 +00004076#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004077/*
4078 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4079 * pattern. Otherwise return eap->arg.
4080 */
4081 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004082skip_grep_pat(exarg_T *eap)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004083{
4084 char_u *p = eap->arg;
4085
Bram Moolenaara37420f2006-02-04 22:37:47 +00004086 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4087 || eap->cmdidx == CMD_vimgrepadd
4088 || eap->cmdidx == CMD_lvimgrepadd
4089 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004090 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004091 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004092 if (p == NULL)
4093 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004094 }
4095 return p;
4096}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004097
4098/*
4099 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4100 * in the command line, so that things like % get expanded.
4101 */
4102 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004103replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004104{
4105 char_u *new_cmdline;
4106 char_u *program;
4107 char_u *pos;
4108 char_u *ptr;
4109 int len;
4110 int i;
4111
4112 /*
4113 * Don't do it when ":vimgrep" is used for ":grep".
4114 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004115 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4116 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4117 || eap->cmdidx == CMD_grepadd
4118 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004119 && !grep_internal(eap->cmdidx))
4120 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004121 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4122 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004123 {
4124 if (*curbuf->b_p_gp == NUL)
4125 program = p_gp;
4126 else
4127 program = curbuf->b_p_gp;
4128 }
4129 else
4130 {
4131 if (*curbuf->b_p_mp == NUL)
4132 program = p_mp;
4133 else
4134 program = curbuf->b_p_mp;
4135 }
4136
4137 p = skipwhite(p);
4138
4139 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4140 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004141 // replace $* by given arguments
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004142 i = 1;
4143 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4144 ++i;
4145 len = (int)STRLEN(p);
Bram Moolenaar51e14382019-05-25 20:21:28 +02004146 new_cmdline = alloc(STRLEN(program) + i * (len - 2) + 1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004147 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004148 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004149 ptr = new_cmdline;
4150 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4151 {
4152 i = (int)(pos - program);
4153 STRNCPY(ptr, program, i);
4154 STRCPY(ptr += i, p);
4155 ptr += len;
4156 program = pos + 2;
4157 }
4158 STRCPY(ptr, program);
4159 }
4160 else
4161 {
Bram Moolenaar51e14382019-05-25 20:21:28 +02004162 new_cmdline = alloc(STRLEN(program) + STRLEN(p) + 2);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004163 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004164 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004165 STRCPY(new_cmdline, program);
4166 STRCAT(new_cmdline, " ");
4167 STRCAT(new_cmdline, p);
4168 }
4169 msg_make(p);
4170
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004171 // 'eap->cmd' is not set here, because it is not used at CMD_make
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004172 vim_free(*cmdlinep);
4173 *cmdlinep = new_cmdline;
4174 p = new_cmdline;
4175 }
4176 return p;
4177}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004178#endif
4179
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180/*
4181 * Expand file name in Ex command argument.
Bram Moolenaar0abb4272019-06-15 16:06:00 +02004182 * When an error is detected, "errormsgp" is set to a non-NULL pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004183 * Return FAIL for failure, OK otherwise.
4184 */
4185 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004186expand_filename(
4187 exarg_T *eap,
4188 char_u **cmdlinep,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004189 char **errormsgp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004191 int has_wildcards; // need to expand wildcards
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192 char_u *repl;
4193 int srclen;
4194 char_u *p;
4195 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004196 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004197
Bram Moolenaar748bf032005-02-02 23:04:36 +00004198#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004199 // Skip a regexp pattern for ":vimgrep[add] pat file..."
Bram Moolenaar748bf032005-02-02 23:04:36 +00004200 p = skip_grep_pat(eap);
4201#else
4202 p = eap->arg;
4203#endif
4204
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205 /*
4206 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4207 * the file name contains a wildcard it should not cause expanding.
4208 * (it will be expanded anyway if there is a wildcard before replacing).
4209 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004210 has_wildcards = mch_has_wildcard(p);
4211 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004212 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004213#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004214 // Skip over `=expr`, wildcards in it are not expanded.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004215 if (p[0] == '`' && p[1] == '=')
4216 {
4217 p += 2;
4218 (void)skip_expr(&p);
4219 if (*p == '`')
4220 ++p;
4221 continue;
4222 }
4223#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224 /*
4225 * Quick check if this cannot be the start of a special string.
4226 * Also removes backslash before '%', '#' and '<'.
4227 */
4228 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4229 {
4230 ++p;
4231 continue;
4232 }
4233
4234 /*
4235 * Try to find a match at this position.
4236 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00004237 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
4238 errormsgp, &escaped);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004239 if (*errormsgp != NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240 return FAIL;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004241 if (repl == NULL) // no match found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242 {
4243 p += srclen;
4244 continue;
4245 }
4246
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004247 // Wildcards won't be expanded below, the replacement is taken
4248 // literally. But do expand "~/file", "~user/file" and "$HOME/file".
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004249 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4250 {
4251 char_u *l = repl;
4252
4253 repl = expand_env_save(repl);
4254 vim_free(l);
4255 }
4256
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004257 // Need to escape white space et al. with a backslash.
4258 // Don't do this for:
4259 // - replacement that already has been escaped: "##"
4260 // - shell commands (may have to use quotes instead).
4261 // - non-unix systems when there is a single argument (spaces don't
4262 // separate arguments then).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00004264 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00004265 && eap->cmdidx != CMD_bang
Bram Moolenaar071d4272004-06-13 20:20:40 +00004266 && eap->cmdidx != CMD_grep
4267 && eap->cmdidx != CMD_grepadd
Bram Moolenaarbf15b8d2017-06-04 20:43:48 +02004268 && eap->cmdidx != CMD_hardcopy
Bram Moolenaar67883b42017-07-27 22:57:00 +02004269 && eap->cmdidx != CMD_lgrep
4270 && eap->cmdidx != CMD_lgrepadd
4271 && eap->cmdidx != CMD_lmake
4272 && eap->cmdidx != CMD_make
4273 && eap->cmdidx != CMD_terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274#ifndef UNIX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004275 && !(eap->argt & EX_NOSPC)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004276#endif
4277 )
4278 {
4279 char_u *l;
4280#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004281 // Don't escape a backslash here, because rem_backslash() doesn't
4282 // remove it later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004283 static char_u *nobslash = (char_u *)" \t\"|";
4284# define ESCAPE_CHARS nobslash
4285#else
4286# define ESCAPE_CHARS escape_chars
4287#endif
4288
4289 for (l = repl; *l; ++l)
4290 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
4291 {
4292 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
4293 if (l != NULL)
4294 {
4295 vim_free(repl);
4296 repl = l;
4297 }
4298 break;
4299 }
4300 }
4301
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004302 // For a shell command a '!' must be escaped.
Bram Moolenaar67883b42017-07-27 22:57:00 +02004303 if ((eap->usefilter || eap->cmdidx == CMD_bang
4304 || eap->cmdidx == CMD_terminal)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004305 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004306 {
4307 char_u *l;
4308
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004309 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004310 if (l != NULL)
4311 {
4312 vim_free(repl);
4313 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004314 }
4315 }
4316
4317 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
4318 vim_free(repl);
4319 if (p == NULL)
4320 return FAIL;
4321 }
4322
4323 /*
4324 * One file argument: Expand wildcards.
4325 * Don't do this with ":r !command" or ":w !command".
4326 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004327 if ((eap->argt & EX_NOSPC) && !eap->usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004328 {
4329 /*
4330 * May do this twice:
4331 * 1. Replace environment variables.
4332 * 2. Replace any other wildcards, remove backslashes.
4333 */
4334 for (n = 1; n <= 2; ++n)
4335 {
4336 if (n == 2)
4337 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004338 /*
4339 * Halve the number of backslashes (this is Vi compatible).
4340 * For Unix and OS/2, when wildcards are expanded, this is
4341 * done by ExpandOne() below.
4342 */
Bram Moolenaare7fedb62015-12-31 19:07:19 +01004343#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004344 if (!has_wildcards)
4345#endif
4346 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004347 }
4348
4349 if (has_wildcards)
4350 {
4351 if (n == 1)
4352 {
4353 /*
4354 * First loop: May expand environment variables. This
4355 * can be done much faster with expand_env() than with
4356 * something else (e.g., calling a shell).
4357 * After expanding environment variables, check again
4358 * if there are still wildcards present.
4359 */
4360 if (vim_strchr(eap->arg, '$') != NULL
4361 || vim_strchr(eap->arg, '~') != NULL)
4362 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00004363 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004364 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004365 has_wildcards = mch_has_wildcard(NameBuff);
4366 p = NameBuff;
4367 }
4368 else
4369 p = NULL;
4370 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004371 else // n == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004372 {
4373 expand_T xpc;
Bram Moolenaarb40c2572019-10-19 21:01:05 +02004374 int options = WILD_LIST_NOTFOUND
4375 | WILD_NOERROR | WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004376
4377 ExpandInit(&xpc);
4378 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004379 if (p_wic)
4380 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004381 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01004382 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383 if (p == NULL)
4384 return FAIL;
4385 }
4386 if (p != NULL)
4387 {
4388 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
4389 p, cmdlinep);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004390 if (n == 2) // p came from ExpandOne()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004391 vim_free(p);
4392 }
4393 }
4394 }
4395 }
4396 return OK;
4397}
4398
4399/*
4400 * Replace part of the command line, keeping eap->cmd, eap->arg and
4401 * eap->nextcmd correct.
4402 * "src" points to the part that is to be replaced, of length "srclen".
4403 * "repl" is the replacement string.
4404 * Returns a pointer to the character after the replaced string.
4405 * Returns NULL for failure.
4406 */
4407 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004408repl_cmdline(
4409 exarg_T *eap,
4410 char_u *src,
4411 int srclen,
4412 char_u *repl,
4413 char_u **cmdlinep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004414{
4415 int len;
4416 int i;
4417 char_u *new_cmdline;
4418
4419 /*
4420 * The new command line is build in new_cmdline[].
4421 * First allocate it.
4422 * Careful: a "+cmd" argument may have been NUL terminated.
4423 */
4424 len = (int)STRLEN(repl);
4425 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004426 if (eap->nextcmd != NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004427 i += (int)STRLEN(eap->nextcmd);// add space for next command
Bram Moolenaar964b3742019-05-24 18:54:09 +02004428 if ((new_cmdline = alloc(i)) == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004429 return NULL; // out of memory!
Bram Moolenaar071d4272004-06-13 20:20:40 +00004430
4431 /*
4432 * Copy the stuff before the expanded part.
4433 * Copy the expanded stuff.
4434 * Copy what came after the expanded part.
4435 * Copy the next commands, if there are any.
4436 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004437 i = (int)(src - *cmdlinep); // length of part before match
Bram Moolenaar071d4272004-06-13 20:20:40 +00004438 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00004439
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440 mch_memmove(new_cmdline + i, repl, (size_t)len);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004441 i += len; // remember the end of the string
Bram Moolenaar071d4272004-06-13 20:20:40 +00004442 STRCPY(new_cmdline + i, src + srclen);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004443 src = new_cmdline + i; // remember where to continue
Bram Moolenaar071d4272004-06-13 20:20:40 +00004444
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004445 if (eap->nextcmd != NULL) // append next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00004446 {
4447 i = (int)STRLEN(new_cmdline) + 1;
4448 STRCPY(new_cmdline + i, eap->nextcmd);
4449 eap->nextcmd = new_cmdline + i;
4450 }
4451 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
4452 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
4453 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
4454 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
4455 vim_free(*cmdlinep);
4456 *cmdlinep = new_cmdline;
4457
4458 return src;
4459}
4460
4461/*
4462 * Check for '|' to separate commands and '"' to start comments.
4463 */
4464 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004465separate_nextcmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004466{
4467 char_u *p;
4468
Bram Moolenaar86b68352004-12-27 21:59:20 +00004469#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004470 p = skip_grep_pat(eap);
4471#else
4472 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004473#endif
4474
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004475 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476 {
4477 if (*p == Ctrl_V)
4478 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004479 if (eap->argt & (EX_CTRLV | EX_XFILE))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004480 ++p; // skip CTRL-V and next char
Bram Moolenaar071d4272004-06-13 20:20:40 +00004481 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004482 // remove CTRL-V and skip next char
Bram Moolenaara7241f52008-06-24 20:39:31 +00004483 STRMOVE(p, p + 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004484 if (*p == NUL) // stop at NUL after CTRL-V
Bram Moolenaar071d4272004-06-13 20:20:40 +00004485 break;
4486 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004487
4488#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004489 // Skip over `=expr` when wildcards are expanded.
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004490 else if (p[0] == '`' && p[1] == '=' && (eap->argt & EX_XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004491 {
4492 p += 2;
4493 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004494 }
4495#endif
4496
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004497 // Check for '"': start of comment or '|': next command
4498 // :@" and :*" do not start a comment!
4499 // :redir @" doesn't either.
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004500 else if ((*p == '"' && !(eap->argt & EX_NOTRLCOM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004501 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
4502 || p != eap->arg)
4503 && (eap->cmdidx != CMD_redir
4504 || p != eap->arg + 1 || p[-1] != '@'))
Bram Moolenaara26b9702020-04-18 19:53:28 +02004505#ifdef FEAT_EVAL
4506 || (*p == '#' && in_vim9script()
4507 && p[1] != '{' && p > eap->cmd && VIM_ISWHITE(p[-1]))
4508#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004509 || *p == '|' || *p == '\n')
4510 {
4511 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004512 * We remove the '\' before the '|', unless EX_CTRLV is used
Bram Moolenaar071d4272004-06-13 20:20:40 +00004513 * AND 'b' is present in 'cpoptions'.
4514 */
4515 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004516 || !(eap->argt & EX_CTRLV)) && *(p - 1) == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004517 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004518 STRMOVE(p - 1, p); // remove the '\'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004519 --p;
4520 }
4521 else
4522 {
4523 eap->nextcmd = check_nextcmd(p);
4524 *p = NUL;
4525 break;
4526 }
4527 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004528 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004529
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004530 if (!(eap->argt & EX_NOTRLCOM)) // remove trailing spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00004531 del_trailing_spaces(eap->arg);
4532}
4533
4534/*
4535 * get + command from ex argument
4536 */
4537 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004538getargcmd(char_u **argp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004539{
4540 char_u *arg = *argp;
4541 char_u *command = NULL;
4542
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004543 if (*arg == '+') // +[command]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004544 {
4545 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02004546 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004547 command = dollar_command;
4548 else
4549 {
4550 command = arg;
4551 arg = skip_cmd_arg(command, TRUE);
4552 if (*arg != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004553 *arg++ = NUL; // terminate command with NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004554 }
4555
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004556 arg = skipwhite(arg); // skip over spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557 *argp = arg;
4558 }
4559 return command;
4560}
4561
4562/*
4563 * Find end of "+command" argument. Skip over "\ " and "\\".
4564 */
Bram Moolenaard0190392019-08-23 21:17:35 +02004565 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004566skip_cmd_arg(
4567 char_u *p,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004568 int rembs) // TRUE to halve the number of backslashes
Bram Moolenaar071d4272004-06-13 20:20:40 +00004569{
4570 while (*p && !vim_isspace(*p))
4571 {
4572 if (*p == '\\' && p[1] != NUL)
4573 {
4574 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00004575 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004576 else
4577 ++p;
4578 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004579 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004580 }
4581 return p;
4582}
4583
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004584 int
4585get_bad_opt(char_u *p, exarg_T *eap)
4586{
4587 if (STRICMP(p, "keep") == 0)
4588 eap->bad_char = BAD_KEEP;
4589 else if (STRICMP(p, "drop") == 0)
4590 eap->bad_char = BAD_DROP;
4591 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
4592 eap->bad_char = *p;
Bram Moolenaar75808492018-06-12 12:39:41 +02004593 else
4594 return FAIL;
4595 return OK;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004596}
4597
Bram Moolenaar071d4272004-06-13 20:20:40 +00004598/*
4599 * Get "++opt=arg" argument.
4600 * Return FAIL or OK.
4601 */
4602 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004603getargopt(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004604{
4605 char_u *arg = eap->arg + 2;
4606 int *pp = NULL;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004607 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004608 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004609
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004610 // ":edit ++[no]bin[ary] file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004611 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
4612 {
4613 if (*arg == 'n')
4614 {
4615 arg += 2;
4616 eap->force_bin = FORCE_NOBIN;
4617 }
4618 else
4619 eap->force_bin = FORCE_BIN;
4620 if (!checkforcmd(&arg, "binary", 3))
4621 return FAIL;
4622 eap->arg = skipwhite(arg);
4623 return OK;
4624 }
4625
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004626 // ":read ++edit file"
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004627 if (STRNCMP(arg, "edit", 4) == 0)
4628 {
4629 eap->read_edit = TRUE;
4630 eap->arg = skipwhite(arg + 4);
4631 return OK;
4632 }
4633
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634 if (STRNCMP(arg, "ff", 2) == 0)
4635 {
4636 arg += 2;
4637 pp = &eap->force_ff;
4638 }
4639 else if (STRNCMP(arg, "fileformat", 10) == 0)
4640 {
4641 arg += 10;
4642 pp = &eap->force_ff;
4643 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004644 else if (STRNCMP(arg, "enc", 3) == 0)
4645 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01004646 if (STRNCMP(arg, "encoding", 8) == 0)
4647 arg += 8;
4648 else
4649 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004650 pp = &eap->force_enc;
4651 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004652 else if (STRNCMP(arg, "bad", 3) == 0)
4653 {
4654 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004655 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004656 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004657
4658 if (pp == NULL || *arg != '=')
4659 return FAIL;
4660
4661 ++arg;
4662 *pp = (int)(arg - eap->cmd);
4663 arg = skip_cmd_arg(arg, FALSE);
4664 eap->arg = skipwhite(arg);
4665 *arg = NUL;
4666
Bram Moolenaar071d4272004-06-13 20:20:40 +00004667 if (pp == &eap->force_ff)
4668 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004669 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
4670 return FAIL;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004671 eap->force_ff = eap->cmd[eap->force_ff];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004672 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004673 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004674 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004675 // Make 'fileencoding' lower case.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004676 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
4677 *p = TOLOWER_ASC(*p);
4678 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004679 else
4680 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004681 // Check ++bad= argument. Must be a single-byte character, "keep" or
4682 // "drop".
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004683 if (get_bad_opt(eap->cmd + bad_char_idx, eap) == FAIL)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004684 return FAIL;
4685 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004686
4687 return OK;
4688}
4689
Bram Moolenaar071d4272004-06-13 20:20:40 +00004690 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004691ex_autocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004692{
4693 /*
Bram Moolenaar26d4b892018-07-04 22:26:28 +02004694 * Disallow autocommands from .exrc and .vimrc in current
Bram Moolenaar071d4272004-06-13 20:20:40 +00004695 * directory for security reasons.
4696 */
4697 if (secure)
4698 {
4699 secure = 2;
4700 eap->errmsg = e_curdir;
4701 }
4702 else if (eap->cmdidx == CMD_autocmd)
4703 do_autocmd(eap->arg, eap->forceit);
4704 else
4705 do_augroup(eap->arg, eap->forceit);
4706}
4707
4708/*
4709 * ":doautocmd": Apply the automatic commands to the current buffer.
4710 */
4711 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004712ex_doautocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004713{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01004714 char_u *arg = eap->arg;
4715 int call_do_modelines = check_nomodeline(&arg);
Bram Moolenaar1610d052016-06-09 22:53:01 +02004716 int did_aucmd;
Bram Moolenaar60542ac2012-02-12 20:14:01 +01004717
Bram Moolenaar1610d052016-06-09 22:53:01 +02004718 (void)do_doautocmd(arg, TRUE, &did_aucmd);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004719 // Only when there is no <nomodeline>.
Bram Moolenaar1610d052016-06-09 22:53:01 +02004720 if (call_do_modelines && did_aucmd)
Bram Moolenaar60542ac2012-02-12 20:14:01 +01004721 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004722}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004723
Bram Moolenaar071d4272004-06-13 20:20:40 +00004724/*
4725 * :[N]bunload[!] [N] [bufname] unload buffer
4726 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
4727 * :[N]bwipeout[!] [N] [bufname] delete buffer really
4728 */
4729 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004730ex_bunload(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004731{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004732 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02004733 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004734 eap->errmsg = do_bufdel(
4735 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
4736 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
4737 : DOBUF_UNLOAD, eap->arg,
4738 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
4739}
4740
4741/*
4742 * :[N]buffer [N] to buffer N
4743 * :[N]sbuffer [N] to buffer N
4744 */
4745 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004746ex_buffer(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004747{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004748 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02004749 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004750 if (*eap->arg)
4751 eap->errmsg = e_trailing;
4752 else
4753 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004754 if (eap->addr_count == 0) // default is current buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
4756 else
4757 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004758 if (eap->do_ecmd_cmd != NULL)
4759 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004760 }
4761}
4762
4763/*
4764 * :[N]bmodified [N] to next mod. buffer
4765 * :[N]sbmodified [N] to next mod. buffer
4766 */
4767 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004768ex_bmodified(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004769{
4770 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004771 if (eap->do_ecmd_cmd != NULL)
4772 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004773}
4774
4775/*
4776 * :[N]bnext [N] to next buffer
4777 * :[N]sbnext [N] split and to next buffer
4778 */
4779 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004780ex_bnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004781{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004782 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01004783 return;
4784
Bram Moolenaar071d4272004-06-13 20:20:40 +00004785 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004786 if (eap->do_ecmd_cmd != NULL)
4787 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004788}
4789
4790/*
4791 * :[N]bNext [N] to previous buffer
4792 * :[N]bprevious [N] to previous buffer
4793 * :[N]sbNext [N] split and to previous buffer
4794 * :[N]sbprevious [N] split and to previous buffer
4795 */
4796 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004797ex_bprevious(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004798{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004799 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01004800 return;
4801
Bram Moolenaar071d4272004-06-13 20:20:40 +00004802 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004803 if (eap->do_ecmd_cmd != NULL)
4804 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004805}
4806
4807/*
4808 * :brewind to first buffer
4809 * :bfirst to first buffer
4810 * :sbrewind split and to first buffer
4811 * :sbfirst split and to first buffer
4812 */
4813 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004814ex_brewind(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004815{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004816 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01004817 return;
4818
Bram Moolenaar071d4272004-06-13 20:20:40 +00004819 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004820 if (eap->do_ecmd_cmd != NULL)
4821 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004822}
4823
4824/*
4825 * :blast to last buffer
4826 * :sblast split and to last buffer
4827 */
4828 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004829ex_blast(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004830{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004831 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01004832 return;
4833
Bram Moolenaar071d4272004-06-13 20:20:40 +00004834 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004835 if (eap->do_ecmd_cmd != NULL)
4836 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004837}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004838
Bram Moolenaar7a092242020-04-16 22:10:49 +02004839/*
4840 * Check if "c" ends an Ex command.
Bram Moolenaara494f562020-04-18 17:45:38 +02004841 * In Vim9 script does not check for white space before # or #{.
Bram Moolenaar7a092242020-04-16 22:10:49 +02004842 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004843 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004844ends_excmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004845{
Bram Moolenaarfbda69b2020-04-13 15:06:53 +02004846#ifdef FEAT_EVAL
Bram Moolenaar2c330432020-04-13 14:41:35 +02004847 if (c == '#')
Bram Moolenaar7a092242020-04-16 22:10:49 +02004848 return in_vim9script();
4849#endif
4850 return (c == NUL || c == '|' || c == '"' || c == '\n');
4851}
4852
4853/*
4854 * Like ends_excmd() but checks that a # in Vim9 script either has "cmd" equal
4855 * to "cmd_start" or has a white space character before it.
4856 */
4857 int
Bram Moolenaar21cfe502020-04-16 23:01:50 +02004858ends_excmd2(char_u *cmd_start UNUSED, char_u *cmd)
Bram Moolenaar7a092242020-04-16 22:10:49 +02004859{
4860 int c = *cmd;
4861
4862#ifdef FEAT_EVAL
Bram Moolenaara26b9702020-04-18 19:53:28 +02004863 if (c == '#' && cmd[1] != '{' && (cmd == cmd_start || VIM_ISWHITE(cmd[-1])))
Bram Moolenaar2c330432020-04-13 14:41:35 +02004864 return in_vim9script();
Bram Moolenaarfbda69b2020-04-13 15:06:53 +02004865#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004866 return (c == NUL || c == '|' || c == '"' || c == '\n');
4867}
4868
4869#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
4870 || defined(PROTO)
4871/*
4872 * Return the next command, after the first '|' or '\n'.
4873 * Return NULL if not found.
4874 */
4875 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004876find_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004877{
4878 while (*p != '|' && *p != '\n')
4879 {
4880 if (*p == NUL)
4881 return NULL;
4882 ++p;
4883 }
4884 return (p + 1);
4885}
4886#endif
4887
4888/*
Bram Moolenaar2256c992016-11-15 21:17:07 +01004889 * Check if *p is a separator between Ex commands, skipping over white space.
4890 * Return NULL if it isn't, the following character if it is.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891 */
4892 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004893check_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004894{
Bram Moolenaar2256c992016-11-15 21:17:07 +01004895 char_u *s = skipwhite(p);
4896
4897 if (*s == '|' || *s == '\n')
4898 return (s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004899 else
4900 return NULL;
4901}
4902
4903/*
4904 * - if there are more files to edit
4905 * - and this is the last window
4906 * - and forceit not used
4907 * - and not repeated twice on a row
4908 * return FAIL and give error message if 'message' TRUE
4909 * return OK otherwise
4910 */
4911 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004912check_more(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004913 int message, // when FALSE check only, no messages
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004914 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004915{
4916 int n = ARGCOUNT - curwin->w_arg_idx - 1;
4917
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00004918 if (!forceit && only_one_window()
4919 && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004920 {
4921 if (message)
4922 {
4923#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
4924 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
4925 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02004926 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927
Bram Moolenaarda6e8912018-08-21 15:12:14 +02004928 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
4929 NGETTEXT("%d more file to edit. Quit anyway?",
4930 "%d more files to edit. Quit anyway?", n), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004931 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
4932 return OK;
4933 return FAIL;
4934 }
4935#endif
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01004936 semsg(NGETTEXT("E173: %d more file to edit",
4937 "E173: %d more files to edit", n), n);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004938 quitmore = 2; // next try to quit is allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004939 }
4940 return FAIL;
4941 }
4942 return OK;
4943}
4944
Bram Moolenaar071d4272004-06-13 20:20:40 +00004945/*
4946 * Function given to ExpandGeneric() to obtain the list of command names.
4947 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004948 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004949get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004950{
4951 if (idx >= (int)CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004952 return get_user_command_name(idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004953 return cmdnames[idx].cmd_name;
4954}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004955
Bram Moolenaar071d4272004-06-13 20:20:40 +00004956 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004957ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004958{
Bram Moolenaare6850792010-05-14 15:28:44 +02004959 if (*eap->arg == NUL)
4960 {
4961#ifdef FEAT_EVAL
4962 char_u *expr = vim_strsave((char_u *)"g:colors_name");
4963 char_u *p = NULL;
4964
4965 if (expr != NULL)
4966 {
4967 ++emsg_off;
Bram Moolenaarb171fb12020-06-24 20:34:03 +02004968 p = eval_to_string(expr, FALSE);
Bram Moolenaare6850792010-05-14 15:28:44 +02004969 --emsg_off;
4970 vim_free(expr);
4971 }
4972 if (p != NULL)
4973 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004974 msg((char *)p);
Bram Moolenaare6850792010-05-14 15:28:44 +02004975 vim_free(p);
4976 }
4977 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01004978 msg("default");
Bram Moolenaare6850792010-05-14 15:28:44 +02004979#else
Bram Moolenaar32526b32019-01-19 17:43:09 +01004980 msg(_("unknown"));
Bram Moolenaare6850792010-05-14 15:28:44 +02004981#endif
4982 }
4983 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004984 semsg(_("E185: Cannot find color scheme '%s'"), eap->arg);
Bram Moolenaarf58d81a2019-01-28 20:19:05 +01004985
4986#ifdef FEAT_VTP
4987 else if (has_vtp_working())
4988 {
4989 // background color change requires clear + redraw
4990 update_screen(CLEAR);
4991 redrawcmd();
4992 }
4993#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004994}
4995
4996 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004997ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004998{
4999 if (*eap->arg == NUL && eap->cmd[2] == '!')
Bram Moolenaar32526b32019-01-19 17:43:09 +01005000 msg(_("Greetings, Vim user!"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005001 do_highlight(eap->arg, eap->forceit, FALSE);
5002}
5003
5004
5005/*
5006 * Call this function if we thought we were going to exit, but we won't
5007 * (because of an error). May need to restore the terminal mode.
5008 */
5009 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005010not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005011{
5012 exiting = FALSE;
5013 settmode(TMODE_RAW);
5014}
5015
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005016 static int
5017before_quit_autocmds(win_T *wp, int quit_all, int forceit)
5018{
5019 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, wp->w_buffer);
5020
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02005021 // Bail out when autocommands closed the window.
5022 // Refuse to quit when the buffer in the last window is being closed (can
5023 // only happen in autocommands).
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005024 if (!win_valid(wp)
5025 || curbuf_locked()
5026 || (wp->w_buffer->b_nwindows == 1 && wp->w_buffer->b_locked > 0))
5027 return TRUE;
5028
5029 if (quit_all || (check_more(FALSE, forceit) == OK && only_one_window()))
5030 {
5031 apply_autocmds(EVENT_EXITPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02005032 // Refuse to quit when locked or when the window was closed or the
5033 // buffer in the last window is being closed (can only happen in
5034 // autocommands).
5035 if (!win_valid(wp) || curbuf_locked()
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005036 || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
5037 return TRUE;
5038 }
5039
5040 return FALSE;
5041}
5042
Bram Moolenaar071d4272004-06-13 20:20:40 +00005043/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01005044 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005045 * ":{nr}quit": quit window {nr}
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02005046 * Also used when closing a terminal window that's the last one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047 */
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02005048 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005049ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005050{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005051 win_T *wp;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005052
Bram Moolenaar071d4272004-06-13 20:20:40 +00005053#ifdef FEAT_CMDWIN
5054 if (cmdwin_type != 0)
5055 {
5056 cmdwin_result = Ctrl_C;
5057 return;
5058 }
5059#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005060 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005061 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005062 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005063 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005064 return;
5065 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005066 if (eap->addr_count > 0)
5067 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01005068 int wnr = eap->line2;
5069
5070 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
5071 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005072 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005073 }
5074 else
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005075 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01005076
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005077 // Refuse to quit when locked.
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02005078 if (curbuf_locked())
5079 return;
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005080
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005081 // Trigger QuitPre and maybe ExitPre
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005082 if (before_quit_autocmds(wp, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005083 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005084
5085#ifdef FEAT_NETBEANS_INTG
5086 netbeansForcedQuit = eap->forceit;
5087#endif
5088
5089 /*
5090 * If there are more files or windows we won't exit.
5091 */
5092 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
5093 exiting = TRUE;
Bram Moolenaarff930ca2017-10-19 17:12:10 +02005094 if ((!buf_hide(wp->w_buffer)
5095 && check_changed(wp->w_buffer, (p_awa ? CCGD_AW : 0)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01005096 | (eap->forceit ? CCGD_FORCEIT : 0)
5097 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005098 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01005099 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005100 {
5101 not_exiting();
5102 }
5103 else
5104 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005105 // quit last window
5106 // Note: only_one_window() returns true, even so a help window is
5107 // still open. In that case only quit, if no address has been
5108 // specified. Example:
5109 // :h|wincmd w|1q - don't quit
5110 // :h|wincmd w|q - quit
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01005111 if (only_one_window() && (ONE_WINDOW || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005112 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02005113 not_exiting();
Bram Moolenaar4033c552017-09-16 20:54:51 +02005114#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005115 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005116#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005117 // close window; may free buffer
Bram Moolenaareb44a682017-08-03 22:44:55 +02005118 win_close(wp, !buf_hide(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005119 }
5120}
5121
5122/*
5123 * ":cquit".
5124 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005125 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005126ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005127{
Bram Moolenaar1860bde2020-01-06 21:47:21 +01005128 // this does not always pass on the exit code to the Manx compiler. why?
5129 getout(eap->addr_count > 0 ? (int)eap->line2 : EXIT_FAILURE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005130}
5131
5132/*
5133 * ":qall": try to quit all windows
5134 */
5135 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005136ex_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005137{
5138# ifdef FEAT_CMDWIN
5139 if (cmdwin_type != 0)
5140 {
5141 if (eap->forceit)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005142 cmdwin_result = K_XF1; // ex_window() takes care of this
Bram Moolenaar071d4272004-06-13 20:20:40 +00005143 else
5144 cmdwin_result = K_XF2;
5145 return;
5146 }
5147# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005148
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005149 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005150 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005151 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005152 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005153 return;
5154 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005155
5156 if (before_quit_autocmds(curwin, TRUE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005157 return;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005158
Bram Moolenaar071d4272004-06-13 20:20:40 +00005159 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01005160 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005161 getout(0);
5162 not_exiting();
5163}
5164
Bram Moolenaar071d4272004-06-13 20:20:40 +00005165/*
5166 * ":close": close current window, unless it is the last one
5167 */
5168 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005169ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005170{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005171 win_T *win;
5172 int winnr = 0;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005173#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02005175 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005176 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005177#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005178 if (!text_locked() && !curbuf_locked())
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005179 {
5180 if (eap->addr_count == 0)
5181 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar6d41c782018-06-06 09:11:12 +02005182 else
5183 {
Bram Moolenaar29323592016-07-24 22:04:11 +02005184 FOR_ALL_WINDOWS(win)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005185 {
5186 winnr++;
5187 if (winnr == eap->line2)
5188 break;
5189 }
5190 if (win == NULL)
5191 win = lastwin;
5192 ex_win_close(eap->forceit, win, NULL);
5193 }
5194 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005195}
5196
Bram Moolenaar4033c552017-09-16 20:54:51 +02005197#ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005198/*
5199 * ":pclose": Close any preview window.
5200 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005202ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005203{
5204 win_T *win;
5205
Bram Moolenaar79648732019-07-18 21:43:07 +02005206 // First close any normal window.
Bram Moolenaar29323592016-07-24 22:04:11 +02005207 FOR_ALL_WINDOWS(win)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005208 if (win->w_p_pvw)
5209 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005210 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar79648732019-07-18 21:43:07 +02005211 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005212 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005213# ifdef FEAT_PROP_POPUP
Bram Moolenaar79648732019-07-18 21:43:07 +02005214 // Also when 'previewpopup' is empty, it might have been cleared.
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02005215 popup_close_preview();
Bram Moolenaar79648732019-07-18 21:43:07 +02005216# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005217}
Bram Moolenaar4033c552017-09-16 20:54:51 +02005218#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005219
Bram Moolenaarf740b292006-02-16 22:11:02 +00005220/*
5221 * Close window "win" and take care of handling closing the last window for a
5222 * modified buffer.
5223 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005224 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005225ex_win_close(
5226 int forceit,
5227 win_T *win,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005228 tabpage_T *tp) // NULL or the tab page "win" is in
Bram Moolenaar071d4272004-06-13 20:20:40 +00005229{
5230 int need_hide;
5231 buf_T *buf = win->w_buffer;
5232
Bram Moolenaarcf844172020-06-26 19:44:06 +02005233 // Never close the autocommand window.
5234 if (win == aucmd_win)
5235 {
5236 emsg(_(e_autocmd_close));
5237 return;
5238 }
5239
Bram Moolenaar071d4272004-06-13 20:20:40 +00005240 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02005241 if (need_hide && !buf_hide(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005242 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02005243#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005244 if ((p_confirm || cmdmod.confirm) && p_write)
5245 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005246 bufref_T bufref;
5247
5248 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005249 dialog_changed(buf, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005250 if (bufref_valid(&bufref) && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005251 return;
5252 need_hide = FALSE;
5253 }
5254 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005255#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005256 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02005257 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005258 return;
5259 }
5260 }
5261
Bram Moolenaar4033c552017-09-16 20:54:51 +02005262#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005263 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005264#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005265
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005266 // free buffer when not hiding it or when it's a scratch buffer
Bram Moolenaarf740b292006-02-16 22:11:02 +00005267 if (tp == NULL)
Bram Moolenaareb44a682017-08-03 22:44:55 +02005268 win_close(win, !need_hide && !buf_hide(buf));
Bram Moolenaarf740b292006-02-16 22:11:02 +00005269 else
Bram Moolenaareb44a682017-08-03 22:44:55 +02005270 win_close_othertab(win, !need_hide && !buf_hide(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005271}
5272
Bram Moolenaar071d4272004-06-13 20:20:40 +00005273/*
Bram Moolenaardea25702017-01-29 15:18:10 +01005274 * Handle the argument for a tabpage related ex command.
5275 * Returns a tabpage number.
5276 * When an error is encountered then eap->errmsg is set.
5277 */
5278 static int
5279get_tabpage_arg(exarg_T *eap)
5280{
5281 int tab_number;
5282 int unaccept_arg0 = (eap->cmdidx == CMD_tabmove) ? 0 : 1;
5283
5284 if (eap->arg && *eap->arg != NUL)
5285 {
5286 char_u *p = eap->arg;
5287 char_u *p_save;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005288 int relative = 0; // argument +N/-N means: go to N places to the
5289 // right/left relative to the current position.
Bram Moolenaardea25702017-01-29 15:18:10 +01005290
5291 if (*p == '-')
5292 {
5293 relative = -1;
5294 p++;
5295 }
5296 else if (*p == '+')
5297 {
5298 relative = 1;
5299 p++;
5300 }
5301
5302 p_save = p;
5303 tab_number = getdigits(&p);
5304
5305 if (relative == 0)
5306 {
5307 if (STRCMP(p, "$") == 0)
5308 tab_number = LAST_TAB_NR;
5309 else if (p == p_save || *p_save == '-' || *p != NUL
5310 || tab_number > LAST_TAB_NR)
5311 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005312 // No numbers as argument.
Bram Moolenaardea25702017-01-29 15:18:10 +01005313 eap->errmsg = e_invarg;
5314 goto theend;
5315 }
5316 }
5317 else
5318 {
5319 if (*p_save == NUL)
5320 tab_number = 1;
5321 else if (p == p_save || *p_save == '-' || *p != NUL
5322 || tab_number == 0)
5323 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005324 // No numbers as argument.
Bram Moolenaardea25702017-01-29 15:18:10 +01005325 eap->errmsg = e_invarg;
5326 goto theend;
5327 }
5328 tab_number = tab_number * relative + tabpage_index(curtab);
5329 if (!unaccept_arg0 && relative == -1)
5330 --tab_number;
5331 }
5332 if (tab_number < unaccept_arg0 || tab_number > LAST_TAB_NR)
5333 eap->errmsg = e_invarg;
5334 }
5335 else if (eap->addr_count > 0)
5336 {
5337 if (unaccept_arg0 && eap->line2 == 0)
Bram Moolenaarc6251552017-01-29 21:42:20 +01005338 {
Bram Moolenaardea25702017-01-29 15:18:10 +01005339 eap->errmsg = e_invrange;
Bram Moolenaarc6251552017-01-29 21:42:20 +01005340 tab_number = 0;
5341 }
Bram Moolenaardea25702017-01-29 15:18:10 +01005342 else
5343 {
5344 tab_number = eap->line2;
Bram Moolenaar82a12462019-01-22 22:41:42 +01005345 if (!unaccept_arg0 && *skipwhite(*eap->cmdlinep) == '-')
Bram Moolenaardea25702017-01-29 15:18:10 +01005346 {
5347 --tab_number;
5348 if (tab_number < unaccept_arg0)
5349 eap->errmsg = e_invarg;
5350 }
5351 }
5352 }
5353 else
5354 {
5355 switch (eap->cmdidx)
5356 {
5357 case CMD_tabnext:
5358 tab_number = tabpage_index(curtab) + 1;
5359 if (tab_number > LAST_TAB_NR)
5360 tab_number = 1;
5361 break;
5362 case CMD_tabmove:
5363 tab_number = LAST_TAB_NR;
5364 break;
5365 default:
5366 tab_number = tabpage_index(curtab);
5367 }
5368 }
5369
5370theend:
5371 return tab_number;
5372}
5373
5374/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00005375 * ":tabclose": close current tab page, unless it is the last one.
5376 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005377 */
5378 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005379ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005380{
Bram Moolenaarf740b292006-02-16 22:11:02 +00005381 tabpage_T *tp;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005382 int tab_number;
Bram Moolenaarf740b292006-02-16 22:11:02 +00005383
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005384# ifdef FEAT_CMDWIN
5385 if (cmdwin_type != 0)
5386 cmdwin_result = K_IGNORE;
5387 else
5388# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005389 if (first_tabpage->tp_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005390 emsg(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00005391 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005392 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005393 tab_number = get_tabpage_arg(eap);
5394 if (eap->errmsg == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005395 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005396 tp = find_tabpage(tab_number);
Bram Moolenaarf740b292006-02-16 22:11:02 +00005397 if (tp == NULL)
5398 {
5399 beep_flush();
5400 return;
5401 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005402 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005403 {
5404 tabpage_close_other(tp, eap->forceit);
5405 return;
5406 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005407 else if (!text_locked() && !curbuf_locked())
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005408 tabpage_close(eap->forceit);
5409 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005410 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005411}
5412
5413/*
5414 * ":tabonly": close all tab pages except the current one
5415 */
5416 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005417ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005418{
5419 tabpage_T *tp;
5420 int done;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005421 int tab_number;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005422
5423# ifdef FEAT_CMDWIN
5424 if (cmdwin_type != 0)
5425 cmdwin_result = K_IGNORE;
5426 else
5427# endif
5428 if (first_tabpage->tp_next == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005429 msg(_("Already only one tab page"));
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005430 else
5431 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005432 tab_number = get_tabpage_arg(eap);
5433 if (eap->errmsg == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005434 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005435 goto_tabpage(tab_number);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005436 // Repeat this up to a 1000 times, because autocommands may
5437 // mess up the lists.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005438 for (done = 0; done < 1000; ++done)
5439 {
5440 FOR_ALL_TABPAGES(tp)
5441 if (tp->tp_topframe != topframe)
5442 {
5443 tabpage_close_other(tp, eap->forceit);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005444 // if we failed to close it quit
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005445 if (valid_tabpage(tp))
5446 done = 1000;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005447 // start over, "tp" is now invalid
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005448 break;
5449 }
5450 if (first_tabpage->tp_next == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005451 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005452 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005453 }
5454 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005455}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005456
5457/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00005458 * Close the current tab page.
5459 */
5460 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005461tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005462{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005463 // First close all the windows but the current one. If that worked then
5464 // close the last window in this tab, that will close it.
Bram Moolenaar459ca562016-11-10 18:16:33 +01005465 if (!ONE_WINDOW)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005466 close_others(TRUE, forceit);
Bram Moolenaar459ca562016-11-10 18:16:33 +01005467 if (ONE_WINDOW)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005468 ex_win_close(forceit, curwin, NULL);
5469# ifdef FEAT_GUI
5470 need_mouse_correct = TRUE;
5471# endif
5472}
5473
5474/*
5475 * Close tab page "tp", which is not the current tab page.
5476 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005477 * Also takes care of the tab pages line disappearing when closing the
5478 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00005479 */
5480 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005481tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005482{
5483 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005484 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005485 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00005486
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005487 // Limit to 1000 windows, autocommands may add a window while we close
5488 // one. OK, so I'm paranoid...
Bram Moolenaarf740b292006-02-16 22:11:02 +00005489 while (++done < 1000)
5490 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005491 wp = tp->tp_firstwin;
5492 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00005493
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005494 // Autocommands may delete the tab page under our fingers and we may
5495 // fail to close a window with a modified buffer.
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005496 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005497 break;
5498 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005499
Bram Moolenaar29323592016-07-24 22:04:11 +02005500 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar29323592016-07-24 22:04:11 +02005501
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005502 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005503 if (h != tabline_height())
5504 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00005505}
5506
5507/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005508 * ":only".
5509 */
5510 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005511ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005512{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005513 win_T *wp;
5514 int wnr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005515# ifdef FEAT_GUI
5516 need_mouse_correct = TRUE;
5517# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005518 if (eap->addr_count > 0)
5519 {
5520 wnr = eap->line2;
5521 for (wp = firstwin; --wnr > 0; )
5522 {
5523 if (wp->w_next == NULL)
5524 break;
5525 else
5526 wp = wp->w_next;
5527 }
5528 win_goto(wp);
5529 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005530 close_others(TRUE, eap->forceit);
5531}
5532
Bram Moolenaar071d4272004-06-13 20:20:40 +00005533 static void
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01005534ex_hide(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005535{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005536 // ":hide" or ":hide | cmd": hide current window
Bram Moolenaar2256c992016-11-15 21:17:07 +01005537 if (!eap->skip)
5538 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02005539#ifdef FEAT_GUI
Bram Moolenaar2256c992016-11-15 21:17:07 +01005540 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005541#endif
Bram Moolenaar2256c992016-11-15 21:17:07 +01005542 if (eap->addr_count == 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005543 win_close(curwin, FALSE); // don't free buffer
Bram Moolenaar2256c992016-11-15 21:17:07 +01005544 else
5545 {
5546 int winnr = 0;
5547 win_T *win;
Bram Moolenaarf240e182014-11-27 18:33:02 +01005548
Bram Moolenaar2256c992016-11-15 21:17:07 +01005549 FOR_ALL_WINDOWS(win)
5550 {
5551 winnr++;
5552 if (winnr == eap->line2)
5553 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005554 }
Bram Moolenaar2256c992016-11-15 21:17:07 +01005555 if (win == NULL)
5556 win = lastwin;
5557 win_close(win, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005558 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005559 }
5560}
5561
5562/*
5563 * ":stop" and ":suspend": Suspend Vim.
5564 */
5565 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005566ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005567{
5568 /*
5569 * Disallow suspending for "rvim".
5570 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005571 if (!check_restricted())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005572 {
5573 if (!eap->forceit)
5574 autowrite_all();
5575 windgoto((int)Rows - 1, 0);
5576 out_char('\n');
5577 out_flush();
5578 stoptermcap();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005579 out_flush(); // needed for SUN to restore xterm buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00005580#ifdef FEAT_TITLE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005581 mch_restore_title(SAVE_RESTORE_BOTH); // restore window titles
Bram Moolenaar071d4272004-06-13 20:20:40 +00005582#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005583 ui_suspend(); // call machine specific function
Bram Moolenaar071d4272004-06-13 20:20:40 +00005584#ifdef FEAT_TITLE
5585 maketitle();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005586 resettitle(); // force updating the title
Bram Moolenaar071d4272004-06-13 20:20:40 +00005587#endif
5588 starttermcap();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005589 scroll_start(); // scroll screen before redrawing
Bram Moolenaar071d4272004-06-13 20:20:40 +00005590 redraw_later_clear();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005591 shell_resized(); // may have resized window
Bram Moolenaar071d4272004-06-13 20:20:40 +00005592 }
5593}
5594
5595/*
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005596 * ":exit", ":xit" and ":wq": Write file and quite the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005597 */
5598 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005599ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005600{
5601#ifdef FEAT_CMDWIN
5602 if (cmdwin_type != 0)
5603 {
5604 cmdwin_result = Ctrl_C;
5605 return;
5606 }
5607#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005608 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005609 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005610 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005611 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005612 return;
5613 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005614
5615 if (before_quit_autocmds(curwin, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005616 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005617
5618 /*
5619 * if more files or windows we won't exit
5620 */
5621 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
5622 exiting = TRUE;
5623 if ( ((eap->cmdidx == CMD_wq
5624 || curbufIsChanged())
5625 && do_write(eap) == FAIL)
5626 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01005627 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005628 {
5629 not_exiting();
5630 }
5631 else
5632 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005633 if (only_one_window()) // quit last window, exit Vim
Bram Moolenaar071d4272004-06-13 20:20:40 +00005634 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02005635 not_exiting();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005636# ifdef FEAT_GUI
5637 need_mouse_correct = TRUE;
5638# endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005639 // Quit current window, may free the buffer.
Bram Moolenaareb44a682017-08-03 22:44:55 +02005640 win_close(curwin, !buf_hide(curwin->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005641 }
5642}
5643
5644/*
5645 * ":print", ":list", ":number".
5646 */
5647 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005648ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005649{
Bram Moolenaardf177f62005-02-22 08:39:57 +00005650 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005651 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +00005652 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005653 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00005654 for ( ;!got_int; ui_breakcheck())
5655 {
5656 print_line(eap->line1,
5657 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
5658 || (eap->flags & EXFLAG_NR)),
5659 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
5660 if (++eap->line1 > eap->line2)
5661 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005662 out_flush(); // show one line at a time
Bram Moolenaardf177f62005-02-22 08:39:57 +00005663 }
5664 setpcmark();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005665 // put cursor at last line
Bram Moolenaardf177f62005-02-22 08:39:57 +00005666 curwin->w_cursor.lnum = eap->line2;
5667 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005668 }
5669
Bram Moolenaar071d4272004-06-13 20:20:40 +00005670 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005671}
5672
5673#ifdef FEAT_BYTEOFF
5674 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005675ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005676{
5677 goto_byte(eap->line2);
5678}
5679#endif
5680
5681/*
5682 * ":shell".
5683 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005684 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005685ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005686{
5687 do_shell(NULL, 0);
5688}
5689
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005690#if defined(HAVE_DROP_FILE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005691
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005692static int drop_busy = FALSE;
5693static int drop_filec;
5694static char_u **drop_filev = NULL;
5695static int drop_split;
5696static void (*drop_callback)(void *);
5697static void *drop_cookie;
5698
5699 static void
5700handle_drop_internal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005701{
5702 exarg_T ea;
5703 int save_msg_scroll = msg_scroll;
5704
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005705 // Setting the argument list may cause screen updates and being called
5706 // recursively. Avoid that by setting drop_busy.
5707 drop_busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005708
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005709 // Check whether the current buffer is changed. If so, we will need
5710 // to split the current window or data could be lost.
5711 // We don't need to check if the 'hidden' option is set, as in this
5712 // case the buffer won't be lost.
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005713 if (!buf_hide(curbuf) && !drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005714 {
5715 ++emsg_off;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005716 drop_split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005717 --emsg_off;
5718 }
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005719 if (drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005720 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005721 if (win_split(0, 0) == FAIL)
5722 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02005723 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005724
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005725 // When splitting the window, create a new alist. Otherwise the
5726 // existing one is overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005727 alist_unlink(curwin->w_alist);
5728 alist_new();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005729 }
5730
5731 /*
5732 * Set up the new argument list.
5733 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005734 alist_set(ALIST(curwin), drop_filec, drop_filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005735
5736 /*
5737 * Move to the first file.
5738 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005739 // Fake up a minimal "next" command for do_argfile()
Bram Moolenaara80faa82020-04-12 19:37:17 +02005740 CLEAR_FIELD(ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005741 ea.cmd = (char_u *)"next";
5742 do_argfile(&ea, 0);
5743
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005744 // do_ecmd() may set need_start_insertmode, but since we never left Insert
5745 // mode that is not needed here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005746 need_start_insertmode = FALSE;
5747
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005748 // Restore msg_scroll, otherwise a following command may cause scrolling
5749 // unexpectedly. The screen will be redrawn by the caller, thus
5750 // msg_scroll being set by displaying a message is irrelevant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005751 msg_scroll = save_msg_scroll;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005752
5753 if (drop_callback != NULL)
5754 drop_callback(drop_cookie);
5755
5756 drop_filev = NULL;
5757 drop_busy = FALSE;
5758}
5759
5760/*
5761 * Handle a file drop. The code is here because a drop is *nearly* like an
5762 * :args command, but not quite (we have a list of exact filenames, so we
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01005763 * don't want to (a) parse a command line, or (b) expand wildcards). So the
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005764 * code is very similar to :args and hence needs access to a lot of the static
5765 * functions in this file.
5766 *
5767 * The "filev" list must have been allocated using alloc(), as should each item
5768 * in the list. This function takes over responsibility for freeing the "filev"
5769 * list.
5770 */
5771 void
5772handle_drop(
5773 int filec, // the number of files dropped
5774 char_u **filev, // the list of files dropped
5775 int split, // force splitting the window
5776 void (*callback)(void *), // to be called after setting the argument
5777 // list
5778 void *cookie) // argument for "callback" (allocated)
5779{
5780 // Cannot handle recursive drops, finish the pending one.
5781 if (drop_busy)
5782 {
5783 FreeWild(filec, filev);
5784 vim_free(cookie);
5785 return;
5786 }
5787
5788 // When calling handle_drop() more than once in a row we only use the last
5789 // one.
5790 if (drop_filev != NULL)
5791 {
5792 FreeWild(drop_filec, drop_filev);
5793 vim_free(drop_cookie);
5794 }
5795
5796 drop_filec = filec;
5797 drop_filev = filev;
5798 drop_split = split;
5799 drop_callback = callback;
5800 drop_cookie = cookie;
5801
5802 // Postpone this when:
5803 // - editing the command line
5804 // - not possible to change the current buffer
5805 // - updating the screen
5806 // As it may change buffers and window structures that are in use and cause
5807 // freed memory to be used.
5808 if (text_locked() || curbuf_locked() || updating_screen)
5809 return;
5810
5811 handle_drop_internal();
5812}
5813
5814/*
5815 * To be called when text is unlocked, curbuf is unlocked or updating_screen is
5816 * reset: Handle a postponed drop.
5817 */
5818 void
5819handle_any_postponed_drop(void)
5820{
5821 if (!drop_busy && drop_filev != NULL
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02005822 && !text_locked() && !curbuf_locked() && !updating_screen)
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005823 handle_drop_internal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005824}
5825#endif
5826
Bram Moolenaar071d4272004-06-13 20:20:40 +00005827/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005828 * ":preserve".
5829 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005830 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005831ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005832{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005833 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005834 ml_preserve(curbuf, TRUE);
5835}
5836
5837/*
5838 * ":recover".
5839 */
5840 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005841ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005842{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005843 // Set recoverymode right away to avoid the ATTENTION prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005844 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01005845 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
5846 | CCGD_MULTWIN
5847 | (eap->forceit ? CCGD_FORCEIT : 0)
5848 | CCGD_EXCMD)
5849
Bram Moolenaar071d4272004-06-13 20:20:40 +00005850 && (*eap->arg == NUL
5851 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
Bram Moolenaar99499b12019-05-23 21:35:48 +02005852 ml_recover(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005853 recoverymode = FALSE;
5854}
5855
5856/*
5857 * Command modifier used in a wrong way.
5858 */
5859 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005860ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005861{
5862 eap->errmsg = e_invcmd;
5863}
5864
Bram Moolenaar071d4272004-06-13 20:20:40 +00005865/*
5866 * :sview [+command] file split window with new file, read-only
5867 * :split [[+command] file] split window with current or new file
5868 * :vsplit [[+command] file] split window vertically with current or new file
5869 * :new [[+command] file] split window with no or new file
5870 * :vnew [[+command] file] split vertically window with no or new file
5871 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005872 *
5873 * :tabedit open new Tab page with empty window
5874 * :tabedit [+command] file open new Tab page and edit "file"
5875 * :tabnew [[+command] file] just like :tabedit
5876 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005877 */
5878 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005879ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005880{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005881 win_T *old_curwin = curwin;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005882#if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005883 char_u *fname = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005884#endif
5885#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005886 int browse_flag = cmdmod.browse;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005887#endif
Bram Moolenaar39902a02018-06-21 22:10:08 +02005888 int use_tab = eap->cmdidx == CMD_tabedit
5889 || eap->cmdidx == CMD_tabfind
5890 || eap->cmdidx == CMD_tabnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005891
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005892 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005893 return;
5894
Bram Moolenaar4033c552017-09-16 20:54:51 +02005895#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005896 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005897#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005898
Bram Moolenaar4033c552017-09-16 20:54:51 +02005899#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005900 // A ":split" in the quickfix window works like ":new". Don't want two
5901 // quickfix windows. But it's OK when doing ":tab split".
Bram Moolenaar05bb9532008-07-04 09:44:11 +00005902 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005903 {
5904 if (eap->cmdidx == CMD_split)
5905 eap->cmdidx = CMD_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005906 if (eap->cmdidx == CMD_vsplit)
5907 eap->cmdidx = CMD_vnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005908 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02005909#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005910
Bram Moolenaar4033c552017-09-16 20:54:51 +02005911#ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005912 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005913 {
5914 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
5915 FNAME_MESS, TRUE, curbuf->b_ffname);
5916 if (fname == NULL)
5917 goto theend;
5918 eap->arg = fname;
5919 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005920# ifdef FEAT_BROWSE
Bram Moolenaar4033c552017-09-16 20:54:51 +02005921 else
5922# endif
5923#endif
5924#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005925 if (cmdmod.browse
Bram Moolenaar071d4272004-06-13 20:20:40 +00005926 && eap->cmdidx != CMD_vnew
Bram Moolenaar071d4272004-06-13 20:20:40 +00005927 && eap->cmdidx != CMD_new)
5928 {
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005929 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005930# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005931 !gui.in_use &&
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005932# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005933 au_has_group((char_u *)"FileExplorer"))
5934 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005935 // No browsing supported but we do have the file explorer:
5936 // Edit the directory.
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005937 if (*eap->arg == NUL || !mch_isdir(eap->arg))
5938 eap->arg = (char_u *)".";
5939 }
5940 else
5941 {
Bram Moolenaar39902a02018-06-21 22:10:08 +02005942 fname = do_browse(0, (char_u *)(use_tab
5943 ? _("Edit File in new tab page")
5944 : _("Edit File in new window")),
Bram Moolenaar071d4272004-06-13 20:20:40 +00005945 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005946 if (fname == NULL)
5947 goto theend;
5948 eap->arg = fname;
5949 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005950 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005951 cmdmod.browse = FALSE; // Don't browse again in do_ecmd().
Bram Moolenaar4033c552017-09-16 20:54:51 +02005952#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005953
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005954 /*
5955 * Either open new tab page or split the window.
5956 */
Bram Moolenaar39902a02018-06-21 22:10:08 +02005957 if (use_tab)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005958 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005959 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
5960 : eap->addr_count == 0 ? 0
5961 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005962 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00005963 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005964
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005965 // set the alternate buffer for the window we came from
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005966 if (curwin != old_curwin
5967 && win_valid(old_curwin)
5968 && old_curwin->w_buffer != curbuf
5969 && !cmdmod.keepalt)
5970 old_curwin->w_alt_fnum = curbuf->b_fnum;
5971 }
5972 }
5973 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005974 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
5975 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005976 // Reset 'scrollbind' when editing another file, but keep it when
5977 // doing ":split" without arguments.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005978 if (*eap->arg != NUL
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01005979# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005980 || cmdmod.browse
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01005981# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005982 )
Bram Moolenaar3368ea22010-09-21 16:56:35 +02005983 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005984 else
5985 do_check_scrollbind(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005986 do_exedit(eap, old_curwin);
5987 }
5988
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005989# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005990 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005991# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005992
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005993# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005994theend:
5995 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005996# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005997}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005998
5999/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006000 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006001 */
6002 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006003tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006004{
6005 exarg_T ea;
6006
Bram Moolenaara80faa82020-04-12 19:37:17 +02006007 CLEAR_FIELD(ea);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006008 ea.cmdidx = CMD_tabnew;
6009 ea.cmd = (char_u *)"tabn";
6010 ea.arg = (char_u *)"";
6011 ex_splitview(&ea);
6012}
6013
6014/*
6015 * :tabnext command
6016 */
6017 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006018ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006019{
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006020 int tab_number;
6021
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02006022 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006023 return;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006024 switch (eap->cmdidx)
6025 {
6026 case CMD_tabfirst:
6027 case CMD_tabrewind:
6028 goto_tabpage(1);
6029 break;
6030 case CMD_tablast:
6031 goto_tabpage(9999);
6032 break;
6033 case CMD_tabprevious:
6034 case CMD_tabNext:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006035 if (eap->arg && *eap->arg != NUL)
6036 {
6037 char_u *p = eap->arg;
6038 char_u *p_save = p;
6039
6040 tab_number = getdigits(&p);
6041 if (p == p_save || *p_save == '-' || *p != NUL
6042 || tab_number == 0)
6043 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006044 // No numbers as argument.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006045 eap->errmsg = e_invarg;
6046 return;
6047 }
6048 }
6049 else
6050 {
6051 if (eap->addr_count == 0)
6052 tab_number = 1;
6053 else
6054 {
6055 tab_number = eap->line2;
6056 if (tab_number < 1)
6057 {
6058 eap->errmsg = e_invrange;
6059 return;
6060 }
6061 }
6062 }
6063 goto_tabpage(-tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006064 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006065 default: // CMD_tabnext
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006066 tab_number = get_tabpage_arg(eap);
6067 if (eap->errmsg == NULL)
6068 goto_tabpage(tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006069 break;
6070 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006071}
6072
6073/*
6074 * :tabmove command
6075 */
6076 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006077ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006078{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02006079 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02006080
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006081 tab_number = get_tabpage_arg(eap);
6082 if (eap->errmsg == NULL)
6083 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006084}
6085
6086/*
6087 * :tabs command: List tabs and their contents.
6088 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006089 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006090ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006091{
6092 tabpage_T *tp;
6093 win_T *wp;
6094 int tabcount = 1;
6095
6096 msg_start();
6097 msg_scroll = TRUE;
6098 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
6099 {
6100 msg_putchar('\n');
6101 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
Bram Moolenaar8820b482017-03-16 17:23:31 +01006102 msg_outtrans_attr(IObuff, HL_ATTR(HLF_T));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006103 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006104 ui_breakcheck();
6105
Bram Moolenaar030f0df2006-02-21 22:02:53 +00006106 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006107 wp = firstwin;
6108 else
6109 wp = tp->tp_firstwin;
6110 for ( ; wp != NULL && !got_int; wp = wp->w_next)
6111 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006112 msg_putchar('\n');
6113 msg_putchar(wp == curwin ? '>' : ' ');
6114 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006115 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
6116 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006117 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02006118 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006119 else
6120 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
6121 IObuff, IOSIZE, TRUE);
6122 msg_outtrans(IObuff);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006123 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006124 ui_breakcheck();
6125 }
6126 }
6127}
6128
Bram Moolenaar071d4272004-06-13 20:20:40 +00006129/*
6130 * ":mode": Set screen mode.
6131 * If no argument given, just get the screen size and redraw.
6132 */
6133 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006134ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006135{
6136 if (*eap->arg == NUL)
6137 shell_resized();
6138 else
Bram Moolenaar3c71aec2020-01-17 19:32:20 +01006139 emsg(_(e_screenmode));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006140}
6141
Bram Moolenaar071d4272004-06-13 20:20:40 +00006142/*
6143 * ":resize".
6144 * set, increment or decrement current window height
6145 */
6146 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006147ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006148{
6149 int n;
6150 win_T *wp = curwin;
6151
6152 if (eap->addr_count > 0)
6153 {
6154 n = eap->line2;
6155 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
6156 ;
6157 }
6158
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006159# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006160 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006161# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006162 n = atol((char *)eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006163 if (cmdmod.split & WSP_VERT)
6164 {
6165 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar02631462017-09-22 15:20:32 +02006166 n += curwin->w_width;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006167 else if (n == 0 && eap->arg[0] == NUL) // default is very wide
Bram Moolenaar071d4272004-06-13 20:20:40 +00006168 n = 9999;
6169 win_setwidth_win((int)n, wp);
6170 }
6171 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006172 {
6173 if (*eap->arg == '-' || *eap->arg == '+')
6174 n += curwin->w_height;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006175 else if (n == 0 && eap->arg[0] == NUL) // default is very high
Bram Moolenaar071d4272004-06-13 20:20:40 +00006176 n = 9999;
6177 win_setheight_win((int)n, wp);
6178 }
6179}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006180
6181/*
6182 * ":find [+command] <file>" command.
6183 */
6184 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006185ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006186{
6187#ifdef FEAT_SEARCHPATH
6188 char_u *fname;
6189 int count;
6190
6191 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
6192 TRUE, curbuf->b_ffname);
6193 if (eap->addr_count > 0)
6194 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006195 // Repeat finding the file "count" times. This matters when it
6196 // appears several times in the path.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006197 count = eap->line2;
6198 while (fname != NULL && --count > 0)
6199 {
6200 vim_free(fname);
6201 fname = find_file_in_path(NULL, 0, FNAME_MESS,
6202 FALSE, curbuf->b_ffname);
6203 }
6204 }
6205
6206 if (fname != NULL)
6207 {
6208 eap->arg = fname;
6209#endif
6210 do_exedit(eap, NULL);
6211#ifdef FEAT_SEARCHPATH
6212 vim_free(fname);
6213 }
6214#endif
6215}
6216
6217/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00006218 * ":open" simulation: for now just work like ":visual".
6219 */
6220 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006221ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006222{
6223 regmatch_T regmatch;
6224 char_u *p;
6225
6226 curwin->w_cursor.lnum = eap->line2;
6227 beginline(BL_SOL | BL_FIX);
6228 if (*eap->arg == '/')
6229 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006230 // ":open /pattern/": put cursor in column found with pattern
Bram Moolenaardf177f62005-02-22 08:39:57 +00006231 ++eap->arg;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006232 p = skip_regexp(eap->arg, '/', p_magic);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006233 *p = NUL;
6234 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
6235 if (regmatch.regprog != NULL)
6236 {
6237 regmatch.rm_ic = p_ic;
6238 p = ml_get_curline();
6239 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006240 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006241 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006242 emsg(_(e_nomatch));
Bram Moolenaar473de612013-06-08 18:19:48 +02006243 vim_regfree(regmatch.regprog);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006244 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006245 // Move to the NUL, ignore any other arguments.
Bram Moolenaardf177f62005-02-22 08:39:57 +00006246 eap->arg += STRLEN(eap->arg);
6247 }
6248 check_cursor();
6249
6250 eap->cmdidx = CMD_visual;
6251 do_exedit(eap, NULL);
6252}
6253
6254/*
6255 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006256 */
6257 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006258ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006259{
6260 do_exedit(eap, NULL);
6261}
6262
6263/*
Bram Moolenaar4b96df52020-01-26 22:00:26 +01006264 * ":edit <file>" command and alike.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006265 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006266 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006267do_exedit(
6268 exarg_T *eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006269 win_T *old_curwin) // curwin before doing a split or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006270{
6271 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006272 int need_hide;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006273 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006274
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01006275 if ((eap->cmdidx != CMD_pedit && ERROR_IF_POPUP_WINDOW)
6276 || ERROR_IF_TERM_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006277 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006278 /*
6279 * ":vi" command ends Ex mode.
6280 */
6281 if (exmode_active && (eap->cmdidx == CMD_visual
6282 || eap->cmdidx == CMD_view))
6283 {
6284 exmode_active = FALSE;
Bram Moolenaar158ea172020-06-18 17:28:39 +02006285 ex_pressedreturn = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006286 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006287 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006288 // Special case: ":global/pat/visual\NLvi-commands"
Bram Moolenaardf177f62005-02-22 08:39:57 +00006289 if (global_busy)
6290 {
6291 int rd = RedrawingDisabled;
6292 int nwr = no_wait_return;
6293 int ms = msg_scroll;
6294#ifdef FEAT_GUI
6295 int he = hold_gui_events;
6296#endif
6297
6298 if (eap->nextcmd != NULL)
6299 {
6300 stuffReadbuff(eap->nextcmd);
6301 eap->nextcmd = NULL;
6302 }
6303
6304 if (exmode_was != EXMODE_VIM)
6305 settmode(TMODE_RAW);
6306 RedrawingDisabled = 0;
6307 no_wait_return = 0;
6308 need_wait_return = FALSE;
6309 msg_scroll = 0;
6310#ifdef FEAT_GUI
6311 hold_gui_events = 0;
6312#endif
6313 must_redraw = CLEAR;
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01006314 pending_exmode_active = TRUE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006315
6316 main_loop(FALSE, TRUE);
6317
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01006318 pending_exmode_active = FALSE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006319 RedrawingDisabled = rd;
6320 no_wait_return = nwr;
6321 msg_scroll = ms;
6322#ifdef FEAT_GUI
6323 hold_gui_events = he;
6324#endif
6325 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006326 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006327 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006328 }
6329
6330 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006331 || eap->cmdidx == CMD_tabnew
6332 || eap->cmdidx == CMD_tabedit
Bram Moolenaar4033c552017-09-16 20:54:51 +02006333 || eap->cmdidx == CMD_vnew) && *eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006334 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006335 // ":new" or ":tabnew" without argument: edit an new empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006336 setpcmark();
6337 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006338 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
6339 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006340 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006341 else if ((eap->cmdidx != CMD_split && eap->cmdidx != CMD_vsplit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006342 || *eap->arg != NUL
6343#ifdef FEAT_BROWSE
6344 || cmdmod.browse
6345#endif
6346 )
6347 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006348 // Can't edit another file when "curbuf_lock" is set. Only ":edit"
6349 // can bring us here, others are stopped earlier.
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006350 if (*eap->arg != NUL && curbuf_locked())
6351 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006352
Bram Moolenaar071d4272004-06-13 20:20:40 +00006353 n = readonlymode;
6354 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
6355 readonlymode = TRUE;
6356 else if (eap->cmdidx == CMD_enew)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006357 readonlymode = FALSE; // 'readonly' doesn't make sense in an
6358 // empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006359 setpcmark();
6360 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
6361 NULL, eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006362 // ":edit" goes to first line if Vi compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00006363 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
6364 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
6365 ? ECMD_ONE : eap->do_ecmd_lnum,
Bram Moolenaareb44a682017-08-03 22:44:55 +02006366 (buf_hide(curbuf) ? ECMD_HIDE : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006367 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006368 // after a split we can use an existing buffer
Bram Moolenaar7b449342014-03-25 13:03:48 +01006369 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006370 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006371 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006372 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006373 // Editing the file failed. If the window was split, close it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006374 if (old_curwin != NULL)
6375 {
6376 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02006377 if (!need_hide || buf_hide(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006378 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006379#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006380 cleanup_T cs;
6381
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006382 // Reset the error/interrupt/exception state here so that
6383 // aborting() returns FALSE when closing a window.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006384 enter_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02006385#endif
6386#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006387 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006388#endif
Bram Moolenaareb44a682017-08-03 22:44:55 +02006389 win_close(curwin, !need_hide && !buf_hide(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006390
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006391#if defined(FEAT_EVAL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006392 // Restore the error/interrupt/exception state if not
6393 // discarded by a new aborting error, interrupt, or
6394 // uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006395 leave_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02006396#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006397 }
6398 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006399 }
6400 else if (readonlymode && curbuf->b_nwindows == 1)
6401 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006402 // When editing an already visited buffer, 'readonly' won't be set
6403 // but the previous value is kept. With ":view" and ":sview" we
6404 // want the file to be readonly, except when another window is
6405 // editing the same buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006406 curbuf->b_p_ro = TRUE;
6407 }
6408 readonlymode = n;
6409 }
6410 else
6411 {
6412 if (eap->do_ecmd_cmd != NULL)
6413 do_cmdline_cmd(eap->do_ecmd_cmd);
6414#ifdef FEAT_TITLE
6415 n = curwin->w_arg_idx_invalid;
6416#endif
6417 check_arg_idx(curwin);
6418#ifdef FEAT_TITLE
6419 if (n != curwin->w_arg_idx_invalid)
6420 maketitle();
6421#endif
6422 }
6423
Bram Moolenaar071d4272004-06-13 20:20:40 +00006424 /*
6425 * if ":split file" worked, set alternate file name in old window to new
6426 * file
6427 */
6428 if (old_curwin != NULL
6429 && *eap->arg != NUL
6430 && curwin != old_curwin
6431 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00006432 && old_curwin->w_buffer != curbuf
6433 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006434 old_curwin->w_alt_fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006435
6436 ex_no_reprint = TRUE;
6437}
6438
6439#ifndef FEAT_GUI
6440/*
6441 * ":gui" and ":gvim" when there is no GUI.
6442 */
6443 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006444ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006445{
6446 eap->errmsg = e_nogvim;
6447}
6448#endif
6449
Bram Moolenaar4f974752019-02-17 17:44:42 +01006450#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006451 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006452ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006453{
6454 gui_make_tearoff(eap->arg);
6455}
6456#endif
6457
Bram Moolenaar29a2c082018-03-05 21:06:23 +01006458#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
6459 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006460 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006461ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006462{
Bram Moolenaar29a2c082018-03-05 21:06:23 +01006463# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
6464 if (gui.in_use)
6465 gui_make_popup(eap->arg, eap->forceit);
6466# ifdef FEAT_TERM_POPUP_MENU
6467 else
6468# endif
6469# endif
6470# ifdef FEAT_TERM_POPUP_MENU
6471 pum_make_popup(eap->arg, eap->forceit);
6472# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006473}
6474#endif
6475
Bram Moolenaar071d4272004-06-13 20:20:40 +00006476 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006477ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006478{
6479 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006480 msg(_("No swap file"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006481 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01006482 msg((char *)curbuf->b_ml.ml_mfp->mf_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006483}
6484
6485/*
6486 * ":syncbind" forces all 'scrollbind' windows to have the same relative
6487 * offset.
6488 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
6489 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006490 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006491ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006492{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006493 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006494 win_T *save_curwin = curwin;
6495 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006496 long topline;
6497 long y;
6498 linenr_T old_linenr = curwin->w_cursor.lnum;
6499
6500 setpcmark();
6501
6502 /*
6503 * determine max topline
6504 */
6505 if (curwin->w_p_scb)
6506 {
6507 topline = curwin->w_topline;
Bram Moolenaar29323592016-07-24 22:04:11 +02006508 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006509 {
6510 if (wp->w_p_scb && wp->w_buffer)
6511 {
Bram Moolenaar375e3392019-01-31 18:26:10 +01006512 y = wp->w_buffer->b_ml.ml_line_count - get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006513 if (topline > y)
6514 topline = y;
6515 }
6516 }
6517 if (topline < 1)
6518 topline = 1;
6519 }
6520 else
6521 {
6522 topline = 1;
6523 }
6524
6525
6526 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006527 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006528 */
Bram Moolenaar29323592016-07-24 22:04:11 +02006529 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006530 {
6531 if (curwin->w_p_scb)
6532 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006533 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006534 y = topline - curwin->w_topline;
6535 if (y > 0)
6536 scrollup(y, TRUE);
6537 else
6538 scrolldown(-y, TRUE);
6539 curwin->w_scbind_pos = topline;
6540 redraw_later(VALID);
6541 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006542 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006543 }
6544 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006545 curwin = save_curwin;
6546 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006547 if (curwin->w_p_scb)
6548 {
6549 did_syncbind = TRUE;
6550 checkpcmark();
6551 if (old_linenr != curwin->w_cursor.lnum)
6552 {
6553 char_u ctrl_o[2];
6554
6555 ctrl_o[0] = Ctrl_O;
6556 ctrl_o[1] = 0;
6557 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
6558 }
6559 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006560}
6561
6562
6563 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006564ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006565{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006566 int i;
6567 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
6568 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006569
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006570 if (eap->usefilter) // :r!cmd
Bram Moolenaar071d4272004-06-13 20:20:40 +00006571 do_bang(1, eap, FALSE, FALSE, TRUE);
6572 else
6573 {
6574 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
6575 return;
6576
6577#ifdef FEAT_BROWSE
6578 if (cmdmod.browse)
6579 {
6580 char_u *browseFile;
6581
Bram Moolenaar7171abe2004-10-11 10:06:20 +00006582 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006583 NULL, NULL, NULL, curbuf);
6584 if (browseFile != NULL)
6585 {
6586 i = readfile(browseFile, NULL,
6587 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
6588 vim_free(browseFile);
6589 }
6590 else
6591 i = OK;
6592 }
6593 else
6594#endif
6595 if (*eap->arg == NUL)
6596 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006597 if (check_fname() == FAIL) // check for no file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00006598 return;
6599 i = readfile(curbuf->b_ffname, curbuf->b_fname,
6600 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
6601 }
6602 else
6603 {
6604 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
6605 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
6606 i = readfile(eap->arg, NULL,
6607 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
6608
6609 }
Bram Moolenaare13b9af2017-01-13 22:01:02 +01006610 if (i != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006611 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006612#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006613 if (!aborting())
6614#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006615 semsg(_(e_notopen), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006616 }
6617 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00006618 {
6619 if (empty && exmode_active)
6620 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006621 // Delete the empty line that remains. Historically ex does
6622 // this but vi doesn't.
Bram Moolenaardf177f62005-02-22 08:39:57 +00006623 if (eap->line2 == 0)
6624 lnum = curbuf->b_ml.ml_line_count;
6625 else
6626 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00006627 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006628 {
Bram Moolenaarca70c072020-05-30 20:30:46 +02006629 ml_delete(lnum);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00006630 if (curwin->w_cursor.lnum > 1
6631 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006632 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00006633 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006634 }
6635 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006636 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006637 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006638 }
6639}
6640
Bram Moolenaarea408852005-06-25 22:49:46 +00006641static char_u *prev_dir = NULL;
6642
6643#if defined(EXITFREE) || defined(PROTO)
6644 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006645free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00006646{
Bram Moolenaard23a8232018-02-10 18:45:26 +01006647 VIM_CLEAR(prev_dir);
6648 VIM_CLEAR(globaldir);
Bram Moolenaarea408852005-06-25 22:49:46 +00006649}
6650#endif
6651
Bram Moolenaarf4258302013-06-02 18:20:17 +02006652/*
Bram Moolenaar002bc792020-06-05 22:33:42 +02006653 * Get the previous directory for the given chdir scope.
6654 */
6655 static char_u *
6656get_prevdir(cdscope_T scope)
6657{
6658 if (scope == CDSCOPE_WINDOW)
6659 return curwin->w_prevdir;
6660 else if (scope == CDSCOPE_TABPAGE)
6661 return curtab->tp_prevdir;
6662 return prev_dir;
6663}
6664
6665/*
Bram Moolenaarf4258302013-06-02 18:20:17 +02006666 * Deal with the side effects of changing the current directory.
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006667 * When 'scope' is CDSCOPE_TABPAGE then this was after an ":tcd" command.
6668 * When 'scope' is CDSCOPE_WINDOW then this was after an ":lcd" command.
Bram Moolenaarf4258302013-06-02 18:20:17 +02006669 */
6670 void
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006671post_chdir(cdscope_T scope)
Bram Moolenaarf4258302013-06-02 18:20:17 +02006672{
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006673 if (scope != CDSCOPE_WINDOW)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006674 // Clear tab local directory for both :cd and :tcd
6675 VIM_CLEAR(curtab->tp_localdir);
Bram Moolenaard23a8232018-02-10 18:45:26 +01006676 VIM_CLEAR(curwin->w_localdir);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006677 if (scope != CDSCOPE_GLOBAL)
Bram Moolenaarf4258302013-06-02 18:20:17 +02006678 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02006679 char_u *pdir = get_prevdir(scope);
6680
6681 // If still in the global directory, need to remember current
6682 // directory as the global directory.
6683 if (globaldir == NULL && pdir != NULL)
6684 globaldir = vim_strsave(pdir);
6685
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006686 // Remember this local directory for the window.
Bram Moolenaarf4258302013-06-02 18:20:17 +02006687 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006688 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006689 if (scope == CDSCOPE_TABPAGE)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006690 curtab->tp_localdir = vim_strsave(NameBuff);
6691 else
6692 curwin->w_localdir = vim_strsave(NameBuff);
6693 }
Bram Moolenaarf4258302013-06-02 18:20:17 +02006694 }
6695 else
6696 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02006697 // We are now in the global directory, no need to remember its name.
Bram Moolenaard23a8232018-02-10 18:45:26 +01006698 VIM_CLEAR(globaldir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006699 }
6700
6701 shorten_fnames(TRUE);
6702}
6703
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006704/*
6705 * Change directory function used by :cd/:tcd/:lcd Ex commands and the
Bram Moolenaar95058722020-06-01 16:26:19 +02006706 * chdir() function.
6707 * scope == CDSCOPE_WINDOW: changes the window-local directory
6708 * scope == CDSCOPE_TABPAGE: changes the tab-local directory
6709 * Otherwise: changes the global directory
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006710 * Returns TRUE if the directory is successfully changed.
6711 */
6712 int
6713changedir_func(
6714 char_u *new_dir,
6715 int forceit,
6716 cdscope_T scope)
6717{
6718 char_u *tofree;
Bram Moolenaar002bc792020-06-05 22:33:42 +02006719 char_u *pdir = NULL;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006720 int dir_differs;
6721 int retval = FALSE;
6722
Bram Moolenaar7cc96922020-01-31 22:41:38 +01006723 if (new_dir == NULL || allbuf_locked())
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006724 return FALSE;
6725
6726 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged() && !forceit)
6727 {
6728 emsg(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
6729 return FALSE;
6730 }
6731
6732 // ":cd -": Change to previous directory
6733 if (STRCMP(new_dir, "-") == 0)
6734 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02006735 pdir = get_prevdir(scope);
6736 if (pdir == NULL)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006737 {
6738 emsg(_("E186: No previous directory"));
6739 return FALSE;
6740 }
Bram Moolenaar002bc792020-06-05 22:33:42 +02006741 new_dir = pdir;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006742 }
6743
Bram Moolenaar002bc792020-06-05 22:33:42 +02006744 // Free the previous directory
6745 tofree = get_prevdir(scope);
6746
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006747 // Save current directory for next ":cd -"
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006748 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar002bc792020-06-05 22:33:42 +02006749 pdir = vim_strsave(NameBuff);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006750 else
Bram Moolenaar002bc792020-06-05 22:33:42 +02006751 pdir = NULL;
6752 if (scope == CDSCOPE_WINDOW)
6753 curwin->w_prevdir = pdir;
6754 else if (scope == CDSCOPE_TABPAGE)
6755 curtab->tp_prevdir = pdir;
6756 else
6757 prev_dir = pdir;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006758
6759#if defined(UNIX) || defined(VMS)
6760 // for UNIX ":cd" means: go to home directory
6761 if (*new_dir == NUL)
6762 {
6763 // use NameBuff for home directory name
6764# ifdef VMS
6765 char_u *p;
6766
6767 p = mch_getenv((char_u *)"SYS$LOGIN");
6768 if (p == NULL || *p == NUL) // empty is the same as not set
6769 NameBuff[0] = NUL;
6770 else
6771 vim_strncpy(NameBuff, p, MAXPATHL - 1);
6772# else
6773 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
6774# endif
6775 new_dir = NameBuff;
6776 }
6777#endif
Bram Moolenaar002bc792020-06-05 22:33:42 +02006778 dir_differs = new_dir == NULL || pdir == NULL
6779 || pathcmp((char *)pdir, (char *)new_dir, -1) != 0;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006780 if (new_dir == NULL || (dir_differs && vim_chdir(new_dir)))
6781 emsg(_(e_failed));
6782 else
6783 {
6784 char_u *acmd_fname;
6785
6786 post_chdir(scope);
6787
6788 if (dir_differs)
6789 {
6790 if (scope == CDSCOPE_WINDOW)
6791 acmd_fname = (char_u *)"window";
6792 else if (scope == CDSCOPE_TABPAGE)
6793 acmd_fname = (char_u *)"tabpage";
6794 else
6795 acmd_fname = (char_u *)"global";
6796 apply_autocmds(EVENT_DIRCHANGED, acmd_fname, new_dir, FALSE,
6797 curbuf);
6798 }
6799 retval = TRUE;
6800 }
6801 vim_free(tofree);
6802
6803 return retval;
6804}
Bram Moolenaarea408852005-06-25 22:49:46 +00006805
Bram Moolenaar071d4272004-06-13 20:20:40 +00006806/*
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006807 * ":cd", ":tcd", ":lcd", ":chdir" ":tchdir" and ":lchdir".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006808 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00006809 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006810ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006811{
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01006812 char_u *new_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006813
6814 new_dir = eap->arg;
6815#if !defined(UNIX) && !defined(VMS)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006816 // for non-UNIX ":cd" means: print current directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006817 if (*new_dir == NUL)
6818 ex_pwd(NULL);
6819 else
6820#endif
6821 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006822 cdscope_T scope = CDSCOPE_GLOBAL;
6823
6824 if (eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir)
6825 scope = CDSCOPE_WINDOW;
6826 else if (eap->cmdidx == CMD_tcd || eap->cmdidx == CMD_tchdir)
6827 scope = CDSCOPE_TABPAGE;
6828
6829 if (changedir_func(new_dir, eap->forceit, scope))
Bram Moolenaardf177f62005-02-22 08:39:57 +00006830 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006831 // Echo the new current directory if the command was typed.
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00006832 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006833 ex_pwd(eap);
6834 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006835 }
6836}
6837
6838/*
6839 * ":pwd".
6840 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006841 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006842ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006843{
6844 if (mch_dirname(NameBuff, MAXPATHL) == OK)
6845 {
6846#ifdef BACKSLASH_IN_FILENAME
6847 slash_adjust(NameBuff);
6848#endif
Bram Moolenaar95058722020-06-01 16:26:19 +02006849 if (p_verbose > 0)
6850 {
6851 char *context = "global";
6852
6853 if (curwin->w_localdir != NULL)
6854 context = "window";
6855 else if (curtab->tp_localdir != NULL)
6856 context = "tabpage";
6857 smsg("[%s] %s", context, (char *)NameBuff);
6858 }
6859 else
6860 msg((char *)NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006861 }
6862 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006863 emsg(_("E187: Unknown"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006864}
6865
6866/*
6867 * ":=".
6868 */
6869 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006870ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006871{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006872 smsg("%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006873 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006874}
6875
6876 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006877ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006878{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006879 int n;
6880 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006881
6882 if (cursor_valid())
6883 {
6884 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
6885 if (n >= 0)
Bram Moolenaar53f81742017-09-22 14:35:51 +02006886 windgoto((int)n, curwin->w_wincol + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006887 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006888
6889 len = eap->line2;
6890 switch (*eap->arg)
6891 {
6892 case 'm': break;
6893 case NUL: len *= 1000L; break;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006894 default: semsg(_(e_invarg2), eap->arg); return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006895 }
6896 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006897}
6898
6899/*
6900 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
6901 */
6902 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006903do_sleep(long msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006904{
Bram Moolenaar52953192019-08-16 10:27:13 +02006905 long done = 0;
Bram Moolenaar975b5272016-03-15 23:10:59 +01006906 long wait_now;
Bram Moolenaar52953192019-08-16 10:27:13 +02006907# ifdef ELAPSED_FUNC
6908 elapsed_T start_tv;
6909
6910 // Remember at what time we started, so that we know how much longer we
6911 // should wait after waiting for a bit.
6912 ELAPSED_INIT(start_tv);
6913# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006914
6915 cursor_on();
Bram Moolenaarf45d7472018-09-30 18:22:26 +02006916 out_flush_cursor(FALSE, FALSE);
Bram Moolenaar52953192019-08-16 10:27:13 +02006917 while (!got_int && done < msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006918 {
Bram Moolenaar975b5272016-03-15 23:10:59 +01006919 wait_now = msec - done > 1000L ? 1000L : msec - done;
6920#ifdef FEAT_TIMERS
6921 {
6922 long due_time = check_due_timer();
6923
6924 if (due_time > 0 && due_time < wait_now)
6925 wait_now = due_time;
6926 }
6927#endif
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02006928#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar28e67e02019-08-15 23:05:49 +02006929 if (has_any_channel() && wait_now > 20L)
6930 wait_now = 20L;
6931#endif
6932#ifdef FEAT_SOUND
6933 if (has_any_sound_callback() && wait_now > 20L)
6934 wait_now = 20L;
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02006935#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +01006936 ui_delay(wait_now, TRUE);
Bram Moolenaar52953192019-08-16 10:27:13 +02006937
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02006938#ifdef FEAT_JOB_CHANNEL
6939 if (has_any_channel())
6940 ui_breakcheck_force(TRUE);
6941 else
6942#endif
6943 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006944#ifdef MESSAGE_QUEUE
Bram Moolenaar52953192019-08-16 10:27:13 +02006945 // Process the netbeans and clientserver messages that may have been
6946 // received in the call to ui_breakcheck() when the GUI is in use. This
6947 // may occur when running a test case.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006948 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02006949#endif
Bram Moolenaar52953192019-08-16 10:27:13 +02006950
6951# ifdef ELAPSED_FUNC
6952 // actual time passed
6953 done = ELAPSED_FUNC(start_tv);
6954# else
6955 // guestimate time passed (will actually be more)
6956 done += wait_now;
6957# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006958 }
Bram Moolenaar1ebff3d2018-07-07 16:18:13 +02006959
6960 // If CTRL-C was typed to interrupt the sleep, drop the CTRL-C from the
6961 // input buffer, otherwise a following call to input() fails.
6962 if (got_int)
6963 (void)vpeekc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006964}
6965
Bram Moolenaar071d4272004-06-13 20:20:40 +00006966/*
6967 * ":winsize" command (obsolete).
6968 */
6969 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006970ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006971{
6972 int w, h;
6973 char_u *arg = eap->arg;
6974 char_u *p;
6975
6976 w = getdigits(&arg);
6977 arg = skipwhite(arg);
6978 p = arg;
6979 h = getdigits(&arg);
6980 if (*p != NUL && *arg == NUL)
6981 set_shellsize(w, h, TRUE);
6982 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006983 emsg(_("E465: :winsize requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006984}
6985
Bram Moolenaar071d4272004-06-13 20:20:40 +00006986 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006987ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006988{
6989 int xchar = NUL;
6990 char_u *p;
6991
6992 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
6993 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006994 // CTRL-W g and CTRL-W CTRL-G have an extra command character
Bram Moolenaar071d4272004-06-13 20:20:40 +00006995 if (eap->arg[1] == NUL)
6996 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006997 emsg(_(e_invarg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006998 return;
6999 }
7000 xchar = eap->arg[1];
7001 p = eap->arg + 2;
7002 }
7003 else
7004 p = eap->arg + 1;
7005
7006 eap->nextcmd = check_nextcmd(p);
7007 p = skipwhite(p);
7008 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007009 emsg(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02007010 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007011 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007012 // Pass flags on for ":vertical wincmd ]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007013 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00007014 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007015 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
7016 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00007017 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007018 }
7019}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007020
Bram Moolenaar843ee412004-06-30 16:16:41 +00007021#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007022/*
7023 * ":winpos".
7024 */
7025 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007026ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007027{
7028 int x, y;
7029 char_u *arg = eap->arg;
7030 char_u *p;
7031
7032 if (*arg == NUL)
7033 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00007034# if defined(FEAT_GUI) || defined(MSWIN)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007035# ifdef VIMDLL
7036 if (gui.in_use ? gui_mch_get_winpos(&x, &y) != FAIL :
7037 mch_get_winpos(&x, &y) != FAIL)
7038# elif defined(FEAT_GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007039 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00007040# else
7041 if (mch_get_winpos(&x, &y) != FAIL)
7042# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007043 {
7044 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
Bram Moolenaar32526b32019-01-19 17:43:09 +01007045 msg((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007046 }
7047 else
7048# endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007049 emsg(_("E188: Obtaining window position not implemented for this platform"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007050 }
7051 else
7052 {
7053 x = getdigits(&arg);
7054 arg = skipwhite(arg);
7055 p = arg;
7056 y = getdigits(&arg);
7057 if (*p == NUL || *arg != NUL)
7058 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007059 emsg(_("E466: :winpos requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007060 return;
7061 }
7062# ifdef FEAT_GUI
7063 if (gui.in_use)
7064 gui_mch_set_winpos(x, y);
7065 else if (gui.starting)
7066 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007067 // Remember the coordinates for when the window is opened.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007068 gui_win_x = x;
7069 gui_win_y = y;
7070 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007071# if defined(HAVE_TGETENT) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007072 else
7073# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007074# endif
7075# if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar843ee412004-06-30 16:16:41 +00007076 mch_set_winpos(x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007077# endif
7078# ifdef HAVE_TGETENT
7079 if (*T_CWP)
7080 term_set_winpos(x, y);
7081# endif
7082 }
7083}
7084#endif
7085
7086/*
7087 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
7088 */
7089 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007090ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007091{
7092 oparg_T oa;
7093
7094 clear_oparg(&oa);
7095 oa.regname = eap->regname;
7096 oa.start.lnum = eap->line1;
7097 oa.end.lnum = eap->line2;
7098 oa.line_count = eap->line2 - eap->line1 + 1;
7099 oa.motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007100 virtual_op = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007101 if (eap->cmdidx != CMD_yank) // position cursor for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00007102 {
7103 setpcmark();
7104 curwin->w_cursor.lnum = eap->line1;
7105 beginline(BL_SOL | BL_FIX);
7106 }
7107
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007108 if (VIsual_active)
7109 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007110
Bram Moolenaar071d4272004-06-13 20:20:40 +00007111 switch (eap->cmdidx)
7112 {
7113 case CMD_delete:
7114 oa.op_type = OP_DELETE;
7115 op_delete(&oa);
7116 break;
7117
7118 case CMD_yank:
7119 oa.op_type = OP_YANK;
7120 (void)op_yank(&oa, FALSE, TRUE);
7121 break;
7122
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007123 default: // CMD_rshift or CMD_lshift
Bram Moolenaar6e707362013-06-14 19:15:58 +02007124 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007125#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02007126 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
7127#else
7128 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00007129#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02007130 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007131 oa.op_type = OP_RSHIFT;
7132 else
7133 oa.op_type = OP_LSHIFT;
7134 op_shift(&oa, FALSE, eap->amount);
7135 break;
7136 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007137 virtual_op = MAYBE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007138 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007139}
7140
7141/*
7142 * ":put".
7143 */
7144 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007145ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007146{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007147 // ":0put" works like ":1put!".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007148 if (eap->line2 == 0)
7149 {
7150 eap->line2 = 1;
7151 eap->forceit = TRUE;
7152 }
7153 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007154 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
7155 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007156}
7157
7158/*
7159 * Handle ":copy" and ":move".
7160 */
7161 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007162ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007163{
7164 long n;
7165
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02007166 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE, FALSE, 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007167 if (eap->arg == NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00007168 {
7169 eap->nextcmd = NULL;
7170 return;
7171 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00007172 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007173
7174 /*
7175 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
7176 */
7177 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
7178 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02007179 emsg(_(e_invrange));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007180 return;
7181 }
7182
7183 if (eap->cmdidx == CMD_move)
7184 {
7185 if (do_move(eap->line1, eap->line2, n) == FAIL)
7186 return;
7187 }
7188 else
7189 ex_copy(eap->line1, eap->line2, n);
7190 u_clearline();
7191 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007192 ex_may_print(eap);
7193}
7194
7195/*
7196 * Print the current line if flags were given to the Ex command.
7197 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02007198 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007199ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007200{
7201 if (eap->flags != 0)
7202 {
7203 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
7204 (eap->flags & EXFLAG_LIST));
7205 ex_no_reprint = TRUE;
7206 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007207}
7208
7209/*
7210 * ":smagic" and ":snomagic".
7211 */
7212 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007213ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007214{
7215 int magic_save = p_magic;
7216
7217 p_magic = (eap->cmdidx == CMD_smagic);
7218 do_sub(eap);
7219 p_magic = magic_save;
7220}
7221
7222/*
7223 * ":join".
7224 */
7225 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007226ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007227{
7228 curwin->w_cursor.lnum = eap->line1;
7229 if (eap->line1 == eap->line2)
7230 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007231 if (eap->addr_count >= 2) // :2,2join does nothing
Bram Moolenaar071d4272004-06-13 20:20:40 +00007232 return;
7233 if (eap->line2 == curbuf->b_ml.ml_line_count)
7234 {
7235 beep_flush();
7236 return;
7237 }
7238 ++eap->line2;
7239 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02007240 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007241 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007242 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007243}
7244
7245/*
7246 * ":[addr]@r" or ":[addr]*r": execute register
7247 */
7248 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007249ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007250{
7251 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00007252 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007253
7254 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4930a762016-09-11 14:39:53 +02007255 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007256
7257#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007258 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00007259#endif
7260
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007261 // get the register name. No name means to use the previous one
Bram Moolenaar071d4272004-06-13 20:20:40 +00007262 c = *eap->arg;
7263 if (c == NUL || (c == '*' && *eap->cmd == '*'))
7264 c = '@';
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007265 // Put the register in the typeahead buffer with the "silent" flag.
Bram Moolenaard333d1e2006-11-07 17:43:47 +00007266 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
7267 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007268 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007269 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00007270 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007271 else
7272 {
7273 int save_efr = exec_from_reg;
7274
7275 exec_from_reg = TRUE;
7276
7277 /*
7278 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00007279 * Continue until the stuff buffer is empty and all added characters
7280 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007281 */
Bram Moolenaar60462872009-11-03 11:40:19 +00007282 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007283 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
7284
7285 exec_from_reg = save_efr;
7286 }
7287}
7288
7289/*
7290 * ":!".
7291 */
7292 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007293ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007294{
7295 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
7296}
7297
7298/*
7299 * ":undo".
7300 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007301 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01007302ex_undo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007303{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007304 if (eap->addr_count == 1) // :undo 123
Bram Moolenaar730cde92010-06-27 05:18:54 +02007305 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00007306 else
7307 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007308}
7309
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007310#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02007311 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007312ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007313{
7314 char_u hash[UNDO_HASH_SIZE];
7315
7316 u_compute_hash(hash);
7317 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
7318}
7319
Bram Moolenaar6df6f472010-07-18 18:04:50 +02007320 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007321ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007322{
7323 char_u hash[UNDO_HASH_SIZE];
7324
7325 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02007326 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007327}
7328#endif
7329
Bram Moolenaar071d4272004-06-13 20:20:40 +00007330/*
7331 * ":redo".
7332 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007333 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007334ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007335{
7336 u_redo(1);
7337}
7338
7339/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007340 * ":earlier" and ":later".
7341 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007342 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007343ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007344{
7345 long count = 0;
7346 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02007347 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007348 char_u *p = eap->arg;
7349
7350 if (*p == NUL)
7351 count = 1;
7352 else if (isdigit(*p))
7353 {
7354 count = getdigits(&p);
7355 switch (*p)
7356 {
7357 case 's': ++p; sec = TRUE; break;
7358 case 'm': ++p; sec = TRUE; count *= 60; break;
7359 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02007360 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
7361 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007362 }
7363 }
7364
7365 if (*p != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007366 semsg(_(e_invarg2), eap->arg);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007367 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02007368 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
7369 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007370}
7371
7372/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007373 * ":redir": start/stop redirection.
7374 */
7375 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007376ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007377{
7378 char *mode;
7379 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00007380 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007381
Bram Moolenaarba768492016-07-08 15:32:54 +02007382#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02007383 if (redir_execute)
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007384 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007385 emsg(_("E930: Cannot use :redir inside execute()"));
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007386 return;
7387 }
Bram Moolenaarba768492016-07-08 15:32:54 +02007388#endif
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007389
Bram Moolenaar071d4272004-06-13 20:20:40 +00007390 if (STRICMP(eap->arg, "END") == 0)
7391 close_redir();
7392 else
7393 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007394 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007395 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007396 ++arg;
7397 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007398 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007399 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007400 mode = "a";
7401 }
7402 else
7403 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00007404 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007405
7406 close_redir();
7407
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007408 // Expand environment variables and "~/".
Bram Moolenaarca472992005-01-21 11:46:23 +00007409 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007410 if (fname == NULL)
7411 return;
7412#ifdef FEAT_BROWSE
7413 if (cmdmod.browse)
7414 {
7415 char_u *browseFile;
7416
Bram Moolenaar7171abe2004-10-11 10:06:20 +00007417 browseFile = do_browse(BROWSE_SAVE,
7418 (char_u *)_("Save Redirection"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +02007419 fname, NULL, NULL,
7420 (char_u *)_(BROWSE_FILTER_ALL_FILES), curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007421 if (browseFile == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007422 return; // operation cancelled
Bram Moolenaar071d4272004-06-13 20:20:40 +00007423 vim_free(fname);
7424 fname = browseFile;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007425 eap->forceit = TRUE; // since dialog already asked
Bram Moolenaar071d4272004-06-13 20:20:40 +00007426 }
7427#endif
7428
7429 redir_fd = open_exfile(fname, eap->forceit, mode);
7430 vim_free(fname);
7431 }
7432#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00007433 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007434 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007435 // redirect to a register a-z (resp. A-Z for appending)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007436 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00007437 ++arg;
7438 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007439# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00007440 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00007441 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00007442# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00007443 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007444 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007445 redir_reg = *arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007446 if (*arg == '>' && arg[1] == '>') // append
Bram Moolenaard9d30582005-05-18 22:10:28 +00007447 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00007448 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007449 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007450 // Can use both "@a" and "@a>".
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00007451 if (*arg == '>')
7452 arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007453 // Make register empty when not using @A-@Z and the
7454 // command is valid.
Bram Moolenaarc188b882007-10-19 14:20:54 +00007455 if (*arg == NUL && !isupper(redir_reg))
7456 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007457 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00007458 }
7459 if (*arg != NUL)
7460 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007461 redir_reg = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007462 semsg(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007463 }
7464 }
7465 else if (*arg == '=' && arg[1] == '>')
7466 {
7467 int append;
7468
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007469 // redirect to a variable
Bram Moolenaardf177f62005-02-22 08:39:57 +00007470 close_redir();
7471 arg += 2;
7472
7473 if (*arg == '>')
7474 {
7475 ++arg;
7476 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007477 }
7478 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00007479 append = FALSE;
7480
7481 if (var_redir_start(skipwhite(arg), append) == OK)
7482 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007483 }
7484#endif
7485
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007486 // TODO: redirect to a buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00007487
Bram Moolenaar071d4272004-06-13 20:20:40 +00007488 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007489 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007490 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00007491
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007492 // Make sure redirection is not off. Can happen for cmdline completion
7493 // that indirectly invokes a command to catch its output.
Bram Moolenaar29b2d262006-09-10 19:07:28 +00007494 if (redir_fd != NULL
7495#ifdef FEAT_EVAL
7496 || redir_reg || redir_vname
7497#endif
7498 )
7499 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007500}
7501
7502/*
7503 * ":redraw": force redraw
7504 */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01007505 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007506ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007507{
7508 int r = RedrawingDisabled;
7509 int p = p_lz;
7510
7511 RedrawingDisabled = 0;
7512 p_lz = FALSE;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01007513 validate_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007514 update_topline();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007515 update_screen(eap->forceit ? CLEAR : VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007516#ifdef FEAT_TITLE
7517 if (need_maketitle)
7518 maketitle();
7519#endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007520#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
7521# ifdef VIMDLL
7522 if (!gui.in_use)
7523# endif
7524 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01007525#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007526 RedrawingDisabled = r;
7527 p_lz = p;
7528
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007529 // Reset msg_didout, so that a message that's there is overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007530 msg_didout = FALSE;
7531 msg_col = 0;
7532
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007533 // No need to wait after an intentional redraw.
Bram Moolenaar56667a52013-07-17 11:54:28 +02007534 need_wait_return = FALSE;
7535
Bram Moolenaar071d4272004-06-13 20:20:40 +00007536 out_flush();
7537}
7538
7539/*
7540 * ":redrawstatus": force redraw of status line(s)
7541 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007542 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007543ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007544{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007545 int r = RedrawingDisabled;
7546 int p = p_lz;
7547
7548 RedrawingDisabled = 0;
7549 p_lz = FALSE;
7550 if (eap->forceit)
7551 status_redraw_all();
7552 else
7553 status_redraw_curbuf();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007554 update_screen(VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007555 RedrawingDisabled = r;
7556 p_lz = p;
7557 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007558}
7559
Bram Moolenaare12bab32019-01-08 22:02:56 +01007560/*
7561 * ":redrawtabline": force redraw of the tabline
7562 */
7563 static void
7564ex_redrawtabline(exarg_T *eap UNUSED)
7565{
7566 int r = RedrawingDisabled;
7567 int p = p_lz;
7568
7569 RedrawingDisabled = 0;
7570 p_lz = FALSE;
7571
7572 draw_tabline();
7573
7574 RedrawingDisabled = r;
7575 p_lz = p;
7576 out_flush();
7577}
7578
Bram Moolenaar071d4272004-06-13 20:20:40 +00007579 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007580close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007581{
7582 if (redir_fd != NULL)
7583 {
7584 fclose(redir_fd);
7585 redir_fd = NULL;
7586 }
7587#ifdef FEAT_EVAL
7588 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007589 if (redir_vname)
7590 {
7591 var_redir_stop();
7592 redir_vname = 0;
7593 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007594#endif
7595}
7596
Bram Moolenaarf96ae0b2019-07-28 15:21:55 +02007597#if (defined(FEAT_SESSION) || defined(FEAT_EVAL)) || defined(PROTO)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007598 int
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02007599vim_mkdir_emsg(char_u *name, int prot UNUSED)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007600{
7601 if (vim_mkdir(name, prot) != 0)
7602 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007603 semsg(_("E739: Cannot create directory: %s"), name);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007604 return FAIL;
7605 }
7606 return OK;
7607}
7608#endif
7609
Bram Moolenaar071d4272004-06-13 20:20:40 +00007610/*
7611 * Open a file for writing for an Ex command, with some checks.
7612 * Return file descriptor, or NULL on failure.
7613 */
7614 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007615open_exfile(
7616 char_u *fname,
7617 int forceit,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007618 char *mode) // "w" for create new file or "a" for append
Bram Moolenaar071d4272004-06-13 20:20:40 +00007619{
7620 FILE *fd;
7621
7622#ifdef UNIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007623 // with Unix it is possible to open a directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007624 if (mch_isdir(fname))
7625 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007626 semsg(_(e_isadir2), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007627 return NULL;
7628 }
7629#endif
7630 if (!forceit && *mode != 'a' && vim_fexists(fname))
7631 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007632 semsg(_("E189: \"%s\" exists (add ! to override)"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007633 return NULL;
7634 }
7635
7636 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007637 semsg(_("E190: Cannot open \"%s\" for writing"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007638
7639 return fd;
7640}
7641
7642/*
7643 * ":mark" and ":k".
7644 */
7645 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007646ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007647{
7648 pos_T pos;
7649
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007650 if (*eap->arg == NUL) // No argument?
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007651 emsg(_(e_argreq));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007652 else if (eap->arg[1] != NUL) // more than one character?
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007653 emsg(_(e_trailing));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007654 else
7655 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007656 pos = curwin->w_cursor; // save curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00007657 curwin->w_cursor.lnum = eap->line2;
7658 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007659 if (setmark(*eap->arg) == FAIL) // set mark
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007660 emsg(_("E191: Argument must be a letter or forward/backward quote"));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007661 curwin->w_cursor = pos; // restore curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00007662 }
7663}
7664
7665/*
7666 * Update w_topline, w_leftcol and the cursor position.
7667 */
7668 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007669update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007670{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007671 check_cursor(); // put cursor on valid line
Bram Moolenaar071d4272004-06-13 20:20:40 +00007672 update_topline();
7673 if (!curwin->w_p_wrap)
7674 validate_cursor();
7675 update_curswant();
7676}
7677
Bram Moolenaar071d4272004-06-13 20:20:40 +00007678/*
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007679 * Save the current State and go to Normal mode.
7680 * Return TRUE if the typeahead could be saved.
7681 */
7682 int
7683save_current_state(save_state_T *sst)
7684{
7685 sst->save_msg_scroll = msg_scroll;
7686 sst->save_restart_edit = restart_edit;
7687 sst->save_msg_didout = msg_didout;
7688 sst->save_State = State;
7689 sst->save_insertmode = p_im;
7690 sst->save_finish_op = finish_op;
7691 sst->save_opcount = opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01007692 sst->save_reg_executing = reg_executing;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007693
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007694 msg_scroll = FALSE; // no msg scrolling in Normal mode
7695 restart_edit = 0; // don't go to Insert mode
7696 p_im = FALSE; // don't use 'insertmode'
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007697
7698 /*
7699 * Save the current typeahead. This is required to allow using ":normal"
7700 * from an event handler and makes sure we don't hang when the argument
7701 * ends with half a command.
7702 */
7703 save_typeahead(&sst->tabuf);
7704 return sst->tabuf.typebuf_valid;
7705}
7706
7707 void
7708restore_current_state(save_state_T *sst)
7709{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007710 // Restore the previous typeahead.
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007711 restore_typeahead(&sst->tabuf);
7712
7713 msg_scroll = sst->save_msg_scroll;
7714 restart_edit = sst->save_restart_edit;
7715 p_im = sst->save_insertmode;
7716 finish_op = sst->save_finish_op;
7717 opcount = sst->save_opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01007718 reg_executing = sst->save_reg_executing;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007719 msg_didout |= sst->save_msg_didout; // don't reset msg_didout now
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007720
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007721 // Restore the state (needed when called from a function executed for
7722 // 'indentexpr'). Update the mouse and cursor, they may have changed.
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007723 State = sst->save_State;
7724#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007725 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007726#endif
7727}
7728
7729/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007730 * ":normal[!] {commands}": Execute normal mode commands.
7731 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01007732 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007733ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007734{
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007735 save_state_T save_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007736 char_u *arg = NULL;
7737 int l;
7738 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007739
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00007740 if (ex_normal_lock > 0)
7741 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007742 emsg(_(e_secure));
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00007743 return;
7744 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007745 if (ex_normal_busy >= p_mmd)
7746 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007747 emsg(_("E192: Recursive use of :normal too deep"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007748 return;
7749 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007750
Bram Moolenaar071d4272004-06-13 20:20:40 +00007751 /*
7752 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
7753 * this for the K_SPECIAL leading byte, otherwise special keys will not
7754 * work.
7755 */
7756 if (has_mbyte)
7757 {
7758 int len = 0;
7759
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007760 // Count the number of characters to be escaped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007761 for (p = eap->arg; *p != NUL; ++p)
7762 {
Bram Moolenaar13505972019-01-24 15:04:48 +01007763#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007764 if (*p == CSI) // leadbyte CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007765 len += 2;
Bram Moolenaar13505972019-01-24 15:04:48 +01007766#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007767 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007768 if (*++p == K_SPECIAL // trailbyte K_SPECIAL or CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01007769#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007770 || *p == CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01007771#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007772 )
7773 len += 2;
7774 }
7775 if (len > 0)
7776 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02007777 arg = alloc(STRLEN(eap->arg) + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007778 if (arg != NULL)
7779 {
7780 len = 0;
7781 for (p = eap->arg; *p != NUL; ++p)
7782 {
7783 arg[len++] = *p;
Bram Moolenaar13505972019-01-24 15:04:48 +01007784#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007785 if (*p == CSI)
7786 {
7787 arg[len++] = KS_EXTRA;
7788 arg[len++] = (int)KE_CSI;
7789 }
Bram Moolenaar13505972019-01-24 15:04:48 +01007790#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007791 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007792 {
7793 arg[len++] = *++p;
7794 if (*p == K_SPECIAL)
7795 {
7796 arg[len++] = KS_SPECIAL;
7797 arg[len++] = KE_FILLER;
7798 }
Bram Moolenaar13505972019-01-24 15:04:48 +01007799#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007800 else if (*p == CSI)
7801 {
7802 arg[len++] = KS_EXTRA;
7803 arg[len++] = (int)KE_CSI;
7804 }
Bram Moolenaar13505972019-01-24 15:04:48 +01007805#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007806 }
7807 arg[len] = NUL;
7808 }
7809 }
7810 }
7811 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007812
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007813 ++ex_normal_busy;
7814 if (save_current_state(&save_state))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007815 {
7816 /*
7817 * Repeat the :normal command for each line in the range. When no
7818 * range given, execute it just once, without positioning the cursor
7819 * first.
7820 */
7821 do
7822 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007823 if (eap->addr_count != 0)
7824 {
7825 curwin->w_cursor.lnum = eap->line1++;
7826 curwin->w_cursor.col = 0;
Bram Moolenaard5d37532017-03-27 23:02:07 +02007827 check_cursor_moved(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007828 }
7829
Bram Moolenaar13505972019-01-24 15:04:48 +01007830 exec_normal_cmd(arg != NULL
7831 ? arg
7832 : eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007833 }
7834 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
7835 }
7836
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007837 // Might not return to the main loop when in an event handler.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007838 update_topline_cursor();
7839
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007840 restore_current_state(&save_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007841 --ex_normal_busy;
Bram Moolenaareda73602014-11-05 09:53:23 +01007842 setmouse();
Bram Moolenaareda73602014-11-05 09:53:23 +01007843#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007844 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaareda73602014-11-05 09:53:23 +01007845#endif
7846
Bram Moolenaar071d4272004-06-13 20:20:40 +00007847 vim_free(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007848}
7849
7850/*
Bram Moolenaar64969662005-12-14 21:59:55 +00007851 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007852 */
7853 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007854ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007855{
Bram Moolenaarfd371682005-01-14 21:42:54 +00007856 if (eap->forceit)
7857 {
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007858 // cursor line can be zero on startup
Bram Moolenaar09ca9322017-09-26 17:40:45 +02007859 if (!curwin->w_cursor.lnum)
7860 curwin->w_cursor.lnum = 1;
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007861 set_cursor_for_append_to_line();
Bram Moolenaarfd371682005-01-14 21:42:54 +00007862 }
Bram Moolenaareb58a242020-04-19 18:13:19 +02007863#ifdef FEAT_TERMINAL
7864 // Ignore this when running in an active terminal.
7865 if (term_job_running(curbuf->b_term))
7866 return;
7867#endif
Bram Moolenaarfd371682005-01-14 21:42:54 +00007868
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007869 // Ignore the command when already in Insert mode. Inserting an
7870 // expression register that invokes a function can do this.
Bram Moolenaara40c5002005-01-09 21:16:21 +00007871 if (State & INSERT)
7872 return;
7873
Bram Moolenaar64969662005-12-14 21:59:55 +00007874 if (eap->cmdidx == CMD_startinsert)
7875 restart_edit = 'a';
7876 else if (eap->cmdidx == CMD_startreplace)
7877 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00007878 else
Bram Moolenaar64969662005-12-14 21:59:55 +00007879 restart_edit = 'V';
7880
7881 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007882 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00007883 if (eap->cmdidx == CMD_startinsert)
7884 restart_edit = 'i';
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007885 curwin->w_curswant = 0; // avoid MAXCOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007886 }
7887}
7888
7889/*
7890 * ":stopinsert"
7891 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007892 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007893ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007894{
7895 restart_edit = 0;
7896 stop_insert_mode = TRUE;
Bram Moolenaarfd773e92016-04-02 19:39:16 +02007897 clearmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007898}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007899
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007900/*
7901 * Execute normal mode command "cmd".
7902 * "remap" can be REMAP_NONE or REMAP_YES.
7903 */
7904 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007905exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007906{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007907 // Stuff the argument into the typeahead buffer.
Bram Moolenaar25281632016-01-21 23:32:32 +01007908 ins_typebuf(cmd, remap, 0, TRUE, silent);
Bram Moolenaar586c70c2018-10-02 16:23:58 +02007909 exec_normal(FALSE, FALSE, FALSE);
Bram Moolenaar25281632016-01-21 23:32:32 +01007910}
Bram Moolenaar25281632016-01-21 23:32:32 +01007911
Bram Moolenaar25281632016-01-21 23:32:32 +01007912/*
7913 * Execute normal_cmd() until there is no typeahead left.
Bram Moolenaar586c70c2018-10-02 16:23:58 +02007914 * When "use_vpeekc" is TRUE use vpeekc() to check for available chars.
Bram Moolenaar25281632016-01-21 23:32:32 +01007915 */
7916 void
Bram Moolenaar586c70c2018-10-02 16:23:58 +02007917exec_normal(int was_typed, int use_vpeekc, int may_use_terminal_loop UNUSED)
Bram Moolenaar25281632016-01-21 23:32:32 +01007918{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007919 oparg_T oa;
Bram Moolenaar905dd902019-04-07 14:21:47 +02007920 int c;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007921
Bram Moolenaar905dd902019-04-07 14:21:47 +02007922 // When calling vpeekc() from feedkeys() it will return Ctrl_C when there
7923 // is nothing to get, so also check for Ctrl_C.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007924 clear_oparg(&oa);
7925 finish_op = FALSE;
Bram Moolenaar586c70c2018-10-02 16:23:58 +02007926 while ((!stuff_empty()
7927 || ((was_typed || !typebuf_typed()) && typebuf.tb_len > 0)
Bram Moolenaar905dd902019-04-07 14:21:47 +02007928 || (use_vpeekc && (c = vpeekc()) != NUL && c != Ctrl_C))
Bram Moolenaar586c70c2018-10-02 16:23:58 +02007929 && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007930 {
7931 update_topline_cursor();
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02007932#ifdef FEAT_TERMINAL
Bram Moolenaar655a82a2018-05-08 22:01:07 +02007933 if (may_use_terminal_loop && term_use_loop()
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02007934 && oa.op_type == OP_NOP && oa.regname == NUL
7935 && !VIsual_active)
7936 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007937 // If terminal_loop() returns OK we got a key that is handled
7938 // in Normal model. With FAIL we first need to position the
7939 // cursor and the screen needs to be redrawn.
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02007940 if (terminal_loop(TRUE) == OK)
7941 normal_cmd(&oa, TRUE);
7942 }
7943 else
7944#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007945 // execute a Normal mode cmd
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02007946 normal_cmd(&oa, TRUE);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007947 }
7948}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007949
Bram Moolenaar071d4272004-06-13 20:20:40 +00007950#ifdef FEAT_FIND_ID
7951 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007952ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007953{
7954 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
7955 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
7956 (linenr_T)1, (linenr_T)MAXLNUM);
7957}
7958
Bram Moolenaar4033c552017-09-16 20:54:51 +02007959#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007960/*
7961 * ":psearch"
7962 */
7963 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007964ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007965{
7966 g_do_tagpreview = p_pvh;
7967 ex_findpat(eap);
7968 g_do_tagpreview = 0;
7969}
7970#endif
7971
7972 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007973ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007974{
7975 int whole = TRUE;
7976 long n;
7977 char_u *p;
7978 int action;
7979
7980 switch (cmdnames[eap->cmdidx].cmd_name[2])
7981 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007982 case 'e': // ":psearch", ":isearch" and ":dsearch"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007983 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
7984 action = ACTION_GOTO;
7985 else
7986 action = ACTION_SHOW;
7987 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007988 case 'i': // ":ilist" and ":dlist"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007989 action = ACTION_SHOW_ALL;
7990 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007991 case 'u': // ":ijump" and ":djump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007992 action = ACTION_GOTO;
7993 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007994 default: // ":isplit" and ":dsplit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007995 action = ACTION_SPLIT;
7996 break;
7997 }
7998
7999 n = 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008000 if (vim_isdigit(*eap->arg)) // get count
Bram Moolenaar071d4272004-06-13 20:20:40 +00008001 {
8002 n = getdigits(&eap->arg);
8003 eap->arg = skipwhite(eap->arg);
8004 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008005 if (*eap->arg == '/') // Match regexp, not just whole words
Bram Moolenaar071d4272004-06-13 20:20:40 +00008006 {
8007 whole = FALSE;
8008 ++eap->arg;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02008009 p = skip_regexp(eap->arg, '/', p_magic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008010 if (*p)
8011 {
8012 *p++ = NUL;
8013 p = skipwhite(p);
8014
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008015 // Check for trailing illegal characters
Bram Moolenaarfaac4102020-04-20 17:46:14 +02008016 if (!ends_excmd2(eap->arg, p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008017 eap->errmsg = e_trailing;
8018 else
8019 eap->nextcmd = check_nextcmd(p);
8020 }
8021 }
8022 if (!eap->skip)
8023 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
8024 whole, !eap->forceit,
8025 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
8026 n, action, eap->line1, eap->line2);
8027}
8028#endif
8029
Bram Moolenaar071d4272004-06-13 20:20:40 +00008030
Bram Moolenaar4033c552017-09-16 20:54:51 +02008031#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00008032/*
8033 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
8034 */
8035 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008036ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008037{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008038 g_do_tagpreview = p_pvh; // will be reset to 0 in ex_tag_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00008039 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
8040}
8041
8042/*
8043 * ":pedit"
8044 */
8045 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008046ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008047{
8048 win_T *curwin_save = curwin;
8049
Bram Moolenaar3a2505c2020-03-09 16:40:41 +01008050 if (ERROR_IF_ANY_POPUP_WINDOW)
8051 return;
8052
Bram Moolenaar026587b2019-08-17 15:08:00 +02008053 // Open the preview window or popup and make it the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008054 g_do_tagpreview = p_pvh;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02008055 prepare_tagpreview(TRUE, TRUE, FALSE);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008056
Bram Moolenaar026587b2019-08-17 15:08:00 +02008057 // Edit the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008058 do_exedit(eap, NULL);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008059
Bram Moolenaar071d4272004-06-13 20:20:40 +00008060 if (curwin != curwin_save && win_valid(curwin_save))
8061 {
Bram Moolenaar026587b2019-08-17 15:08:00 +02008062 // Return cursor to where we were
Bram Moolenaar071d4272004-06-13 20:20:40 +00008063 validate_cursor();
8064 redraw_later(VALID);
8065 win_enter(curwin_save, TRUE);
8066 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01008067# ifdef FEAT_PROP_POPUP
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008068 else if (WIN_IS_POPUP(curwin))
8069 {
8070 // can't keep focus in popup window
8071 win_enter(firstwin, TRUE);
8072 }
8073# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008074 g_do_tagpreview = 0;
8075}
Bram Moolenaar4033c552017-09-16 20:54:51 +02008076#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008077
8078/*
8079 * ":stag", ":stselect" and ":stjump".
8080 */
8081 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008082ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008083{
8084 postponed_split = -1;
8085 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008086 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008087 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
8088 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008089 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008090}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008091
8092/*
8093 * ":tag", ":tselect", ":tjump", ":tnext", etc.
8094 */
8095 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008096ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008097{
8098 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
8099}
8100
8101 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008102ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008103{
8104 int cmd;
8105
8106 switch (name[1])
8107 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008108 case 'j': cmd = DT_JUMP; // ":tjump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008109 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008110 case 's': cmd = DT_SELECT; // ":tselect"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008111 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008112 case 'p': cmd = DT_PREV; // ":tprevious"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008113 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008114 case 'N': cmd = DT_PREV; // ":tNext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008115 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008116 case 'n': cmd = DT_NEXT; // ":tnext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008117 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008118 case 'o': cmd = DT_POP; // ":pop"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008119 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008120 case 'f': // ":tfirst"
8121 case 'r': cmd = DT_FIRST; // ":trewind"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008122 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008123 case 'l': cmd = DT_LAST; // ":tlast"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008124 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008125 default: // ":tag"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008126#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +00008127 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008128 {
Bram Moolenaard4db7712016-11-12 19:16:46 +01008129 ex_cstag(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008130 return;
8131 }
8132#endif
8133 cmd = DT_TAG;
8134 break;
8135 }
8136
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00008137 if (name[0] == 'l')
8138 {
8139#ifndef FEAT_QUICKFIX
8140 ex_ni(eap);
8141 return;
8142#else
8143 cmd = DT_LTAG;
8144#endif
8145 }
8146
Bram Moolenaar071d4272004-06-13 20:20:40 +00008147 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
8148 eap->forceit, TRUE);
8149}
8150
8151/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008152 * Check "str" for starting with a special cmdline variable.
8153 * If found return one of the SPEC_ values and set "*usedlen" to the length of
8154 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
8155 */
8156 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008157find_cmdline_var(char_u *src, int *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008158{
8159 int len;
8160 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +00008161 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008162 "%",
8163#define SPEC_PERC 0
8164 "#",
Bram Moolenaar65f08472017-09-10 18:16:20 +02008165#define SPEC_HASH (SPEC_PERC + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008166 "<cword>", // cursor word
Bram Moolenaar65f08472017-09-10 18:16:20 +02008167#define SPEC_CWORD (SPEC_HASH + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008168 "<cWORD>", // cursor WORD
Bram Moolenaar65f08472017-09-10 18:16:20 +02008169#define SPEC_CCWORD (SPEC_CWORD + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008170 "<cexpr>", // expr under cursor
Bram Moolenaar65f08472017-09-10 18:16:20 +02008171#define SPEC_CEXPR (SPEC_CCWORD + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008172 "<cfile>", // cursor path name
Bram Moolenaar65f08472017-09-10 18:16:20 +02008173#define SPEC_CFILE (SPEC_CEXPR + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008174 "<sfile>", // ":so" file name
Bram Moolenaar65f08472017-09-10 18:16:20 +02008175#define SPEC_SFILE (SPEC_CFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008176 "<slnum>", // ":so" file line number
Bram Moolenaar65f08472017-09-10 18:16:20 +02008177#define SPEC_SLNUM (SPEC_SFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008178 "<afile>", // autocommand file name
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008179#define SPEC_AFILE (SPEC_SLNUM + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008180 "<abuf>", // autocommand buffer number
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008181#define SPEC_ABUF (SPEC_AFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008182 "<amatch>", // autocommand match name
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008183#define SPEC_AMATCH (SPEC_ABUF + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008184 "<sflnum>", // script file line number
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008185#define SPEC_SFLNUM (SPEC_AMATCH + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008186#ifdef FEAT_CLIENTSERVER
8187 "<client>"
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008188# define SPEC_CLIENT (SPEC_SFLNUM + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008189#endif
8190 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008191
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008192 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008193 {
8194 len = (int)STRLEN(spec_str[i]);
8195 if (STRNCMP(src, spec_str[i], len) == 0)
8196 {
8197 *usedlen = len;
8198 return i;
8199 }
8200 }
8201 return -1;
8202}
8203
8204/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008205 * Evaluate cmdline variables.
8206 *
8207 * change '%' to curbuf->b_ffname
8208 * '#' to curwin->w_altfile
8209 * '<cword>' to word under the cursor
8210 * '<cWORD>' to WORD under the cursor
Bram Moolenaar8071cb22019-07-12 17:58:01 +02008211 * '<cexpr>' to C-expression under the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00008212 * '<cfile>' to path name under the cursor
8213 * '<sfile>' to sourced file name
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008214 * '<slnum>' to sourced file line number
Bram Moolenaar071d4272004-06-13 20:20:40 +00008215 * '<afile>' to file name for autocommand
8216 * '<abuf>' to buffer number for autocommand
8217 * '<amatch>' to matching name for autocommand
8218 *
8219 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
8220 * "" for error without a message) and NULL is returned.
8221 * Returns an allocated string if a valid match was found.
8222 * Returns NULL if no match was found. "usedlen" then still contains the
8223 * number of characters to skip.
8224 */
8225 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008226eval_vars(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008227 char_u *src, // pointer into commandline
8228 char_u *srcstart, // beginning of valid memory for src
8229 int *usedlen, // characters after src that are used
8230 linenr_T *lnump, // line number for :e command, or NULL
8231 char **errormsg, // pointer to error message
8232 int *escaped) // return value has escaped white space (can
8233 // be NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008234{
8235 int i;
8236 char_u *s;
8237 char_u *result;
8238 char_u *resultbuf = NULL;
8239 int resultlen;
8240 buf_T *buf;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008241 int valid = VALID_HEAD + VALID_PATH; // assume valid result
Bram Moolenaar071d4272004-06-13 20:20:40 +00008242 int spec_idx;
Bram Moolenaarfd249462018-07-28 16:14:30 +02008243 int tilde_file = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008244 int skip_mod = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008245 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008246
8247 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +00008248 if (escaped != NULL)
8249 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008250
8251 /*
8252 * Check if there is something to do.
8253 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008254 spec_idx = find_cmdline_var(src, usedlen);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008255 if (spec_idx < 0) // no match
Bram Moolenaar071d4272004-06-13 20:20:40 +00008256 {
8257 *usedlen = 1;
8258 return NULL;
8259 }
8260
8261 /*
8262 * Skip when preceded with a backslash "\%" and "\#".
8263 * Note: In "\\%" the % is also not recognized!
8264 */
8265 if (src > srcstart && src[-1] == '\\')
8266 {
8267 *usedlen = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008268 STRMOVE(src - 1, src); // remove backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00008269 return NULL;
8270 }
8271
8272 /*
8273 * word or WORD under cursor
8274 */
Bram Moolenaar65f08472017-09-10 18:16:20 +02008275 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD
8276 || spec_idx == SPEC_CEXPR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008277 {
Bram Moolenaar65f08472017-09-10 18:16:20 +02008278 resultlen = find_ident_under_cursor(&result,
8279 spec_idx == SPEC_CWORD ? (FIND_IDENT | FIND_STRING)
8280 : spec_idx == SPEC_CEXPR ? (FIND_IDENT | FIND_STRING | FIND_EVAL)
8281 : FIND_STRING);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008282 if (resultlen == 0)
8283 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008284 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008285 return NULL;
8286 }
8287 }
8288
8289 /*
8290 * '#': Alternate file name
8291 * '%': Current file name
8292 * File name under the cursor
8293 * File name for autocommand
8294 * and following modifiers
8295 */
8296 else
8297 {
8298 switch (spec_idx)
8299 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008300 case SPEC_PERC: // '%': current file
Bram Moolenaar071d4272004-06-13 20:20:40 +00008301 if (curbuf->b_fname == NULL)
8302 {
8303 result = (char_u *)"";
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008304 valid = 0; // Must have ":p:h" to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00008305 }
8306 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008307 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008308 result = curbuf->b_fname;
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008309 tilde_file = STRCMP(result, "~") == 0;
8310 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008311 break;
8312
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008313 case SPEC_HASH: // '#' or "#99": alternate file
8314 if (src[1] == '#') // "##": the argument list
Bram Moolenaar071d4272004-06-13 20:20:40 +00008315 {
8316 result = arg_all();
8317 resultbuf = result;
8318 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +00008319 if (escaped != NULL)
8320 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008321 skip_mod = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008322 break;
8323 }
8324 s = src + 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008325 if (*s == '<') // "#<99" uses v:oldfiles
Bram Moolenaard812df62008-11-09 12:46:09 +00008326 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008327 i = (int)getdigits(&s);
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008328 if (s == src + 2 && src[1] == '-')
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008329 // just a minus sign, don't skip over it
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008330 s--;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008331 *usedlen = (int)(s - src); // length of what we expand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008332
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008333 if (src[1] == '<' && i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008334 {
Bram Moolenaard812df62008-11-09 12:46:09 +00008335 if (*usedlen < 2)
8336 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008337 // Should we give an error message for #<text?
Bram Moolenaard812df62008-11-09 12:46:09 +00008338 *usedlen = 1;
8339 return NULL;
8340 }
8341#ifdef FEAT_EVAL
8342 result = list_find_str(get_vim_var_list(VV_OLDFILES),
8343 (long)i);
8344 if (result == NULL)
8345 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008346 *errormsg = "";
Bram Moolenaard812df62008-11-09 12:46:09 +00008347 return NULL;
8348 }
8349#else
Bram Moolenaar8e481e82019-01-15 20:07:48 +01008350 *errormsg = _("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008351 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00008352#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008353 }
8354 else
Bram Moolenaard812df62008-11-09 12:46:09 +00008355 {
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008356 if (i == 0 && src[1] == '<' && *usedlen > 1)
8357 *usedlen = 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00008358 buf = buflist_findnr(i);
8359 if (buf == NULL)
8360 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008361 *errormsg = _("E194: No alternate file name to substitute for '#'");
Bram Moolenaard812df62008-11-09 12:46:09 +00008362 return NULL;
8363 }
8364 if (lnump != NULL)
8365 *lnump = ECMD_LAST;
8366 if (buf->b_fname == NULL)
8367 {
8368 result = (char_u *)"";
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008369 valid = 0; // Must have ":p:h" to be valid
Bram Moolenaard812df62008-11-09 12:46:09 +00008370 }
8371 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008372 {
Bram Moolenaard812df62008-11-09 12:46:09 +00008373 result = buf->b_fname;
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008374 tilde_file = STRCMP(result, "~") == 0;
8375 }
Bram Moolenaard812df62008-11-09 12:46:09 +00008376 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008377 break;
8378
8379#ifdef FEAT_SEARCHPATH
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008380 case SPEC_CFILE: // file name under cursor
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008381 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008382 if (result == NULL)
8383 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008384 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008385 return NULL;
8386 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008387 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00008388 break;
8389#endif
8390
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008391 case SPEC_AFILE: // file name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008392 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +00008393 if (result != NULL && !autocmd_fname_full)
8394 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008395 // Still need to turn the fname into a full path. It is
8396 // postponed to avoid a delay when <afile> is not used.
Bram Moolenaarf6dad432008-09-18 19:29:58 +00008397 autocmd_fname_full = TRUE;
8398 result = FullName_save(autocmd_fname, FALSE);
8399 vim_free(autocmd_fname);
8400 autocmd_fname = result;
8401 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008402 if (result == NULL)
8403 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008404 *errormsg = _("E495: no autocommand file name to substitute for \"<afile>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008405 return NULL;
8406 }
Bram Moolenaara0174af2008-01-02 20:08:25 +00008407 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008408 break;
8409
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008410 case SPEC_ABUF: // buffer number for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008411 if (autocmd_bufnr <= 0)
8412 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008413 *errormsg = _("E496: no autocommand buffer number to substitute for \"<abuf>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008414 return NULL;
8415 }
8416 sprintf((char *)strbuf, "%d", autocmd_bufnr);
8417 result = strbuf;
8418 break;
8419
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008420 case SPEC_AMATCH: // match name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008421 result = autocmd_match;
8422 if (result == NULL)
8423 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008424 *errormsg = _("E497: no autocommand match name to substitute for \"<amatch>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008425 return NULL;
8426 }
8427 break;
8428
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008429 case SPEC_SFILE: // file name for ":so" command
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008430 result = estack_sfile();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008431 if (result == NULL)
8432 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008433 *errormsg = _("E498: no :source file name to substitute for \"<sfile>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008434 return NULL;
8435 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008436 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00008437 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008438
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008439 case SPEC_SLNUM: // line in file for ":so" command
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008440 if (SOURCING_NAME == NULL || SOURCING_LNUM == 0)
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008441 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008442 *errormsg = _("E842: no line number to use for \"<slnum>\"");
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008443 return NULL;
8444 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008445 sprintf((char *)strbuf, "%ld", SOURCING_LNUM);
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008446 result = strbuf;
8447 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008448
8449#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008450 case SPEC_SFLNUM: // line in script file
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008451 if (current_sctx.sc_lnum + SOURCING_LNUM == 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008452 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008453 *errormsg = _("E961: no line number to use for \"<sflnum>\"");
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008454 return NULL;
8455 }
8456 sprintf((char *)strbuf, "%ld",
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008457 (long)(current_sctx.sc_lnum + SOURCING_LNUM));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008458 result = strbuf;
8459 break;
8460#endif
8461
8462#ifdef FEAT_CLIENTSERVER
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008463 case SPEC_CLIENT: // Source of last submitted input
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008464 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
8465 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008466 result = strbuf;
8467 break;
8468#endif
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008469
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02008470 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008471 result = (char_u *)""; // avoid gcc warning
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02008472 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008473 }
8474
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008475 resultlen = (int)STRLEN(result); // length of new string
8476 if (src[*usedlen] == '<') // remove the file name extension
Bram Moolenaar071d4272004-06-13 20:20:40 +00008477 {
8478 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008479 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008480 resultlen = (int)(s - result);
8481 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008482 else if (!skip_mod)
8483 {
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008484 valid |= modify_fname(src, tilde_file, usedlen, &result, &resultbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008485 &resultlen);
8486 if (result == NULL)
8487 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008488 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008489 return NULL;
8490 }
8491 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008492 }
8493
8494 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
8495 {
8496 if (valid != VALID_HEAD + VALID_PATH)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008497 // xgettext:no-c-format
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008498 *errormsg = _("E499: Empty file name for '%' or '#', only works with \":p:h\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008499 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008500 *errormsg = _("E500: Evaluates to an empty string");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008501 result = NULL;
8502 }
8503 else
8504 result = vim_strnsave(result, resultlen);
8505 vim_free(resultbuf);
8506 return result;
8507}
8508
8509/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008510 * Expand the <sfile> string in "arg".
8511 *
8512 * Returns an allocated string, or NULL for any error.
8513 */
8514 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008515expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008516{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008517 char *errormsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008518 int len;
8519 char_u *result;
8520 char_u *newres;
8521 char_u *repl;
8522 int srclen;
8523 char_u *p;
8524
8525 result = vim_strsave(arg);
8526 if (result == NULL)
8527 return NULL;
8528
8529 for (p = result; *p; )
8530 {
8531 if (STRNCMP(p, "<sfile>", 7) != 0)
8532 ++p;
8533 else
8534 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008535 // replace "<sfile>" with the sourced file name, and do ":" stuff
Bram Moolenaar63b92542007-03-27 14:57:09 +00008536 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008537 if (errormsg != NULL)
8538 {
8539 if (*errormsg)
8540 emsg(errormsg);
8541 vim_free(result);
8542 return NULL;
8543 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008544 if (repl == NULL) // no match (cannot happen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008545 {
8546 p += srclen;
8547 continue;
8548 }
8549 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
8550 newres = alloc(len);
8551 if (newres == NULL)
8552 {
8553 vim_free(repl);
8554 vim_free(result);
8555 return NULL;
8556 }
8557 mch_memmove(newres, result, (size_t)(p - result));
8558 STRCPY(newres + (p - result), repl);
8559 len = (int)STRLEN(newres);
8560 STRCAT(newres, p + srclen);
8561 vim_free(repl);
8562 vim_free(result);
8563 result = newres;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008564 p = newres + len; // continue after the match
Bram Moolenaar071d4272004-06-13 20:20:40 +00008565 }
8566 }
8567
8568 return result;
8569}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008570
Bram Moolenaar071d4272004-06-13 20:20:40 +00008571#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00008572/*
Bram Moolenaard9462e32011-04-11 21:35:11 +02008573 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +00008574 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +00008575 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008576 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008577dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008578{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008579 if (fname == NULL)
8580 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +02008581 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008582}
8583#endif
8584
8585/*
8586 * ":behave {mswin,xterm}"
8587 */
8588 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008589ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008590{
8591 if (STRCMP(eap->arg, "mswin") == 0)
8592 {
8593 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
8594 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
8595 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
8596 set_option_value((char_u *)"keymodel", 0L,
8597 (char_u *)"startsel,stopsel", 0);
8598 }
8599 else if (STRCMP(eap->arg, "xterm") == 0)
8600 {
8601 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
8602 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
8603 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
8604 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
8605 }
8606 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008607 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008608}
8609
Bram Moolenaar071d4272004-06-13 20:20:40 +00008610static int filetype_detect = FALSE;
8611static int filetype_plugin = FALSE;
8612static int filetype_indent = FALSE;
8613
8614/*
8615 * ":filetype [plugin] [indent] {on,off,detect}"
8616 * on: Load the filetype.vim file to install autocommands for file types.
8617 * off: Load the ftoff.vim file to remove all autocommands for file types.
8618 * plugin on: load filetype.vim and ftplugin.vim
8619 * plugin off: load ftplugof.vim
8620 * indent on: load filetype.vim and indent.vim
8621 * indent off: load indoff.vim
8622 */
8623 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008624ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008625{
8626 char_u *arg = eap->arg;
8627 int plugin = FALSE;
8628 int indent = FALSE;
8629
8630 if (*eap->arg == NUL)
8631 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008632 // Print current status.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008633 smsg("filetype detection:%s plugin:%s indent:%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00008634 filetype_detect ? "ON" : "OFF",
8635 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
8636 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
8637 return;
8638 }
8639
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008640 // Accept "plugin" and "indent" in any order.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008641 for (;;)
8642 {
8643 if (STRNCMP(arg, "plugin", 6) == 0)
8644 {
8645 plugin = TRUE;
8646 arg = skipwhite(arg + 6);
8647 continue;
8648 }
8649 if (STRNCMP(arg, "indent", 6) == 0)
8650 {
8651 indent = TRUE;
8652 arg = skipwhite(arg + 6);
8653 continue;
8654 }
8655 break;
8656 }
8657 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
8658 {
8659 if (*arg == 'o' || !filetype_detect)
8660 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008661 source_runtime((char_u *)FILETYPE_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008662 filetype_detect = TRUE;
8663 if (plugin)
8664 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008665 source_runtime((char_u *)FTPLUGIN_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008666 filetype_plugin = TRUE;
8667 }
8668 if (indent)
8669 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008670 source_runtime((char_u *)INDENT_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008671 filetype_indent = TRUE;
8672 }
8673 }
8674 if (*arg == 'd')
8675 {
Bram Moolenaar1610d052016-06-09 22:53:01 +02008676 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +00008677 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008678 }
8679 }
8680 else if (STRCMP(arg, "off") == 0)
8681 {
8682 if (plugin || indent)
8683 {
8684 if (plugin)
8685 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008686 source_runtime((char_u *)FTPLUGOF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008687 filetype_plugin = FALSE;
8688 }
8689 if (indent)
8690 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008691 source_runtime((char_u *)INDOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008692 filetype_indent = FALSE;
8693 }
8694 }
8695 else
8696 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008697 source_runtime((char_u *)FTOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008698 filetype_detect = FALSE;
8699 }
8700 }
8701 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008702 semsg(_(e_invarg2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008703}
8704
8705/*
Bram Moolenaar3e545692017-06-04 19:00:32 +02008706 * ":setfiletype [FALLBACK] {name}"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008707 */
8708 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008709ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008710{
8711 if (!did_filetype)
Bram Moolenaar3e545692017-06-04 19:00:32 +02008712 {
8713 char_u *arg = eap->arg;
8714
8715 if (STRNCMP(arg, "FALLBACK ", 9) == 0)
8716 arg += 9;
8717
8718 set_option_value((char_u *)"filetype", 0L, arg, OPT_LOCAL);
8719 if (arg != eap->arg)
8720 did_filetype = FALSE;
8721 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008722}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008723
Bram Moolenaar071d4272004-06-13 20:20:40 +00008724 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008725ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008726{
8727#ifdef FEAT_DIGRAPHS
8728 if (*eap->arg != NUL)
8729 putdigraph(eap->arg);
8730 else
Bram Moolenaareae8ae12018-12-14 18:53:02 +01008731 listdigraphs(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008732#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008733 emsg(_("E196: No digraphs in this version"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008734#endif
8735}
8736
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02008737#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
8738 void
8739set_no_hlsearch(int flag)
8740{
8741 no_hlsearch = flag;
8742# ifdef FEAT_EVAL
8743 set_vim_var_nr(VV_HLSEARCH, !no_hlsearch && p_hls);
8744# endif
8745}
8746
Bram Moolenaar071d4272004-06-13 20:20:40 +00008747/*
8748 * ":nohlsearch"
8749 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008750 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008751ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008752{
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02008753 set_no_hlsearch(TRUE);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00008754 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008755}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008756#endif
8757
8758#ifdef FEAT_CRYPT
8759/*
8760 * ":X": Get crypt key
8761 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008762 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008763ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008764{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +01008765 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02008766 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008767}
8768#endif
8769
8770#ifdef FEAT_FOLDING
8771 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008772ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008773{
8774 if (foldManualAllowed(TRUE))
8775 foldCreate(eap->line1, eap->line2);
8776}
8777
8778 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008779ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008780{
8781 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
8782 eap->forceit, FALSE);
8783}
8784
8785 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008786ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008787{
8788 linenr_T lnum;
8789
Bram Moolenaar4facea32019-10-12 20:17:40 +02008790# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02008791 start_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +02008792# endif
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02008793
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008794 // First set the marks for all lines closed/open.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008795 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
8796 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
8797 ml_setmarked(lnum);
8798
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008799 // Execute the command on the marked lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008800 global_exe(eap->arg);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008801 ml_clearmarked(); // clear rest of the marks
Bram Moolenaar4facea32019-10-12 20:17:40 +02008802# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02008803 end_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +02008804# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008805}
8806#endif
Bram Moolenaarf5291f32017-09-14 22:55:37 +02008807
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008808#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar39665952018-08-15 20:59:48 +02008809/*
8810 * Returns TRUE if the supplied Ex cmdidx is for a location list command
8811 * instead of a quickfix command.
8812 */
8813 int
8814is_loclist_cmd(int cmdidx)
8815{
Bram Moolenaar74c8be22018-08-23 22:51:40 +02008816 if (cmdidx < 0 || cmdidx >= CMD_SIZE)
Bram Moolenaar39665952018-08-15 20:59:48 +02008817 return FALSE;
8818 return cmdnames[cmdidx].cmd_name[0] == 'l';
8819}
8820#endif
8821
Bram Moolenaar4facea32019-10-12 20:17:40 +02008822#if defined(FEAT_TIMERS) || defined(PROTO)
Bram Moolenaarf5291f32017-09-14 22:55:37 +02008823 int
8824get_pressedreturn(void)
8825{
8826 return ex_pressedreturn;
8827}
8828
8829 void
8830set_pressedreturn(int val)
8831{
8832 ex_pressedreturn = val;
8833}
8834#endif