blob: 6a2e91c9fea7caf00ecc3048a63276ece44bab6f [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
Bram Moolenaar6e36b1c2020-07-17 20:47:51 +02001652 static int
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001653comment_start(char_u *p, int starts_with_colon UNUSED)
1654{
1655#ifdef FEAT_EVAL
1656 if (in_vim9script())
1657 return p[0] == '#' && p[1] != '{' && !starts_with_colon;
1658#endif
1659 return *p == '"';
1660}
1661
Bram Moolenaarf240e182014-11-27 18:33:02 +01001662# define CURRENT_WIN_NR current_win_nr(curwin)
1663# define LAST_WIN_NR current_win_nr(NULL)
1664# define CURRENT_TAB_NR current_tab_nr(curtab)
1665# define LAST_TAB_NR current_tab_nr(NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001666
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667/*
1668 * Execute one Ex command.
1669 *
1670 * If 'sourcing' is TRUE, the command will be included in the error message.
1671 *
1672 * 1. skip comment lines and leading space
1673 * 2. handle command modifiers
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001674 * 3. find the command
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001675 * 4. parse range
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001676 * 5. Parse the command.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001677 * 6. parse arguments
1678 * 7. switch on command name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001679 *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001680 * Note: "fgetline" can be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681 *
1682 * This function may be called recursively!
1683 */
1684#if (_MSC_VER == 1200)
1685/*
Bram Moolenaared203462004-06-16 11:19:22 +00001686 * Avoid optimisation bug in VC++ version 6.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687 */
Bram Moolenaar281bdce2005-01-25 21:53:18 +00001688 #pragma optimize( "g", off )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689#endif
1690 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001691do_one_cmd(
Bram Moolenaarddef1292019-12-16 17:10:33 +01001692 char_u **cmdlinep,
1693 int sourcing,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001694#ifdef FEAT_EVAL
Bram Moolenaarddef1292019-12-16 17:10:33 +01001695 cstack_T *cstack,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001696#endif
Bram Moolenaarddef1292019-12-16 17:10:33 +01001697 char_u *(*fgetline)(int, void *, int, int),
1698 void *cookie) // argument for fgetline()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001699{
Bram Moolenaarddef1292019-12-16 17:10:33 +01001700 char_u *p;
1701 linenr_T lnum;
1702 long n;
1703 char *errormsg = NULL; // error message
1704 char_u *after_modifier = NULL;
1705 exarg_T ea; // Ex command arguments
1706 int save_msg_scroll = msg_scroll;
1707 cmdmod_T save_cmdmod;
1708 int save_reg_executing = reg_executing;
1709 int ni; // set when Not Implemented
1710 char_u *cmd;
Bram Moolenaar6e36b1c2020-07-17 20:47:51 +02001711 int starts_with_colon = FALSE;
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001712#ifdef FEAT_EVAL
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001713 int starts_with_quote;
1714 int vim9script = in_vim9script();
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001715#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716
Bram Moolenaara80faa82020-04-12 19:37:17 +02001717 CLEAR_FIELD(ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718 ea.line1 = 1;
1719 ea.line2 = 1;
1720#ifdef FEAT_EVAL
1721 ++ex_nesting_level;
1722#endif
1723
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001724 // When the last file has not been edited :q has to be typed twice.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725 if (quitmore
1726#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001727 // avoid that a function call in 'statusline' does this
Bram Moolenaar89d40322006-08-29 15:30:07 +00001728 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001729#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001730 // avoid that an autocommand, e.g. QuitPre, does this
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001731 && !getline_equal(fgetline, cookie, getnextac))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732 --quitmore;
1733
1734 /*
1735 * Reset browse, confirm, etc.. They are restored when returning, for
1736 * recursive calls.
1737 */
1738 save_cmdmod = cmdmod;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001740 // "#!anything" is handled like a comment.
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001741 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1742 goto doend;
1743
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02001744/*
1745 * 1. Skip comment lines and leading white space and colons.
1746 * 2. Handle command modifiers.
1747 */
1748 // The "ea" structure holds the arguments that can be used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749 ea.cmd = *cmdlinep;
Bram Moolenaareffed932018-08-14 13:38:17 +02001750 ea.cmdlinep = cmdlinep;
1751 ea.getline = fgetline;
1752 ea.cookie = cookie;
1753#ifdef FEAT_EVAL
1754 ea.cstack = cstack;
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001755 starts_with_colon = *skipwhite(ea.cmd) == ':';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001756#endif
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02001757 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaareffed932018-08-14 13:38:17 +02001758 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001760 after_modifier = ea.cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001761
1762#ifdef FEAT_EVAL
1763 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
1764 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
1765#else
1766 ea.skip = (if_level > 0);
1767#endif
1768
Bram Moolenaar071d4272004-06-13 20:20:40 +00001769/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001770 * 3. Skip over the range to find the command. Let "p" point to after it.
1771 *
1772 * We need the command to know what kind of range it uses.
1773 */
1774 cmd = ea.cmd;
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001775#ifdef FEAT_EVAL
Bram Moolenaar13e12b82020-07-24 18:47:22 +02001776 starts_with_quote = vim9script && !starts_with_colon && *ea.cmd == '\'';
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001777 if (!starts_with_quote)
1778#endif
1779 ea.cmd = skip_range(ea.cmd, NULL);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001780 if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
1781 ea.cmd = skipwhite(ea.cmd + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001782
1783#ifdef FEAT_EVAL
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001784 if (vim9script && !starts_with_colon)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02001785 {
1786 if (ea.cmd > cmd)
1787 {
1788 emsg(_(e_colon_required));
1789 goto doend;
1790 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001791 p = find_ex_command(&ea, NULL, lookup_scriptvar, NULL);
Bram Moolenaardf069ee2020-06-22 23:02:51 +02001792 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001793 else
1794#endif
1795 p = find_ex_command(&ea, NULL, NULL, NULL);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001796
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001797#ifdef FEAT_EVAL
1798# ifdef FEAT_PROFILE
1799 // Count this line for profiling if skip is TRUE.
1800 if (do_profiling == PROF_YES
1801 && (!ea.skip || cstack->cs_idx == 0 || (cstack->cs_idx > 0
1802 && (cstack->cs_flags[cstack->cs_idx - 1] & CSF_ACTIVE))))
1803 {
1804 int skip = did_emsg || got_int || did_throw;
1805
1806 if (ea.cmdidx == CMD_catch)
1807 skip = !skip && !(cstack->cs_idx >= 0
1808 && (cstack->cs_flags[cstack->cs_idx] & CSF_THROWN)
1809 && !(cstack->cs_flags[cstack->cs_idx] & CSF_CAUGHT));
1810 else if (ea.cmdidx == CMD_else || ea.cmdidx == CMD_elseif)
1811 skip = skip || !(cstack->cs_idx >= 0
1812 && !(cstack->cs_flags[cstack->cs_idx]
1813 & (CSF_ACTIVE | CSF_TRUE)));
1814 else if (ea.cmdidx == CMD_finally)
1815 skip = FALSE;
1816 else if (ea.cmdidx != CMD_endif
1817 && ea.cmdidx != CMD_endfor
1818 && ea.cmdidx != CMD_endtry
1819 && ea.cmdidx != CMD_endwhile)
1820 skip = ea.skip;
1821
1822 if (!skip)
1823 {
1824 if (getline_equal(fgetline, cookie, get_func_line))
1825 func_line_exec(getline_cookie(fgetline, cookie));
1826 else if (getline_equal(fgetline, cookie, getsourceline))
1827 script_line_exec();
1828 }
1829 }
1830# endif
1831
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001832 // May go to debug mode. If this happens and the ">quit" debug command is
1833 // used, throw an interrupt exception and skip the next command.
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001834 dbg_check_breakpoint(&ea);
1835 if (!ea.skip && got_int)
1836 {
1837 ea.skip = TRUE;
1838 (void)do_intthrow(cstack);
1839 }
1840#endif
1841
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001842/*
1843 * 4. parse a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844 *
1845 * where 'addr' is:
1846 *
1847 * % (entire file)
1848 * $ [+-NUM]
1849 * 'x [+-NUM] (where x denotes a currently defined mark)
1850 * . [+-NUM]
1851 * [+-NUM]..
1852 * NUM
1853 *
1854 * The ea.cmd pointer is updated to point to the first character following the
1855 * range spec. If an initial address is found, but no second, the upper bound
1856 * is equal to the lower.
1857 */
1858
Bram Moolenaar25190db2019-05-04 15:05:28 +02001859 // ea.addr_type for user commands is set by find_ucmd
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001860 if (!IS_USER_CMDIDX(ea.cmdidx))
1861 {
1862 if (ea.cmdidx != CMD_SIZE)
1863 ea.addr_type = cmdnames[(int)ea.cmdidx].cmd_addr_type;
1864 else
1865 ea.addr_type = ADDR_LINES;
1866
Bram Moolenaar25190db2019-05-04 15:05:28 +02001867 // :wincmd range depends on the argument.
Bram Moolenaar164f3262015-01-14 21:22:01 +01001868 if (ea.cmdidx == CMD_wincmd && p != NULL)
1869 get_wincmd_addr_type(skipwhite(p), &ea);
Bram Moolenaar25190db2019-05-04 15:05:28 +02001870#ifdef FEAT_QUICKFIX
1871 // :.cc in quickfix window uses line number
1872 if ((ea.cmdidx == CMD_cc || ea.cmdidx == CMD_ll) && bt_quickfix(curbuf))
1873 ea.addr_type = ADDR_OTHER;
1874#endif
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001875 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001876
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001877 ea.cmd = cmd;
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001878#ifdef FEAT_EVAL
1879 if (!starts_with_quote)
1880#endif
1881 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
1882 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883
Bram Moolenaar071d4272004-06-13 20:20:40 +00001884/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001885 * 5. Parse the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886 */
1887
1888 /*
1889 * Skip ':' and any white space
1890 */
1891 ea.cmd = skipwhite(ea.cmd);
1892 while (*ea.cmd == ':')
1893 ea.cmd = skipwhite(ea.cmd + 1);
1894
1895 /*
1896 * If we got a line, but no command, then go to the line.
1897 * If we find a '|' or '\n' we set ea.nextcmd.
1898 */
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001899 if (*ea.cmd == NUL || comment_start(ea.cmd, starts_with_colon)
1900 || (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001901 {
1902 /*
1903 * strange vi behaviour:
1904 * ":3" jumps to line 3
1905 * ":3|..." prints line 3
1906 * ":|" prints current line
1907 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001908 if (ea.skip) // skip this if inside :if
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001910 if (*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001911 {
1912 ea.cmdidx = CMD_print;
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001913 ea.argt = EX_RANGE+EX_COUNT+EX_TRLBAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001914 if ((errormsg = invalid_range(&ea)) == NULL)
1915 {
1916 correct_range(&ea);
1917 ex_print(&ea);
1918 }
1919 }
1920 else if (ea.addr_count != 0)
1921 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001922 if (ea.line2 > curbuf->b_ml.ml_line_count)
1923 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001924 // With '-' in 'cpoptions' a line number past the file is an
1925 // error, otherwise put it at the end of the file.
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001926 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
1927 ea.line2 = -1;
1928 else
1929 ea.line2 = curbuf->b_ml.ml_line_count;
1930 }
1931
1932 if (ea.line2 < 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001933 errormsg = _(e_invrange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001934 else
1935 {
1936 if (ea.line2 == 0)
1937 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938 else
1939 curwin->w_cursor.lnum = ea.line2;
1940 beginline(BL_SOL | BL_FIX);
1941 }
1942 }
1943 goto doend;
1944 }
1945
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001946 // If this looks like an undefined user command and there are CmdUndefined
1947 // autocommands defined, trigger the matching autocommands.
Bram Moolenaard5005162014-08-22 23:05:54 +02001948 if (p != NULL && ea.cmdidx == CMD_SIZE && !ea.skip
1949 && ASCII_ISUPPER(*ea.cmd)
1950 && has_cmdundefined())
1951 {
Bram Moolenaard5005162014-08-22 23:05:54 +02001952 int ret;
1953
Bram Moolenaar5a31b462014-08-23 14:16:20 +02001954 p = ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02001955 while (ASCII_ISALNUM(*p))
1956 ++p;
Bram Moolenaar158ea172020-06-18 17:28:39 +02001957 p = vim_strnsave(ea.cmd, p - ea.cmd);
Bram Moolenaard5005162014-08-22 23:05:54 +02001958 ret = apply_autocmds(EVENT_CMDUNDEFINED, p, p, TRUE, NULL);
1959 vim_free(p);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001960 // If the autocommands did something and didn't cause an error, try
1961 // finding the command again.
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001962 p = (ret
1963#ifdef FEAT_EVAL
1964 && !aborting()
Bram Moolenaard5005162014-08-22 23:05:54 +02001965#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001966 ) ? find_ex_command(&ea, NULL, NULL, NULL) : ea.cmd;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001967 }
Bram Moolenaard5005162014-08-22 23:05:54 +02001968
Bram Moolenaar071d4272004-06-13 20:20:40 +00001969 if (p == NULL)
1970 {
1971 if (!ea.skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001972 errormsg = _("E464: Ambiguous use of user-defined command");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001973 goto doend;
1974 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001975 // Check for wrong commands.
Bram Moolenaar6f9a4762017-06-22 20:39:17 +02001976 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78
1977 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978 {
1979 errormsg = uc_fun_cmd();
1980 goto doend;
1981 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001982
Bram Moolenaar071d4272004-06-13 20:20:40 +00001983 if (ea.cmdidx == CMD_SIZE)
1984 {
1985 if (!ea.skip)
1986 {
1987 STRCPY(IObuff, _("E492: Not an editor command"));
1988 if (!sourcing)
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001989 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001990 // If the modifier was parsed OK the error must be in the
1991 // following command
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001992 if (after_modifier != NULL)
1993 append_command(after_modifier);
1994 else
1995 append_command(*cmdlinep);
1996 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001997 errormsg = (char *)IObuff;
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001998 did_emsg_syntax = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999 }
2000 goto doend;
2001 }
2002
Bram Moolenaar958636c2014-10-21 20:01:58 +02002003 ni = (!IS_USER_CMDIDX(ea.cmdidx)
2004 && (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00002005#ifdef HAVE_EX_SCRIPT_NI
2006 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
2007#endif
2008 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009
2010#ifndef FEAT_EVAL
2011 /*
2012 * When the expression evaluation is disabled, recognize the ":if" and
2013 * ":endif" commands and ignore everything in between it.
2014 */
2015 if (ea.cmdidx == CMD_if)
2016 ++if_level;
2017 if (if_level)
2018 {
2019 if (ea.cmdidx == CMD_endif)
2020 --if_level;
2021 goto doend;
2022 }
2023
2024#endif
2025
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002026 // forced commands
Bram Moolenaar196b3b02008-06-20 16:51:41 +00002027 if (*p == '!' && ea.cmdidx != CMD_substitute
2028 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002029 {
2030 ++p;
2031 ea.forceit = TRUE;
2032 }
2033 else
2034 ea.forceit = FALSE;
2035
2036/*
Bram Moolenaarb7316892019-05-01 18:08:42 +02002037 * 6. Parse arguments. Then check for errors.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002038 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02002039 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002040 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002041
2042 if (!ea.skip)
2043 {
2044#ifdef HAVE_SANDBOX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002045 if (sandbox != 0 && !(ea.argt & EX_SBOXOK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046 {
Bram Moolenaar8c62a082019-02-08 14:34:10 +01002047 // Command not allowed in sandbox.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002048 errormsg = _(e_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002049 goto doend;
2050 }
2051#endif
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002052 if (restricted != 0 && (ea.argt & EX_RESTRICT))
Bram Moolenaar8c62a082019-02-08 14:34:10 +01002053 {
2054 errormsg = _("E981: Command not allowed in rvim");
2055 goto doend;
2056 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002057 if (!curbuf->b_p_ma && (ea.argt & EX_MODIFY))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002058 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002059 // Command not allowed in non-'modifiable' buffer
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002060 errormsg = _(e_modifiable);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002061 goto doend;
2062 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002063
Bram Moolenaar37394ff2020-07-25 19:38:18 +02002064 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002065 {
Bram Moolenaar37394ff2020-07-25 19:38:18 +02002066#ifdef FEAT_CMDWIN
2067 if (cmdwin_type != 0 && !(ea.argt & EX_CMDWIN))
2068 {
2069 // Command not allowed in the command line window
2070 errormsg = _(e_cmdwin);
2071 goto doend;
2072 }
2073#endif
2074 if (text_locked() && !(ea.argt & EX_LOCK_OK))
2075 {
2076 // Command not allowed when text is locked
2077 errormsg = _(get_text_locked_msg());
2078 goto doend;
2079 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002080 }
Bram Moolenaar379fb762018-08-30 15:58:28 +02002081
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002082 // Disallow editing another buffer when "curbuf_lock" is set.
2083 // Do allow ":checktime" (it is postponed).
2084 // Do allow ":edit" (check for an argument later).
2085 // Do allow ":file" with no arguments (check for an argument later).
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002086 if (!(ea.argt & EX_CMDWIN)
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002087 && ea.cmdidx != CMD_checktime
Bram Moolenaar379fb762018-08-30 15:58:28 +02002088 && ea.cmdidx != CMD_edit
2089 && ea.cmdidx != CMD_file
Bram Moolenaar958636c2014-10-21 20:01:58 +02002090 && !IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002091 && curbuf_locked())
2092 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002093
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002094 if (!ni && !(ea.argt & EX_RANGE) && ea.addr_count > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002095 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002096 // no range allowed
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002097 errormsg = _(e_norange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002098 goto doend;
2099 }
2100 }
2101
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002102 if (!ni && !(ea.argt & EX_BANG) && ea.forceit) // no <!> allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002103 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002104 errormsg = _(e_nobang);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002105 goto doend;
2106 }
2107
2108 /*
2109 * Don't complain about the range if it is not used
2110 * (could happen if line_count is accidentally set to 0).
2111 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002112 if (!ea.skip && !ni && (ea.argt & EX_RANGE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002113 {
2114 /*
2115 * If the range is backwards, ask for confirmation and, if given, swap
2116 * ea.line1 & ea.line2 so it's forwards again.
2117 * When global command is busy, don't ask, will fail below.
2118 */
2119 if (!global_busy && ea.line1 > ea.line2)
2120 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002121 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002122 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002123 if (sourcing || exmode_active)
2124 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002125 errormsg = _("E493: Backwards range given");
Bram Moolenaara5792f52005-11-23 21:25:05 +00002126 goto doend;
2127 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002128 if (ask_yesno((char_u *)
2129 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002130 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002131 }
2132 lnum = ea.line1;
2133 ea.line1 = ea.line2;
2134 ea.line2 = lnum;
2135 }
2136 if ((errormsg = invalid_range(&ea)) != NULL)
2137 goto doend;
2138 }
2139
Bram Moolenaarb7316892019-05-01 18:08:42 +02002140 if ((ea.addr_type == ADDR_OTHER) && ea.addr_count == 0)
2141 // default is 1, not cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142 ea.line2 = 1;
2143
2144 correct_range(&ea);
2145
2146#ifdef FEAT_FOLDING
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002147 if (((ea.argt & EX_WHOLEFOLD) || ea.addr_count >= 2) && !global_busy
Bram Moolenaara3306952016-01-02 21:41:06 +01002148 && ea.addr_type == ADDR_LINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002149 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002150 // Put the first line at the start of a closed fold, put the last line
2151 // at the end of a closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002152 (void)hasFolding(ea.line1, &ea.line1, NULL);
2153 (void)hasFolding(ea.line2, NULL, &ea.line2);
2154 }
2155#endif
2156
2157#ifdef FEAT_QUICKFIX
2158 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002159 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160 * option here, so things like % get expanded.
2161 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002162 p = replace_makeprg(&ea, p, cmdlinep);
2163 if (p == NULL)
2164 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165#endif
2166
2167 /*
2168 * Skip to start of argument.
2169 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2170 */
2171 if (ea.cmdidx == CMD_bang)
2172 ea.arg = p;
2173 else
2174 ea.arg = skipwhite(p);
2175
Bram Moolenaar379fb762018-08-30 15:58:28 +02002176 // ":file" cannot be run with an argument when "curbuf_lock" is set
2177 if (ea.cmdidx == CMD_file && *ea.arg != NUL && curbuf_locked())
2178 goto doend;
2179
Bram Moolenaar071d4272004-06-13 20:20:40 +00002180 /*
2181 * Check for "++opt=val" argument.
2182 * Must be first, allow ":w ++enc=utf8 !cmd"
2183 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002184 if (ea.argt & EX_ARGOPT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2186 if (getargopt(&ea) == FAIL && !ni)
2187 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002188 errormsg = _(e_invarg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002189 goto doend;
2190 }
2191
2192 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2193 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002194 if (*ea.arg == '>') // append
Bram Moolenaar071d4272004-06-13 20:20:40 +00002195 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002196 if (*++ea.arg != '>') // typed wrong
Bram Moolenaar071d4272004-06-13 20:20:40 +00002197 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002198 errormsg = _("E494: Use w or w>>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002199 goto doend;
2200 }
2201 ea.arg = skipwhite(ea.arg + 1);
2202 ea.append = TRUE;
2203 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002204 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) // :w !filter
Bram Moolenaar071d4272004-06-13 20:20:40 +00002205 {
2206 ++ea.arg;
2207 ea.usefilter = TRUE;
2208 }
2209 }
2210
2211 if (ea.cmdidx == CMD_read)
2212 {
2213 if (ea.forceit)
2214 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002215 ea.usefilter = TRUE; // :r! filter if ea.forceit
Bram Moolenaar071d4272004-06-13 20:20:40 +00002216 ea.forceit = FALSE;
2217 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002218 else if (*ea.arg == '!') // :r !filter
Bram Moolenaar071d4272004-06-13 20:20:40 +00002219 {
2220 ++ea.arg;
2221 ea.usefilter = TRUE;
2222 }
2223 }
2224
2225 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2226 {
2227 ea.amount = 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002228 while (*ea.arg == *ea.cmd) // count number of '>' or '<'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002229 {
2230 ++ea.arg;
2231 ++ea.amount;
2232 }
2233 ea.arg = skipwhite(ea.arg);
2234 }
2235
2236 /*
2237 * Check for "+command" argument, before checking for next command.
2238 * Don't do this for ":read !cmd" and ":write !cmd".
2239 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002240 if ((ea.argt & EX_CMDARG) && !ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002241 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2242
2243 /*
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002244 * Check for '|' to separate commands and '"' or '#' to start comments.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002245 * Don't do this for ":read !cmd" and ":write !cmd".
2246 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002247 if ((ea.argt & EX_TRLBAR) && !ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002248 separate_nextcmd(&ea);
2249
2250 /*
2251 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002252 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2253 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002254 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002255 else if (ea.cmdidx == CMD_bang
Bram Moolenaar67883b42017-07-27 22:57:00 +02002256 || ea.cmdidx == CMD_terminal
Bram Moolenaardf177f62005-02-22 08:39:57 +00002257 || ea.cmdidx == CMD_global
2258 || ea.cmdidx == CMD_vglobal
2259 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002260 {
2261 for (p = ea.arg; *p; ++p)
2262 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002263 // Remove one backslash before a newline, so that it's possible to
2264 // pass a newline to the shell and also a newline that is preceded
2265 // with a backslash. This makes it impossible to end a shell
2266 // command in a backslash, but that doesn't appear useful.
2267 // Halving the number of backslashes is incompatible with previous
2268 // versions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002269 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002270 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271 else if (*p == '\n')
2272 {
2273 ea.nextcmd = p + 1;
2274 *p = NUL;
2275 break;
2276 }
2277 }
2278 }
2279
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002280 if ((ea.argt & EX_DFLALL) && ea.addr_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002281 {
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002282 buf_T *buf;
2283
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284 ea.line1 = 1;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002285 switch (ea.addr_type)
2286 {
2287 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02002288 case ADDR_OTHER:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002289 ea.line2 = curbuf->b_ml.ml_line_count;
2290 break;
2291 case ADDR_LOADED_BUFFERS:
2292 buf = firstbuf;
2293 while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL)
2294 buf = buf->b_next;
2295 ea.line1 = buf->b_fnum;
2296 buf = lastbuf;
2297 while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL)
2298 buf = buf->b_prev;
2299 ea.line2 = buf->b_fnum;
2300 break;
2301 case ADDR_BUFFERS:
2302 ea.line1 = firstbuf->b_fnum;
2303 ea.line2 = lastbuf->b_fnum;
2304 break;
2305 case ADDR_WINDOWS:
2306 ea.line2 = LAST_WIN_NR;
2307 break;
2308 case ADDR_TABS:
2309 ea.line2 = LAST_TAB_NR;
2310 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01002311 case ADDR_TABS_RELATIVE:
2312 ea.line2 = 1;
2313 break;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002314 case ADDR_ARGUMENTS:
2315 if (ARGCOUNT == 0)
2316 ea.line1 = ea.line2 = 0;
2317 else
2318 ea.line2 = ARGCOUNT;
2319 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02002320 case ADDR_QUICKFIX_VALID:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002321#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02002322 ea.line2 = qf_get_valid_size(&ea);
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002323 if (ea.line2 == 0)
2324 ea.line2 = 1;
Bram Moolenaare906c502015-09-09 21:10:39 +02002325#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002326 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02002327 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02002328 case ADDR_UNSIGNED:
2329 case ADDR_QUICKFIX:
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002330 iemsg(_("INTERNAL: Cannot use EX_DFLALL with ADDR_NONE, ADDR_UNSIGNED or ADDR_QUICKFIX"));
Bram Moolenaarb7316892019-05-01 18:08:42 +02002331 break;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002332 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002333 }
2334
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002335 // accept numbered register only when no count allowed (:put)
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002336 if ( (ea.argt & EX_REGSTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002337 && *ea.arg != NUL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002338 // Do not allow register = for user commands
Bram Moolenaar958636c2014-10-21 20:01:58 +02002339 && (!IS_USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002340 && !((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002342#ifndef FEAT_CLIPBOARD
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002343 // check these explicitly for a more specific error message
Bram Moolenaar85de2062011-05-05 14:26:41 +02002344 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345 {
Bram Moolenaar99b12722019-01-14 20:16:40 +01002346 errormsg = _(e_invalidreg);
Bram Moolenaar85de2062011-05-05 14:26:41 +02002347 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348 }
2349#endif
Bram Moolenaar958636c2014-10-21 20:01:58 +02002350 if (valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2351 && !IS_USER_CMDIDX(ea.cmdidx))))
Bram Moolenaar85de2062011-05-05 14:26:41 +02002352 {
2353 ea.regname = *ea.arg++;
2354#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002355 // for '=' register: accept the rest of the line as an expression
Bram Moolenaar85de2062011-05-05 14:26:41 +02002356 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2357 {
2358 set_expr_line(vim_strsave(ea.arg));
2359 ea.arg += STRLEN(ea.arg);
2360 }
2361#endif
2362 ea.arg = skipwhite(ea.arg);
2363 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002364 }
2365
2366 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002367 * Check for a count. When accepting a EX_BUFNAME, don't use "123foo" as a
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368 * count, it's a buffer name.
2369 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002370 if ((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)
2371 && (!(ea.argt & EX_BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01002372 || VIM_ISWHITE(*p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002373 {
2374 n = getdigits(&ea.arg);
2375 ea.arg = skipwhite(ea.arg);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002376 if (n <= 0 && !ni && (ea.argt & EX_ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002378 errormsg = _(e_zerocount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002379 goto doend;
2380 }
Bram Moolenaarb7316892019-05-01 18:08:42 +02002381 if (ea.addr_type != ADDR_LINES) // e.g. :buffer 2, :sleep 3
Bram Moolenaar071d4272004-06-13 20:20:40 +00002382 {
2383 ea.line2 = n;
2384 if (ea.addr_count == 0)
2385 ea.addr_count = 1;
2386 }
2387 else
2388 {
2389 ea.line1 = ea.line2;
2390 ea.line2 += n - 1;
2391 ++ea.addr_count;
2392 /*
2393 * Be vi compatible: no error message for out of range.
2394 */
Bram Moolenaarb7316892019-05-01 18:08:42 +02002395 if (ea.line2 > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002396 ea.line2 = curbuf->b_ml.ml_line_count;
2397 }
2398 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002399
2400 /*
2401 * Check for flags: 'l', 'p' and '#'.
2402 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002403 if (ea.argt & EX_FLAGS)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002404 get_flags(&ea);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002405 if (!ni && !(ea.argt & EX_EXTRA) && *ea.arg != NUL
2406 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & EX_TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002408 // no arguments allowed but there is something
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02002409 errormsg = ex_errmsg(e_trailing_arg, ea.arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410 goto doend;
2411 }
2412
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002413 if (!ni && (ea.argt & EX_NEEDARG) && *ea.arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002415 errormsg = _(e_argreq);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002416 goto doend;
2417 }
2418
2419#ifdef FEAT_EVAL
2420 /*
2421 * Skip the command when it's not going to be executed.
2422 * The commands like :if, :endif, etc. always need to be executed.
2423 * Also make an exception for commands that handle a trailing command
2424 * themselves.
2425 */
2426 if (ea.skip)
2427 {
2428 switch (ea.cmdidx)
2429 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002430 // commands that need evaluation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002431 case CMD_while:
2432 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002433 case CMD_for:
2434 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435 case CMD_if:
2436 case CMD_elseif:
2437 case CMD_else:
2438 case CMD_endif:
2439 case CMD_try:
2440 case CMD_catch:
2441 case CMD_finally:
2442 case CMD_endtry:
2443 case CMD_function:
Bram Moolenaarab55c682020-03-01 19:41:43 +01002444 case CMD_def:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002445 break;
2446
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002447 // Commands that handle '|' themselves. Check: A command should
2448 // either have the EX_TRLBAR flag, appear in this list or appear in
2449 // the list at ":help :bar".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002450 case CMD_aboveleft:
2451 case CMD_and:
2452 case CMD_belowright:
2453 case CMD_botright:
2454 case CMD_browse:
2455 case CMD_call:
2456 case CMD_confirm:
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01002457 case CMD_const:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002458 case CMD_delfunction:
2459 case CMD_djump:
2460 case CMD_dlist:
2461 case CMD_dsearch:
2462 case CMD_dsplit:
2463 case CMD_echo:
2464 case CMD_echoerr:
2465 case CMD_echomsg:
2466 case CMD_echon:
Bram Moolenaara76b3152020-02-16 16:20:21 +01002467 case CMD_eval:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468 case CMD_execute:
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002469 case CMD_filter:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002470 case CMD_help:
2471 case CMD_hide:
2472 case CMD_ijump:
2473 case CMD_ilist:
2474 case CMD_isearch:
2475 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002476 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477 case CMD_keepjumps:
2478 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01002479 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480 case CMD_leftabove:
2481 case CMD_let:
2482 case CMD_lockmarks:
Bram Moolenaarcc4423a2019-11-26 17:05:00 +01002483 case CMD_lockvar:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002484 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002486 case CMD_mzscheme:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01002487 case CMD_noautocmd:
2488 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002489 case CMD_perl:
2490 case CMD_psearch:
2491 case CMD_python:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002492 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002493 case CMD_python3:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494 case CMD_return:
2495 case CMD_rightbelow:
2496 case CMD_ruby:
2497 case CMD_silent:
2498 case CMD_smagic:
2499 case CMD_snomagic:
2500 case CMD_substitute:
2501 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002502 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503 case CMD_tcl:
2504 case CMD_throw:
2505 case CMD_tilde:
2506 case CMD_topleft:
2507 case CMD_unlet:
Bram Moolenaarcc4423a2019-11-26 17:05:00 +01002508 case CMD_unlockvar:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509 case CMD_verbose:
2510 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002511 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512 break;
2513
2514 default: goto doend;
2515 }
2516 }
2517#endif
2518
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002519 if (ea.argt & EX_XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520 {
2521 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2522 goto doend;
2523 }
2524
Bram Moolenaar071d4272004-06-13 20:20:40 +00002525 /*
2526 * Accept buffer name. Cannot be used at the same time with a buffer
2527 * number. Don't do this for a user command.
2528 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002529 if ((ea.argt & EX_BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
Bram Moolenaar958636c2014-10-21 20:01:58 +02002530 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002531 {
2532 /*
2533 * :bdelete, :bwipeout and :bunload take several arguments, separated
2534 * by spaces: find next space (skipping over escaped characters).
2535 * The others take one argument: ignore trailing spaces.
2536 */
2537 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2538 || ea.cmdidx == CMD_bunload)
2539 p = skiptowhite_esc(ea.arg);
2540 else
2541 {
2542 p = ea.arg + STRLEN(ea.arg);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002543 while (p > ea.arg && VIM_ISWHITE(p[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544 --p;
2545 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002546 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & EX_BUFUNL) != 0,
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002547 FALSE, FALSE);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002548 if (ea.line2 < 0) // failed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002549 goto doend;
2550 ea.addr_count = 1;
2551 ea.arg = skipwhite(p);
2552 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002554 // The :try command saves the emsg_silent flag, reset it here when
2555 // ":silent! try" was used, it should only apply to :try itself.
Bram Moolenaareffed932018-08-14 13:38:17 +02002556 if (ea.cmdidx == CMD_try && ea.did_esilent > 0)
Bram Moolenaar2be57332018-02-13 18:05:18 +01002557 {
Bram Moolenaareffed932018-08-14 13:38:17 +02002558 emsg_silent -= ea.did_esilent;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002559 if (emsg_silent < 0)
2560 emsg_silent = 0;
Bram Moolenaareffed932018-08-14 13:38:17 +02002561 ea.did_esilent = 0;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002562 }
2563
Bram Moolenaar071d4272004-06-13 20:20:40 +00002564/*
Bram Moolenaar2be57332018-02-13 18:05:18 +01002565 * 7. Execute the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002566 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002567
Bram Moolenaar958636c2014-10-21 20:01:58 +02002568 if (IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569 {
2570 /*
2571 * Execute a user-defined command.
2572 */
2573 do_ucmd(&ea);
2574 }
2575 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576 {
2577 /*
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002578 * Call the function to execute the builtin command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002579 */
2580 ea.errmsg = NULL;
2581 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2582 if (ea.errmsg != NULL)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02002583 errormsg = ea.errmsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002584 }
2585
2586#ifdef FEAT_EVAL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002587 // Set flag that any command was executed, used by ex_vim9script().
Bram Moolenaar16531552020-02-08 16:00:46 +01002588 if (getline_equal(ea.getline, ea.cookie, getsourceline)
2589 && current_sctx.sc_sid > 0)
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002590 SCRIPT_ITEM(current_sctx.sc_sid)->sn_had_command = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002591
Bram Moolenaar071d4272004-06-13 20:20:40 +00002592 /*
2593 * If the command just executed called do_cmdline(), any throw or ":return"
2594 * or ":finish" encountered there must also check the cstack of the still
2595 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2596 * exception, or reanimate a returned function or finished script file and
2597 * return or finish it again.
2598 */
2599 if (need_rethrow)
2600 do_throw(cstack);
2601 else if (check_cstack)
2602 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002603 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002605 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606 && current_func_returned())
2607 do_return(&ea, TRUE, FALSE, NULL);
2608 }
2609 need_rethrow = check_cstack = FALSE;
2610#endif
2611
2612doend:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002613 if (curwin->w_cursor.lnum == 0) // can happen with zero line number
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002614 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002615 curwin->w_cursor.lnum = 1;
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002616 curwin->w_cursor.col = 0;
2617 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002618
2619 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2620 {
2621 if (sourcing)
2622 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002623 if (errormsg != (char *)IObuff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624 {
2625 STRCPY(IObuff, errormsg);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002626 errormsg = (char *)IObuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002627 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002628 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002629 }
2630 emsg(errormsg);
2631 }
2632#ifdef FEAT_EVAL
2633 do_errthrow(cstack,
Bram Moolenaar958636c2014-10-21 20:01:58 +02002634 (ea.cmdidx != CMD_SIZE && !IS_USER_CMDIDX(ea.cmdidx))
2635 ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002636#endif
2637
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002638 undo_cmdmod(&ea, save_msg_scroll);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639 cmdmod = save_cmdmod;
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01002640 reg_executing = save_reg_executing;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002641
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002642#ifdef HAVE_SANDBOX
Bram Moolenaareffed932018-08-14 13:38:17 +02002643 if (ea.did_sandbox)
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002644 --sandbox;
2645#endif
2646
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002647 if (ea.nextcmd && *ea.nextcmd == NUL) // not really a next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00002648 ea.nextcmd = NULL;
2649
2650#ifdef FEAT_EVAL
2651 --ex_nesting_level;
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002652 vim_free(ea.cmdline_tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002653#endif
2654
2655 return ea.nextcmd;
2656}
2657#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00002658 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659#endif
2660
Bram Moolenaar8930caa2020-07-23 16:37:03 +02002661static char ex_error_buf[MSG_BUF_LEN];
2662
2663/*
2664 * Return an error message with argument included.
2665 * Uses a static buffer, only the last error will be kept.
2666 * "msg" will be translated, caller should use N_().
2667 */
2668 char *
2669ex_errmsg(char *msg, char_u *arg)
2670{
2671 vim_snprintf(ex_error_buf, MSG_BUF_LEN, _(msg), arg);
2672 return ex_error_buf;
2673}
2674
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675/*
Bram Moolenaareffed932018-08-14 13:38:17 +02002676 * Parse and skip over command modifiers:
2677 * - update eap->cmd
2678 * - store flags in "cmdmod".
2679 * - Set ex_pressedreturn for an empty command line.
2680 * - set msg_silent for ":silent"
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002681 * - set 'eventignore' to "all" for ":noautocmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002682 * - set p_verbose for ":verbose"
2683 * - Increment "sandbox" for ":sandbox"
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002684 * When "skip_only" is TRUE the global variables are not changed, except for
2685 * "cmdmod".
Bram Moolenaareffed932018-08-14 13:38:17 +02002686 * Return FAIL when the command is not to be executed.
2687 * May set "errormsg" to an error message.
2688 */
2689 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002690parse_command_modifiers(exarg_T *eap, char **errormsg, int skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002691{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002692 char_u *p;
2693 int starts_with_colon = FALSE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002694
Bram Moolenaara80faa82020-04-12 19:37:17 +02002695 CLEAR_FIELD(cmdmod);
Bram Moolenaareffed932018-08-14 13:38:17 +02002696 eap->verbose_save = -1;
2697 eap->save_msg_silent = -1;
2698
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002699 // Repeat until no more command modifiers are found.
Bram Moolenaareffed932018-08-14 13:38:17 +02002700 for (;;)
2701 {
Bram Moolenaareffed932018-08-14 13:38:17 +02002702 while (*eap->cmd == ' ' || *eap->cmd == '\t' || *eap->cmd == ':')
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002703 {
2704 if (*eap->cmd == ':')
2705 starts_with_colon = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002706 ++eap->cmd;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002707 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002708
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002709 // in ex mode, an empty line works like :+
Bram Moolenaareffed932018-08-14 13:38:17 +02002710 if (*eap->cmd == NUL && exmode_active
2711 && (getline_equal(eap->getline, eap->cookie, getexmodeline)
2712 || getline_equal(eap->getline, eap->cookie, getexline))
2713 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2714 {
2715 eap->cmd = (char_u *)"+";
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002716 if (!skip_only)
2717 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002718 }
2719
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002720 // ignore comment and empty lines
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002721 if (comment_start(eap->cmd, starts_with_colon))
Bram Moolenaareffed932018-08-14 13:38:17 +02002722 return FAIL;
2723 if (*eap->cmd == NUL)
2724 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002725 if (!skip_only)
2726 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002727 return FAIL;
2728 }
2729
Bram Moolenaareffed932018-08-14 13:38:17 +02002730 p = skip_range(eap->cmd, NULL);
2731 switch (*p)
2732 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002733 // When adding an entry, also modify cmd_exists().
Bram Moolenaareffed932018-08-14 13:38:17 +02002734 case 'a': if (!checkforcmd(&eap->cmd, "aboveleft", 3))
2735 break;
2736 cmdmod.split |= WSP_ABOVE;
2737 continue;
2738
2739 case 'b': if (checkforcmd(&eap->cmd, "belowright", 3))
2740 {
2741 cmdmod.split |= WSP_BELOW;
2742 continue;
2743 }
2744 if (checkforcmd(&eap->cmd, "browse", 3))
2745 {
2746#ifdef FEAT_BROWSE_CMD
2747 cmdmod.browse = TRUE;
2748#endif
2749 continue;
2750 }
2751 if (!checkforcmd(&eap->cmd, "botright", 2))
2752 break;
2753 cmdmod.split |= WSP_BOT;
2754 continue;
2755
2756 case 'c': if (!checkforcmd(&eap->cmd, "confirm", 4))
2757 break;
2758#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2759 cmdmod.confirm = TRUE;
2760#endif
2761 continue;
2762
2763 case 'k': if (checkforcmd(&eap->cmd, "keepmarks", 3))
2764 {
2765 cmdmod.keepmarks = TRUE;
2766 continue;
2767 }
2768 if (checkforcmd(&eap->cmd, "keepalt", 5))
2769 {
2770 cmdmod.keepalt = TRUE;
2771 continue;
2772 }
2773 if (checkforcmd(&eap->cmd, "keeppatterns", 5))
2774 {
2775 cmdmod.keeppatterns = TRUE;
2776 continue;
2777 }
2778 if (!checkforcmd(&eap->cmd, "keepjumps", 5))
2779 break;
2780 cmdmod.keepjumps = TRUE;
2781 continue;
2782
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002783 case 'f': // only accept ":filter {pat} cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002784 {
2785 char_u *reg_pat;
2786
2787 if (!checkforcmd(&p, "filter", 4)
2788 || *p == NUL || ends_excmd(*p))
2789 break;
2790 if (*p == '!')
2791 {
2792 cmdmod.filter_force = TRUE;
2793 p = skipwhite(p + 1);
2794 if (*p == NUL || ends_excmd(*p))
2795 break;
2796 }
Bram Moolenaar1e624c92020-07-11 14:08:04 +02002797#ifdef FEAT_EVAL
Bram Moolenaarb074e8b2020-07-11 13:40:45 +02002798 // Avoid that "filter(arg)" is recognized.
2799 if (in_vim9script() && !VIM_ISWHITE(*p))
2800 break;
Bram Moolenaar1e624c92020-07-11 14:08:04 +02002801#endif
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002802 if (skip_only)
2803 p = skip_vimgrep_pat(p, NULL, NULL);
2804 else
2805 // NOTE: This puts a NUL after the pattern.
2806 p = skip_vimgrep_pat(p, &reg_pat, NULL);
Bram Moolenaareffed932018-08-14 13:38:17 +02002807 if (p == NULL || *p == NUL)
2808 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002809 if (!skip_only)
2810 {
2811 cmdmod.filter_regmatch.regprog =
Bram Moolenaareffed932018-08-14 13:38:17 +02002812 vim_regcomp(reg_pat, RE_MAGIC);
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002813 if (cmdmod.filter_regmatch.regprog == NULL)
2814 break;
2815 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002816 eap->cmd = p;
2817 continue;
2818 }
2819
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002820 // ":hide" and ":hide | cmd" are not modifiers
Bram Moolenaareffed932018-08-14 13:38:17 +02002821 case 'h': if (p != eap->cmd || !checkforcmd(&p, "hide", 3)
2822 || *p == NUL || ends_excmd(*p))
2823 break;
2824 eap->cmd = p;
2825 cmdmod.hide = TRUE;
2826 continue;
2827
2828 case 'l': if (checkforcmd(&eap->cmd, "lockmarks", 3))
2829 {
2830 cmdmod.lockmarks = TRUE;
2831 continue;
2832 }
2833
2834 if (!checkforcmd(&eap->cmd, "leftabove", 5))
2835 break;
2836 cmdmod.split |= WSP_ABOVE;
2837 continue;
2838
2839 case 'n': if (checkforcmd(&eap->cmd, "noautocmd", 3))
2840 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002841 if (cmdmod.save_ei == NULL && !skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002842 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002843 // Set 'eventignore' to "all". Restore the
2844 // existing option value later.
Bram Moolenaareffed932018-08-14 13:38:17 +02002845 cmdmod.save_ei = vim_strsave(p_ei);
2846 set_string_option_direct((char_u *)"ei", -1,
2847 (char_u *)"all", OPT_FREE, SID_NONE);
2848 }
2849 continue;
2850 }
2851 if (!checkforcmd(&eap->cmd, "noswapfile", 3))
2852 break;
2853 cmdmod.noswapfile = TRUE;
2854 continue;
2855
2856 case 'r': if (!checkforcmd(&eap->cmd, "rightbelow", 6))
2857 break;
2858 cmdmod.split |= WSP_BELOW;
2859 continue;
2860
2861 case 's': if (checkforcmd(&eap->cmd, "sandbox", 3))
2862 {
2863#ifdef HAVE_SANDBOX
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002864 if (!skip_only)
2865 {
2866 if (!eap->did_sandbox)
2867 ++sandbox;
2868 eap->did_sandbox = TRUE;
2869 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002870#endif
2871 continue;
2872 }
2873 if (!checkforcmd(&eap->cmd, "silent", 3))
2874 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002875 if (!skip_only)
2876 {
2877 if (eap->save_msg_silent == -1)
2878 eap->save_msg_silent = msg_silent;
2879 ++msg_silent;
2880 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002881 if (*eap->cmd == '!' && !VIM_ISWHITE(eap->cmd[-1]))
2882 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002883 // ":silent!", but not "silent !cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002884 eap->cmd = skipwhite(eap->cmd + 1);
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002885 if (!skip_only)
2886 {
2887 ++emsg_silent;
2888 ++eap->did_esilent;
2889 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002890 }
2891 continue;
2892
2893 case 't': if (checkforcmd(&p, "tab", 3))
2894 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002895 if (!skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002896 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002897 long tabnr = get_address(eap, &eap->cmd,
2898 ADDR_TABS, eap->skip,
2899 skip_only, FALSE, 1);
2900 if (tabnr == MAXLNUM)
2901 cmdmod.tab = tabpage_index(curtab) + 1;
2902 else
Bram Moolenaareffed932018-08-14 13:38:17 +02002903 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002904 if (tabnr < 0 || tabnr > LAST_TAB_NR)
2905 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002906 *errormsg = _(e_invrange);
Bram Moolenaar548e5982018-12-26 21:45:00 +01002907 return FAIL;
2908 }
2909 cmdmod.tab = tabnr + 1;
Bram Moolenaareffed932018-08-14 13:38:17 +02002910 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002911 }
2912 eap->cmd = p;
2913 continue;
2914 }
2915 if (!checkforcmd(&eap->cmd, "topleft", 2))
2916 break;
2917 cmdmod.split |= WSP_TOP;
2918 continue;
2919
2920 case 'u': if (!checkforcmd(&eap->cmd, "unsilent", 3))
2921 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002922 if (!skip_only)
2923 {
2924 if (eap->save_msg_silent == -1)
2925 eap->save_msg_silent = msg_silent;
2926 msg_silent = 0;
2927 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002928 continue;
2929
2930 case 'v': if (checkforcmd(&eap->cmd, "vertical", 4))
2931 {
2932 cmdmod.split |= WSP_VERT;
2933 continue;
2934 }
2935 if (!checkforcmd(&p, "verbose", 4))
2936 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002937 if (!skip_only)
2938 {
2939 if (eap->verbose_save < 0)
2940 eap->verbose_save = p_verbose;
2941 if (vim_isdigit(*eap->cmd))
2942 p_verbose = atoi((char *)eap->cmd);
2943 else
2944 p_verbose = 1;
2945 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002946 eap->cmd = p;
2947 continue;
2948 }
2949 break;
2950 }
2951
2952 return OK;
2953}
2954
2955/*
Bram Moolenaarb074e8b2020-07-11 13:40:45 +02002956 * Undo and free contents of "cmdmod".
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002957 */
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002958 void
2959undo_cmdmod(exarg_T *eap, int save_msg_scroll)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002960{
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002961 if (eap->verbose_save >= 0)
2962 p_verbose = eap->verbose_save;
2963
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002964 if (cmdmod.save_ei != NULL)
2965 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002966 // Restore 'eventignore' to the value before ":noautocmd".
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002967 set_string_option_direct((char_u *)"ei", -1, cmdmod.save_ei,
2968 OPT_FREE, SID_NONE);
2969 free_string_option(cmdmod.save_ei);
2970 }
2971
2972 if (cmdmod.filter_regmatch.regprog != NULL)
2973 vim_regfree(cmdmod.filter_regmatch.regprog);
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002974
2975 if (eap->save_msg_silent != -1)
2976 {
2977 // messages could be enabled for a serious error, need to check if the
2978 // counters don't become negative
2979 if (!did_emsg || msg_silent > eap->save_msg_silent)
2980 msg_silent = eap->save_msg_silent;
2981 emsg_silent -= eap->did_esilent;
2982 if (emsg_silent < 0)
2983 emsg_silent = 0;
2984 // Restore msg_scroll, it's set by file I/O commands, even when no
2985 // message is actually displayed.
2986 msg_scroll = save_msg_scroll;
2987
2988 // "silent reg" or "silent echo x" inside "redir" leaves msg_col
2989 // somewhere in the line. Put it back in the first column.
2990 if (redirecting())
2991 msg_col = 0;
2992 }
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002993}
2994
2995/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002996 * Parse the address range, if any, in "eap".
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02002997 * May set the last search pattern, unless "silent" is TRUE.
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002998 * Return FAIL and set "errormsg" or return OK.
2999 */
3000 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003001parse_cmd_address(exarg_T *eap, char **errormsg, int silent)
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003002{
3003 int address_count = 1;
3004 linenr_T lnum;
3005
3006 // Repeat for all ',' or ';' separated addresses.
3007 for (;;)
3008 {
3009 eap->line1 = eap->line2;
3010 switch (eap->addr_type)
3011 {
3012 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003013 case ADDR_OTHER:
Bram Moolenaarb5383b12020-05-18 19:46:48 +02003014 // Default is the cursor line number. Avoid using an invalid
3015 // line number though.
3016 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
3017 eap->line2 = curbuf->b_ml.ml_line_count;
3018 else
3019 eap->line2 = curwin->w_cursor.lnum;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003020 break;
3021 case ADDR_WINDOWS:
3022 eap->line2 = CURRENT_WIN_NR;
3023 break;
3024 case ADDR_ARGUMENTS:
3025 eap->line2 = curwin->w_arg_idx + 1;
3026 if (eap->line2 > ARGCOUNT)
3027 eap->line2 = ARGCOUNT;
3028 break;
3029 case ADDR_LOADED_BUFFERS:
3030 case ADDR_BUFFERS:
3031 eap->line2 = curbuf->b_fnum;
3032 break;
3033 case ADDR_TABS:
3034 eap->line2 = CURRENT_TAB_NR;
3035 break;
3036 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003037 case ADDR_UNSIGNED:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003038 eap->line2 = 1;
3039 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003040 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003041#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02003042 eap->line2 = qf_get_cur_idx(eap);
3043#endif
3044 break;
3045 case ADDR_QUICKFIX_VALID:
3046#ifdef FEAT_QUICKFIX
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003047 eap->line2 = qf_get_cur_valid_idx(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003048#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003049 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003050 case ADDR_NONE:
3051 // Will give an error later if a range is found.
3052 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003053 }
3054 eap->cmd = skipwhite(eap->cmd);
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003055 lnum = get_address(eap, &eap->cmd, eap->addr_type, eap->skip, silent,
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003056 eap->addr_count == 0, address_count++);
3057 if (eap->cmd == NULL) // error detected
3058 return FAIL;
3059 if (lnum == MAXLNUM)
3060 {
3061 if (*eap->cmd == '%') // '%' - all lines
3062 {
3063 ++eap->cmd;
3064 switch (eap->addr_type)
3065 {
3066 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003067 case ADDR_OTHER:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003068 eap->line1 = 1;
3069 eap->line2 = curbuf->b_ml.ml_line_count;
3070 break;
3071 case ADDR_LOADED_BUFFERS:
3072 {
3073 buf_T *buf = firstbuf;
3074
3075 while (buf->b_next != NULL
3076 && buf->b_ml.ml_mfp == NULL)
3077 buf = buf->b_next;
3078 eap->line1 = buf->b_fnum;
3079 buf = lastbuf;
3080 while (buf->b_prev != NULL
3081 && buf->b_ml.ml_mfp == NULL)
3082 buf = buf->b_prev;
3083 eap->line2 = buf->b_fnum;
3084 break;
3085 }
3086 case ADDR_BUFFERS:
3087 eap->line1 = firstbuf->b_fnum;
3088 eap->line2 = lastbuf->b_fnum;
3089 break;
3090 case ADDR_WINDOWS:
3091 case ADDR_TABS:
3092 if (IS_USER_CMDIDX(eap->cmdidx))
3093 {
3094 eap->line1 = 1;
3095 eap->line2 = eap->addr_type == ADDR_WINDOWS
3096 ? LAST_WIN_NR : LAST_TAB_NR;
3097 }
3098 else
3099 {
3100 // there is no Vim command which uses '%' and
3101 // ADDR_WINDOWS or ADDR_TABS
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003102 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003103 return FAIL;
3104 }
3105 break;
3106 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003107 case ADDR_UNSIGNED:
3108 case ADDR_QUICKFIX:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003109 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003110 return FAIL;
3111 case ADDR_ARGUMENTS:
3112 if (ARGCOUNT == 0)
3113 eap->line1 = eap->line2 = 0;
3114 else
3115 {
3116 eap->line1 = 1;
3117 eap->line2 = ARGCOUNT;
3118 }
3119 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003120 case ADDR_QUICKFIX_VALID:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003121#ifdef FEAT_QUICKFIX
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003122 eap->line1 = 1;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003123 eap->line2 = qf_get_valid_size(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003124 if (eap->line2 == 0)
3125 eap->line2 = 1;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003126#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003127 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003128 case ADDR_NONE:
3129 // Will give an error later if a range is found.
3130 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003131 }
3132 ++eap->addr_count;
3133 }
3134 else if (*eap->cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
3135 {
3136 pos_T *fp;
3137
3138 // '*' - visual area
3139 if (eap->addr_type != ADDR_LINES)
3140 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003141 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003142 return FAIL;
3143 }
3144
3145 ++eap->cmd;
3146 if (!eap->skip)
3147 {
3148 fp = getmark('<', FALSE);
3149 if (check_mark(fp) == FAIL)
3150 return FAIL;
3151 eap->line1 = fp->lnum;
3152 fp = getmark('>', FALSE);
3153 if (check_mark(fp) == FAIL)
3154 return FAIL;
3155 eap->line2 = fp->lnum;
3156 ++eap->addr_count;
3157 }
3158 }
3159 }
3160 else
3161 eap->line2 = lnum;
3162 eap->addr_count++;
3163
3164 if (*eap->cmd == ';')
3165 {
3166 if (!eap->skip)
3167 {
3168 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar0e717042020-04-27 19:29:01 +02003169 // Don't leave the cursor on an illegal line or column, but do
3170 // accept zero as address, so 0;/PATTERN/ works correctly.
3171 if (eap->line2 > 0)
3172 check_cursor();
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003173 }
3174 }
3175 else if (*eap->cmd != ',')
3176 break;
3177 ++eap->cmd;
3178 }
3179
3180 // One address given: set start and end lines.
3181 if (eap->addr_count == 1)
3182 {
3183 eap->line1 = eap->line2;
3184 // ... but only implicit: really no address given
3185 if (lnum == MAXLNUM)
3186 eap->addr_count = 0;
3187 }
3188 return OK;
3189}
3190
3191/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003192 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193 * If there is a match advance "pp" to the argument and return TRUE.
3194 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003195 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003196checkforcmd(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003197 char_u **pp, // start of command
3198 char *cmd, // name of command
3199 int len) // required length
Bram Moolenaar071d4272004-06-13 20:20:40 +00003200{
3201 int i;
3202
3203 for (i = 0; cmd[i] != NUL; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003204 if (((char_u *)cmd)[i] != (*pp)[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003205 break;
3206 if (i >= len && !isalpha((*pp)[i]))
3207 {
3208 *pp = skipwhite(*pp + i);
3209 return TRUE;
3210 }
3211 return FALSE;
3212}
3213
3214/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003215 * Append "cmd" to the error message in IObuff.
3216 * Takes care of limiting the length and handling 0xa0, which would be
3217 * invisible otherwise.
3218 */
3219 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003220append_command(char_u *cmd)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003221{
3222 char_u *s = cmd;
3223 char_u *d;
3224
3225 STRCAT(IObuff, ": ");
3226 d = IObuff + STRLEN(IObuff);
3227 while (*s != NUL && d - IObuff < IOSIZE - 7)
3228 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003229 if (enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) : *s == 0xa0)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003230 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003231 s += enc_utf8 ? 2 : 1;
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003232 STRCPY(d, "<a0>");
3233 d += 4;
3234 }
3235 else
3236 MB_COPY_CHAR(s, d);
3237 }
3238 *d = NUL;
3239}
3240
3241/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003242 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003243 * Start of the name can be found at eap->cmd.
Bram Moolenaar25190db2019-05-04 15:05:28 +02003244 * Sets eap->cmdidx and returns a pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003245 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003246 *
3247 * If "lookup" is not NULL recognize expression without "eval" or "call" and
3248 * assignment without "let". Sets eap->cmdidx to the command while returning
3249 * "eap->cmd".
3250 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003251 * Returns NULL for an ambiguous user command.
3252 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003253 char_u *
3254find_ex_command(
3255 exarg_T *eap,
Bram Moolenaara494f562020-04-18 17:45:38 +02003256 int *full UNUSED,
Bram Moolenaarb84a3812020-05-01 15:44:29 +02003257 void *(*lookup)(char_u *, size_t, cctx_T *) UNUSED,
Bram Moolenaara494f562020-04-18 17:45:38 +02003258 cctx_T *cctx UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003259{
3260 int len;
3261 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003262 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003263
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003264#ifdef FEAT_EVAL
3265 /*
3266 * Recognize a Vim9 script function/method call and assignment:
3267 * "lvar = value", "lvar(arg)", "[1, 2 3]->Func()"
3268 */
Bram Moolenaar0c6ceaf2020-02-22 18:36:32 +01003269 p = eap->cmd;
Bram Moolenaar3d48e252020-07-15 14:15:52 +02003270 if (lookup != NULL && (vim_strchr((char_u *)"{('[", *p) != NULL
3271 || ((p = to_name_const_end(eap->cmd)) > eap->cmd
3272 && *p != NUL)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003273 {
3274 int oplen;
3275 int heredoc;
3276
Bram Moolenaar3d48e252020-07-15 14:15:52 +02003277 if (
3278 // "(..." is an expression.
3279 // "funcname(" is always a function call.
3280 *p == '('
3281 || (p == eap->cmd
3282 ? (
3283 // "{..." is an dict expression.
3284 *eap->cmd == '{'
3285 // "'string'->func()" is an expression.
3286 || *eap->cmd == '\''
3287 // "g:varname" is an expression.
3288 || eap->cmd[1] == ':'
3289 )
3290 : (
3291 // "varname[]" is an expression.
3292 *p == '['
3293 // "varname->func()" is an expression.
3294 || (*p == '-' && p[1] == '>')
3295 // "varname.expr" is an expression.
3296 || (*p == '.' && ASCII_ISALPHA(p[1]))
3297 )))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003298 {
3299 eap->cmdidx = CMD_eval;
3300 return eap->cmd;
3301 }
3302
Bram Moolenaard2ef6b32020-07-02 21:11:34 +02003303 // "[...]->Method()" is a list expression, but "[a, b] = Func()" is
3304 // an assignment.
3305 // If there is no line break inside the "[...]" then "p" is advanced to
3306 // after the "]" by to_name_const_end(): check if a "=" follows.
3307 // If "[...]" has a line break "p" still points at the "[" and it can't
3308 // be an assignment.
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003309 if (*eap->cmd == '[')
Bram Moolenaard2ef6b32020-07-02 21:11:34 +02003310 {
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003311 p = to_name_const_end(eap->cmd);
3312 if (p == eap->cmd || *skipwhite(p) != '=')
3313 {
3314 eap->cmdidx = CMD_eval;
3315 return eap->cmd;
3316 }
3317 if (p > eap->cmd && *skipwhite(p) == '=')
3318 {
3319 eap->cmdidx = CMD_let;
3320 return eap->cmd;
3321 }
Bram Moolenaard2ef6b32020-07-02 21:11:34 +02003322 }
3323
Bram Moolenaar5f195932020-07-01 20:07:14 +02003324 // Recognize an assignment if we recognize the variable name:
3325 // "g:var = expr"
3326 // "var = expr" where "var" is a local var name.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003327 oplen = assignment_len(skipwhite(p), &heredoc);
3328 if (oplen > 0)
3329 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003330 if (((p - eap->cmd) > 2 && eap->cmd[1] == ':')
Bram Moolenaarb84a3812020-05-01 15:44:29 +02003331 || lookup(eap->cmd, p - eap->cmd, cctx) != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003332 {
3333 eap->cmdidx = CMD_let;
3334 return eap->cmd;
3335 }
3336 }
Bram Moolenaar43e969d2020-07-23 21:14:43 +02003337
3338 // Recognize using a type for a w:, b:, t: or g: variable:
3339 // "w:varname: number = 123".
3340 if (eap->cmd[1] == ':' && *p == ':')
3341 {
3342 eap->cmdidx = CMD_eval;
3343 return eap->cmd;
3344 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003345 }
3346#endif
3347
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348 /*
3349 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00003350 * Exceptions:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003351 * - the 'k' command can directly be followed by any character.
3352 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003353 * but :sre[wind] is another command, as are :scr[iptnames],
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00003355 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003356 */
3357 p = eap->cmd;
3358 if (*p == 'k')
3359 {
3360 eap->cmdidx = CMD_k;
3361 ++p;
3362 }
3363 else if (p[0] == 's'
Bram Moolenaar204b93f2015-08-04 22:02:51 +02003364 && ((p[1] == 'c' && (p[2] == NUL || (p[2] != 's' && p[2] != 'r'
3365 && (p[3] == NUL || (p[3] != 'i' && p[4] != 'p')))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366 || p[1] == 'g'
3367 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
3368 || p[1] == 'I'
3369 || (p[1] == 'r' && p[2] != 'e')))
3370 {
3371 eap->cmdidx = CMD_substitute;
3372 ++p;
3373 }
3374 else
3375 {
3376 while (ASCII_ISALPHA(*p))
3377 ++p;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003378 // for python 3.x support ":py3", ":python3", ":py3file", etc.
Bram Moolenaar55d5c032010-07-17 23:52:29 +02003379 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003380 {
Bram Moolenaarb6590522010-07-21 16:00:43 +02003381 while (ASCII_ISALNUM(*p))
3382 ++p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003383 }
3384 else if (*p == '9' && STRNCMP("vim9", eap->cmd, 4) == 0)
3385 {
3386 // include "9" for "vim9script"
3387 ++p;
3388 while (ASCII_ISALPHA(*p))
3389 ++p;
3390 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02003391
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003392 // check for non-alpha command
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3394 ++p;
3395 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003396 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
3397 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003398 // Check for ":dl", ":dell", etc. to ":deletel": that's
3399 // :delete with the 'l' flag. Same for 'p'.
Bram Moolenaardf177f62005-02-22 08:39:57 +00003400 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003401 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00003402 break;
3403 if (i == len - 1)
3404 {
3405 --len;
3406 if (p[-1] == 'l')
3407 eap->flags |= EXFLAG_LIST;
3408 else
3409 eap->flags |= EXFLAG_PRINT;
3410 }
3411 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003413 if (ASCII_ISLOWER(eap->cmd[0]))
3414 {
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003415 int c1 = eap->cmd[0];
Bram Moolenaar94f82cb2019-07-24 22:30:27 +02003416 int c2 = len == 1 ? NUL : eap->cmd[1];
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003417
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003418 if (command_count != (int)CMD_SIZE)
3419 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003420 iemsg(_("E943: Command table needs to be updated, run 'make cmdidxs'"));
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003421 getout(1);
3422 }
3423
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003424 // Use a precomputed index for fast look-up in cmdnames[]
3425 // taking into account the first 2 letters of eap->cmd.
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003426 eap->cmdidx = cmdidxs1[CharOrdLow(c1)];
3427 if (ASCII_ISLOWER(c2))
3428 eap->cmdidx += cmdidxs2[CharOrdLow(c1)][CharOrdLow(c2)];
3429 }
Bram Moolenaara494f562020-04-18 17:45:38 +02003430 else if (ASCII_ISUPPER(eap->cmd[0]))
3431 eap->cmdidx = CMD_Next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432 else
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003433 eap->cmdidx = CMD_bang;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434
3435 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
3436 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
3437 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
3438 (size_t)len) == 0)
3439 {
3440#ifdef FEAT_EVAL
3441 if (full != NULL
3442 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
3443 *full = TRUE;
3444#endif
3445 break;
3446 }
3447
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003448 // Look for a user defined command as a last resort. Let ":Print" be
3449 // overruled by a user defined command.
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01003450 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
3451 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452 {
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003453 // User defined commands may contain digits.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454 while (ASCII_ISALNUM(*p))
3455 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003456 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003458 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459 eap->cmdidx = CMD_SIZE;
3460 }
3461
3462 return p;
3463}
3464
3465#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003466static struct cmdmod
3467{
3468 char *name;
3469 int minlen;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003470 int has_count; // :123verbose :3tab
Bram Moolenaared53fb92007-11-24 20:50:24 +00003471} cmdmods[] = {
3472 {"aboveleft", 3, FALSE},
3473 {"belowright", 3, FALSE},
3474 {"botright", 2, FALSE},
3475 {"browse", 3, FALSE},
3476 {"confirm", 4, FALSE},
Bram Moolenaar7b668e82016-08-23 23:51:21 +02003477 {"filter", 4, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003478 {"hide", 3, FALSE},
3479 {"keepalt", 5, FALSE},
3480 {"keepjumps", 5, FALSE},
3481 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01003482 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003483 {"leftabove", 5, FALSE},
3484 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003485 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003486 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003487 {"rightbelow", 6, FALSE},
3488 {"sandbox", 3, FALSE},
3489 {"silent", 3, FALSE},
3490 {"tab", 3, TRUE},
3491 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003492 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003493 {"verbose", 4, TRUE},
3494 {"vertical", 4, FALSE},
3495};
3496
3497/*
3498 * Return length of a command modifier (including optional count).
3499 * Return zero when it's not a modifier.
3500 */
3501 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003502modifier_len(char_u *cmd)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003503{
3504 int i, j;
3505 char_u *p = cmd;
3506
3507 if (VIM_ISDIGIT(*cmd))
3508 p = skipwhite(skipdigits(cmd));
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003509 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003510 {
3511 for (j = 0; p[j] != NUL; ++j)
3512 if (p[j] != cmdmods[i].name[j])
3513 break;
Bram Moolenaar2d473ab2013-06-12 17:12:24 +02003514 if (!ASCII_ISALPHA(p[j]) && j >= cmdmods[i].minlen
Bram Moolenaared53fb92007-11-24 20:50:24 +00003515 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003516 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003517 }
3518 return 0;
3519}
3520
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521/*
3522 * Return > 0 if an Ex command "name" exists.
3523 * Return 2 if there is an exact match.
3524 * Return 3 if there is an ambiguous match.
3525 */
3526 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003527cmd_exists(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528{
3529 exarg_T ea;
3530 int full = FALSE;
3531 int i;
3532 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003533 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003535 // Check command modifiers.
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003536 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537 {
3538 for (j = 0; name[j] != NUL; ++j)
3539 if (name[j] != cmdmods[i].name[j])
3540 break;
3541 if (name[j] == NUL && j >= cmdmods[i].minlen)
3542 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3543 }
3544
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003545 // Check built-in commands and user defined commands.
3546 // For ":2match" and ":3match" we need to skip the number.
Bram Moolenaara9587612006-05-04 21:47:50 +00003547 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003549 p = find_ex_command(&ea, &full, NULL, NULL);
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003550 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003552 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3553 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003554 if (*skipwhite(p) != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003555 return 0; // trailing garbage
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3557}
3558#endif
3559
Bram Moolenaard0190392019-08-23 21:17:35 +02003560 cmdidx_T
3561excmd_get_cmdidx(char_u *cmd, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562{
Bram Moolenaard0190392019-08-23 21:17:35 +02003563 cmdidx_T idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564
Bram Moolenaard0190392019-08-23 21:17:35 +02003565 for (idx = (cmdidx_T)0; (int)idx < (int)CMD_SIZE;
3566 idx = (cmdidx_T)((int)idx + 1))
3567 if (STRNCMP(cmdnames[(int)idx].cmd_name, cmd, (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568 break;
3569
Bram Moolenaard0190392019-08-23 21:17:35 +02003570 return idx;
3571}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003572
Bram Moolenaard0190392019-08-23 21:17:35 +02003573 long
3574excmd_get_argt(cmdidx_T idx)
3575{
3576 return (long)cmdnames[(int)idx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003577}
3578
3579/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003580 * Skip a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581 *
3582 * Backslashed delimiters after / or ? will be skipped, and commands will
3583 * not be expanded between /'s and ?'s or after "'".
3584 *
Bram Moolenaardf069ee2020-06-22 23:02:51 +02003585 * Also skip white space and ":" characters after the range.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586 * Returns the "cmd" pointer advanced to beyond the range.
3587 */
3588 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003589skip_range(
3590 char_u *cmd,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003591 int *ctx) // pointer to xp_context or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003592{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003593 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003594
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01003595 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;\\", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003596 {
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01003597 if (*cmd == '\\')
3598 {
3599 if (cmd[1] == '?' || cmd[1] == '/' || cmd[1] == '&')
3600 ++cmd;
3601 else
3602 break;
3603 }
3604 else if (*cmd == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605 {
3606 if (*++cmd == NUL && ctx != NULL)
3607 *ctx = EXPAND_NOTHING;
3608 }
3609 else if (*cmd == '/' || *cmd == '?')
3610 {
3611 delim = *cmd++;
3612 while (*cmd != NUL && *cmd != delim)
3613 if (*cmd++ == '\\' && *cmd != NUL)
3614 ++cmd;
3615 if (*cmd == NUL && ctx != NULL)
3616 *ctx = EXPAND_NOTHING;
3617 }
3618 if (*cmd != NUL)
3619 ++cmd;
3620 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00003621
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003622 // Skip ":" and white space.
Bram Moolenaardf177f62005-02-22 08:39:57 +00003623 while (*cmd == ':')
3624 cmd = skipwhite(cmd + 1);
3625
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626 return cmd;
3627}
3628
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003629 static void
3630addr_error(cmd_addr_T addr_type)
3631{
3632 if (addr_type == ADDR_NONE)
3633 emsg(_(e_norange));
3634 else
3635 emsg(_(e_invrange));
3636}
3637
Bram Moolenaar071d4272004-06-13 20:20:40 +00003638/*
Bram Moolenaar198cb662018-09-06 21:44:17 +02003639 * Get a single EX address.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003640 *
3641 * Set ptr to the next character after the part that was interpreted.
3642 * Set ptr to NULL when an error is encountered.
Bram Moolenaar198cb662018-09-06 21:44:17 +02003643 * This may set the last used search pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644 *
3645 * Return MAXLNUM when no Ex address was found.
3646 */
3647 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003648get_address(
3649 exarg_T *eap UNUSED,
3650 char_u **ptr,
Bram Moolenaar1b03a192019-12-08 17:08:29 +01003651 cmd_addr_T addr_type,
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003652 int skip, // only skip the address, don't use it
3653 int silent, // no errors or side effects
3654 int to_other_file, // flag: may jump to other file
3655 int address_count UNUSED) // 1 for first address, >1 after comma
Bram Moolenaar071d4272004-06-13 20:20:40 +00003656{
3657 int c;
3658 int i;
3659 long n;
3660 char_u *cmd;
3661 pos_T pos;
3662 pos_T *fp;
3663 linenr_T lnum;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003664 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665
3666 cmd = skipwhite(*ptr);
3667 lnum = MAXLNUM;
3668 do
3669 {
3670 switch (*cmd)
3671 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003672 case '.': // '.' - Cursor position
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003673 ++cmd;
3674 switch (addr_type)
3675 {
3676 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003677 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003678 lnum = curwin->w_cursor.lnum;
3679 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003680 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003681 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003682 break;
3683 case ADDR_ARGUMENTS:
3684 lnum = curwin->w_arg_idx + 1;
3685 break;
3686 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003687 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003688 lnum = curbuf->b_fnum;
3689 break;
3690 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003691 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003692 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003693 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003694 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003695 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003696 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003697 cmd = NULL;
3698 goto error;
3699 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003700 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003701#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02003702 lnum = qf_get_cur_idx(eap);
3703#endif
3704 break;
3705 case ADDR_QUICKFIX_VALID:
3706#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003707 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02003708#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003709 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003710 }
3711 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003712
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003713 case '$': // '$' - last line
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003714 ++cmd;
3715 switch (addr_type)
3716 {
3717 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003718 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719 lnum = curbuf->b_ml.ml_line_count;
3720 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003721 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003722 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003723 break;
3724 case ADDR_ARGUMENTS:
3725 lnum = ARGCOUNT;
3726 break;
3727 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003728 buf = lastbuf;
3729 while (buf->b_ml.ml_mfp == NULL)
3730 {
3731 if (buf->b_prev == NULL)
3732 break;
3733 buf = buf->b_prev;
3734 }
3735 lnum = buf->b_fnum;
3736 break;
3737 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003738 lnum = lastbuf->b_fnum;
3739 break;
3740 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003741 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003742 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003743 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003744 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003745 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003746 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003747 cmd = NULL;
3748 goto error;
3749 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003750 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003751#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003752 lnum = qf_get_size(eap);
3753 if (lnum == 0)
3754 lnum = 1;
Bram Moolenaare906c502015-09-09 21:10:39 +02003755#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003756 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003757 case ADDR_QUICKFIX_VALID:
3758#ifdef FEAT_QUICKFIX
3759 lnum = qf_get_valid_size(eap);
3760 if (lnum == 0)
3761 lnum = 1;
3762#endif
3763 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003764 }
3765 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003766
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003767 case '\'': // ''' - mark
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003768 if (*++cmd == NUL)
3769 {
3770 cmd = NULL;
3771 goto error;
3772 }
3773 if (addr_type != ADDR_LINES)
3774 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003775 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003776 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003777 goto error;
3778 }
3779 if (skip)
3780 ++cmd;
3781 else
3782 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003783 // Only accept a mark in another file when it is
3784 // used by itself: ":'M".
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003785 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
3786 ++cmd;
3787 if (fp == (pos_T *)-1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003788 // Jumped to another file.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003789 lnum = curwin->w_cursor.lnum;
3790 else
3791 {
3792 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003793 {
3794 cmd = NULL;
3795 goto error;
3796 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003797 lnum = fp->lnum;
3798 }
3799 }
3800 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003801
3802 case '/':
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003803 case '?': // '/' or '?' - search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003804 c = *cmd++;
3805 if (addr_type != ADDR_LINES)
3806 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003807 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003808 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003809 goto error;
3810 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003811 if (skip) // skip "/pat/"
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003812 {
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02003813 cmd = skip_regexp(cmd, c, (int)p_magic);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003814 if (*cmd == c)
3815 ++cmd;
3816 }
3817 else
3818 {
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003819 int flags;
3820
3821 pos = curwin->w_cursor; // save curwin->w_cursor
3822
3823 // When '/' or '?' follows another address, start from
3824 // there.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003825 if (lnum != MAXLNUM)
3826 curwin->w_cursor.lnum = lnum;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003827
3828 // Start a forward search at the end of the line (unless
3829 // before the first line).
3830 // Start a backward search at the start of the line.
3831 // This makes sure we never match in the current
3832 // line, and can match anywhere in the
3833 // next/previous line.
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01003834 if (c == '/' && curwin->w_cursor.lnum > 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003835 curwin->w_cursor.col = MAXCOL;
3836 else
3837 curwin->w_cursor.col = 0;
3838 searchcmdlen = 0;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003839 flags = silent ? 0 : SEARCH_HIS | SEARCH_MSG;
Bram Moolenaarc036e872020-02-21 21:30:52 +01003840 if (!do_search(NULL, c, c, cmd, 1L, flags, NULL))
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003841 {
3842 curwin->w_cursor = pos;
3843 cmd = NULL;
3844 goto error;
3845 }
3846 lnum = curwin->w_cursor.lnum;
3847 curwin->w_cursor = pos;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003848 // adjust command string pointer
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003849 cmd += searchcmdlen;
3850 }
3851 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003852
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003853 case '\\': // "\?", "\/" or "\&", repeat search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003854 ++cmd;
3855 if (addr_type != ADDR_LINES)
3856 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003857 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003858 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003859 goto error;
3860 }
3861 if (*cmd == '&')
3862 i = RE_SUBST;
3863 else if (*cmd == '?' || *cmd == '/')
3864 i = RE_SEARCH;
3865 else
3866 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003867 emsg(_(e_backslash));
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003868 cmd = NULL;
3869 goto error;
3870 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003871
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003872 if (!skip)
3873 {
3874 /*
3875 * When search follows another address, start from
3876 * there.
3877 */
3878 if (lnum != MAXLNUM)
3879 pos.lnum = lnum;
3880 else
3881 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003883 /*
3884 * Start the search just like for the above
3885 * do_search().
3886 */
3887 if (*cmd != '?')
3888 pos.col = MAXCOL;
3889 else
3890 pos.col = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02003891 pos.coladd = 0;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01003892 if (searchit(curwin, curbuf, &pos, NULL,
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003893 *cmd == '?' ? BACKWARD : FORWARD,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02003894 (char_u *)"", 1L, SEARCH_MSG, i, NULL) != FAIL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003895 lnum = pos.lnum;
3896 else
3897 {
3898 cmd = NULL;
3899 goto error;
3900 }
3901 }
3902 ++cmd;
3903 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003904
3905 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003906 if (VIM_ISDIGIT(*cmd)) // absolute line number
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003907 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003908 }
3909
3910 for (;;)
3911 {
3912 cmd = skipwhite(cmd);
3913 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
3914 break;
3915
3916 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003917 {
3918 switch (addr_type)
3919 {
3920 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003921 case ADDR_OTHER:
3922 // "+1" is same as ".+1"
Bram Moolenaarf240e182014-11-27 18:33:02 +01003923 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003924 break;
3925 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003926 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003927 break;
3928 case ADDR_ARGUMENTS:
3929 lnum = curwin->w_arg_idx + 1;
3930 break;
3931 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003932 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003933 lnum = curbuf->b_fnum;
3934 break;
3935 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003936 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003937 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003938 case ADDR_TABS_RELATIVE:
3939 lnum = 1;
3940 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003941 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003942#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02003943 lnum = qf_get_cur_idx(eap);
3944#endif
3945 break;
3946 case ADDR_QUICKFIX_VALID:
3947#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003948 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02003949#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003950 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003951 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003952 case ADDR_UNSIGNED:
3953 lnum = 0;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003954 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003955 }
3956 }
3957
Bram Moolenaar071d4272004-06-13 20:20:40 +00003958 if (VIM_ISDIGIT(*cmd))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003959 i = '+'; // "number" is same as "+number"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003960 else
3961 i = *cmd++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003962 if (!VIM_ISDIGIT(*cmd)) // '+' is '+1', but '+0' is not '+1'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003963 n = 1;
3964 else
3965 n = getdigits(&cmd);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003966
3967 if (addr_type == ADDR_TABS_RELATIVE)
3968 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003969 emsg(_(e_invrange));
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003970 cmd = NULL;
3971 goto error;
3972 }
3973 else if (addr_type == ADDR_LOADED_BUFFERS
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003974 || addr_type == ADDR_BUFFERS)
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003975 lnum = compute_buffer_local_count(
3976 addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977 else
Bram Moolenaarded27822017-01-02 14:27:34 +01003978 {
3979#ifdef FEAT_FOLDING
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003980 // Relative line addressing, need to adjust for folded lines
3981 // now, but only do it after the first address.
Bram Moolenaarded27822017-01-02 14:27:34 +01003982 if (addr_type == ADDR_LINES && (i == '-' || i == '+')
3983 && address_count >= 2)
3984 (void)hasFolding(lnum, NULL, &lnum);
3985#endif
3986 if (i == '-')
3987 lnum -= n;
3988 else
3989 lnum += n;
3990 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003991 }
3992 } while (*cmd == '/' || *cmd == '?');
3993
3994error:
3995 *ptr = cmd;
3996 return lnum;
3997}
3998
3999/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004000 * Get flags from an Ex command argument.
4001 */
4002 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004003get_flags(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004004{
4005 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4006 {
4007 if (*eap->arg == 'l')
4008 eap->flags |= EXFLAG_LIST;
4009 else if (*eap->arg == 'p')
4010 eap->flags |= EXFLAG_PRINT;
4011 else
4012 eap->flags |= EXFLAG_NR;
4013 eap->arg = skipwhite(eap->arg + 1);
4014 }
4015}
4016
4017/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004018 * Function called for command which is Not Implemented. NI!
4019 */
4020 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004021ex_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004022{
4023 if (!eap->skip)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02004024 eap->errmsg =
4025 _("E319: Sorry, the command is not available in this version");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004026}
4027
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004028#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004029/*
4030 * Function called for script command which is Not Implemented. NI!
4031 * Skips over ":perl <<EOF" constructs.
4032 */
4033 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004034ex_script_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004035{
4036 if (!eap->skip)
4037 ex_ni(eap);
4038 else
4039 vim_free(script_get(eap, eap->arg));
4040}
4041#endif
4042
4043/*
4044 * Check range in Ex command for validity.
4045 * Return NULL when valid, error message when invalid.
4046 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004047 static char *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004048invalid_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049{
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004050 buf_T *buf;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004051
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052 if ( eap->line1 < 0
4053 || eap->line2 < 0
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004054 || eap->line1 > eap->line2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004055 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004056
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004057 if (eap->argt & EX_RANGE)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004058 {
Bram Moolenaarb7316892019-05-01 18:08:42 +02004059 switch (eap->addr_type)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004060 {
4061 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004062 if (eap->line2 > curbuf->b_ml.ml_line_count
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004063#ifdef FEAT_DIFF
4064 + (eap->cmdidx == CMD_diffget)
4065#endif
4066 )
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004067 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004068 break;
4069 case ADDR_ARGUMENTS:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004070 // add 1 if ARGCOUNT is 0
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004071 if (eap->line2 > ARGCOUNT + (!ARGCOUNT))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004072 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004073 break;
4074 case ADDR_BUFFERS:
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02004075 // Only a boundary check, not whether the buffers actually
4076 // exist.
4077 if (eap->line1 < 1 || eap->line2 > get_highest_fnum())
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004078 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004079 break;
4080 case ADDR_LOADED_BUFFERS:
4081 buf = firstbuf;
4082 while (buf->b_ml.ml_mfp == NULL)
4083 {
4084 if (buf->b_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004085 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004086 buf = buf->b_next;
4087 }
4088 if (eap->line1 < buf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004089 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004090 buf = lastbuf;
4091 while (buf->b_ml.ml_mfp == NULL)
4092 {
4093 if (buf->b_prev == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004094 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004095 buf = buf->b_prev;
4096 }
4097 if (eap->line2 > buf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004098 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004099 break;
4100 case ADDR_WINDOWS:
Bram Moolenaar8be63882015-01-14 11:25:05 +01004101 if (eap->line2 > LAST_WIN_NR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004102 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004103 break;
4104 case ADDR_TABS:
4105 if (eap->line2 > LAST_TAB_NR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004106 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004107 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004108 case ADDR_TABS_RELATIVE:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004109 case ADDR_OTHER:
4110 // Any range is OK.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004111 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004112 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004113#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004114 // No error for value that is too big, will use the last entry.
4115 if (eap->line2 <= 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004116 return _(e_invrange);
Bram Moolenaare906c502015-09-09 21:10:39 +02004117#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004118 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004119 case ADDR_QUICKFIX_VALID:
4120#ifdef FEAT_QUICKFIX
4121 if ((eap->line2 != 1 && eap->line2 > qf_get_valid_size(eap))
4122 || eap->line2 < 0)
4123 return _(e_invrange);
4124#endif
4125 break;
4126 case ADDR_UNSIGNED:
4127 if (eap->line2 < 0)
4128 return _(e_invrange);
4129 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004130 case ADDR_NONE:
4131 // Will give an error elsewhere.
4132 break;
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004133 }
4134 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004135 return NULL;
4136}
4137
4138/*
4139 * Correct the range for zero line number, if required.
4140 */
4141 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004142correct_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143{
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004144 if (!(eap->argt & EX_ZEROR)) // zero in range not allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145 {
4146 if (eap->line1 == 0)
4147 eap->line1 = 1;
4148 if (eap->line2 == 0)
4149 eap->line2 = 1;
4150 }
4151}
4152
Bram Moolenaar748bf032005-02-02 23:04:36 +00004153#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004154/*
4155 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4156 * pattern. Otherwise return eap->arg.
4157 */
4158 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004159skip_grep_pat(exarg_T *eap)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004160{
4161 char_u *p = eap->arg;
4162
Bram Moolenaara37420f2006-02-04 22:37:47 +00004163 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4164 || eap->cmdidx == CMD_vimgrepadd
4165 || eap->cmdidx == CMD_lvimgrepadd
4166 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004167 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004168 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004169 if (p == NULL)
4170 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004171 }
4172 return p;
4173}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004174
4175/*
4176 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4177 * in the command line, so that things like % get expanded.
4178 */
4179 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004180replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004181{
4182 char_u *new_cmdline;
4183 char_u *program;
4184 char_u *pos;
4185 char_u *ptr;
4186 int len;
4187 int i;
4188
4189 /*
4190 * Don't do it when ":vimgrep" is used for ":grep".
4191 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004192 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4193 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4194 || eap->cmdidx == CMD_grepadd
4195 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004196 && !grep_internal(eap->cmdidx))
4197 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004198 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4199 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004200 {
4201 if (*curbuf->b_p_gp == NUL)
4202 program = p_gp;
4203 else
4204 program = curbuf->b_p_gp;
4205 }
4206 else
4207 {
4208 if (*curbuf->b_p_mp == NUL)
4209 program = p_mp;
4210 else
4211 program = curbuf->b_p_mp;
4212 }
4213
4214 p = skipwhite(p);
4215
4216 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4217 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004218 // replace $* by given arguments
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004219 i = 1;
4220 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4221 ++i;
4222 len = (int)STRLEN(p);
Bram Moolenaar51e14382019-05-25 20:21:28 +02004223 new_cmdline = alloc(STRLEN(program) + i * (len - 2) + 1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004224 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004225 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004226 ptr = new_cmdline;
4227 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4228 {
4229 i = (int)(pos - program);
4230 STRNCPY(ptr, program, i);
4231 STRCPY(ptr += i, p);
4232 ptr += len;
4233 program = pos + 2;
4234 }
4235 STRCPY(ptr, program);
4236 }
4237 else
4238 {
Bram Moolenaar51e14382019-05-25 20:21:28 +02004239 new_cmdline = alloc(STRLEN(program) + STRLEN(p) + 2);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004240 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004241 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004242 STRCPY(new_cmdline, program);
4243 STRCAT(new_cmdline, " ");
4244 STRCAT(new_cmdline, p);
4245 }
4246 msg_make(p);
4247
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004248 // 'eap->cmd' is not set here, because it is not used at CMD_make
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004249 vim_free(*cmdlinep);
4250 *cmdlinep = new_cmdline;
4251 p = new_cmdline;
4252 }
4253 return p;
4254}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004255#endif
4256
Bram Moolenaar071d4272004-06-13 20:20:40 +00004257/*
4258 * Expand file name in Ex command argument.
Bram Moolenaar0abb4272019-06-15 16:06:00 +02004259 * When an error is detected, "errormsgp" is set to a non-NULL pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004260 * Return FAIL for failure, OK otherwise.
4261 */
4262 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004263expand_filename(
4264 exarg_T *eap,
4265 char_u **cmdlinep,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004266 char **errormsgp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004267{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004268 int has_wildcards; // need to expand wildcards
Bram Moolenaar071d4272004-06-13 20:20:40 +00004269 char_u *repl;
4270 int srclen;
4271 char_u *p;
4272 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004273 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274
Bram Moolenaar748bf032005-02-02 23:04:36 +00004275#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004276 // Skip a regexp pattern for ":vimgrep[add] pat file..."
Bram Moolenaar748bf032005-02-02 23:04:36 +00004277 p = skip_grep_pat(eap);
4278#else
4279 p = eap->arg;
4280#endif
4281
Bram Moolenaar071d4272004-06-13 20:20:40 +00004282 /*
4283 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4284 * the file name contains a wildcard it should not cause expanding.
4285 * (it will be expanded anyway if there is a wildcard before replacing).
4286 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004287 has_wildcards = mch_has_wildcard(p);
4288 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004289 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004290#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004291 // Skip over `=expr`, wildcards in it are not expanded.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004292 if (p[0] == '`' && p[1] == '=')
4293 {
4294 p += 2;
4295 (void)skip_expr(&p);
4296 if (*p == '`')
4297 ++p;
4298 continue;
4299 }
4300#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004301 /*
4302 * Quick check if this cannot be the start of a special string.
4303 * Also removes backslash before '%', '#' and '<'.
4304 */
4305 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4306 {
4307 ++p;
4308 continue;
4309 }
4310
4311 /*
4312 * Try to find a match at this position.
4313 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00004314 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
4315 errormsgp, &escaped);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004316 if (*errormsgp != NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00004317 return FAIL;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004318 if (repl == NULL) // no match found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319 {
4320 p += srclen;
4321 continue;
4322 }
4323
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004324 // Wildcards won't be expanded below, the replacement is taken
4325 // literally. But do expand "~/file", "~user/file" and "$HOME/file".
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004326 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4327 {
4328 char_u *l = repl;
4329
4330 repl = expand_env_save(repl);
4331 vim_free(l);
4332 }
4333
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004334 // Need to escape white space et al. with a backslash.
4335 // Don't do this for:
4336 // - replacement that already has been escaped: "##"
4337 // - shell commands (may have to use quotes instead).
4338 // - non-unix systems when there is a single argument (spaces don't
4339 // separate arguments then).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004340 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00004341 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00004342 && eap->cmdidx != CMD_bang
Bram Moolenaar071d4272004-06-13 20:20:40 +00004343 && eap->cmdidx != CMD_grep
4344 && eap->cmdidx != CMD_grepadd
Bram Moolenaarbf15b8d2017-06-04 20:43:48 +02004345 && eap->cmdidx != CMD_hardcopy
Bram Moolenaar67883b42017-07-27 22:57:00 +02004346 && eap->cmdidx != CMD_lgrep
4347 && eap->cmdidx != CMD_lgrepadd
4348 && eap->cmdidx != CMD_lmake
4349 && eap->cmdidx != CMD_make
4350 && eap->cmdidx != CMD_terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00004351#ifndef UNIX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004352 && !(eap->argt & EX_NOSPC)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004353#endif
4354 )
4355 {
4356 char_u *l;
4357#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004358 // Don't escape a backslash here, because rem_backslash() doesn't
4359 // remove it later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004360 static char_u *nobslash = (char_u *)" \t\"|";
4361# define ESCAPE_CHARS nobslash
4362#else
4363# define ESCAPE_CHARS escape_chars
4364#endif
4365
4366 for (l = repl; *l; ++l)
4367 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
4368 {
4369 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
4370 if (l != NULL)
4371 {
4372 vim_free(repl);
4373 repl = l;
4374 }
4375 break;
4376 }
4377 }
4378
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004379 // For a shell command a '!' must be escaped.
Bram Moolenaar67883b42017-07-27 22:57:00 +02004380 if ((eap->usefilter || eap->cmdidx == CMD_bang
4381 || eap->cmdidx == CMD_terminal)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004382 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383 {
4384 char_u *l;
4385
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004386 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004387 if (l != NULL)
4388 {
4389 vim_free(repl);
4390 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004391 }
4392 }
4393
4394 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
4395 vim_free(repl);
4396 if (p == NULL)
4397 return FAIL;
4398 }
4399
4400 /*
4401 * One file argument: Expand wildcards.
4402 * Don't do this with ":r !command" or ":w !command".
4403 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004404 if ((eap->argt & EX_NOSPC) && !eap->usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405 {
4406 /*
4407 * May do this twice:
4408 * 1. Replace environment variables.
4409 * 2. Replace any other wildcards, remove backslashes.
4410 */
4411 for (n = 1; n <= 2; ++n)
4412 {
4413 if (n == 2)
4414 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004415 /*
4416 * Halve the number of backslashes (this is Vi compatible).
4417 * For Unix and OS/2, when wildcards are expanded, this is
4418 * done by ExpandOne() below.
4419 */
Bram Moolenaare7fedb62015-12-31 19:07:19 +01004420#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004421 if (!has_wildcards)
4422#endif
4423 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004424 }
4425
4426 if (has_wildcards)
4427 {
4428 if (n == 1)
4429 {
4430 /*
4431 * First loop: May expand environment variables. This
4432 * can be done much faster with expand_env() than with
4433 * something else (e.g., calling a shell).
4434 * After expanding environment variables, check again
4435 * if there are still wildcards present.
4436 */
4437 if (vim_strchr(eap->arg, '$') != NULL
4438 || vim_strchr(eap->arg, '~') != NULL)
4439 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00004440 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004441 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004442 has_wildcards = mch_has_wildcard(NameBuff);
4443 p = NameBuff;
4444 }
4445 else
4446 p = NULL;
4447 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004448 else // n == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004449 {
4450 expand_T xpc;
Bram Moolenaarb40c2572019-10-19 21:01:05 +02004451 int options = WILD_LIST_NOTFOUND
4452 | WILD_NOERROR | WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004453
4454 ExpandInit(&xpc);
4455 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004456 if (p_wic)
4457 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004458 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01004459 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004460 if (p == NULL)
4461 return FAIL;
4462 }
4463 if (p != NULL)
4464 {
4465 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
4466 p, cmdlinep);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004467 if (n == 2) // p came from ExpandOne()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004468 vim_free(p);
4469 }
4470 }
4471 }
4472 }
4473 return OK;
4474}
4475
4476/*
4477 * Replace part of the command line, keeping eap->cmd, eap->arg and
4478 * eap->nextcmd correct.
4479 * "src" points to the part that is to be replaced, of length "srclen".
4480 * "repl" is the replacement string.
4481 * Returns a pointer to the character after the replaced string.
4482 * Returns NULL for failure.
4483 */
4484 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004485repl_cmdline(
4486 exarg_T *eap,
4487 char_u *src,
4488 int srclen,
4489 char_u *repl,
4490 char_u **cmdlinep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004491{
4492 int len;
4493 int i;
4494 char_u *new_cmdline;
4495
4496 /*
4497 * The new command line is build in new_cmdline[].
4498 * First allocate it.
4499 * Careful: a "+cmd" argument may have been NUL terminated.
4500 */
4501 len = (int)STRLEN(repl);
4502 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004503 if (eap->nextcmd != NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004504 i += (int)STRLEN(eap->nextcmd);// add space for next command
Bram Moolenaar964b3742019-05-24 18:54:09 +02004505 if ((new_cmdline = alloc(i)) == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004506 return NULL; // out of memory!
Bram Moolenaar071d4272004-06-13 20:20:40 +00004507
4508 /*
4509 * Copy the stuff before the expanded part.
4510 * Copy the expanded stuff.
4511 * Copy what came after the expanded part.
4512 * Copy the next commands, if there are any.
4513 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004514 i = (int)(src - *cmdlinep); // length of part before match
Bram Moolenaar071d4272004-06-13 20:20:40 +00004515 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00004516
Bram Moolenaar071d4272004-06-13 20:20:40 +00004517 mch_memmove(new_cmdline + i, repl, (size_t)len);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004518 i += len; // remember the end of the string
Bram Moolenaar071d4272004-06-13 20:20:40 +00004519 STRCPY(new_cmdline + i, src + srclen);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004520 src = new_cmdline + i; // remember where to continue
Bram Moolenaar071d4272004-06-13 20:20:40 +00004521
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004522 if (eap->nextcmd != NULL) // append next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00004523 {
4524 i = (int)STRLEN(new_cmdline) + 1;
4525 STRCPY(new_cmdline + i, eap->nextcmd);
4526 eap->nextcmd = new_cmdline + i;
4527 }
4528 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
4529 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
4530 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
4531 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
4532 vim_free(*cmdlinep);
4533 *cmdlinep = new_cmdline;
4534
4535 return src;
4536}
4537
4538/*
4539 * Check for '|' to separate commands and '"' to start comments.
4540 */
4541 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004542separate_nextcmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004543{
4544 char_u *p;
4545
Bram Moolenaar86b68352004-12-27 21:59:20 +00004546#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004547 p = skip_grep_pat(eap);
4548#else
4549 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004550#endif
4551
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004552 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004553 {
4554 if (*p == Ctrl_V)
4555 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004556 if (eap->argt & (EX_CTRLV | EX_XFILE))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004557 ++p; // skip CTRL-V and next char
Bram Moolenaar071d4272004-06-13 20:20:40 +00004558 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004559 // remove CTRL-V and skip next char
Bram Moolenaara7241f52008-06-24 20:39:31 +00004560 STRMOVE(p, p + 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004561 if (*p == NUL) // stop at NUL after CTRL-V
Bram Moolenaar071d4272004-06-13 20:20:40 +00004562 break;
4563 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004564
4565#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004566 // Skip over `=expr` when wildcards are expanded.
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004567 else if (p[0] == '`' && p[1] == '=' && (eap->argt & EX_XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004568 {
4569 p += 2;
4570 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004571 }
4572#endif
4573
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004574 // Check for '"': start of comment or '|': next command
4575 // :@" and :*" do not start a comment!
4576 // :redir @" doesn't either.
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004577 else if ((*p == '"'
Bram Moolenaara26b9702020-04-18 19:53:28 +02004578#ifdef FEAT_EVAL
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004579 && !in_vim9script()
4580#endif
4581 && !(eap->argt & EX_NOTRLCOM)
4582 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
4583 || p != eap->arg)
4584 && (eap->cmdidx != CMD_redir
4585 || p != eap->arg + 1 || p[-1] != '@'))
4586#ifdef FEAT_EVAL
4587 || (*p == '#'
4588 && in_vim9script()
4589 && p[1] != '{'
4590 && p > eap->cmd && VIM_ISWHITE(p[-1]))
Bram Moolenaara26b9702020-04-18 19:53:28 +02004591#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004592 || *p == '|' || *p == '\n')
4593 {
4594 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004595 * We remove the '\' before the '|', unless EX_CTRLV is used
Bram Moolenaar071d4272004-06-13 20:20:40 +00004596 * AND 'b' is present in 'cpoptions'.
4597 */
4598 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004599 || !(eap->argt & EX_CTRLV)) && *(p - 1) == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004600 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004601 STRMOVE(p - 1, p); // remove the '\'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004602 --p;
4603 }
4604 else
4605 {
4606 eap->nextcmd = check_nextcmd(p);
4607 *p = NUL;
4608 break;
4609 }
4610 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004611 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004612
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004613 if (!(eap->argt & EX_NOTRLCOM)) // remove trailing spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614 del_trailing_spaces(eap->arg);
4615}
4616
4617/*
4618 * get + command from ex argument
4619 */
4620 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004621getargcmd(char_u **argp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004622{
4623 char_u *arg = *argp;
4624 char_u *command = NULL;
4625
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004626 if (*arg == '+') // +[command]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004627 {
4628 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02004629 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004630 command = dollar_command;
4631 else
4632 {
4633 command = arg;
4634 arg = skip_cmd_arg(command, TRUE);
4635 if (*arg != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004636 *arg++ = NUL; // terminate command with NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004637 }
4638
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004639 arg = skipwhite(arg); // skip over spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00004640 *argp = arg;
4641 }
4642 return command;
4643}
4644
4645/*
4646 * Find end of "+command" argument. Skip over "\ " and "\\".
4647 */
Bram Moolenaard0190392019-08-23 21:17:35 +02004648 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004649skip_cmd_arg(
4650 char_u *p,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004651 int rembs) // TRUE to halve the number of backslashes
Bram Moolenaar071d4272004-06-13 20:20:40 +00004652{
4653 while (*p && !vim_isspace(*p))
4654 {
4655 if (*p == '\\' && p[1] != NUL)
4656 {
4657 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00004658 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004659 else
4660 ++p;
4661 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004662 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004663 }
4664 return p;
4665}
4666
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004667 int
4668get_bad_opt(char_u *p, exarg_T *eap)
4669{
4670 if (STRICMP(p, "keep") == 0)
4671 eap->bad_char = BAD_KEEP;
4672 else if (STRICMP(p, "drop") == 0)
4673 eap->bad_char = BAD_DROP;
4674 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
4675 eap->bad_char = *p;
Bram Moolenaar75808492018-06-12 12:39:41 +02004676 else
4677 return FAIL;
4678 return OK;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004679}
4680
Bram Moolenaar071d4272004-06-13 20:20:40 +00004681/*
4682 * Get "++opt=arg" argument.
4683 * Return FAIL or OK.
4684 */
4685 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004686getargopt(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004687{
4688 char_u *arg = eap->arg + 2;
4689 int *pp = NULL;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004690 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004691 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004692
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004693 // ":edit ++[no]bin[ary] file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
4695 {
4696 if (*arg == 'n')
4697 {
4698 arg += 2;
4699 eap->force_bin = FORCE_NOBIN;
4700 }
4701 else
4702 eap->force_bin = FORCE_BIN;
4703 if (!checkforcmd(&arg, "binary", 3))
4704 return FAIL;
4705 eap->arg = skipwhite(arg);
4706 return OK;
4707 }
4708
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004709 // ":read ++edit file"
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004710 if (STRNCMP(arg, "edit", 4) == 0)
4711 {
4712 eap->read_edit = TRUE;
4713 eap->arg = skipwhite(arg + 4);
4714 return OK;
4715 }
4716
Bram Moolenaar071d4272004-06-13 20:20:40 +00004717 if (STRNCMP(arg, "ff", 2) == 0)
4718 {
4719 arg += 2;
4720 pp = &eap->force_ff;
4721 }
4722 else if (STRNCMP(arg, "fileformat", 10) == 0)
4723 {
4724 arg += 10;
4725 pp = &eap->force_ff;
4726 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004727 else if (STRNCMP(arg, "enc", 3) == 0)
4728 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01004729 if (STRNCMP(arg, "encoding", 8) == 0)
4730 arg += 8;
4731 else
4732 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004733 pp = &eap->force_enc;
4734 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004735 else if (STRNCMP(arg, "bad", 3) == 0)
4736 {
4737 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004738 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004739 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004740
4741 if (pp == NULL || *arg != '=')
4742 return FAIL;
4743
4744 ++arg;
4745 *pp = (int)(arg - eap->cmd);
4746 arg = skip_cmd_arg(arg, FALSE);
4747 eap->arg = skipwhite(arg);
4748 *arg = NUL;
4749
Bram Moolenaar071d4272004-06-13 20:20:40 +00004750 if (pp == &eap->force_ff)
4751 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004752 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
4753 return FAIL;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004754 eap->force_ff = eap->cmd[eap->force_ff];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004756 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004757 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004758 // Make 'fileencoding' lower case.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004759 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
4760 *p = TOLOWER_ASC(*p);
4761 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004762 else
4763 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004764 // Check ++bad= argument. Must be a single-byte character, "keep" or
4765 // "drop".
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004766 if (get_bad_opt(eap->cmd + bad_char_idx, eap) == FAIL)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004767 return FAIL;
4768 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004769
4770 return OK;
4771}
4772
Bram Moolenaar071d4272004-06-13 20:20:40 +00004773 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004774ex_autocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004775{
4776 /*
Bram Moolenaar26d4b892018-07-04 22:26:28 +02004777 * Disallow autocommands from .exrc and .vimrc in current
Bram Moolenaar071d4272004-06-13 20:20:40 +00004778 * directory for security reasons.
4779 */
4780 if (secure)
4781 {
4782 secure = 2;
Bram Moolenaar8930caa2020-07-23 16:37:03 +02004783 eap->errmsg = _(e_curdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004784 }
4785 else if (eap->cmdidx == CMD_autocmd)
4786 do_autocmd(eap->arg, eap->forceit);
4787 else
4788 do_augroup(eap->arg, eap->forceit);
4789}
4790
4791/*
4792 * ":doautocmd": Apply the automatic commands to the current buffer.
4793 */
4794 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004795ex_doautocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004796{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01004797 char_u *arg = eap->arg;
4798 int call_do_modelines = check_nomodeline(&arg);
Bram Moolenaar1610d052016-06-09 22:53:01 +02004799 int did_aucmd;
Bram Moolenaar60542ac2012-02-12 20:14:01 +01004800
Bram Moolenaar1610d052016-06-09 22:53:01 +02004801 (void)do_doautocmd(arg, TRUE, &did_aucmd);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004802 // Only when there is no <nomodeline>.
Bram Moolenaar1610d052016-06-09 22:53:01 +02004803 if (call_do_modelines && did_aucmd)
Bram Moolenaar60542ac2012-02-12 20:14:01 +01004804 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004805}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004806
Bram Moolenaar071d4272004-06-13 20:20:40 +00004807/*
4808 * :[N]bunload[!] [N] [bufname] unload buffer
4809 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
4810 * :[N]bwipeout[!] [N] [bufname] delete buffer really
4811 */
4812 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004813ex_bunload(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004814{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004815 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02004816 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004817 eap->errmsg = do_bufdel(
4818 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
4819 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
4820 : DOBUF_UNLOAD, eap->arg,
4821 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
4822}
4823
4824/*
4825 * :[N]buffer [N] to buffer N
4826 * :[N]sbuffer [N] to buffer N
4827 */
4828 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004829ex_buffer(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 Moolenaar815b76b2019-06-01 14:15:52 +02004832 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004833 if (*eap->arg)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02004834 eap->errmsg = ex_errmsg(e_trailing_arg, eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004835 else
4836 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004837 if (eap->addr_count == 0) // default is current buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00004838 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
4839 else
4840 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004841 if (eap->do_ecmd_cmd != NULL)
4842 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004843 }
4844}
4845
4846/*
4847 * :[N]bmodified [N] to next mod. buffer
4848 * :[N]sbmodified [N] to next mod. buffer
4849 */
4850 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004851ex_bmodified(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004852{
4853 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004854 if (eap->do_ecmd_cmd != NULL)
4855 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004856}
4857
4858/*
4859 * :[N]bnext [N] to next buffer
4860 * :[N]sbnext [N] split and to next buffer
4861 */
4862 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004863ex_bnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004864{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004865 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01004866 return;
4867
Bram Moolenaar071d4272004-06-13 20:20:40 +00004868 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004869 if (eap->do_ecmd_cmd != NULL)
4870 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004871}
4872
4873/*
4874 * :[N]bNext [N] to previous buffer
4875 * :[N]bprevious [N] to previous buffer
4876 * :[N]sbNext [N] split and to previous buffer
4877 * :[N]sbprevious [N] split and to previous buffer
4878 */
4879 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004880ex_bprevious(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004881{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004882 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01004883 return;
4884
Bram Moolenaar071d4272004-06-13 20:20:40 +00004885 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004886 if (eap->do_ecmd_cmd != NULL)
4887 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004888}
4889
4890/*
4891 * :brewind to first buffer
4892 * :bfirst to first buffer
4893 * :sbrewind split and to first buffer
4894 * :sbfirst split and to first buffer
4895 */
4896 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004897ex_brewind(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004898{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004899 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01004900 return;
4901
Bram Moolenaar071d4272004-06-13 20:20:40 +00004902 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004903 if (eap->do_ecmd_cmd != NULL)
4904 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004905}
4906
4907/*
4908 * :blast to last buffer
4909 * :sblast split and to last buffer
4910 */
4911 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004912ex_blast(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004913{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004914 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01004915 return;
4916
Bram Moolenaar071d4272004-06-13 20:20:40 +00004917 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004918 if (eap->do_ecmd_cmd != NULL)
4919 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004920}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004921
Bram Moolenaar7a092242020-04-16 22:10:49 +02004922/*
4923 * Check if "c" ends an Ex command.
Bram Moolenaara494f562020-04-18 17:45:38 +02004924 * In Vim9 script does not check for white space before # or #{.
Bram Moolenaar7a092242020-04-16 22:10:49 +02004925 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004926 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004927ends_excmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004928{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004929 int comment_char = '"';
4930
Bram Moolenaarfbda69b2020-04-13 15:06:53 +02004931#ifdef FEAT_EVAL
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004932 if (in_vim9script())
4933 comment_char = '#';
Bram Moolenaar7a092242020-04-16 22:10:49 +02004934#endif
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004935 return (c == NUL || c == '|' || c == comment_char || c == '\n');
Bram Moolenaar7a092242020-04-16 22:10:49 +02004936}
4937
4938/*
4939 * Like ends_excmd() but checks that a # in Vim9 script either has "cmd" equal
4940 * to "cmd_start" or has a white space character before it.
4941 */
4942 int
Bram Moolenaar21cfe502020-04-16 23:01:50 +02004943ends_excmd2(char_u *cmd_start UNUSED, char_u *cmd)
Bram Moolenaar7a092242020-04-16 22:10:49 +02004944{
4945 int c = *cmd;
4946
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004947 if (c == NUL || c == '|' || c == '\n')
4948 return TRUE;
Bram Moolenaar7a092242020-04-16 22:10:49 +02004949#ifdef FEAT_EVAL
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004950 if (in_vim9script())
4951 return c == '#' && cmd[1] != '{'
4952 && (cmd == cmd_start || VIM_ISWHITE(cmd[-1]));
Bram Moolenaarfbda69b2020-04-13 15:06:53 +02004953#endif
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004954 return c == '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004955}
4956
4957#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
4958 || defined(PROTO)
4959/*
4960 * Return the next command, after the first '|' or '\n'.
4961 * Return NULL if not found.
4962 */
4963 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004964find_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004965{
4966 while (*p != '|' && *p != '\n')
4967 {
4968 if (*p == NUL)
4969 return NULL;
4970 ++p;
4971 }
4972 return (p + 1);
4973}
4974#endif
4975
4976/*
Bram Moolenaar2256c992016-11-15 21:17:07 +01004977 * Check if *p is a separator between Ex commands, skipping over white space.
4978 * Return NULL if it isn't, the following character if it is.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004979 */
4980 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004981check_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004982{
Bram Moolenaar2256c992016-11-15 21:17:07 +01004983 char_u *s = skipwhite(p);
4984
4985 if (*s == '|' || *s == '\n')
4986 return (s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004987 else
4988 return NULL;
4989}
4990
4991/*
4992 * - if there are more files to edit
4993 * - and this is the last window
4994 * - and forceit not used
4995 * - and not repeated twice on a row
4996 * return FAIL and give error message if 'message' TRUE
4997 * return OK otherwise
4998 */
4999 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005000check_more(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005001 int message, // when FALSE check only, no messages
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005002 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005003{
5004 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5005
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005006 if (!forceit && only_one_window()
5007 && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005008 {
5009 if (message)
5010 {
5011#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
5012 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
5013 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005014 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005015
Bram Moolenaarda6e8912018-08-21 15:12:14 +02005016 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
5017 NGETTEXT("%d more file to edit. Quit anyway?",
5018 "%d more files to edit. Quit anyway?", n), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5020 return OK;
5021 return FAIL;
5022 }
5023#endif
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01005024 semsg(NGETTEXT("E173: %d more file to edit",
5025 "E173: %d more files to edit", n), n);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005026 quitmore = 2; // next try to quit is allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00005027 }
5028 return FAIL;
5029 }
5030 return OK;
5031}
5032
Bram Moolenaar071d4272004-06-13 20:20:40 +00005033/*
5034 * Function given to ExpandGeneric() to obtain the list of command names.
5035 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005036 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005037get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005038{
5039 if (idx >= (int)CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005040 return get_user_command_name(idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005041 return cmdnames[idx].cmd_name;
5042}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005043
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005045ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005046{
Bram Moolenaare6850792010-05-14 15:28:44 +02005047 if (*eap->arg == NUL)
5048 {
5049#ifdef FEAT_EVAL
5050 char_u *expr = vim_strsave((char_u *)"g:colors_name");
5051 char_u *p = NULL;
5052
5053 if (expr != NULL)
5054 {
5055 ++emsg_off;
Bram Moolenaarb171fb12020-06-24 20:34:03 +02005056 p = eval_to_string(expr, FALSE);
Bram Moolenaare6850792010-05-14 15:28:44 +02005057 --emsg_off;
5058 vim_free(expr);
5059 }
5060 if (p != NULL)
5061 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005062 msg((char *)p);
Bram Moolenaare6850792010-05-14 15:28:44 +02005063 vim_free(p);
5064 }
5065 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005066 msg("default");
Bram Moolenaare6850792010-05-14 15:28:44 +02005067#else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005068 msg(_("unknown"));
Bram Moolenaare6850792010-05-14 15:28:44 +02005069#endif
5070 }
5071 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005072 semsg(_("E185: Cannot find color scheme '%s'"), eap->arg);
Bram Moolenaarf58d81a2019-01-28 20:19:05 +01005073
5074#ifdef FEAT_VTP
5075 else if (has_vtp_working())
5076 {
5077 // background color change requires clear + redraw
5078 update_screen(CLEAR);
5079 redrawcmd();
5080 }
5081#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005082}
5083
5084 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005085ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086{
5087 if (*eap->arg == NUL && eap->cmd[2] == '!')
Bram Moolenaar32526b32019-01-19 17:43:09 +01005088 msg(_("Greetings, Vim user!"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005089 do_highlight(eap->arg, eap->forceit, FALSE);
5090}
5091
5092
5093/*
5094 * Call this function if we thought we were going to exit, but we won't
5095 * (because of an error). May need to restore the terminal mode.
5096 */
5097 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005098not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005099{
5100 exiting = FALSE;
5101 settmode(TMODE_RAW);
5102}
5103
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005104 static int
5105before_quit_autocmds(win_T *wp, int quit_all, int forceit)
5106{
5107 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, wp->w_buffer);
5108
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02005109 // Bail out when autocommands closed the window.
5110 // Refuse to quit when the buffer in the last window is being closed (can
5111 // only happen in autocommands).
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005112 if (!win_valid(wp)
5113 || curbuf_locked()
5114 || (wp->w_buffer->b_nwindows == 1 && wp->w_buffer->b_locked > 0))
5115 return TRUE;
5116
5117 if (quit_all || (check_more(FALSE, forceit) == OK && only_one_window()))
5118 {
5119 apply_autocmds(EVENT_EXITPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02005120 // Refuse to quit when locked or when the window was closed or the
5121 // buffer in the last window is being closed (can only happen in
5122 // autocommands).
5123 if (!win_valid(wp) || curbuf_locked()
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005124 || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
5125 return TRUE;
5126 }
5127
5128 return FALSE;
5129}
5130
Bram Moolenaar071d4272004-06-13 20:20:40 +00005131/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01005132 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005133 * ":{nr}quit": quit window {nr}
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02005134 * Also used when closing a terminal window that's the last one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005135 */
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02005136 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005137ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005138{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005139 win_T *wp;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005140
Bram Moolenaar071d4272004-06-13 20:20:40 +00005141#ifdef FEAT_CMDWIN
5142 if (cmdwin_type != 0)
5143 {
5144 cmdwin_result = Ctrl_C;
5145 return;
5146 }
5147#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005148 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005149 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005150 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005151 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005152 return;
5153 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005154 if (eap->addr_count > 0)
5155 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01005156 int wnr = eap->line2;
5157
5158 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
5159 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005160 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005161 }
5162 else
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005163 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01005164
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005165 // Refuse to quit when locked.
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02005166 if (curbuf_locked())
5167 return;
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005168
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005169 // Trigger QuitPre and maybe ExitPre
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005170 if (before_quit_autocmds(wp, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005171 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005172
5173#ifdef FEAT_NETBEANS_INTG
5174 netbeansForcedQuit = eap->forceit;
5175#endif
5176
5177 /*
5178 * If there are more files or windows we won't exit.
5179 */
5180 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
5181 exiting = TRUE;
Bram Moolenaarff930ca2017-10-19 17:12:10 +02005182 if ((!buf_hide(wp->w_buffer)
5183 && check_changed(wp->w_buffer, (p_awa ? CCGD_AW : 0)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01005184 | (eap->forceit ? CCGD_FORCEIT : 0)
5185 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005186 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01005187 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005188 {
5189 not_exiting();
5190 }
5191 else
5192 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005193 // quit last window
5194 // Note: only_one_window() returns true, even so a help window is
5195 // still open. In that case only quit, if no address has been
5196 // specified. Example:
5197 // :h|wincmd w|1q - don't quit
5198 // :h|wincmd w|q - quit
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01005199 if (only_one_window() && (ONE_WINDOW || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005200 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02005201 not_exiting();
Bram Moolenaar4033c552017-09-16 20:54:51 +02005202#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005204#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005205 // close window; may free buffer
Bram Moolenaareb44a682017-08-03 22:44:55 +02005206 win_close(wp, !buf_hide(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207 }
5208}
5209
5210/*
5211 * ":cquit".
5212 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005213 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005214ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005215{
Bram Moolenaar1860bde2020-01-06 21:47:21 +01005216 // this does not always pass on the exit code to the Manx compiler. why?
5217 getout(eap->addr_count > 0 ? (int)eap->line2 : EXIT_FAILURE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005218}
5219
5220/*
5221 * ":qall": try to quit all windows
5222 */
5223 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005224ex_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005225{
5226# ifdef FEAT_CMDWIN
5227 if (cmdwin_type != 0)
5228 {
5229 if (eap->forceit)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005230 cmdwin_result = K_XF1; // ex_window() takes care of this
Bram Moolenaar071d4272004-06-13 20:20:40 +00005231 else
5232 cmdwin_result = K_XF2;
5233 return;
5234 }
5235# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005236
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005237 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005238 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005239 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005240 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005241 return;
5242 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005243
5244 if (before_quit_autocmds(curwin, TRUE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005245 return;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005246
Bram Moolenaar071d4272004-06-13 20:20:40 +00005247 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01005248 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005249 getout(0);
5250 not_exiting();
5251}
5252
Bram Moolenaar071d4272004-06-13 20:20:40 +00005253/*
5254 * ":close": close current window, unless it is the last one
5255 */
5256 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005257ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005258{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005259 win_T *win;
5260 int winnr = 0;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005261#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00005262 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02005263 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005264 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005265#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005266 if (!text_locked() && !curbuf_locked())
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005267 {
5268 if (eap->addr_count == 0)
5269 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar6d41c782018-06-06 09:11:12 +02005270 else
5271 {
Bram Moolenaar29323592016-07-24 22:04:11 +02005272 FOR_ALL_WINDOWS(win)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005273 {
5274 winnr++;
5275 if (winnr == eap->line2)
5276 break;
5277 }
5278 if (win == NULL)
5279 win = lastwin;
5280 ex_win_close(eap->forceit, win, NULL);
5281 }
5282 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005283}
5284
Bram Moolenaar4033c552017-09-16 20:54:51 +02005285#ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005286/*
5287 * ":pclose": Close any preview window.
5288 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005289 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005290ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005291{
5292 win_T *win;
5293
Bram Moolenaar79648732019-07-18 21:43:07 +02005294 // First close any normal window.
Bram Moolenaar29323592016-07-24 22:04:11 +02005295 FOR_ALL_WINDOWS(win)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005296 if (win->w_p_pvw)
5297 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005298 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar79648732019-07-18 21:43:07 +02005299 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005300 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005301# ifdef FEAT_PROP_POPUP
Bram Moolenaar79648732019-07-18 21:43:07 +02005302 // Also when 'previewpopup' is empty, it might have been cleared.
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02005303 popup_close_preview();
Bram Moolenaar79648732019-07-18 21:43:07 +02005304# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005305}
Bram Moolenaar4033c552017-09-16 20:54:51 +02005306#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005307
Bram Moolenaarf740b292006-02-16 22:11:02 +00005308/*
5309 * Close window "win" and take care of handling closing the last window for a
5310 * modified buffer.
5311 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005312 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005313ex_win_close(
5314 int forceit,
5315 win_T *win,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005316 tabpage_T *tp) // NULL or the tab page "win" is in
Bram Moolenaar071d4272004-06-13 20:20:40 +00005317{
5318 int need_hide;
5319 buf_T *buf = win->w_buffer;
5320
Bram Moolenaarcf844172020-06-26 19:44:06 +02005321 // Never close the autocommand window.
5322 if (win == aucmd_win)
5323 {
5324 emsg(_(e_autocmd_close));
5325 return;
5326 }
5327
Bram Moolenaar071d4272004-06-13 20:20:40 +00005328 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02005329 if (need_hide && !buf_hide(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005330 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02005331#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005332 if ((p_confirm || cmdmod.confirm) && p_write)
5333 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005334 bufref_T bufref;
5335
5336 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005337 dialog_changed(buf, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005338 if (bufref_valid(&bufref) && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005339 return;
5340 need_hide = FALSE;
5341 }
5342 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005343#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005344 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02005345 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005346 return;
5347 }
5348 }
5349
Bram Moolenaar4033c552017-09-16 20:54:51 +02005350#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005351 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005352#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005353
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005354 // free buffer when not hiding it or when it's a scratch buffer
Bram Moolenaarf740b292006-02-16 22:11:02 +00005355 if (tp == NULL)
Bram Moolenaareb44a682017-08-03 22:44:55 +02005356 win_close(win, !need_hide && !buf_hide(buf));
Bram Moolenaarf740b292006-02-16 22:11:02 +00005357 else
Bram Moolenaareb44a682017-08-03 22:44:55 +02005358 win_close_othertab(win, !need_hide && !buf_hide(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005359}
5360
Bram Moolenaar071d4272004-06-13 20:20:40 +00005361/*
Bram Moolenaardea25702017-01-29 15:18:10 +01005362 * Handle the argument for a tabpage related ex command.
5363 * Returns a tabpage number.
5364 * When an error is encountered then eap->errmsg is set.
5365 */
5366 static int
5367get_tabpage_arg(exarg_T *eap)
5368{
5369 int tab_number;
5370 int unaccept_arg0 = (eap->cmdidx == CMD_tabmove) ? 0 : 1;
5371
5372 if (eap->arg && *eap->arg != NUL)
5373 {
5374 char_u *p = eap->arg;
5375 char_u *p_save;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005376 int relative = 0; // argument +N/-N means: go to N places to the
5377 // right/left relative to the current position.
Bram Moolenaardea25702017-01-29 15:18:10 +01005378
5379 if (*p == '-')
5380 {
5381 relative = -1;
5382 p++;
5383 }
5384 else if (*p == '+')
5385 {
5386 relative = 1;
5387 p++;
5388 }
5389
5390 p_save = p;
5391 tab_number = getdigits(&p);
5392
5393 if (relative == 0)
5394 {
5395 if (STRCMP(p, "$") == 0)
5396 tab_number = LAST_TAB_NR;
5397 else if (p == p_save || *p_save == '-' || *p != NUL
5398 || tab_number > LAST_TAB_NR)
5399 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005400 // No numbers as argument.
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005401 eap->errmsg = ex_errmsg(e_invarg2, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01005402 goto theend;
5403 }
5404 }
5405 else
5406 {
5407 if (*p_save == NUL)
5408 tab_number = 1;
5409 else if (p == p_save || *p_save == '-' || *p != NUL
5410 || tab_number == 0)
5411 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005412 // No numbers as argument.
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005413 eap->errmsg = ex_errmsg(e_invarg2, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01005414 goto theend;
5415 }
5416 tab_number = tab_number * relative + tabpage_index(curtab);
5417 if (!unaccept_arg0 && relative == -1)
5418 --tab_number;
5419 }
5420 if (tab_number < unaccept_arg0 || tab_number > LAST_TAB_NR)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005421 eap->errmsg = ex_errmsg(e_invarg2, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01005422 }
5423 else if (eap->addr_count > 0)
5424 {
5425 if (unaccept_arg0 && eap->line2 == 0)
Bram Moolenaarc6251552017-01-29 21:42:20 +01005426 {
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005427 eap->errmsg = _(e_invrange);
Bram Moolenaarc6251552017-01-29 21:42:20 +01005428 tab_number = 0;
5429 }
Bram Moolenaardea25702017-01-29 15:18:10 +01005430 else
5431 {
5432 tab_number = eap->line2;
Bram Moolenaar82a12462019-01-22 22:41:42 +01005433 if (!unaccept_arg0 && *skipwhite(*eap->cmdlinep) == '-')
Bram Moolenaardea25702017-01-29 15:18:10 +01005434 {
5435 --tab_number;
5436 if (tab_number < unaccept_arg0)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005437 eap->errmsg = _(e_invrange);
Bram Moolenaardea25702017-01-29 15:18:10 +01005438 }
5439 }
5440 }
5441 else
5442 {
5443 switch (eap->cmdidx)
5444 {
5445 case CMD_tabnext:
5446 tab_number = tabpage_index(curtab) + 1;
5447 if (tab_number > LAST_TAB_NR)
5448 tab_number = 1;
5449 break;
5450 case CMD_tabmove:
5451 tab_number = LAST_TAB_NR;
5452 break;
5453 default:
5454 tab_number = tabpage_index(curtab);
5455 }
5456 }
5457
5458theend:
5459 return tab_number;
5460}
5461
5462/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00005463 * ":tabclose": close current tab page, unless it is the last one.
5464 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005465 */
5466 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005467ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005468{
Bram Moolenaarf740b292006-02-16 22:11:02 +00005469 tabpage_T *tp;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005470 int tab_number;
Bram Moolenaarf740b292006-02-16 22:11:02 +00005471
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005472# ifdef FEAT_CMDWIN
5473 if (cmdwin_type != 0)
5474 cmdwin_result = K_IGNORE;
5475 else
5476# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005477 if (first_tabpage->tp_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005478 emsg(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00005479 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005480 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005481 tab_number = get_tabpage_arg(eap);
5482 if (eap->errmsg == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005483 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005484 tp = find_tabpage(tab_number);
Bram Moolenaarf740b292006-02-16 22:11:02 +00005485 if (tp == NULL)
5486 {
5487 beep_flush();
5488 return;
5489 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005490 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005491 {
5492 tabpage_close_other(tp, eap->forceit);
5493 return;
5494 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005495 else if (!text_locked() && !curbuf_locked())
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005496 tabpage_close(eap->forceit);
5497 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005498 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005499}
5500
5501/*
5502 * ":tabonly": close all tab pages except the current one
5503 */
5504 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005505ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005506{
5507 tabpage_T *tp;
5508 int done;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005509 int tab_number;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005510
5511# ifdef FEAT_CMDWIN
5512 if (cmdwin_type != 0)
5513 cmdwin_result = K_IGNORE;
5514 else
5515# endif
5516 if (first_tabpage->tp_next == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005517 msg(_("Already only one tab page"));
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005518 else
5519 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005520 tab_number = get_tabpage_arg(eap);
5521 if (eap->errmsg == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005522 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005523 goto_tabpage(tab_number);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005524 // Repeat this up to a 1000 times, because autocommands may
5525 // mess up the lists.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005526 for (done = 0; done < 1000; ++done)
5527 {
5528 FOR_ALL_TABPAGES(tp)
5529 if (tp->tp_topframe != topframe)
5530 {
5531 tabpage_close_other(tp, eap->forceit);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005532 // if we failed to close it quit
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005533 if (valid_tabpage(tp))
5534 done = 1000;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005535 // start over, "tp" is now invalid
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005536 break;
5537 }
5538 if (first_tabpage->tp_next == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005539 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005540 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005541 }
5542 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005543}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005544
5545/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00005546 * Close the current tab page.
5547 */
5548 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005549tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005550{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005551 // First close all the windows but the current one. If that worked then
5552 // close the last window in this tab, that will close it.
Bram Moolenaar459ca562016-11-10 18:16:33 +01005553 if (!ONE_WINDOW)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005554 close_others(TRUE, forceit);
Bram Moolenaar459ca562016-11-10 18:16:33 +01005555 if (ONE_WINDOW)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005556 ex_win_close(forceit, curwin, NULL);
5557# ifdef FEAT_GUI
5558 need_mouse_correct = TRUE;
5559# endif
5560}
5561
5562/*
5563 * Close tab page "tp", which is not the current tab page.
5564 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005565 * Also takes care of the tab pages line disappearing when closing the
5566 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00005567 */
5568 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005569tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005570{
5571 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005572 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005573 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00005574
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005575 // Limit to 1000 windows, autocommands may add a window while we close
5576 // one. OK, so I'm paranoid...
Bram Moolenaarf740b292006-02-16 22:11:02 +00005577 while (++done < 1000)
5578 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005579 wp = tp->tp_firstwin;
5580 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00005581
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005582 // Autocommands may delete the tab page under our fingers and we may
5583 // fail to close a window with a modified buffer.
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005584 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005585 break;
5586 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005587
Bram Moolenaar29323592016-07-24 22:04:11 +02005588 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar29323592016-07-24 22:04:11 +02005589
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005590 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005591 if (h != tabline_height())
5592 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00005593}
5594
5595/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005596 * ":only".
5597 */
5598 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005599ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005600{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005601 win_T *wp;
5602 int wnr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005603# ifdef FEAT_GUI
5604 need_mouse_correct = TRUE;
5605# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005606 if (eap->addr_count > 0)
5607 {
5608 wnr = eap->line2;
5609 for (wp = firstwin; --wnr > 0; )
5610 {
5611 if (wp->w_next == NULL)
5612 break;
5613 else
5614 wp = wp->w_next;
5615 }
5616 win_goto(wp);
5617 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005618 close_others(TRUE, eap->forceit);
5619}
5620
Bram Moolenaar071d4272004-06-13 20:20:40 +00005621 static void
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01005622ex_hide(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005623{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005624 // ":hide" or ":hide | cmd": hide current window
Bram Moolenaar2256c992016-11-15 21:17:07 +01005625 if (!eap->skip)
5626 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02005627#ifdef FEAT_GUI
Bram Moolenaar2256c992016-11-15 21:17:07 +01005628 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005629#endif
Bram Moolenaar2256c992016-11-15 21:17:07 +01005630 if (eap->addr_count == 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005631 win_close(curwin, FALSE); // don't free buffer
Bram Moolenaar2256c992016-11-15 21:17:07 +01005632 else
5633 {
5634 int winnr = 0;
5635 win_T *win;
Bram Moolenaarf240e182014-11-27 18:33:02 +01005636
Bram Moolenaar2256c992016-11-15 21:17:07 +01005637 FOR_ALL_WINDOWS(win)
5638 {
5639 winnr++;
5640 if (winnr == eap->line2)
5641 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005642 }
Bram Moolenaar2256c992016-11-15 21:17:07 +01005643 if (win == NULL)
5644 win = lastwin;
5645 win_close(win, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005646 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005647 }
5648}
5649
5650/*
5651 * ":stop" and ":suspend": Suspend Vim.
5652 */
5653 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005654ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005655{
5656 /*
5657 * Disallow suspending for "rvim".
5658 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005659 if (!check_restricted())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005660 {
5661 if (!eap->forceit)
5662 autowrite_all();
5663 windgoto((int)Rows - 1, 0);
5664 out_char('\n');
5665 out_flush();
5666 stoptermcap();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005667 out_flush(); // needed for SUN to restore xterm buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00005668#ifdef FEAT_TITLE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005669 mch_restore_title(SAVE_RESTORE_BOTH); // restore window titles
Bram Moolenaar071d4272004-06-13 20:20:40 +00005670#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005671 ui_suspend(); // call machine specific function
Bram Moolenaar071d4272004-06-13 20:20:40 +00005672#ifdef FEAT_TITLE
5673 maketitle();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005674 resettitle(); // force updating the title
Bram Moolenaar071d4272004-06-13 20:20:40 +00005675#endif
5676 starttermcap();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005677 scroll_start(); // scroll screen before redrawing
Bram Moolenaar071d4272004-06-13 20:20:40 +00005678 redraw_later_clear();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005679 shell_resized(); // may have resized window
Bram Moolenaar071d4272004-06-13 20:20:40 +00005680 }
5681}
5682
5683/*
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005684 * ":exit", ":xit" and ":wq": Write file and quite the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005685 */
5686 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005687ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005688{
Bram Moolenaar461f2122020-07-28 20:25:47 +02005689#ifdef FEAT_EVAL
Bram Moolenaarae616492020-07-28 20:07:27 +02005690 if (not_in_vim9(eap) == FAIL)
5691 return;
Bram Moolenaar461f2122020-07-28 20:25:47 +02005692#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005693#ifdef FEAT_CMDWIN
5694 if (cmdwin_type != 0)
5695 {
5696 cmdwin_result = Ctrl_C;
5697 return;
5698 }
5699#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005700 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005701 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005702 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005703 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005704 return;
5705 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005706
5707 if (before_quit_autocmds(curwin, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005708 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005709
5710 /*
5711 * if more files or windows we won't exit
5712 */
5713 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
5714 exiting = TRUE;
5715 if ( ((eap->cmdidx == CMD_wq
5716 || curbufIsChanged())
5717 && do_write(eap) == FAIL)
5718 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01005719 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005720 {
5721 not_exiting();
5722 }
5723 else
5724 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005725 if (only_one_window()) // quit last window, exit Vim
Bram Moolenaar071d4272004-06-13 20:20:40 +00005726 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02005727 not_exiting();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005728# ifdef FEAT_GUI
5729 need_mouse_correct = TRUE;
5730# endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005731 // Quit current window, may free the buffer.
Bram Moolenaareb44a682017-08-03 22:44:55 +02005732 win_close(curwin, !buf_hide(curwin->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005733 }
5734}
5735
5736/*
5737 * ":print", ":list", ":number".
5738 */
5739 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005740ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005741{
Bram Moolenaardf177f62005-02-22 08:39:57 +00005742 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005743 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +00005744 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005745 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00005746 for ( ;!got_int; ui_breakcheck())
5747 {
5748 print_line(eap->line1,
5749 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
5750 || (eap->flags & EXFLAG_NR)),
5751 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
5752 if (++eap->line1 > eap->line2)
5753 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005754 out_flush(); // show one line at a time
Bram Moolenaardf177f62005-02-22 08:39:57 +00005755 }
5756 setpcmark();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005757 // put cursor at last line
Bram Moolenaardf177f62005-02-22 08:39:57 +00005758 curwin->w_cursor.lnum = eap->line2;
5759 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005760 }
5761
Bram Moolenaar071d4272004-06-13 20:20:40 +00005762 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005763}
5764
5765#ifdef FEAT_BYTEOFF
5766 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005767ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005768{
5769 goto_byte(eap->line2);
5770}
5771#endif
5772
5773/*
5774 * ":shell".
5775 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005776 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005777ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005778{
5779 do_shell(NULL, 0);
5780}
5781
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005782#if defined(HAVE_DROP_FILE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005783
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005784static int drop_busy = FALSE;
5785static int drop_filec;
5786static char_u **drop_filev = NULL;
5787static int drop_split;
5788static void (*drop_callback)(void *);
5789static void *drop_cookie;
5790
5791 static void
5792handle_drop_internal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005793{
5794 exarg_T ea;
5795 int save_msg_scroll = msg_scroll;
5796
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005797 // Setting the argument list may cause screen updates and being called
5798 // recursively. Avoid that by setting drop_busy.
5799 drop_busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005800
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005801 // Check whether the current buffer is changed. If so, we will need
5802 // to split the current window or data could be lost.
5803 // We don't need to check if the 'hidden' option is set, as in this
5804 // case the buffer won't be lost.
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005805 if (!buf_hide(curbuf) && !drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005806 {
5807 ++emsg_off;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005808 drop_split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005809 --emsg_off;
5810 }
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005811 if (drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005812 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005813 if (win_split(0, 0) == FAIL)
5814 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02005815 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005816
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005817 // When splitting the window, create a new alist. Otherwise the
5818 // existing one is overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005819 alist_unlink(curwin->w_alist);
5820 alist_new();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005821 }
5822
5823 /*
5824 * Set up the new argument list.
5825 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005826 alist_set(ALIST(curwin), drop_filec, drop_filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005827
5828 /*
5829 * Move to the first file.
5830 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005831 // Fake up a minimal "next" command for do_argfile()
Bram Moolenaara80faa82020-04-12 19:37:17 +02005832 CLEAR_FIELD(ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005833 ea.cmd = (char_u *)"next";
5834 do_argfile(&ea, 0);
5835
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005836 // do_ecmd() may set need_start_insertmode, but since we never left Insert
5837 // mode that is not needed here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005838 need_start_insertmode = FALSE;
5839
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005840 // Restore msg_scroll, otherwise a following command may cause scrolling
5841 // unexpectedly. The screen will be redrawn by the caller, thus
5842 // msg_scroll being set by displaying a message is irrelevant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005843 msg_scroll = save_msg_scroll;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005844
5845 if (drop_callback != NULL)
5846 drop_callback(drop_cookie);
5847
5848 drop_filev = NULL;
5849 drop_busy = FALSE;
5850}
5851
5852/*
5853 * Handle a file drop. The code is here because a drop is *nearly* like an
5854 * :args command, but not quite (we have a list of exact filenames, so we
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01005855 * don't want to (a) parse a command line, or (b) expand wildcards). So the
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005856 * code is very similar to :args and hence needs access to a lot of the static
5857 * functions in this file.
5858 *
5859 * The "filev" list must have been allocated using alloc(), as should each item
5860 * in the list. This function takes over responsibility for freeing the "filev"
5861 * list.
5862 */
5863 void
5864handle_drop(
5865 int filec, // the number of files dropped
5866 char_u **filev, // the list of files dropped
5867 int split, // force splitting the window
5868 void (*callback)(void *), // to be called after setting the argument
5869 // list
5870 void *cookie) // argument for "callback" (allocated)
5871{
5872 // Cannot handle recursive drops, finish the pending one.
5873 if (drop_busy)
5874 {
5875 FreeWild(filec, filev);
5876 vim_free(cookie);
5877 return;
5878 }
5879
5880 // When calling handle_drop() more than once in a row we only use the last
5881 // one.
5882 if (drop_filev != NULL)
5883 {
5884 FreeWild(drop_filec, drop_filev);
5885 vim_free(drop_cookie);
5886 }
5887
5888 drop_filec = filec;
5889 drop_filev = filev;
5890 drop_split = split;
5891 drop_callback = callback;
5892 drop_cookie = cookie;
5893
5894 // Postpone this when:
5895 // - editing the command line
5896 // - not possible to change the current buffer
5897 // - updating the screen
5898 // As it may change buffers and window structures that are in use and cause
5899 // freed memory to be used.
5900 if (text_locked() || curbuf_locked() || updating_screen)
5901 return;
5902
5903 handle_drop_internal();
5904}
5905
5906/*
5907 * To be called when text is unlocked, curbuf is unlocked or updating_screen is
5908 * reset: Handle a postponed drop.
5909 */
5910 void
5911handle_any_postponed_drop(void)
5912{
5913 if (!drop_busy && drop_filev != NULL
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02005914 && !text_locked() && !curbuf_locked() && !updating_screen)
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005915 handle_drop_internal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005916}
5917#endif
5918
Bram Moolenaar071d4272004-06-13 20:20:40 +00005919/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005920 * ":preserve".
5921 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005922 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005923ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005924{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005925 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005926 ml_preserve(curbuf, TRUE);
5927}
5928
5929/*
5930 * ":recover".
5931 */
5932 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005933ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005934{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005935 // Set recoverymode right away to avoid the ATTENTION prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005936 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01005937 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
5938 | CCGD_MULTWIN
5939 | (eap->forceit ? CCGD_FORCEIT : 0)
5940 | CCGD_EXCMD)
5941
Bram Moolenaar071d4272004-06-13 20:20:40 +00005942 && (*eap->arg == NUL
5943 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
Bram Moolenaar99499b12019-05-23 21:35:48 +02005944 ml_recover(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005945 recoverymode = FALSE;
5946}
5947
5948/*
5949 * Command modifier used in a wrong way.
5950 */
5951 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005952ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005953{
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005954 eap->errmsg = _(e_invcmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005955}
5956
Bram Moolenaar071d4272004-06-13 20:20:40 +00005957/*
5958 * :sview [+command] file split window with new file, read-only
5959 * :split [[+command] file] split window with current or new file
5960 * :vsplit [[+command] file] split window vertically with current or new file
5961 * :new [[+command] file] split window with no or new file
5962 * :vnew [[+command] file] split vertically window with no or new file
5963 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005964 *
5965 * :tabedit open new Tab page with empty window
5966 * :tabedit [+command] file open new Tab page and edit "file"
5967 * :tabnew [[+command] file] just like :tabedit
5968 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005969 */
5970 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005971ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005972{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005973 win_T *old_curwin = curwin;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005974#if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005975 char_u *fname = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005976#endif
5977#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005978 int browse_flag = cmdmod.browse;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005979#endif
Bram Moolenaar39902a02018-06-21 22:10:08 +02005980 int use_tab = eap->cmdidx == CMD_tabedit
5981 || eap->cmdidx == CMD_tabfind
5982 || eap->cmdidx == CMD_tabnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005983
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005984 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005985 return;
5986
Bram Moolenaar4033c552017-09-16 20:54:51 +02005987#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005988 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005989#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005990
Bram Moolenaar4033c552017-09-16 20:54:51 +02005991#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005992 // A ":split" in the quickfix window works like ":new". Don't want two
5993 // quickfix windows. But it's OK when doing ":tab split".
Bram Moolenaar05bb9532008-07-04 09:44:11 +00005994 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005995 {
5996 if (eap->cmdidx == CMD_split)
5997 eap->cmdidx = CMD_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005998 if (eap->cmdidx == CMD_vsplit)
5999 eap->cmdidx = CMD_vnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006000 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006001#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006002
Bram Moolenaar4033c552017-09-16 20:54:51 +02006003#ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006004 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006005 {
6006 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
6007 FNAME_MESS, TRUE, curbuf->b_ffname);
6008 if (fname == NULL)
6009 goto theend;
6010 eap->arg = fname;
6011 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006012# ifdef FEAT_BROWSE
Bram Moolenaar4033c552017-09-16 20:54:51 +02006013 else
6014# endif
6015#endif
6016#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006017 if (cmdmod.browse
Bram Moolenaar071d4272004-06-13 20:20:40 +00006018 && eap->cmdidx != CMD_vnew
Bram Moolenaar071d4272004-06-13 20:20:40 +00006019 && eap->cmdidx != CMD_new)
6020 {
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006021 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006022# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006023 !gui.in_use &&
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006024# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006025 au_has_group((char_u *)"FileExplorer"))
6026 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006027 // No browsing supported but we do have the file explorer:
6028 // Edit the directory.
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006029 if (*eap->arg == NUL || !mch_isdir(eap->arg))
6030 eap->arg = (char_u *)".";
6031 }
6032 else
6033 {
Bram Moolenaar39902a02018-06-21 22:10:08 +02006034 fname = do_browse(0, (char_u *)(use_tab
6035 ? _("Edit File in new tab page")
6036 : _("Edit File in new window")),
Bram Moolenaar071d4272004-06-13 20:20:40 +00006037 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006038 if (fname == NULL)
6039 goto theend;
6040 eap->arg = fname;
6041 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006042 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006043 cmdmod.browse = FALSE; // Don't browse again in do_ecmd().
Bram Moolenaar4033c552017-09-16 20:54:51 +02006044#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006045
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006046 /*
6047 * Either open new tab page or split the window.
6048 */
Bram Moolenaar39902a02018-06-21 22:10:08 +02006049 if (use_tab)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006050 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006051 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
6052 : eap->addr_count == 0 ? 0
6053 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006054 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00006055 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006056
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006057 // set the alternate buffer for the window we came from
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006058 if (curwin != old_curwin
6059 && win_valid(old_curwin)
6060 && old_curwin->w_buffer != curbuf
6061 && !cmdmod.keepalt)
6062 old_curwin->w_alt_fnum = curbuf->b_fnum;
6063 }
6064 }
6065 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006066 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
6067 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006068 // Reset 'scrollbind' when editing another file, but keep it when
6069 // doing ":split" without arguments.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006070 if (*eap->arg != NUL
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01006071# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006072 || cmdmod.browse
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01006073# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006074 )
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006075 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006076 else
6077 do_check_scrollbind(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006078 do_exedit(eap, old_curwin);
6079 }
6080
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006081# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006082 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006083# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006084
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006085# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006086theend:
6087 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006088# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006089}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006090
6091/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006092 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006093 */
6094 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006095tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006096{
6097 exarg_T ea;
6098
Bram Moolenaara80faa82020-04-12 19:37:17 +02006099 CLEAR_FIELD(ea);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006100 ea.cmdidx = CMD_tabnew;
6101 ea.cmd = (char_u *)"tabn";
6102 ea.arg = (char_u *)"";
6103 ex_splitview(&ea);
6104}
6105
6106/*
6107 * :tabnext command
6108 */
6109 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006110ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006111{
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006112 int tab_number;
6113
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02006114 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006115 return;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006116 switch (eap->cmdidx)
6117 {
6118 case CMD_tabfirst:
6119 case CMD_tabrewind:
6120 goto_tabpage(1);
6121 break;
6122 case CMD_tablast:
6123 goto_tabpage(9999);
6124 break;
6125 case CMD_tabprevious:
6126 case CMD_tabNext:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006127 if (eap->arg && *eap->arg != NUL)
6128 {
6129 char_u *p = eap->arg;
6130 char_u *p_save = p;
6131
6132 tab_number = getdigits(&p);
6133 if (p == p_save || *p_save == '-' || *p != NUL
6134 || tab_number == 0)
6135 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006136 // No numbers as argument.
Bram Moolenaar8930caa2020-07-23 16:37:03 +02006137 eap->errmsg = ex_errmsg(e_invarg2, eap->arg);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006138 return;
6139 }
6140 }
6141 else
6142 {
6143 if (eap->addr_count == 0)
6144 tab_number = 1;
6145 else
6146 {
6147 tab_number = eap->line2;
6148 if (tab_number < 1)
6149 {
Bram Moolenaar8930caa2020-07-23 16:37:03 +02006150 eap->errmsg = _(e_invrange);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006151 return;
6152 }
6153 }
6154 }
6155 goto_tabpage(-tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006156 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006157 default: // CMD_tabnext
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006158 tab_number = get_tabpage_arg(eap);
6159 if (eap->errmsg == NULL)
6160 goto_tabpage(tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006161 break;
6162 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006163}
6164
6165/*
6166 * :tabmove command
6167 */
6168 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006169ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006170{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02006171 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02006172
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006173 tab_number = get_tabpage_arg(eap);
6174 if (eap->errmsg == NULL)
6175 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006176}
6177
6178/*
6179 * :tabs command: List tabs and their contents.
6180 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006181 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006182ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006183{
6184 tabpage_T *tp;
6185 win_T *wp;
6186 int tabcount = 1;
6187
6188 msg_start();
6189 msg_scroll = TRUE;
6190 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
6191 {
6192 msg_putchar('\n');
6193 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
Bram Moolenaar8820b482017-03-16 17:23:31 +01006194 msg_outtrans_attr(IObuff, HL_ATTR(HLF_T));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006195 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006196 ui_breakcheck();
6197
Bram Moolenaar030f0df2006-02-21 22:02:53 +00006198 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006199 wp = firstwin;
6200 else
6201 wp = tp->tp_firstwin;
6202 for ( ; wp != NULL && !got_int; wp = wp->w_next)
6203 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006204 msg_putchar('\n');
6205 msg_putchar(wp == curwin ? '>' : ' ');
6206 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006207 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
6208 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006209 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02006210 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006211 else
6212 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
6213 IObuff, IOSIZE, TRUE);
6214 msg_outtrans(IObuff);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006215 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006216 ui_breakcheck();
6217 }
6218 }
6219}
6220
Bram Moolenaar071d4272004-06-13 20:20:40 +00006221/*
6222 * ":mode": Set screen mode.
6223 * If no argument given, just get the screen size and redraw.
6224 */
6225 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006226ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006227{
6228 if (*eap->arg == NUL)
6229 shell_resized();
6230 else
Bram Moolenaar3c71aec2020-01-17 19:32:20 +01006231 emsg(_(e_screenmode));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006232}
6233
Bram Moolenaar071d4272004-06-13 20:20:40 +00006234/*
6235 * ":resize".
6236 * set, increment or decrement current window height
6237 */
6238 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006239ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006240{
6241 int n;
6242 win_T *wp = curwin;
6243
6244 if (eap->addr_count > 0)
6245 {
6246 n = eap->line2;
6247 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
6248 ;
6249 }
6250
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006251# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006252 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006253# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006254 n = atol((char *)eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006255 if (cmdmod.split & WSP_VERT)
6256 {
6257 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar02631462017-09-22 15:20:32 +02006258 n += curwin->w_width;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006259 else if (n == 0 && eap->arg[0] == NUL) // default is very wide
Bram Moolenaar071d4272004-06-13 20:20:40 +00006260 n = 9999;
6261 win_setwidth_win((int)n, wp);
6262 }
6263 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006264 {
6265 if (*eap->arg == '-' || *eap->arg == '+')
6266 n += curwin->w_height;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006267 else if (n == 0 && eap->arg[0] == NUL) // default is very high
Bram Moolenaar071d4272004-06-13 20:20:40 +00006268 n = 9999;
6269 win_setheight_win((int)n, wp);
6270 }
6271}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006272
6273/*
6274 * ":find [+command] <file>" command.
6275 */
6276 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006277ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006278{
6279#ifdef FEAT_SEARCHPATH
6280 char_u *fname;
6281 int count;
6282
6283 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
6284 TRUE, curbuf->b_ffname);
6285 if (eap->addr_count > 0)
6286 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006287 // Repeat finding the file "count" times. This matters when it
6288 // appears several times in the path.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006289 count = eap->line2;
6290 while (fname != NULL && --count > 0)
6291 {
6292 vim_free(fname);
6293 fname = find_file_in_path(NULL, 0, FNAME_MESS,
6294 FALSE, curbuf->b_ffname);
6295 }
6296 }
6297
6298 if (fname != NULL)
6299 {
6300 eap->arg = fname;
6301#endif
6302 do_exedit(eap, NULL);
6303#ifdef FEAT_SEARCHPATH
6304 vim_free(fname);
6305 }
6306#endif
6307}
6308
6309/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00006310 * ":open" simulation: for now just work like ":visual".
6311 */
6312 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006313ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006314{
6315 regmatch_T regmatch;
6316 char_u *p;
6317
6318 curwin->w_cursor.lnum = eap->line2;
6319 beginline(BL_SOL | BL_FIX);
6320 if (*eap->arg == '/')
6321 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006322 // ":open /pattern/": put cursor in column found with pattern
Bram Moolenaardf177f62005-02-22 08:39:57 +00006323 ++eap->arg;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006324 p = skip_regexp(eap->arg, '/', p_magic);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006325 *p = NUL;
6326 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
6327 if (regmatch.regprog != NULL)
6328 {
6329 regmatch.rm_ic = p_ic;
6330 p = ml_get_curline();
6331 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006332 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006333 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006334 emsg(_(e_nomatch));
Bram Moolenaar473de612013-06-08 18:19:48 +02006335 vim_regfree(regmatch.regprog);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006336 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006337 // Move to the NUL, ignore any other arguments.
Bram Moolenaardf177f62005-02-22 08:39:57 +00006338 eap->arg += STRLEN(eap->arg);
6339 }
6340 check_cursor();
6341
6342 eap->cmdidx = CMD_visual;
6343 do_exedit(eap, NULL);
6344}
6345
6346/*
6347 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006348 */
6349 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006350ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006351{
6352 do_exedit(eap, NULL);
6353}
6354
6355/*
Bram Moolenaar4b96df52020-01-26 22:00:26 +01006356 * ":edit <file>" command and alike.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006357 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006358 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006359do_exedit(
6360 exarg_T *eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006361 win_T *old_curwin) // curwin before doing a split or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006362{
6363 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006364 int need_hide;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006365 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006366
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01006367 if ((eap->cmdidx != CMD_pedit && ERROR_IF_POPUP_WINDOW)
6368 || ERROR_IF_TERM_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006369 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006370 /*
6371 * ":vi" command ends Ex mode.
6372 */
6373 if (exmode_active && (eap->cmdidx == CMD_visual
6374 || eap->cmdidx == CMD_view))
6375 {
6376 exmode_active = FALSE;
Bram Moolenaar158ea172020-06-18 17:28:39 +02006377 ex_pressedreturn = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006378 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006379 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006380 // Special case: ":global/pat/visual\NLvi-commands"
Bram Moolenaardf177f62005-02-22 08:39:57 +00006381 if (global_busy)
6382 {
6383 int rd = RedrawingDisabled;
6384 int nwr = no_wait_return;
6385 int ms = msg_scroll;
6386#ifdef FEAT_GUI
6387 int he = hold_gui_events;
6388#endif
6389
6390 if (eap->nextcmd != NULL)
6391 {
6392 stuffReadbuff(eap->nextcmd);
6393 eap->nextcmd = NULL;
6394 }
6395
6396 if (exmode_was != EXMODE_VIM)
6397 settmode(TMODE_RAW);
6398 RedrawingDisabled = 0;
6399 no_wait_return = 0;
6400 need_wait_return = FALSE;
6401 msg_scroll = 0;
6402#ifdef FEAT_GUI
6403 hold_gui_events = 0;
6404#endif
6405 must_redraw = CLEAR;
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01006406 pending_exmode_active = TRUE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006407
6408 main_loop(FALSE, TRUE);
6409
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01006410 pending_exmode_active = FALSE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006411 RedrawingDisabled = rd;
6412 no_wait_return = nwr;
6413 msg_scroll = ms;
6414#ifdef FEAT_GUI
6415 hold_gui_events = he;
6416#endif
6417 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006418 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006419 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006420 }
6421
6422 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006423 || eap->cmdidx == CMD_tabnew
6424 || eap->cmdidx == CMD_tabedit
Bram Moolenaar4033c552017-09-16 20:54:51 +02006425 || eap->cmdidx == CMD_vnew) && *eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006426 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006427 // ":new" or ":tabnew" without argument: edit an new empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006428 setpcmark();
6429 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006430 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
6431 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006432 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006433 else if ((eap->cmdidx != CMD_split && eap->cmdidx != CMD_vsplit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006434 || *eap->arg != NUL
6435#ifdef FEAT_BROWSE
6436 || cmdmod.browse
6437#endif
6438 )
6439 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006440 // Can't edit another file when "curbuf_lock" is set. Only ":edit"
6441 // can bring us here, others are stopped earlier.
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006442 if (*eap->arg != NUL && curbuf_locked())
6443 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006444
Bram Moolenaar071d4272004-06-13 20:20:40 +00006445 n = readonlymode;
6446 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
6447 readonlymode = TRUE;
6448 else if (eap->cmdidx == CMD_enew)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006449 readonlymode = FALSE; // 'readonly' doesn't make sense in an
6450 // empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006451 setpcmark();
6452 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
6453 NULL, eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006454 // ":edit" goes to first line if Vi compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00006455 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
6456 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
6457 ? ECMD_ONE : eap->do_ecmd_lnum,
Bram Moolenaareb44a682017-08-03 22:44:55 +02006458 (buf_hide(curbuf) ? ECMD_HIDE : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006459 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006460 // after a split we can use an existing buffer
Bram Moolenaar7b449342014-03-25 13:03:48 +01006461 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006462 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006463 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006464 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006465 // Editing the file failed. If the window was split, close it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006466 if (old_curwin != NULL)
6467 {
6468 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02006469 if (!need_hide || buf_hide(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006470 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006471#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006472 cleanup_T cs;
6473
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006474 // Reset the error/interrupt/exception state here so that
6475 // aborting() returns FALSE when closing a window.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006476 enter_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02006477#endif
6478#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006479 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006480#endif
Bram Moolenaareb44a682017-08-03 22:44:55 +02006481 win_close(curwin, !need_hide && !buf_hide(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006482
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006483#if defined(FEAT_EVAL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006484 // Restore the error/interrupt/exception state if not
6485 // discarded by a new aborting error, interrupt, or
6486 // uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006487 leave_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02006488#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006489 }
6490 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006491 }
6492 else if (readonlymode && curbuf->b_nwindows == 1)
6493 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006494 // When editing an already visited buffer, 'readonly' won't be set
6495 // but the previous value is kept. With ":view" and ":sview" we
6496 // want the file to be readonly, except when another window is
6497 // editing the same buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006498 curbuf->b_p_ro = TRUE;
6499 }
6500 readonlymode = n;
6501 }
6502 else
6503 {
6504 if (eap->do_ecmd_cmd != NULL)
6505 do_cmdline_cmd(eap->do_ecmd_cmd);
6506#ifdef FEAT_TITLE
6507 n = curwin->w_arg_idx_invalid;
6508#endif
6509 check_arg_idx(curwin);
6510#ifdef FEAT_TITLE
6511 if (n != curwin->w_arg_idx_invalid)
6512 maketitle();
6513#endif
6514 }
6515
Bram Moolenaar071d4272004-06-13 20:20:40 +00006516 /*
6517 * if ":split file" worked, set alternate file name in old window to new
6518 * file
6519 */
6520 if (old_curwin != NULL
6521 && *eap->arg != NUL
6522 && curwin != old_curwin
6523 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00006524 && old_curwin->w_buffer != curbuf
6525 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006526 old_curwin->w_alt_fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006527
6528 ex_no_reprint = TRUE;
6529}
6530
6531#ifndef FEAT_GUI
6532/*
6533 * ":gui" and ":gvim" when there is no GUI.
6534 */
6535 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006536ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006537{
Bram Moolenaar8930caa2020-07-23 16:37:03 +02006538 eap->errmsg = _(e_nogvim);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006539}
6540#endif
6541
Bram Moolenaar4f974752019-02-17 17:44:42 +01006542#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006543 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006544ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006545{
6546 gui_make_tearoff(eap->arg);
6547}
6548#endif
6549
Bram Moolenaar29a2c082018-03-05 21:06:23 +01006550#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
6551 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006552 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006553ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006554{
Bram Moolenaar29a2c082018-03-05 21:06:23 +01006555# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
6556 if (gui.in_use)
6557 gui_make_popup(eap->arg, eap->forceit);
6558# ifdef FEAT_TERM_POPUP_MENU
6559 else
6560# endif
6561# endif
6562# ifdef FEAT_TERM_POPUP_MENU
6563 pum_make_popup(eap->arg, eap->forceit);
6564# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006565}
6566#endif
6567
Bram Moolenaar071d4272004-06-13 20:20:40 +00006568 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006569ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006570{
6571 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006572 msg(_("No swap file"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006573 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01006574 msg((char *)curbuf->b_ml.ml_mfp->mf_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006575}
6576
6577/*
6578 * ":syncbind" forces all 'scrollbind' windows to have the same relative
6579 * offset.
6580 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
6581 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006582 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006583ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006584{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006585 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006586 win_T *save_curwin = curwin;
6587 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006588 long topline;
6589 long y;
6590 linenr_T old_linenr = curwin->w_cursor.lnum;
6591
6592 setpcmark();
6593
6594 /*
6595 * determine max topline
6596 */
6597 if (curwin->w_p_scb)
6598 {
6599 topline = curwin->w_topline;
Bram Moolenaar29323592016-07-24 22:04:11 +02006600 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006601 {
6602 if (wp->w_p_scb && wp->w_buffer)
6603 {
Bram Moolenaar375e3392019-01-31 18:26:10 +01006604 y = wp->w_buffer->b_ml.ml_line_count - get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006605 if (topline > y)
6606 topline = y;
6607 }
6608 }
6609 if (topline < 1)
6610 topline = 1;
6611 }
6612 else
6613 {
6614 topline = 1;
6615 }
6616
6617
6618 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006619 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006620 */
Bram Moolenaar29323592016-07-24 22:04:11 +02006621 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006622 {
6623 if (curwin->w_p_scb)
6624 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006625 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006626 y = topline - curwin->w_topline;
6627 if (y > 0)
6628 scrollup(y, TRUE);
6629 else
6630 scrolldown(-y, TRUE);
6631 curwin->w_scbind_pos = topline;
6632 redraw_later(VALID);
6633 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006634 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006635 }
6636 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006637 curwin = save_curwin;
6638 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006639 if (curwin->w_p_scb)
6640 {
6641 did_syncbind = TRUE;
6642 checkpcmark();
6643 if (old_linenr != curwin->w_cursor.lnum)
6644 {
6645 char_u ctrl_o[2];
6646
6647 ctrl_o[0] = Ctrl_O;
6648 ctrl_o[1] = 0;
6649 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
6650 }
6651 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006652}
6653
6654
6655 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006656ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006657{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006658 int i;
6659 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
6660 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006661
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006662 if (eap->usefilter) // :r!cmd
Bram Moolenaar071d4272004-06-13 20:20:40 +00006663 do_bang(1, eap, FALSE, FALSE, TRUE);
6664 else
6665 {
6666 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
6667 return;
6668
6669#ifdef FEAT_BROWSE
6670 if (cmdmod.browse)
6671 {
6672 char_u *browseFile;
6673
Bram Moolenaar7171abe2004-10-11 10:06:20 +00006674 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006675 NULL, NULL, NULL, curbuf);
6676 if (browseFile != NULL)
6677 {
6678 i = readfile(browseFile, NULL,
6679 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
6680 vim_free(browseFile);
6681 }
6682 else
6683 i = OK;
6684 }
6685 else
6686#endif
6687 if (*eap->arg == NUL)
6688 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006689 if (check_fname() == FAIL) // check for no file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00006690 return;
6691 i = readfile(curbuf->b_ffname, curbuf->b_fname,
6692 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
6693 }
6694 else
6695 {
6696 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
6697 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
6698 i = readfile(eap->arg, NULL,
6699 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
6700
6701 }
Bram Moolenaare13b9af2017-01-13 22:01:02 +01006702 if (i != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006703 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006704#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006705 if (!aborting())
6706#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006707 semsg(_(e_notopen), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006708 }
6709 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00006710 {
6711 if (empty && exmode_active)
6712 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006713 // Delete the empty line that remains. Historically ex does
6714 // this but vi doesn't.
Bram Moolenaardf177f62005-02-22 08:39:57 +00006715 if (eap->line2 == 0)
6716 lnum = curbuf->b_ml.ml_line_count;
6717 else
6718 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00006719 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006720 {
Bram Moolenaarca70c072020-05-30 20:30:46 +02006721 ml_delete(lnum);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00006722 if (curwin->w_cursor.lnum > 1
6723 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006724 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00006725 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006726 }
6727 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006728 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006729 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006730 }
6731}
6732
Bram Moolenaarea408852005-06-25 22:49:46 +00006733static char_u *prev_dir = NULL;
6734
6735#if defined(EXITFREE) || defined(PROTO)
6736 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006737free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00006738{
Bram Moolenaard23a8232018-02-10 18:45:26 +01006739 VIM_CLEAR(prev_dir);
6740 VIM_CLEAR(globaldir);
Bram Moolenaarea408852005-06-25 22:49:46 +00006741}
6742#endif
6743
Bram Moolenaarf4258302013-06-02 18:20:17 +02006744/*
Bram Moolenaar002bc792020-06-05 22:33:42 +02006745 * Get the previous directory for the given chdir scope.
6746 */
6747 static char_u *
6748get_prevdir(cdscope_T scope)
6749{
6750 if (scope == CDSCOPE_WINDOW)
6751 return curwin->w_prevdir;
6752 else if (scope == CDSCOPE_TABPAGE)
6753 return curtab->tp_prevdir;
6754 return prev_dir;
6755}
6756
6757/*
Bram Moolenaarf4258302013-06-02 18:20:17 +02006758 * Deal with the side effects of changing the current directory.
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006759 * When 'scope' is CDSCOPE_TABPAGE then this was after an ":tcd" command.
6760 * When 'scope' is CDSCOPE_WINDOW then this was after an ":lcd" command.
Bram Moolenaarf4258302013-06-02 18:20:17 +02006761 */
6762 void
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006763post_chdir(cdscope_T scope)
Bram Moolenaarf4258302013-06-02 18:20:17 +02006764{
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006765 if (scope != CDSCOPE_WINDOW)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006766 // Clear tab local directory for both :cd and :tcd
6767 VIM_CLEAR(curtab->tp_localdir);
Bram Moolenaard23a8232018-02-10 18:45:26 +01006768 VIM_CLEAR(curwin->w_localdir);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006769 if (scope != CDSCOPE_GLOBAL)
Bram Moolenaarf4258302013-06-02 18:20:17 +02006770 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02006771 char_u *pdir = get_prevdir(scope);
6772
6773 // If still in the global directory, need to remember current
6774 // directory as the global directory.
6775 if (globaldir == NULL && pdir != NULL)
6776 globaldir = vim_strsave(pdir);
6777
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006778 // Remember this local directory for the window.
Bram Moolenaarf4258302013-06-02 18:20:17 +02006779 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006780 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006781 if (scope == CDSCOPE_TABPAGE)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006782 curtab->tp_localdir = vim_strsave(NameBuff);
6783 else
6784 curwin->w_localdir = vim_strsave(NameBuff);
6785 }
Bram Moolenaarf4258302013-06-02 18:20:17 +02006786 }
6787 else
6788 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02006789 // We are now in the global directory, no need to remember its name.
Bram Moolenaard23a8232018-02-10 18:45:26 +01006790 VIM_CLEAR(globaldir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006791 }
6792
6793 shorten_fnames(TRUE);
6794}
6795
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006796/*
6797 * Change directory function used by :cd/:tcd/:lcd Ex commands and the
Bram Moolenaar95058722020-06-01 16:26:19 +02006798 * chdir() function.
6799 * scope == CDSCOPE_WINDOW: changes the window-local directory
6800 * scope == CDSCOPE_TABPAGE: changes the tab-local directory
6801 * Otherwise: changes the global directory
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006802 * Returns TRUE if the directory is successfully changed.
6803 */
6804 int
6805changedir_func(
6806 char_u *new_dir,
6807 int forceit,
6808 cdscope_T scope)
6809{
6810 char_u *tofree;
Bram Moolenaar002bc792020-06-05 22:33:42 +02006811 char_u *pdir = NULL;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006812 int dir_differs;
6813 int retval = FALSE;
6814
Bram Moolenaar7cc96922020-01-31 22:41:38 +01006815 if (new_dir == NULL || allbuf_locked())
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006816 return FALSE;
6817
6818 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged() && !forceit)
6819 {
6820 emsg(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
6821 return FALSE;
6822 }
6823
6824 // ":cd -": Change to previous directory
6825 if (STRCMP(new_dir, "-") == 0)
6826 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02006827 pdir = get_prevdir(scope);
6828 if (pdir == NULL)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006829 {
6830 emsg(_("E186: No previous directory"));
6831 return FALSE;
6832 }
Bram Moolenaar002bc792020-06-05 22:33:42 +02006833 new_dir = pdir;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006834 }
6835
Bram Moolenaar002bc792020-06-05 22:33:42 +02006836 // Free the previous directory
6837 tofree = get_prevdir(scope);
6838
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006839 // Save current directory for next ":cd -"
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006840 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar002bc792020-06-05 22:33:42 +02006841 pdir = vim_strsave(NameBuff);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006842 else
Bram Moolenaar002bc792020-06-05 22:33:42 +02006843 pdir = NULL;
6844 if (scope == CDSCOPE_WINDOW)
6845 curwin->w_prevdir = pdir;
6846 else if (scope == CDSCOPE_TABPAGE)
6847 curtab->tp_prevdir = pdir;
6848 else
6849 prev_dir = pdir;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006850
6851#if defined(UNIX) || defined(VMS)
6852 // for UNIX ":cd" means: go to home directory
6853 if (*new_dir == NUL)
6854 {
6855 // use NameBuff for home directory name
6856# ifdef VMS
6857 char_u *p;
6858
6859 p = mch_getenv((char_u *)"SYS$LOGIN");
6860 if (p == NULL || *p == NUL) // empty is the same as not set
6861 NameBuff[0] = NUL;
6862 else
6863 vim_strncpy(NameBuff, p, MAXPATHL - 1);
6864# else
6865 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
6866# endif
6867 new_dir = NameBuff;
6868 }
6869#endif
Bram Moolenaar002bc792020-06-05 22:33:42 +02006870 dir_differs = new_dir == NULL || pdir == NULL
6871 || pathcmp((char *)pdir, (char *)new_dir, -1) != 0;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006872 if (new_dir == NULL || (dir_differs && vim_chdir(new_dir)))
6873 emsg(_(e_failed));
6874 else
6875 {
6876 char_u *acmd_fname;
6877
6878 post_chdir(scope);
6879
6880 if (dir_differs)
6881 {
6882 if (scope == CDSCOPE_WINDOW)
6883 acmd_fname = (char_u *)"window";
6884 else if (scope == CDSCOPE_TABPAGE)
6885 acmd_fname = (char_u *)"tabpage";
6886 else
6887 acmd_fname = (char_u *)"global";
6888 apply_autocmds(EVENT_DIRCHANGED, acmd_fname, new_dir, FALSE,
6889 curbuf);
6890 }
6891 retval = TRUE;
6892 }
6893 vim_free(tofree);
6894
6895 return retval;
6896}
Bram Moolenaarea408852005-06-25 22:49:46 +00006897
Bram Moolenaar071d4272004-06-13 20:20:40 +00006898/*
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006899 * ":cd", ":tcd", ":lcd", ":chdir" ":tchdir" and ":lchdir".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006900 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00006901 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006902ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006903{
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01006904 char_u *new_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006905
6906 new_dir = eap->arg;
6907#if !defined(UNIX) && !defined(VMS)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006908 // for non-UNIX ":cd" means: print current directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006909 if (*new_dir == NUL)
6910 ex_pwd(NULL);
6911 else
6912#endif
6913 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006914 cdscope_T scope = CDSCOPE_GLOBAL;
6915
6916 if (eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir)
6917 scope = CDSCOPE_WINDOW;
6918 else if (eap->cmdidx == CMD_tcd || eap->cmdidx == CMD_tchdir)
6919 scope = CDSCOPE_TABPAGE;
6920
6921 if (changedir_func(new_dir, eap->forceit, scope))
Bram Moolenaardf177f62005-02-22 08:39:57 +00006922 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006923 // Echo the new current directory if the command was typed.
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00006924 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006925 ex_pwd(eap);
6926 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006927 }
6928}
6929
6930/*
6931 * ":pwd".
6932 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006933 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006934ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006935{
6936 if (mch_dirname(NameBuff, MAXPATHL) == OK)
6937 {
6938#ifdef BACKSLASH_IN_FILENAME
6939 slash_adjust(NameBuff);
6940#endif
Bram Moolenaar95058722020-06-01 16:26:19 +02006941 if (p_verbose > 0)
6942 {
6943 char *context = "global";
6944
6945 if (curwin->w_localdir != NULL)
6946 context = "window";
6947 else if (curtab->tp_localdir != NULL)
6948 context = "tabpage";
6949 smsg("[%s] %s", context, (char *)NameBuff);
6950 }
6951 else
6952 msg((char *)NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006953 }
6954 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006955 emsg(_("E187: Unknown"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006956}
6957
6958/*
6959 * ":=".
6960 */
6961 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006962ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006963{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006964 smsg("%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006965 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006966}
6967
6968 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006969ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006970{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006971 int n;
6972 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006973
6974 if (cursor_valid())
6975 {
6976 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
6977 if (n >= 0)
Bram Moolenaar53f81742017-09-22 14:35:51 +02006978 windgoto((int)n, curwin->w_wincol + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006979 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006980
6981 len = eap->line2;
6982 switch (*eap->arg)
6983 {
6984 case 'm': break;
6985 case NUL: len *= 1000L; break;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006986 default: semsg(_(e_invarg2), eap->arg); return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006987 }
6988 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006989}
6990
6991/*
6992 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
6993 */
6994 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006995do_sleep(long msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006996{
Bram Moolenaar52953192019-08-16 10:27:13 +02006997 long done = 0;
Bram Moolenaar975b5272016-03-15 23:10:59 +01006998 long wait_now;
Bram Moolenaar52953192019-08-16 10:27:13 +02006999# ifdef ELAPSED_FUNC
7000 elapsed_T start_tv;
7001
7002 // Remember at what time we started, so that we know how much longer we
7003 // should wait after waiting for a bit.
7004 ELAPSED_INIT(start_tv);
7005# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007006
7007 cursor_on();
Bram Moolenaarf45d7472018-09-30 18:22:26 +02007008 out_flush_cursor(FALSE, FALSE);
Bram Moolenaar52953192019-08-16 10:27:13 +02007009 while (!got_int && done < msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007010 {
Bram Moolenaar975b5272016-03-15 23:10:59 +01007011 wait_now = msec - done > 1000L ? 1000L : msec - done;
7012#ifdef FEAT_TIMERS
7013 {
7014 long due_time = check_due_timer();
7015
7016 if (due_time > 0 && due_time < wait_now)
7017 wait_now = due_time;
7018 }
7019#endif
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007020#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar28e67e02019-08-15 23:05:49 +02007021 if (has_any_channel() && wait_now > 20L)
7022 wait_now = 20L;
7023#endif
7024#ifdef FEAT_SOUND
7025 if (has_any_sound_callback() && wait_now > 20L)
7026 wait_now = 20L;
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007027#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +01007028 ui_delay(wait_now, TRUE);
Bram Moolenaar52953192019-08-16 10:27:13 +02007029
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007030#ifdef FEAT_JOB_CHANNEL
7031 if (has_any_channel())
7032 ui_breakcheck_force(TRUE);
7033 else
7034#endif
7035 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007036#ifdef MESSAGE_QUEUE
Bram Moolenaar52953192019-08-16 10:27:13 +02007037 // Process the netbeans and clientserver messages that may have been
7038 // received in the call to ui_breakcheck() when the GUI is in use. This
7039 // may occur when running a test case.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007040 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02007041#endif
Bram Moolenaar52953192019-08-16 10:27:13 +02007042
7043# ifdef ELAPSED_FUNC
7044 // actual time passed
7045 done = ELAPSED_FUNC(start_tv);
7046# else
7047 // guestimate time passed (will actually be more)
7048 done += wait_now;
7049# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007050 }
Bram Moolenaar1ebff3d2018-07-07 16:18:13 +02007051
7052 // If CTRL-C was typed to interrupt the sleep, drop the CTRL-C from the
7053 // input buffer, otherwise a following call to input() fails.
7054 if (got_int)
7055 (void)vpeekc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007056}
7057
Bram Moolenaar071d4272004-06-13 20:20:40 +00007058/*
7059 * ":winsize" command (obsolete).
7060 */
7061 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007062ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007063{
7064 int w, h;
7065 char_u *arg = eap->arg;
7066 char_u *p;
7067
7068 w = getdigits(&arg);
7069 arg = skipwhite(arg);
7070 p = arg;
7071 h = getdigits(&arg);
7072 if (*p != NUL && *arg == NUL)
7073 set_shellsize(w, h, TRUE);
7074 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007075 emsg(_("E465: :winsize requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007076}
7077
Bram Moolenaar071d4272004-06-13 20:20:40 +00007078 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007079ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007080{
7081 int xchar = NUL;
7082 char_u *p;
7083
7084 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
7085 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007086 // CTRL-W g and CTRL-W CTRL-G have an extra command character
Bram Moolenaar071d4272004-06-13 20:20:40 +00007087 if (eap->arg[1] == NUL)
7088 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007089 emsg(_(e_invarg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007090 return;
7091 }
7092 xchar = eap->arg[1];
7093 p = eap->arg + 2;
7094 }
7095 else
7096 p = eap->arg + 1;
7097
7098 eap->nextcmd = check_nextcmd(p);
7099 p = skipwhite(p);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02007100 if (*p != NUL && *p != (
7101#ifdef FEAT_EVAL
7102 in_vim9script() ? '#' :
7103#endif
7104 '"')
7105 && eap->nextcmd == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007106 emsg(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02007107 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007108 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007109 // Pass flags on for ":vertical wincmd ]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007110 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00007111 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007112 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
7113 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00007114 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007115 }
7116}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007117
Bram Moolenaar843ee412004-06-30 16:16:41 +00007118#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007119/*
7120 * ":winpos".
7121 */
7122 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007123ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007124{
7125 int x, y;
7126 char_u *arg = eap->arg;
7127 char_u *p;
7128
7129 if (*arg == NUL)
7130 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00007131# if defined(FEAT_GUI) || defined(MSWIN)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007132# ifdef VIMDLL
7133 if (gui.in_use ? gui_mch_get_winpos(&x, &y) != FAIL :
7134 mch_get_winpos(&x, &y) != FAIL)
7135# elif defined(FEAT_GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007136 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00007137# else
7138 if (mch_get_winpos(&x, &y) != FAIL)
7139# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007140 {
7141 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
Bram Moolenaar32526b32019-01-19 17:43:09 +01007142 msg((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007143 }
7144 else
7145# endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007146 emsg(_("E188: Obtaining window position not implemented for this platform"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007147 }
7148 else
7149 {
7150 x = getdigits(&arg);
7151 arg = skipwhite(arg);
7152 p = arg;
7153 y = getdigits(&arg);
7154 if (*p == NUL || *arg != NUL)
7155 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007156 emsg(_("E466: :winpos requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007157 return;
7158 }
7159# ifdef FEAT_GUI
7160 if (gui.in_use)
7161 gui_mch_set_winpos(x, y);
7162 else if (gui.starting)
7163 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007164 // Remember the coordinates for when the window is opened.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007165 gui_win_x = x;
7166 gui_win_y = y;
7167 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007168# if defined(HAVE_TGETENT) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007169 else
7170# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007171# endif
7172# if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar843ee412004-06-30 16:16:41 +00007173 mch_set_winpos(x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007174# endif
7175# ifdef HAVE_TGETENT
7176 if (*T_CWP)
7177 term_set_winpos(x, y);
7178# endif
7179 }
7180}
7181#endif
7182
7183/*
7184 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
7185 */
7186 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007187ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007188{
7189 oparg_T oa;
7190
7191 clear_oparg(&oa);
7192 oa.regname = eap->regname;
7193 oa.start.lnum = eap->line1;
7194 oa.end.lnum = eap->line2;
7195 oa.line_count = eap->line2 - eap->line1 + 1;
7196 oa.motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007197 virtual_op = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007198 if (eap->cmdidx != CMD_yank) // position cursor for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00007199 {
7200 setpcmark();
7201 curwin->w_cursor.lnum = eap->line1;
7202 beginline(BL_SOL | BL_FIX);
7203 }
7204
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007205 if (VIsual_active)
7206 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007207
Bram Moolenaar071d4272004-06-13 20:20:40 +00007208 switch (eap->cmdidx)
7209 {
7210 case CMD_delete:
7211 oa.op_type = OP_DELETE;
7212 op_delete(&oa);
7213 break;
7214
7215 case CMD_yank:
7216 oa.op_type = OP_YANK;
7217 (void)op_yank(&oa, FALSE, TRUE);
7218 break;
7219
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007220 default: // CMD_rshift or CMD_lshift
Bram Moolenaar6e707362013-06-14 19:15:58 +02007221 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007222#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02007223 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
7224#else
7225 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00007226#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02007227 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007228 oa.op_type = OP_RSHIFT;
7229 else
7230 oa.op_type = OP_LSHIFT;
7231 op_shift(&oa, FALSE, eap->amount);
7232 break;
7233 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007234 virtual_op = MAYBE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007235 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007236}
7237
7238/*
7239 * ":put".
7240 */
7241 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007242ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007243{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007244 // ":0put" works like ":1put!".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007245 if (eap->line2 == 0)
7246 {
7247 eap->line2 = 1;
7248 eap->forceit = TRUE;
7249 }
7250 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007251 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
7252 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007253}
7254
7255/*
7256 * Handle ":copy" and ":move".
7257 */
7258 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007259ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007260{
7261 long n;
7262
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02007263 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE, FALSE, 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007264 if (eap->arg == NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00007265 {
7266 eap->nextcmd = NULL;
7267 return;
7268 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00007269 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007270
7271 /*
7272 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
7273 */
7274 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
7275 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02007276 emsg(_(e_invrange));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007277 return;
7278 }
7279
7280 if (eap->cmdidx == CMD_move)
7281 {
7282 if (do_move(eap->line1, eap->line2, n) == FAIL)
7283 return;
7284 }
7285 else
7286 ex_copy(eap->line1, eap->line2, n);
7287 u_clearline();
7288 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007289 ex_may_print(eap);
7290}
7291
7292/*
7293 * Print the current line if flags were given to the Ex command.
7294 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02007295 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007296ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007297{
7298 if (eap->flags != 0)
7299 {
7300 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
7301 (eap->flags & EXFLAG_LIST));
7302 ex_no_reprint = TRUE;
7303 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007304}
7305
7306/*
7307 * ":smagic" and ":snomagic".
7308 */
7309 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007310ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007311{
7312 int magic_save = p_magic;
7313
7314 p_magic = (eap->cmdidx == CMD_smagic);
7315 do_sub(eap);
7316 p_magic = magic_save;
7317}
7318
7319/*
7320 * ":join".
7321 */
7322 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007323ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007324{
7325 curwin->w_cursor.lnum = eap->line1;
7326 if (eap->line1 == eap->line2)
7327 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007328 if (eap->addr_count >= 2) // :2,2join does nothing
Bram Moolenaar071d4272004-06-13 20:20:40 +00007329 return;
7330 if (eap->line2 == curbuf->b_ml.ml_line_count)
7331 {
7332 beep_flush();
7333 return;
7334 }
7335 ++eap->line2;
7336 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02007337 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007338 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007339 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007340}
7341
7342/*
7343 * ":[addr]@r" or ":[addr]*r": execute register
7344 */
7345 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007346ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007347{
7348 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00007349 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007350
7351 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4930a762016-09-11 14:39:53 +02007352 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007353
7354#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007355 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00007356#endif
7357
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007358 // get the register name. No name means to use the previous one
Bram Moolenaar071d4272004-06-13 20:20:40 +00007359 c = *eap->arg;
7360 if (c == NUL || (c == '*' && *eap->cmd == '*'))
7361 c = '@';
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007362 // Put the register in the typeahead buffer with the "silent" flag.
Bram Moolenaard333d1e2006-11-07 17:43:47 +00007363 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
7364 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007365 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007366 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00007367 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007368 else
7369 {
7370 int save_efr = exec_from_reg;
7371
7372 exec_from_reg = TRUE;
7373
7374 /*
7375 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00007376 * Continue until the stuff buffer is empty and all added characters
7377 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007378 */
Bram Moolenaar60462872009-11-03 11:40:19 +00007379 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007380 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
7381
7382 exec_from_reg = save_efr;
7383 }
7384}
7385
7386/*
7387 * ":!".
7388 */
7389 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007390ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007391{
7392 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
7393}
7394
7395/*
7396 * ":undo".
7397 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007398 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01007399ex_undo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007400{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007401 if (eap->addr_count == 1) // :undo 123
Bram Moolenaar730cde92010-06-27 05:18:54 +02007402 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00007403 else
7404 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007405}
7406
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007407#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02007408 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007409ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007410{
7411 char_u hash[UNDO_HASH_SIZE];
7412
7413 u_compute_hash(hash);
7414 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
7415}
7416
Bram Moolenaar6df6f472010-07-18 18:04:50 +02007417 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007418ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007419{
7420 char_u hash[UNDO_HASH_SIZE];
7421
7422 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02007423 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007424}
7425#endif
7426
Bram Moolenaar071d4272004-06-13 20:20:40 +00007427/*
7428 * ":redo".
7429 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007430 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007431ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007432{
7433 u_redo(1);
7434}
7435
7436/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007437 * ":earlier" and ":later".
7438 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007439 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007440ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007441{
7442 long count = 0;
7443 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02007444 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007445 char_u *p = eap->arg;
7446
7447 if (*p == NUL)
7448 count = 1;
7449 else if (isdigit(*p))
7450 {
7451 count = getdigits(&p);
7452 switch (*p)
7453 {
7454 case 's': ++p; sec = TRUE; break;
7455 case 'm': ++p; sec = TRUE; count *= 60; break;
7456 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02007457 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
7458 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007459 }
7460 }
7461
7462 if (*p != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007463 semsg(_(e_invarg2), eap->arg);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007464 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02007465 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
7466 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007467}
7468
7469/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007470 * ":redir": start/stop redirection.
7471 */
7472 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007473ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007474{
7475 char *mode;
7476 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00007477 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007478
Bram Moolenaarba768492016-07-08 15:32:54 +02007479#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02007480 if (redir_execute)
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007481 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007482 emsg(_("E930: Cannot use :redir inside execute()"));
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007483 return;
7484 }
Bram Moolenaarba768492016-07-08 15:32:54 +02007485#endif
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007486
Bram Moolenaar071d4272004-06-13 20:20:40 +00007487 if (STRICMP(eap->arg, "END") == 0)
7488 close_redir();
7489 else
7490 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007491 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007492 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007493 ++arg;
7494 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007495 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007496 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007497 mode = "a";
7498 }
7499 else
7500 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00007501 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007502
7503 close_redir();
7504
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007505 // Expand environment variables and "~/".
Bram Moolenaarca472992005-01-21 11:46:23 +00007506 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007507 if (fname == NULL)
7508 return;
7509#ifdef FEAT_BROWSE
7510 if (cmdmod.browse)
7511 {
7512 char_u *browseFile;
7513
Bram Moolenaar7171abe2004-10-11 10:06:20 +00007514 browseFile = do_browse(BROWSE_SAVE,
7515 (char_u *)_("Save Redirection"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +02007516 fname, NULL, NULL,
7517 (char_u *)_(BROWSE_FILTER_ALL_FILES), curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007518 if (browseFile == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007519 return; // operation cancelled
Bram Moolenaar071d4272004-06-13 20:20:40 +00007520 vim_free(fname);
7521 fname = browseFile;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007522 eap->forceit = TRUE; // since dialog already asked
Bram Moolenaar071d4272004-06-13 20:20:40 +00007523 }
7524#endif
7525
7526 redir_fd = open_exfile(fname, eap->forceit, mode);
7527 vim_free(fname);
7528 }
7529#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00007530 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007531 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007532 // redirect to a register a-z (resp. A-Z for appending)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007533 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00007534 ++arg;
7535 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007536# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00007537 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00007538 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00007539# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00007540 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007541 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007542 redir_reg = *arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007543 if (*arg == '>' && arg[1] == '>') // append
Bram Moolenaard9d30582005-05-18 22:10:28 +00007544 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00007545 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007546 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007547 // Can use both "@a" and "@a>".
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00007548 if (*arg == '>')
7549 arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007550 // Make register empty when not using @A-@Z and the
7551 // command is valid.
Bram Moolenaarc188b882007-10-19 14:20:54 +00007552 if (*arg == NUL && !isupper(redir_reg))
7553 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007554 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00007555 }
7556 if (*arg != NUL)
7557 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007558 redir_reg = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007559 semsg(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007560 }
7561 }
7562 else if (*arg == '=' && arg[1] == '>')
7563 {
7564 int append;
7565
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007566 // redirect to a variable
Bram Moolenaardf177f62005-02-22 08:39:57 +00007567 close_redir();
7568 arg += 2;
7569
7570 if (*arg == '>')
7571 {
7572 ++arg;
7573 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007574 }
7575 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00007576 append = FALSE;
7577
7578 if (var_redir_start(skipwhite(arg), append) == OK)
7579 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007580 }
7581#endif
7582
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007583 // TODO: redirect to a buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00007584
Bram Moolenaar071d4272004-06-13 20:20:40 +00007585 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007586 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007587 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00007588
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007589 // Make sure redirection is not off. Can happen for cmdline completion
7590 // that indirectly invokes a command to catch its output.
Bram Moolenaar29b2d262006-09-10 19:07:28 +00007591 if (redir_fd != NULL
7592#ifdef FEAT_EVAL
7593 || redir_reg || redir_vname
7594#endif
7595 )
7596 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007597}
7598
7599/*
7600 * ":redraw": force redraw
7601 */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01007602 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007603ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007604{
7605 int r = RedrawingDisabled;
7606 int p = p_lz;
7607
7608 RedrawingDisabled = 0;
7609 p_lz = FALSE;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01007610 validate_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007611 update_topline();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007612 update_screen(eap->forceit ? CLEAR : VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007613#ifdef FEAT_TITLE
7614 if (need_maketitle)
7615 maketitle();
7616#endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007617#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
7618# ifdef VIMDLL
7619 if (!gui.in_use)
7620# endif
7621 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01007622#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007623 RedrawingDisabled = r;
7624 p_lz = p;
7625
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007626 // Reset msg_didout, so that a message that's there is overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007627 msg_didout = FALSE;
7628 msg_col = 0;
7629
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007630 // No need to wait after an intentional redraw.
Bram Moolenaar56667a52013-07-17 11:54:28 +02007631 need_wait_return = FALSE;
7632
Bram Moolenaar071d4272004-06-13 20:20:40 +00007633 out_flush();
7634}
7635
7636/*
7637 * ":redrawstatus": force redraw of status line(s)
7638 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007639 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007640ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007641{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007642 int r = RedrawingDisabled;
7643 int p = p_lz;
7644
7645 RedrawingDisabled = 0;
7646 p_lz = FALSE;
7647 if (eap->forceit)
7648 status_redraw_all();
7649 else
7650 status_redraw_curbuf();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007651 update_screen(VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007652 RedrawingDisabled = r;
7653 p_lz = p;
7654 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007655}
7656
Bram Moolenaare12bab32019-01-08 22:02:56 +01007657/*
7658 * ":redrawtabline": force redraw of the tabline
7659 */
7660 static void
7661ex_redrawtabline(exarg_T *eap UNUSED)
7662{
7663 int r = RedrawingDisabled;
7664 int p = p_lz;
7665
7666 RedrawingDisabled = 0;
7667 p_lz = FALSE;
7668
7669 draw_tabline();
7670
7671 RedrawingDisabled = r;
7672 p_lz = p;
7673 out_flush();
7674}
7675
Bram Moolenaar071d4272004-06-13 20:20:40 +00007676 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007677close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007678{
7679 if (redir_fd != NULL)
7680 {
7681 fclose(redir_fd);
7682 redir_fd = NULL;
7683 }
7684#ifdef FEAT_EVAL
7685 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007686 if (redir_vname)
7687 {
7688 var_redir_stop();
7689 redir_vname = 0;
7690 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007691#endif
7692}
7693
Bram Moolenaarf96ae0b2019-07-28 15:21:55 +02007694#if (defined(FEAT_SESSION) || defined(FEAT_EVAL)) || defined(PROTO)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007695 int
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02007696vim_mkdir_emsg(char_u *name, int prot UNUSED)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007697{
7698 if (vim_mkdir(name, prot) != 0)
7699 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007700 semsg(_("E739: Cannot create directory: %s"), name);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007701 return FAIL;
7702 }
7703 return OK;
7704}
7705#endif
7706
Bram Moolenaar071d4272004-06-13 20:20:40 +00007707/*
7708 * Open a file for writing for an Ex command, with some checks.
7709 * Return file descriptor, or NULL on failure.
7710 */
7711 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007712open_exfile(
7713 char_u *fname,
7714 int forceit,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007715 char *mode) // "w" for create new file or "a" for append
Bram Moolenaar071d4272004-06-13 20:20:40 +00007716{
7717 FILE *fd;
7718
7719#ifdef UNIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007720 // with Unix it is possible to open a directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007721 if (mch_isdir(fname))
7722 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007723 semsg(_(e_isadir2), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007724 return NULL;
7725 }
7726#endif
7727 if (!forceit && *mode != 'a' && vim_fexists(fname))
7728 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007729 semsg(_("E189: \"%s\" exists (add ! to override)"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007730 return NULL;
7731 }
7732
7733 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007734 semsg(_("E190: Cannot open \"%s\" for writing"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007735
7736 return fd;
7737}
7738
7739/*
7740 * ":mark" and ":k".
7741 */
7742 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007743ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007744{
7745 pos_T pos;
7746
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007747 if (*eap->arg == NUL) // No argument?
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007748 emsg(_(e_argreq));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007749 else if (eap->arg[1] != NUL) // more than one character?
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02007750 semsg(_(e_trailing_arg), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007751 else
7752 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007753 pos = curwin->w_cursor; // save curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00007754 curwin->w_cursor.lnum = eap->line2;
7755 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007756 if (setmark(*eap->arg) == FAIL) // set mark
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007757 emsg(_("E191: Argument must be a letter or forward/backward quote"));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007758 curwin->w_cursor = pos; // restore curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00007759 }
7760}
7761
7762/*
7763 * Update w_topline, w_leftcol and the cursor position.
7764 */
7765 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007766update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007767{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007768 check_cursor(); // put cursor on valid line
Bram Moolenaar071d4272004-06-13 20:20:40 +00007769 update_topline();
7770 if (!curwin->w_p_wrap)
7771 validate_cursor();
7772 update_curswant();
7773}
7774
Bram Moolenaar071d4272004-06-13 20:20:40 +00007775/*
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007776 * Save the current State and go to Normal mode.
7777 * Return TRUE if the typeahead could be saved.
7778 */
7779 int
7780save_current_state(save_state_T *sst)
7781{
7782 sst->save_msg_scroll = msg_scroll;
7783 sst->save_restart_edit = restart_edit;
7784 sst->save_msg_didout = msg_didout;
7785 sst->save_State = State;
7786 sst->save_insertmode = p_im;
7787 sst->save_finish_op = finish_op;
7788 sst->save_opcount = opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01007789 sst->save_reg_executing = reg_executing;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007790
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007791 msg_scroll = FALSE; // no msg scrolling in Normal mode
7792 restart_edit = 0; // don't go to Insert mode
7793 p_im = FALSE; // don't use 'insertmode'
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007794
7795 /*
7796 * Save the current typeahead. This is required to allow using ":normal"
7797 * from an event handler and makes sure we don't hang when the argument
7798 * ends with half a command.
7799 */
7800 save_typeahead(&sst->tabuf);
7801 return sst->tabuf.typebuf_valid;
7802}
7803
7804 void
7805restore_current_state(save_state_T *sst)
7806{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007807 // Restore the previous typeahead.
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007808 restore_typeahead(&sst->tabuf);
7809
7810 msg_scroll = sst->save_msg_scroll;
7811 restart_edit = sst->save_restart_edit;
7812 p_im = sst->save_insertmode;
7813 finish_op = sst->save_finish_op;
7814 opcount = sst->save_opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01007815 reg_executing = sst->save_reg_executing;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007816 msg_didout |= sst->save_msg_didout; // don't reset msg_didout now
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007817
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007818 // Restore the state (needed when called from a function executed for
7819 // 'indentexpr'). Update the mouse and cursor, they may have changed.
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007820 State = sst->save_State;
7821#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007822 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007823#endif
7824}
7825
7826/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007827 * ":normal[!] {commands}": Execute normal mode commands.
7828 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01007829 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007830ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007831{
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007832 save_state_T save_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007833 char_u *arg = NULL;
7834 int l;
7835 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007836
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00007837 if (ex_normal_lock > 0)
7838 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007839 emsg(_(e_secure));
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00007840 return;
7841 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007842 if (ex_normal_busy >= p_mmd)
7843 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007844 emsg(_("E192: Recursive use of :normal too deep"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007845 return;
7846 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007847
Bram Moolenaar071d4272004-06-13 20:20:40 +00007848 /*
7849 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
7850 * this for the K_SPECIAL leading byte, otherwise special keys will not
7851 * work.
7852 */
7853 if (has_mbyte)
7854 {
7855 int len = 0;
7856
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007857 // Count the number of characters to be escaped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007858 for (p = eap->arg; *p != NUL; ++p)
7859 {
Bram Moolenaar13505972019-01-24 15:04:48 +01007860#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007861 if (*p == CSI) // leadbyte CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007862 len += 2;
Bram Moolenaar13505972019-01-24 15:04:48 +01007863#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007864 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007865 if (*++p == K_SPECIAL // trailbyte K_SPECIAL or CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01007866#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007867 || *p == CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01007868#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007869 )
7870 len += 2;
7871 }
7872 if (len > 0)
7873 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02007874 arg = alloc(STRLEN(eap->arg) + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007875 if (arg != NULL)
7876 {
7877 len = 0;
7878 for (p = eap->arg; *p != NUL; ++p)
7879 {
7880 arg[len++] = *p;
Bram Moolenaar13505972019-01-24 15:04:48 +01007881#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007882 if (*p == CSI)
7883 {
7884 arg[len++] = KS_EXTRA;
7885 arg[len++] = (int)KE_CSI;
7886 }
Bram Moolenaar13505972019-01-24 15:04:48 +01007887#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007888 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007889 {
7890 arg[len++] = *++p;
7891 if (*p == K_SPECIAL)
7892 {
7893 arg[len++] = KS_SPECIAL;
7894 arg[len++] = KE_FILLER;
7895 }
Bram Moolenaar13505972019-01-24 15:04:48 +01007896#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007897 else if (*p == CSI)
7898 {
7899 arg[len++] = KS_EXTRA;
7900 arg[len++] = (int)KE_CSI;
7901 }
Bram Moolenaar13505972019-01-24 15:04:48 +01007902#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007903 }
7904 arg[len] = NUL;
7905 }
7906 }
7907 }
7908 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007909
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007910 ++ex_normal_busy;
7911 if (save_current_state(&save_state))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007912 {
7913 /*
7914 * Repeat the :normal command for each line in the range. When no
7915 * range given, execute it just once, without positioning the cursor
7916 * first.
7917 */
7918 do
7919 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007920 if (eap->addr_count != 0)
7921 {
7922 curwin->w_cursor.lnum = eap->line1++;
7923 curwin->w_cursor.col = 0;
Bram Moolenaard5d37532017-03-27 23:02:07 +02007924 check_cursor_moved(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007925 }
7926
Bram Moolenaar13505972019-01-24 15:04:48 +01007927 exec_normal_cmd(arg != NULL
7928 ? arg
7929 : eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007930 }
7931 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
7932 }
7933
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007934 // Might not return to the main loop when in an event handler.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007935 update_topline_cursor();
7936
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007937 restore_current_state(&save_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007938 --ex_normal_busy;
Bram Moolenaareda73602014-11-05 09:53:23 +01007939 setmouse();
Bram Moolenaareda73602014-11-05 09:53:23 +01007940#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007941 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaareda73602014-11-05 09:53:23 +01007942#endif
7943
Bram Moolenaar071d4272004-06-13 20:20:40 +00007944 vim_free(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007945}
7946
7947/*
Bram Moolenaar64969662005-12-14 21:59:55 +00007948 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007949 */
7950 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007951ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007952{
Bram Moolenaarfd371682005-01-14 21:42:54 +00007953 if (eap->forceit)
7954 {
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007955 // cursor line can be zero on startup
Bram Moolenaar09ca9322017-09-26 17:40:45 +02007956 if (!curwin->w_cursor.lnum)
7957 curwin->w_cursor.lnum = 1;
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007958 set_cursor_for_append_to_line();
Bram Moolenaarfd371682005-01-14 21:42:54 +00007959 }
Bram Moolenaareb58a242020-04-19 18:13:19 +02007960#ifdef FEAT_TERMINAL
7961 // Ignore this when running in an active terminal.
7962 if (term_job_running(curbuf->b_term))
7963 return;
7964#endif
Bram Moolenaarfd371682005-01-14 21:42:54 +00007965
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007966 // Ignore the command when already in Insert mode. Inserting an
7967 // expression register that invokes a function can do this.
Bram Moolenaara40c5002005-01-09 21:16:21 +00007968 if (State & INSERT)
7969 return;
7970
Bram Moolenaar64969662005-12-14 21:59:55 +00007971 if (eap->cmdidx == CMD_startinsert)
7972 restart_edit = 'a';
7973 else if (eap->cmdidx == CMD_startreplace)
7974 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00007975 else
Bram Moolenaar64969662005-12-14 21:59:55 +00007976 restart_edit = 'V';
7977
7978 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007979 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00007980 if (eap->cmdidx == CMD_startinsert)
7981 restart_edit = 'i';
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007982 curwin->w_curswant = 0; // avoid MAXCOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007983 }
7984}
7985
7986/*
7987 * ":stopinsert"
7988 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007989 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007990ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007991{
7992 restart_edit = 0;
7993 stop_insert_mode = TRUE;
Bram Moolenaarfd773e92016-04-02 19:39:16 +02007994 clearmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007995}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007996
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007997/*
7998 * Execute normal mode command "cmd".
7999 * "remap" can be REMAP_NONE or REMAP_YES.
8000 */
8001 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008002exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008003{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008004 // Stuff the argument into the typeahead buffer.
Bram Moolenaar25281632016-01-21 23:32:32 +01008005 ins_typebuf(cmd, remap, 0, TRUE, silent);
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008006 exec_normal(FALSE, FALSE, FALSE);
Bram Moolenaar25281632016-01-21 23:32:32 +01008007}
Bram Moolenaar25281632016-01-21 23:32:32 +01008008
Bram Moolenaar25281632016-01-21 23:32:32 +01008009/*
8010 * Execute normal_cmd() until there is no typeahead left.
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008011 * When "use_vpeekc" is TRUE use vpeekc() to check for available chars.
Bram Moolenaar25281632016-01-21 23:32:32 +01008012 */
8013 void
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008014exec_normal(int was_typed, int use_vpeekc, int may_use_terminal_loop UNUSED)
Bram Moolenaar25281632016-01-21 23:32:32 +01008015{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008016 oparg_T oa;
Bram Moolenaar905dd902019-04-07 14:21:47 +02008017 int c;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008018
Bram Moolenaar905dd902019-04-07 14:21:47 +02008019 // When calling vpeekc() from feedkeys() it will return Ctrl_C when there
8020 // is nothing to get, so also check for Ctrl_C.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008021 clear_oparg(&oa);
8022 finish_op = FALSE;
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008023 while ((!stuff_empty()
8024 || ((was_typed || !typebuf_typed()) && typebuf.tb_len > 0)
Bram Moolenaar905dd902019-04-07 14:21:47 +02008025 || (use_vpeekc && (c = vpeekc()) != NUL && c != Ctrl_C))
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008026 && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008027 {
8028 update_topline_cursor();
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008029#ifdef FEAT_TERMINAL
Bram Moolenaar655a82a2018-05-08 22:01:07 +02008030 if (may_use_terminal_loop && term_use_loop()
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008031 && oa.op_type == OP_NOP && oa.regname == NUL
8032 && !VIsual_active)
8033 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008034 // If terminal_loop() returns OK we got a key that is handled
8035 // in Normal model. With FAIL we first need to position the
8036 // cursor and the screen needs to be redrawn.
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008037 if (terminal_loop(TRUE) == OK)
8038 normal_cmd(&oa, TRUE);
8039 }
8040 else
8041#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008042 // execute a Normal mode cmd
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008043 normal_cmd(&oa, TRUE);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008044 }
8045}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008046
Bram Moolenaar071d4272004-06-13 20:20:40 +00008047#ifdef FEAT_FIND_ID
8048 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008049ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008050{
8051 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
8052 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
8053 (linenr_T)1, (linenr_T)MAXLNUM);
8054}
8055
Bram Moolenaar4033c552017-09-16 20:54:51 +02008056#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008057/*
8058 * ":psearch"
8059 */
8060 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008061ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008062{
8063 g_do_tagpreview = p_pvh;
8064 ex_findpat(eap);
8065 g_do_tagpreview = 0;
8066}
8067#endif
8068
8069 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008070ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008071{
8072 int whole = TRUE;
8073 long n;
8074 char_u *p;
8075 int action;
8076
8077 switch (cmdnames[eap->cmdidx].cmd_name[2])
8078 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008079 case 'e': // ":psearch", ":isearch" and ":dsearch"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008080 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
8081 action = ACTION_GOTO;
8082 else
8083 action = ACTION_SHOW;
8084 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008085 case 'i': // ":ilist" and ":dlist"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008086 action = ACTION_SHOW_ALL;
8087 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008088 case 'u': // ":ijump" and ":djump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008089 action = ACTION_GOTO;
8090 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008091 default: // ":isplit" and ":dsplit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008092 action = ACTION_SPLIT;
8093 break;
8094 }
8095
8096 n = 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008097 if (vim_isdigit(*eap->arg)) // get count
Bram Moolenaar071d4272004-06-13 20:20:40 +00008098 {
8099 n = getdigits(&eap->arg);
8100 eap->arg = skipwhite(eap->arg);
8101 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008102 if (*eap->arg == '/') // Match regexp, not just whole words
Bram Moolenaar071d4272004-06-13 20:20:40 +00008103 {
8104 whole = FALSE;
8105 ++eap->arg;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02008106 p = skip_regexp(eap->arg, '/', p_magic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008107 if (*p)
8108 {
8109 *p++ = NUL;
8110 p = skipwhite(p);
8111
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008112 // Check for trailing illegal characters
Bram Moolenaarfaac4102020-04-20 17:46:14 +02008113 if (!ends_excmd2(eap->arg, p))
Bram Moolenaar8930caa2020-07-23 16:37:03 +02008114 eap->errmsg = ex_errmsg(e_trailing_arg, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008115 else
8116 eap->nextcmd = check_nextcmd(p);
8117 }
8118 }
8119 if (!eap->skip)
8120 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
8121 whole, !eap->forceit,
8122 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
8123 n, action, eap->line1, eap->line2);
8124}
8125#endif
8126
Bram Moolenaar071d4272004-06-13 20:20:40 +00008127
Bram Moolenaar4033c552017-09-16 20:54:51 +02008128#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00008129/*
8130 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
8131 */
8132 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008133ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008134{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008135 g_do_tagpreview = p_pvh; // will be reset to 0 in ex_tag_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00008136 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
8137}
8138
8139/*
8140 * ":pedit"
8141 */
8142 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008143ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008144{
8145 win_T *curwin_save = curwin;
8146
Bram Moolenaar3a2505c2020-03-09 16:40:41 +01008147 if (ERROR_IF_ANY_POPUP_WINDOW)
8148 return;
8149
Bram Moolenaar026587b2019-08-17 15:08:00 +02008150 // Open the preview window or popup and make it the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008151 g_do_tagpreview = p_pvh;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02008152 prepare_tagpreview(TRUE, TRUE, FALSE);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008153
Bram Moolenaar026587b2019-08-17 15:08:00 +02008154 // Edit the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008155 do_exedit(eap, NULL);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008156
Bram Moolenaar071d4272004-06-13 20:20:40 +00008157 if (curwin != curwin_save && win_valid(curwin_save))
8158 {
Bram Moolenaar026587b2019-08-17 15:08:00 +02008159 // Return cursor to where we were
Bram Moolenaar071d4272004-06-13 20:20:40 +00008160 validate_cursor();
8161 redraw_later(VALID);
8162 win_enter(curwin_save, TRUE);
8163 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01008164# ifdef FEAT_PROP_POPUP
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008165 else if (WIN_IS_POPUP(curwin))
8166 {
8167 // can't keep focus in popup window
8168 win_enter(firstwin, TRUE);
8169 }
8170# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008171 g_do_tagpreview = 0;
8172}
Bram Moolenaar4033c552017-09-16 20:54:51 +02008173#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008174
8175/*
8176 * ":stag", ":stselect" and ":stjump".
8177 */
8178 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008179ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008180{
8181 postponed_split = -1;
8182 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008183 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008184 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
8185 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008186 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008187}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008188
8189/*
8190 * ":tag", ":tselect", ":tjump", ":tnext", etc.
8191 */
8192 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008193ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008194{
8195 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
8196}
8197
8198 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008199ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008200{
8201 int cmd;
8202
8203 switch (name[1])
8204 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008205 case 'j': cmd = DT_JUMP; // ":tjump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008206 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008207 case 's': cmd = DT_SELECT; // ":tselect"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008208 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008209 case 'p': cmd = DT_PREV; // ":tprevious"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008210 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008211 case 'N': cmd = DT_PREV; // ":tNext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008212 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008213 case 'n': cmd = DT_NEXT; // ":tnext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008214 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008215 case 'o': cmd = DT_POP; // ":pop"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008216 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008217 case 'f': // ":tfirst"
8218 case 'r': cmd = DT_FIRST; // ":trewind"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008219 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008220 case 'l': cmd = DT_LAST; // ":tlast"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008221 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008222 default: // ":tag"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008223#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +00008224 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008225 {
Bram Moolenaard4db7712016-11-12 19:16:46 +01008226 ex_cstag(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008227 return;
8228 }
8229#endif
8230 cmd = DT_TAG;
8231 break;
8232 }
8233
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00008234 if (name[0] == 'l')
8235 {
8236#ifndef FEAT_QUICKFIX
8237 ex_ni(eap);
8238 return;
8239#else
8240 cmd = DT_LTAG;
8241#endif
8242 }
8243
Bram Moolenaar071d4272004-06-13 20:20:40 +00008244 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
8245 eap->forceit, TRUE);
8246}
8247
8248/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008249 * Check "str" for starting with a special cmdline variable.
8250 * If found return one of the SPEC_ values and set "*usedlen" to the length of
8251 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
8252 */
8253 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008254find_cmdline_var(char_u *src, int *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008255{
8256 int len;
8257 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +00008258 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008259 "%",
8260#define SPEC_PERC 0
8261 "#",
Bram Moolenaar65f08472017-09-10 18:16:20 +02008262#define SPEC_HASH (SPEC_PERC + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008263 "<cword>", // cursor word
Bram Moolenaar65f08472017-09-10 18:16:20 +02008264#define SPEC_CWORD (SPEC_HASH + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008265 "<cWORD>", // cursor WORD
Bram Moolenaar65f08472017-09-10 18:16:20 +02008266#define SPEC_CCWORD (SPEC_CWORD + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008267 "<cexpr>", // expr under cursor
Bram Moolenaar65f08472017-09-10 18:16:20 +02008268#define SPEC_CEXPR (SPEC_CCWORD + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008269 "<cfile>", // cursor path name
Bram Moolenaar65f08472017-09-10 18:16:20 +02008270#define SPEC_CFILE (SPEC_CEXPR + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008271 "<sfile>", // ":so" file name
Bram Moolenaar65f08472017-09-10 18:16:20 +02008272#define SPEC_SFILE (SPEC_CFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008273 "<slnum>", // ":so" file line number
Bram Moolenaar65f08472017-09-10 18:16:20 +02008274#define SPEC_SLNUM (SPEC_SFILE + 1)
Bram Moolenaara5d04232020-07-26 15:37:02 +02008275 "<stack>", // call stack
8276#define SPEC_STACK (SPEC_SLNUM + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008277 "<afile>", // autocommand file name
Bram Moolenaara5d04232020-07-26 15:37:02 +02008278#define SPEC_AFILE (SPEC_STACK + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008279 "<abuf>", // autocommand buffer number
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008280#define SPEC_ABUF (SPEC_AFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008281 "<amatch>", // autocommand match name
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008282#define SPEC_AMATCH (SPEC_ABUF + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008283 "<sflnum>", // script file line number
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008284#define SPEC_SFLNUM (SPEC_AMATCH + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008285#ifdef FEAT_CLIENTSERVER
8286 "<client>"
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008287# define SPEC_CLIENT (SPEC_SFLNUM + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008288#endif
8289 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008290
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008291 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008292 {
8293 len = (int)STRLEN(spec_str[i]);
8294 if (STRNCMP(src, spec_str[i], len) == 0)
8295 {
8296 *usedlen = len;
8297 return i;
8298 }
8299 }
8300 return -1;
8301}
8302
8303/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008304 * Evaluate cmdline variables.
8305 *
8306 * change '%' to curbuf->b_ffname
8307 * '#' to curwin->w_altfile
8308 * '<cword>' to word under the cursor
8309 * '<cWORD>' to WORD under the cursor
Bram Moolenaar8071cb22019-07-12 17:58:01 +02008310 * '<cexpr>' to C-expression under the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00008311 * '<cfile>' to path name under the cursor
8312 * '<sfile>' to sourced file name
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008313 * '<slnum>' to sourced file line number
Bram Moolenaar071d4272004-06-13 20:20:40 +00008314 * '<afile>' to file name for autocommand
8315 * '<abuf>' to buffer number for autocommand
8316 * '<amatch>' to matching name for autocommand
8317 *
8318 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
8319 * "" for error without a message) and NULL is returned.
8320 * Returns an allocated string if a valid match was found.
8321 * Returns NULL if no match was found. "usedlen" then still contains the
8322 * number of characters to skip.
8323 */
8324 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008325eval_vars(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008326 char_u *src, // pointer into commandline
8327 char_u *srcstart, // beginning of valid memory for src
8328 int *usedlen, // characters after src that are used
8329 linenr_T *lnump, // line number for :e command, or NULL
8330 char **errormsg, // pointer to error message
8331 int *escaped) // return value has escaped white space (can
8332 // be NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008333{
8334 int i;
8335 char_u *s;
8336 char_u *result;
8337 char_u *resultbuf = NULL;
8338 int resultlen;
8339 buf_T *buf;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008340 int valid = VALID_HEAD + VALID_PATH; // assume valid result
Bram Moolenaar071d4272004-06-13 20:20:40 +00008341 int spec_idx;
Bram Moolenaarfd249462018-07-28 16:14:30 +02008342 int tilde_file = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008343 int skip_mod = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008344 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008345
8346 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +00008347 if (escaped != NULL)
8348 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008349
8350 /*
8351 * Check if there is something to do.
8352 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008353 spec_idx = find_cmdline_var(src, usedlen);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008354 if (spec_idx < 0) // no match
Bram Moolenaar071d4272004-06-13 20:20:40 +00008355 {
8356 *usedlen = 1;
8357 return NULL;
8358 }
8359
8360 /*
8361 * Skip when preceded with a backslash "\%" and "\#".
8362 * Note: In "\\%" the % is also not recognized!
8363 */
8364 if (src > srcstart && src[-1] == '\\')
8365 {
8366 *usedlen = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008367 STRMOVE(src - 1, src); // remove backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00008368 return NULL;
8369 }
8370
8371 /*
8372 * word or WORD under cursor
8373 */
Bram Moolenaar65f08472017-09-10 18:16:20 +02008374 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD
8375 || spec_idx == SPEC_CEXPR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008376 {
Bram Moolenaar65f08472017-09-10 18:16:20 +02008377 resultlen = find_ident_under_cursor(&result,
8378 spec_idx == SPEC_CWORD ? (FIND_IDENT | FIND_STRING)
8379 : spec_idx == SPEC_CEXPR ? (FIND_IDENT | FIND_STRING | FIND_EVAL)
8380 : FIND_STRING);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008381 if (resultlen == 0)
8382 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008383 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008384 return NULL;
8385 }
8386 }
8387
8388 /*
8389 * '#': Alternate file name
8390 * '%': Current file name
8391 * File name under the cursor
8392 * File name for autocommand
8393 * and following modifiers
8394 */
8395 else
8396 {
8397 switch (spec_idx)
8398 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008399 case SPEC_PERC: // '%': current file
Bram Moolenaar071d4272004-06-13 20:20:40 +00008400 if (curbuf->b_fname == NULL)
8401 {
8402 result = (char_u *)"";
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008403 valid = 0; // Must have ":p:h" to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00008404 }
8405 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008406 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008407 result = curbuf->b_fname;
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008408 tilde_file = STRCMP(result, "~") == 0;
8409 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008410 break;
8411
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008412 case SPEC_HASH: // '#' or "#99": alternate file
8413 if (src[1] == '#') // "##": the argument list
Bram Moolenaar071d4272004-06-13 20:20:40 +00008414 {
8415 result = arg_all();
8416 resultbuf = result;
8417 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +00008418 if (escaped != NULL)
8419 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008420 skip_mod = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008421 break;
8422 }
8423 s = src + 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008424 if (*s == '<') // "#<99" uses v:oldfiles
Bram Moolenaard812df62008-11-09 12:46:09 +00008425 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008426 i = (int)getdigits(&s);
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008427 if (s == src + 2 && src[1] == '-')
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008428 // just a minus sign, don't skip over it
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008429 s--;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008430 *usedlen = (int)(s - src); // length of what we expand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008431
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008432 if (src[1] == '<' && i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008433 {
Bram Moolenaard812df62008-11-09 12:46:09 +00008434 if (*usedlen < 2)
8435 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008436 // Should we give an error message for #<text?
Bram Moolenaard812df62008-11-09 12:46:09 +00008437 *usedlen = 1;
8438 return NULL;
8439 }
8440#ifdef FEAT_EVAL
8441 result = list_find_str(get_vim_var_list(VV_OLDFILES),
8442 (long)i);
8443 if (result == NULL)
8444 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008445 *errormsg = "";
Bram Moolenaard812df62008-11-09 12:46:09 +00008446 return NULL;
8447 }
8448#else
Bram Moolenaar8e481e82019-01-15 20:07:48 +01008449 *errormsg = _("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008450 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00008451#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008452 }
8453 else
Bram Moolenaard812df62008-11-09 12:46:09 +00008454 {
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008455 if (i == 0 && src[1] == '<' && *usedlen > 1)
8456 *usedlen = 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00008457 buf = buflist_findnr(i);
8458 if (buf == NULL)
8459 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008460 *errormsg = _("E194: No alternate file name to substitute for '#'");
Bram Moolenaard812df62008-11-09 12:46:09 +00008461 return NULL;
8462 }
8463 if (lnump != NULL)
8464 *lnump = ECMD_LAST;
8465 if (buf->b_fname == NULL)
8466 {
8467 result = (char_u *)"";
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008468 valid = 0; // Must have ":p:h" to be valid
Bram Moolenaard812df62008-11-09 12:46:09 +00008469 }
8470 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008471 {
Bram Moolenaard812df62008-11-09 12:46:09 +00008472 result = buf->b_fname;
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008473 tilde_file = STRCMP(result, "~") == 0;
8474 }
Bram Moolenaard812df62008-11-09 12:46:09 +00008475 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008476 break;
8477
8478#ifdef FEAT_SEARCHPATH
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008479 case SPEC_CFILE: // file name under cursor
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008480 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008481 if (result == NULL)
8482 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008483 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008484 return NULL;
8485 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008486 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00008487 break;
8488#endif
8489
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008490 case SPEC_AFILE: // file name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008491 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +00008492 if (result != NULL && !autocmd_fname_full)
8493 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008494 // Still need to turn the fname into a full path. It is
8495 // postponed to avoid a delay when <afile> is not used.
Bram Moolenaarf6dad432008-09-18 19:29:58 +00008496 autocmd_fname_full = TRUE;
8497 result = FullName_save(autocmd_fname, FALSE);
8498 vim_free(autocmd_fname);
8499 autocmd_fname = result;
8500 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008501 if (result == NULL)
8502 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008503 *errormsg = _("E495: no autocommand file name to substitute for \"<afile>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008504 return NULL;
8505 }
Bram Moolenaara0174af2008-01-02 20:08:25 +00008506 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008507 break;
8508
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008509 case SPEC_ABUF: // buffer number for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008510 if (autocmd_bufnr <= 0)
8511 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008512 *errormsg = _("E496: no autocommand buffer number to substitute for \"<abuf>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008513 return NULL;
8514 }
8515 sprintf((char *)strbuf, "%d", autocmd_bufnr);
8516 result = strbuf;
8517 break;
8518
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008519 case SPEC_AMATCH: // match name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008520 result = autocmd_match;
8521 if (result == NULL)
8522 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008523 *errormsg = _("E497: no autocommand match name to substitute for \"<amatch>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008524 return NULL;
8525 }
8526 break;
8527
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008528 case SPEC_SFILE: // file name for ":so" command
Bram Moolenaara5d04232020-07-26 15:37:02 +02008529 case SPEC_STACK: // call stack
8530 result = estack_sfile(spec_idx == SPEC_SFILE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008531 if (result == NULL)
8532 {
Bram Moolenaara5d04232020-07-26 15:37:02 +02008533 *errormsg = spec_idx == SPEC_SFILE
8534 ? _("E498: no :source file name to substitute for \"<sfile>\"")
8535 : _("E489: no call stack to substitute for \"<stack>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008536 return NULL;
8537 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008538 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00008539 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008540
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008541 case SPEC_SLNUM: // line in file for ":so" command
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008542 if (SOURCING_NAME == NULL || SOURCING_LNUM == 0)
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008543 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008544 *errormsg = _("E842: no line number to use for \"<slnum>\"");
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008545 return NULL;
8546 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008547 sprintf((char *)strbuf, "%ld", SOURCING_LNUM);
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008548 result = strbuf;
8549 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008550
8551#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008552 case SPEC_SFLNUM: // line in script file
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008553 if (current_sctx.sc_lnum + SOURCING_LNUM == 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008554 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008555 *errormsg = _("E961: no line number to use for \"<sflnum>\"");
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008556 return NULL;
8557 }
8558 sprintf((char *)strbuf, "%ld",
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008559 (long)(current_sctx.sc_lnum + SOURCING_LNUM));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008560 result = strbuf;
8561 break;
8562#endif
8563
8564#ifdef FEAT_CLIENTSERVER
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008565 case SPEC_CLIENT: // Source of last submitted input
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008566 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
8567 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008568 result = strbuf;
8569 break;
8570#endif
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008571
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02008572 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008573 result = (char_u *)""; // avoid gcc warning
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02008574 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008575 }
8576
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008577 resultlen = (int)STRLEN(result); // length of new string
8578 if (src[*usedlen] == '<') // remove the file name extension
Bram Moolenaar071d4272004-06-13 20:20:40 +00008579 {
8580 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008581 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008582 resultlen = (int)(s - result);
8583 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008584 else if (!skip_mod)
8585 {
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008586 valid |= modify_fname(src, tilde_file, usedlen, &result, &resultbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008587 &resultlen);
8588 if (result == NULL)
8589 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008590 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008591 return NULL;
8592 }
8593 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008594 }
8595
8596 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
8597 {
8598 if (valid != VALID_HEAD + VALID_PATH)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008599 // xgettext:no-c-format
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008600 *errormsg = _("E499: Empty file name for '%' or '#', only works with \":p:h\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008601 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008602 *errormsg = _("E500: Evaluates to an empty string");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008603 result = NULL;
8604 }
8605 else
8606 result = vim_strnsave(result, resultlen);
8607 vim_free(resultbuf);
8608 return result;
8609}
8610
8611/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008612 * Expand the <sfile> string in "arg".
8613 *
8614 * Returns an allocated string, or NULL for any error.
8615 */
8616 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008617expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008618{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008619 char *errormsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008620 int len;
8621 char_u *result;
8622 char_u *newres;
8623 char_u *repl;
8624 int srclen;
8625 char_u *p;
8626
8627 result = vim_strsave(arg);
8628 if (result == NULL)
8629 return NULL;
8630
8631 for (p = result; *p; )
8632 {
8633 if (STRNCMP(p, "<sfile>", 7) != 0)
8634 ++p;
8635 else
8636 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008637 // replace "<sfile>" with the sourced file name, and do ":" stuff
Bram Moolenaar63b92542007-03-27 14:57:09 +00008638 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008639 if (errormsg != NULL)
8640 {
8641 if (*errormsg)
8642 emsg(errormsg);
8643 vim_free(result);
8644 return NULL;
8645 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008646 if (repl == NULL) // no match (cannot happen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008647 {
8648 p += srclen;
8649 continue;
8650 }
8651 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
8652 newres = alloc(len);
8653 if (newres == NULL)
8654 {
8655 vim_free(repl);
8656 vim_free(result);
8657 return NULL;
8658 }
8659 mch_memmove(newres, result, (size_t)(p - result));
8660 STRCPY(newres + (p - result), repl);
8661 len = (int)STRLEN(newres);
8662 STRCAT(newres, p + srclen);
8663 vim_free(repl);
8664 vim_free(result);
8665 result = newres;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008666 p = newres + len; // continue after the match
Bram Moolenaar071d4272004-06-13 20:20:40 +00008667 }
8668 }
8669
8670 return result;
8671}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008672
Bram Moolenaar071d4272004-06-13 20:20:40 +00008673#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00008674/*
Bram Moolenaard9462e32011-04-11 21:35:11 +02008675 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +00008676 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +00008677 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008678 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008679dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008680{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008681 if (fname == NULL)
8682 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +02008683 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008684}
8685#endif
8686
8687/*
8688 * ":behave {mswin,xterm}"
8689 */
8690 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008691ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008692{
8693 if (STRCMP(eap->arg, "mswin") == 0)
8694 {
8695 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
8696 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
8697 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
8698 set_option_value((char_u *)"keymodel", 0L,
8699 (char_u *)"startsel,stopsel", 0);
8700 }
8701 else if (STRCMP(eap->arg, "xterm") == 0)
8702 {
8703 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
8704 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
8705 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
8706 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
8707 }
8708 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008709 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008710}
8711
Bram Moolenaar071d4272004-06-13 20:20:40 +00008712static int filetype_detect = FALSE;
8713static int filetype_plugin = FALSE;
8714static int filetype_indent = FALSE;
8715
8716/*
8717 * ":filetype [plugin] [indent] {on,off,detect}"
8718 * on: Load the filetype.vim file to install autocommands for file types.
8719 * off: Load the ftoff.vim file to remove all autocommands for file types.
8720 * plugin on: load filetype.vim and ftplugin.vim
8721 * plugin off: load ftplugof.vim
8722 * indent on: load filetype.vim and indent.vim
8723 * indent off: load indoff.vim
8724 */
8725 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008726ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008727{
8728 char_u *arg = eap->arg;
8729 int plugin = FALSE;
8730 int indent = FALSE;
8731
8732 if (*eap->arg == NUL)
8733 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008734 // Print current status.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008735 smsg("filetype detection:%s plugin:%s indent:%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00008736 filetype_detect ? "ON" : "OFF",
8737 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
8738 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
8739 return;
8740 }
8741
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008742 // Accept "plugin" and "indent" in any order.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008743 for (;;)
8744 {
8745 if (STRNCMP(arg, "plugin", 6) == 0)
8746 {
8747 plugin = TRUE;
8748 arg = skipwhite(arg + 6);
8749 continue;
8750 }
8751 if (STRNCMP(arg, "indent", 6) == 0)
8752 {
8753 indent = TRUE;
8754 arg = skipwhite(arg + 6);
8755 continue;
8756 }
8757 break;
8758 }
8759 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
8760 {
8761 if (*arg == 'o' || !filetype_detect)
8762 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008763 source_runtime((char_u *)FILETYPE_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008764 filetype_detect = TRUE;
8765 if (plugin)
8766 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008767 source_runtime((char_u *)FTPLUGIN_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008768 filetype_plugin = TRUE;
8769 }
8770 if (indent)
8771 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008772 source_runtime((char_u *)INDENT_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008773 filetype_indent = TRUE;
8774 }
8775 }
8776 if (*arg == 'd')
8777 {
Bram Moolenaar1610d052016-06-09 22:53:01 +02008778 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +00008779 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008780 }
8781 }
8782 else if (STRCMP(arg, "off") == 0)
8783 {
8784 if (plugin || indent)
8785 {
8786 if (plugin)
8787 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008788 source_runtime((char_u *)FTPLUGOF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008789 filetype_plugin = FALSE;
8790 }
8791 if (indent)
8792 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008793 source_runtime((char_u *)INDOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008794 filetype_indent = FALSE;
8795 }
8796 }
8797 else
8798 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008799 source_runtime((char_u *)FTOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008800 filetype_detect = FALSE;
8801 }
8802 }
8803 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008804 semsg(_(e_invarg2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008805}
8806
8807/*
Bram Moolenaar3e545692017-06-04 19:00:32 +02008808 * ":setfiletype [FALLBACK] {name}"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008809 */
8810 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008811ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008812{
8813 if (!did_filetype)
Bram Moolenaar3e545692017-06-04 19:00:32 +02008814 {
8815 char_u *arg = eap->arg;
8816
8817 if (STRNCMP(arg, "FALLBACK ", 9) == 0)
8818 arg += 9;
8819
8820 set_option_value((char_u *)"filetype", 0L, arg, OPT_LOCAL);
8821 if (arg != eap->arg)
8822 did_filetype = FALSE;
8823 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008824}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008825
Bram Moolenaar071d4272004-06-13 20:20:40 +00008826 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008827ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008828{
8829#ifdef FEAT_DIGRAPHS
8830 if (*eap->arg != NUL)
8831 putdigraph(eap->arg);
8832 else
Bram Moolenaareae8ae12018-12-14 18:53:02 +01008833 listdigraphs(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008834#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008835 emsg(_("E196: No digraphs in this version"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008836#endif
8837}
8838
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02008839#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
8840 void
8841set_no_hlsearch(int flag)
8842{
8843 no_hlsearch = flag;
8844# ifdef FEAT_EVAL
8845 set_vim_var_nr(VV_HLSEARCH, !no_hlsearch && p_hls);
8846# endif
8847}
8848
Bram Moolenaar071d4272004-06-13 20:20:40 +00008849/*
8850 * ":nohlsearch"
8851 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008852 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008853ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008854{
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02008855 set_no_hlsearch(TRUE);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00008856 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008857}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008858#endif
8859
8860#ifdef FEAT_CRYPT
8861/*
8862 * ":X": Get crypt key
8863 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008864 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008865ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008866{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +01008867 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02008868 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008869}
8870#endif
8871
8872#ifdef FEAT_FOLDING
8873 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008874ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008875{
8876 if (foldManualAllowed(TRUE))
8877 foldCreate(eap->line1, eap->line2);
8878}
8879
8880 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008881ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008882{
8883 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
8884 eap->forceit, FALSE);
8885}
8886
8887 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008888ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008889{
8890 linenr_T lnum;
8891
Bram Moolenaar4facea32019-10-12 20:17:40 +02008892# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02008893 start_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +02008894# endif
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02008895
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008896 // First set the marks for all lines closed/open.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008897 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
8898 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
8899 ml_setmarked(lnum);
8900
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008901 // Execute the command on the marked lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008902 global_exe(eap->arg);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008903 ml_clearmarked(); // clear rest of the marks
Bram Moolenaar4facea32019-10-12 20:17:40 +02008904# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02008905 end_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +02008906# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008907}
8908#endif
Bram Moolenaarf5291f32017-09-14 22:55:37 +02008909
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008910#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar39665952018-08-15 20:59:48 +02008911/*
8912 * Returns TRUE if the supplied Ex cmdidx is for a location list command
8913 * instead of a quickfix command.
8914 */
8915 int
8916is_loclist_cmd(int cmdidx)
8917{
Bram Moolenaar74c8be22018-08-23 22:51:40 +02008918 if (cmdidx < 0 || cmdidx >= CMD_SIZE)
Bram Moolenaar39665952018-08-15 20:59:48 +02008919 return FALSE;
8920 return cmdnames[cmdidx].cmd_name[0] == 'l';
8921}
8922#endif
8923
Bram Moolenaar4facea32019-10-12 20:17:40 +02008924#if defined(FEAT_TIMERS) || defined(PROTO)
Bram Moolenaarf5291f32017-09-14 22:55:37 +02008925 int
8926get_pressedreturn(void)
8927{
8928 return ex_pressedreturn;
8929}
8930
8931 void
8932set_pressedreturn(int val)
8933{
8934 ex_pressedreturn = val;
8935}
8936#endif