blob: bb83eba877691e8c40f44906a0e12ad77112d010 [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 Moolenaarf5be8cd2020-07-17 20:36:00 +02001652 int
1653comment_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 Moolenaar83f37b92020-02-23 14:35:01 +01001711#ifdef FEAT_EVAL
1712 int starts_with_colon;
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
1776 starts_with_quote = vim9script && *ea.cmd == '\'';
1777 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 Moolenaar8071cb22019-07-12 17:58:01 +02002064 if (text_locked() && !(ea.argt & EX_CMDWIN)
Bram Moolenaar958636c2014-10-21 20:01:58 +02002065 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002067 // Command not allowed when editing the command line.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002068 errormsg = _(get_text_locked_msg());
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069 goto doend;
2070 }
Bram Moolenaar379fb762018-08-30 15:58:28 +02002071
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002072 // Disallow editing another buffer when "curbuf_lock" is set.
2073 // Do allow ":checktime" (it is postponed).
2074 // Do allow ":edit" (check for an argument later).
2075 // Do allow ":file" with no arguments (check for an argument later).
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002076 if (!(ea.argt & EX_CMDWIN)
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002077 && ea.cmdidx != CMD_checktime
Bram Moolenaar379fb762018-08-30 15:58:28 +02002078 && ea.cmdidx != CMD_edit
2079 && ea.cmdidx != CMD_file
Bram Moolenaar958636c2014-10-21 20:01:58 +02002080 && !IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002081 && curbuf_locked())
2082 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002083
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002084 if (!ni && !(ea.argt & EX_RANGE) && ea.addr_count > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002085 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002086 // no range allowed
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002087 errormsg = _(e_norange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002088 goto doend;
2089 }
2090 }
2091
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002092 if (!ni && !(ea.argt & EX_BANG) && ea.forceit) // no <!> allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002093 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002094 errormsg = _(e_nobang);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002095 goto doend;
2096 }
2097
2098 /*
2099 * Don't complain about the range if it is not used
2100 * (could happen if line_count is accidentally set to 0).
2101 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002102 if (!ea.skip && !ni && (ea.argt & EX_RANGE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002103 {
2104 /*
2105 * If the range is backwards, ask for confirmation and, if given, swap
2106 * ea.line1 & ea.line2 so it's forwards again.
2107 * When global command is busy, don't ask, will fail below.
2108 */
2109 if (!global_busy && ea.line1 > ea.line2)
2110 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002111 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002112 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002113 if (sourcing || exmode_active)
2114 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002115 errormsg = _("E493: Backwards range given");
Bram Moolenaara5792f52005-11-23 21:25:05 +00002116 goto doend;
2117 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118 if (ask_yesno((char_u *)
2119 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002120 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002121 }
2122 lnum = ea.line1;
2123 ea.line1 = ea.line2;
2124 ea.line2 = lnum;
2125 }
2126 if ((errormsg = invalid_range(&ea)) != NULL)
2127 goto doend;
2128 }
2129
Bram Moolenaarb7316892019-05-01 18:08:42 +02002130 if ((ea.addr_type == ADDR_OTHER) && ea.addr_count == 0)
2131 // default is 1, not cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132 ea.line2 = 1;
2133
2134 correct_range(&ea);
2135
2136#ifdef FEAT_FOLDING
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002137 if (((ea.argt & EX_WHOLEFOLD) || ea.addr_count >= 2) && !global_busy
Bram Moolenaara3306952016-01-02 21:41:06 +01002138 && ea.addr_type == ADDR_LINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002139 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002140 // Put the first line at the start of a closed fold, put the last line
2141 // at the end of a closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142 (void)hasFolding(ea.line1, &ea.line1, NULL);
2143 (void)hasFolding(ea.line2, NULL, &ea.line2);
2144 }
2145#endif
2146
2147#ifdef FEAT_QUICKFIX
2148 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002149 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002150 * option here, so things like % get expanded.
2151 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002152 p = replace_makeprg(&ea, p, cmdlinep);
2153 if (p == NULL)
2154 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155#endif
2156
2157 /*
2158 * Skip to start of argument.
2159 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2160 */
2161 if (ea.cmdidx == CMD_bang)
2162 ea.arg = p;
2163 else
2164 ea.arg = skipwhite(p);
2165
Bram Moolenaar379fb762018-08-30 15:58:28 +02002166 // ":file" cannot be run with an argument when "curbuf_lock" is set
2167 if (ea.cmdidx == CMD_file && *ea.arg != NUL && curbuf_locked())
2168 goto doend;
2169
Bram Moolenaar071d4272004-06-13 20:20:40 +00002170 /*
2171 * Check for "++opt=val" argument.
2172 * Must be first, allow ":w ++enc=utf8 !cmd"
2173 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002174 if (ea.argt & EX_ARGOPT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2176 if (getargopt(&ea) == FAIL && !ni)
2177 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002178 errormsg = _(e_invarg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002179 goto doend;
2180 }
2181
2182 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2183 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002184 if (*ea.arg == '>') // append
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002186 if (*++ea.arg != '>') // typed wrong
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002188 errormsg = _("E494: Use w or w>>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002189 goto doend;
2190 }
2191 ea.arg = skipwhite(ea.arg + 1);
2192 ea.append = TRUE;
2193 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002194 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) // :w !filter
Bram Moolenaar071d4272004-06-13 20:20:40 +00002195 {
2196 ++ea.arg;
2197 ea.usefilter = TRUE;
2198 }
2199 }
2200
2201 if (ea.cmdidx == CMD_read)
2202 {
2203 if (ea.forceit)
2204 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002205 ea.usefilter = TRUE; // :r! filter if ea.forceit
Bram Moolenaar071d4272004-06-13 20:20:40 +00002206 ea.forceit = FALSE;
2207 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002208 else if (*ea.arg == '!') // :r !filter
Bram Moolenaar071d4272004-06-13 20:20:40 +00002209 {
2210 ++ea.arg;
2211 ea.usefilter = TRUE;
2212 }
2213 }
2214
2215 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2216 {
2217 ea.amount = 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002218 while (*ea.arg == *ea.cmd) // count number of '>' or '<'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002219 {
2220 ++ea.arg;
2221 ++ea.amount;
2222 }
2223 ea.arg = skipwhite(ea.arg);
2224 }
2225
2226 /*
2227 * Check for "+command" argument, before checking for next command.
2228 * Don't do this for ":read !cmd" and ":write !cmd".
2229 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002230 if ((ea.argt & EX_CMDARG) && !ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2232
2233 /*
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002234 * Check for '|' to separate commands and '"' or '#' to start comments.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002235 * Don't do this for ":read !cmd" and ":write !cmd".
2236 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002237 if ((ea.argt & EX_TRLBAR) && !ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002238 separate_nextcmd(&ea);
2239
2240 /*
2241 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002242 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2243 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002244 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002245 else if (ea.cmdidx == CMD_bang
Bram Moolenaar67883b42017-07-27 22:57:00 +02002246 || ea.cmdidx == CMD_terminal
Bram Moolenaardf177f62005-02-22 08:39:57 +00002247 || ea.cmdidx == CMD_global
2248 || ea.cmdidx == CMD_vglobal
2249 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002250 {
2251 for (p = ea.arg; *p; ++p)
2252 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002253 // Remove one backslash before a newline, so that it's possible to
2254 // pass a newline to the shell and also a newline that is preceded
2255 // with a backslash. This makes it impossible to end a shell
2256 // command in a backslash, but that doesn't appear useful.
2257 // Halving the number of backslashes is incompatible with previous
2258 // versions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002260 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002261 else if (*p == '\n')
2262 {
2263 ea.nextcmd = p + 1;
2264 *p = NUL;
2265 break;
2266 }
2267 }
2268 }
2269
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002270 if ((ea.argt & EX_DFLALL) && ea.addr_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271 {
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002272 buf_T *buf;
2273
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274 ea.line1 = 1;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002275 switch (ea.addr_type)
2276 {
2277 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02002278 case ADDR_OTHER:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002279 ea.line2 = curbuf->b_ml.ml_line_count;
2280 break;
2281 case ADDR_LOADED_BUFFERS:
2282 buf = firstbuf;
2283 while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL)
2284 buf = buf->b_next;
2285 ea.line1 = buf->b_fnum;
2286 buf = lastbuf;
2287 while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL)
2288 buf = buf->b_prev;
2289 ea.line2 = buf->b_fnum;
2290 break;
2291 case ADDR_BUFFERS:
2292 ea.line1 = firstbuf->b_fnum;
2293 ea.line2 = lastbuf->b_fnum;
2294 break;
2295 case ADDR_WINDOWS:
2296 ea.line2 = LAST_WIN_NR;
2297 break;
2298 case ADDR_TABS:
2299 ea.line2 = LAST_TAB_NR;
2300 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01002301 case ADDR_TABS_RELATIVE:
2302 ea.line2 = 1;
2303 break;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002304 case ADDR_ARGUMENTS:
2305 if (ARGCOUNT == 0)
2306 ea.line1 = ea.line2 = 0;
2307 else
2308 ea.line2 = ARGCOUNT;
2309 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02002310 case ADDR_QUICKFIX_VALID:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002311#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02002312 ea.line2 = qf_get_valid_size(&ea);
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002313 if (ea.line2 == 0)
2314 ea.line2 = 1;
Bram Moolenaare906c502015-09-09 21:10:39 +02002315#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002316 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02002317 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02002318 case ADDR_UNSIGNED:
2319 case ADDR_QUICKFIX:
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002320 iemsg(_("INTERNAL: Cannot use EX_DFLALL with ADDR_NONE, ADDR_UNSIGNED or ADDR_QUICKFIX"));
Bram Moolenaarb7316892019-05-01 18:08:42 +02002321 break;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002322 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323 }
2324
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002325 // accept numbered register only when no count allowed (:put)
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002326 if ( (ea.argt & EX_REGSTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327 && *ea.arg != NUL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002328 // Do not allow register = for user commands
Bram Moolenaar958636c2014-10-21 20:01:58 +02002329 && (!IS_USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002330 && !((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002331 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002332#ifndef FEAT_CLIPBOARD
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002333 // check these explicitly for a more specific error message
Bram Moolenaar85de2062011-05-05 14:26:41 +02002334 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002335 {
Bram Moolenaar99b12722019-01-14 20:16:40 +01002336 errormsg = _(e_invalidreg);
Bram Moolenaar85de2062011-05-05 14:26:41 +02002337 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002338 }
2339#endif
Bram Moolenaar958636c2014-10-21 20:01:58 +02002340 if (valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2341 && !IS_USER_CMDIDX(ea.cmdidx))))
Bram Moolenaar85de2062011-05-05 14:26:41 +02002342 {
2343 ea.regname = *ea.arg++;
2344#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002345 // for '=' register: accept the rest of the line as an expression
Bram Moolenaar85de2062011-05-05 14:26:41 +02002346 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2347 {
2348 set_expr_line(vim_strsave(ea.arg));
2349 ea.arg += STRLEN(ea.arg);
2350 }
2351#endif
2352 ea.arg = skipwhite(ea.arg);
2353 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002354 }
2355
2356 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002357 * Check for a count. When accepting a EX_BUFNAME, don't use "123foo" as a
Bram Moolenaar071d4272004-06-13 20:20:40 +00002358 * count, it's a buffer name.
2359 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002360 if ((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)
2361 && (!(ea.argt & EX_BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01002362 || VIM_ISWHITE(*p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363 {
2364 n = getdigits(&ea.arg);
2365 ea.arg = skipwhite(ea.arg);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002366 if (n <= 0 && !ni && (ea.argt & EX_ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002368 errormsg = _(e_zerocount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369 goto doend;
2370 }
Bram Moolenaarb7316892019-05-01 18:08:42 +02002371 if (ea.addr_type != ADDR_LINES) // e.g. :buffer 2, :sleep 3
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372 {
2373 ea.line2 = n;
2374 if (ea.addr_count == 0)
2375 ea.addr_count = 1;
2376 }
2377 else
2378 {
2379 ea.line1 = ea.line2;
2380 ea.line2 += n - 1;
2381 ++ea.addr_count;
2382 /*
2383 * Be vi compatible: no error message for out of range.
2384 */
Bram Moolenaarb7316892019-05-01 18:08:42 +02002385 if (ea.line2 > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002386 ea.line2 = curbuf->b_ml.ml_line_count;
2387 }
2388 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002389
2390 /*
2391 * Check for flags: 'l', 'p' and '#'.
2392 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002393 if (ea.argt & EX_FLAGS)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002394 get_flags(&ea);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002395 if (!ni && !(ea.argt & EX_EXTRA) && *ea.arg != NUL
2396 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & EX_TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002398 // no arguments allowed but there is something
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002399 errormsg = _(e_trailing);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400 goto doend;
2401 }
2402
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002403 if (!ni && (ea.argt & EX_NEEDARG) && *ea.arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002404 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002405 errormsg = _(e_argreq);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406 goto doend;
2407 }
2408
2409#ifdef FEAT_EVAL
2410 /*
2411 * Skip the command when it's not going to be executed.
2412 * The commands like :if, :endif, etc. always need to be executed.
2413 * Also make an exception for commands that handle a trailing command
2414 * themselves.
2415 */
2416 if (ea.skip)
2417 {
2418 switch (ea.cmdidx)
2419 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002420 // commands that need evaluation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421 case CMD_while:
2422 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002423 case CMD_for:
2424 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425 case CMD_if:
2426 case CMD_elseif:
2427 case CMD_else:
2428 case CMD_endif:
2429 case CMD_try:
2430 case CMD_catch:
2431 case CMD_finally:
2432 case CMD_endtry:
2433 case CMD_function:
Bram Moolenaarab55c682020-03-01 19:41:43 +01002434 case CMD_def:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435 break;
2436
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002437 // Commands that handle '|' themselves. Check: A command should
2438 // either have the EX_TRLBAR flag, appear in this list or appear in
2439 // the list at ":help :bar".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002440 case CMD_aboveleft:
2441 case CMD_and:
2442 case CMD_belowright:
2443 case CMD_botright:
2444 case CMD_browse:
2445 case CMD_call:
2446 case CMD_confirm:
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01002447 case CMD_const:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002448 case CMD_delfunction:
2449 case CMD_djump:
2450 case CMD_dlist:
2451 case CMD_dsearch:
2452 case CMD_dsplit:
2453 case CMD_echo:
2454 case CMD_echoerr:
2455 case CMD_echomsg:
2456 case CMD_echon:
Bram Moolenaara76b3152020-02-16 16:20:21 +01002457 case CMD_eval:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002458 case CMD_execute:
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002459 case CMD_filter:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002460 case CMD_help:
2461 case CMD_hide:
2462 case CMD_ijump:
2463 case CMD_ilist:
2464 case CMD_isearch:
2465 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002466 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002467 case CMD_keepjumps:
2468 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01002469 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002470 case CMD_leftabove:
2471 case CMD_let:
2472 case CMD_lockmarks:
Bram Moolenaarcc4423a2019-11-26 17:05:00 +01002473 case CMD_lockvar:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002474 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002475 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002476 case CMD_mzscheme:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01002477 case CMD_noautocmd:
2478 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002479 case CMD_perl:
2480 case CMD_psearch:
2481 case CMD_python:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002482 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002483 case CMD_python3:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484 case CMD_return:
2485 case CMD_rightbelow:
2486 case CMD_ruby:
2487 case CMD_silent:
2488 case CMD_smagic:
2489 case CMD_snomagic:
2490 case CMD_substitute:
2491 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002492 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493 case CMD_tcl:
2494 case CMD_throw:
2495 case CMD_tilde:
2496 case CMD_topleft:
2497 case CMD_unlet:
Bram Moolenaarcc4423a2019-11-26 17:05:00 +01002498 case CMD_unlockvar:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002499 case CMD_verbose:
2500 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002501 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002502 break;
2503
2504 default: goto doend;
2505 }
2506 }
2507#endif
2508
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002509 if (ea.argt & EX_XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002510 {
2511 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2512 goto doend;
2513 }
2514
Bram Moolenaar071d4272004-06-13 20:20:40 +00002515 /*
2516 * Accept buffer name. Cannot be used at the same time with a buffer
2517 * number. Don't do this for a user command.
2518 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002519 if ((ea.argt & EX_BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
Bram Moolenaar958636c2014-10-21 20:01:58 +02002520 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002521 {
2522 /*
2523 * :bdelete, :bwipeout and :bunload take several arguments, separated
2524 * by spaces: find next space (skipping over escaped characters).
2525 * The others take one argument: ignore trailing spaces.
2526 */
2527 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2528 || ea.cmdidx == CMD_bunload)
2529 p = skiptowhite_esc(ea.arg);
2530 else
2531 {
2532 p = ea.arg + STRLEN(ea.arg);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002533 while (p > ea.arg && VIM_ISWHITE(p[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002534 --p;
2535 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002536 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & EX_BUFUNL) != 0,
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002537 FALSE, FALSE);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002538 if (ea.line2 < 0) // failed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002539 goto doend;
2540 ea.addr_count = 1;
2541 ea.arg = skipwhite(p);
2542 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002543
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002544 // The :try command saves the emsg_silent flag, reset it here when
2545 // ":silent! try" was used, it should only apply to :try itself.
Bram Moolenaareffed932018-08-14 13:38:17 +02002546 if (ea.cmdidx == CMD_try && ea.did_esilent > 0)
Bram Moolenaar2be57332018-02-13 18:05:18 +01002547 {
Bram Moolenaareffed932018-08-14 13:38:17 +02002548 emsg_silent -= ea.did_esilent;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002549 if (emsg_silent < 0)
2550 emsg_silent = 0;
Bram Moolenaareffed932018-08-14 13:38:17 +02002551 ea.did_esilent = 0;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002552 }
2553
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554/*
Bram Moolenaar2be57332018-02-13 18:05:18 +01002555 * 7. Execute the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002556 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002557
Bram Moolenaar958636c2014-10-21 20:01:58 +02002558 if (IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002559 {
2560 /*
2561 * Execute a user-defined command.
2562 */
2563 do_ucmd(&ea);
2564 }
2565 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002566 {
2567 /*
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002568 * Call the function to execute the builtin command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569 */
2570 ea.errmsg = NULL;
2571 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2572 if (ea.errmsg != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002573 errormsg = _(ea.errmsg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002574 }
2575
2576#ifdef FEAT_EVAL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002577 // Set flag that any command was executed, used by ex_vim9script().
Bram Moolenaar16531552020-02-08 16:00:46 +01002578 if (getline_equal(ea.getline, ea.cookie, getsourceline)
2579 && current_sctx.sc_sid > 0)
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002580 SCRIPT_ITEM(current_sctx.sc_sid)->sn_had_command = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002581
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582 /*
2583 * If the command just executed called do_cmdline(), any throw or ":return"
2584 * or ":finish" encountered there must also check the cstack of the still
2585 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2586 * exception, or reanimate a returned function or finished script file and
2587 * return or finish it again.
2588 */
2589 if (need_rethrow)
2590 do_throw(cstack);
2591 else if (check_cstack)
2592 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002593 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002594 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002595 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002596 && current_func_returned())
2597 do_return(&ea, TRUE, FALSE, NULL);
2598 }
2599 need_rethrow = check_cstack = FALSE;
2600#endif
2601
2602doend:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002603 if (curwin->w_cursor.lnum == 0) // can happen with zero line number
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002604 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002605 curwin->w_cursor.lnum = 1;
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002606 curwin->w_cursor.col = 0;
2607 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002608
2609 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2610 {
2611 if (sourcing)
2612 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002613 if (errormsg != (char *)IObuff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002614 {
2615 STRCPY(IObuff, errormsg);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002616 errormsg = (char *)IObuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002618 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002619 }
2620 emsg(errormsg);
2621 }
2622#ifdef FEAT_EVAL
2623 do_errthrow(cstack,
Bram Moolenaar958636c2014-10-21 20:01:58 +02002624 (ea.cmdidx != CMD_SIZE && !IS_USER_CMDIDX(ea.cmdidx))
2625 ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002626#endif
2627
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002628 undo_cmdmod(&ea, save_msg_scroll);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002629 cmdmod = save_cmdmod;
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01002630 reg_executing = save_reg_executing;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002632#ifdef HAVE_SANDBOX
Bram Moolenaareffed932018-08-14 13:38:17 +02002633 if (ea.did_sandbox)
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002634 --sandbox;
2635#endif
2636
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002637 if (ea.nextcmd && *ea.nextcmd == NUL) // not really a next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00002638 ea.nextcmd = NULL;
2639
2640#ifdef FEAT_EVAL
2641 --ex_nesting_level;
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002642 vim_free(ea.cmdline_tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643#endif
2644
2645 return ea.nextcmd;
2646}
2647#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00002648 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649#endif
2650
2651/*
Bram Moolenaareffed932018-08-14 13:38:17 +02002652 * Parse and skip over command modifiers:
2653 * - update eap->cmd
2654 * - store flags in "cmdmod".
2655 * - Set ex_pressedreturn for an empty command line.
2656 * - set msg_silent for ":silent"
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002657 * - set 'eventignore' to "all" for ":noautocmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002658 * - set p_verbose for ":verbose"
2659 * - Increment "sandbox" for ":sandbox"
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002660 * When "skip_only" is TRUE the global variables are not changed, except for
2661 * "cmdmod".
Bram Moolenaareffed932018-08-14 13:38:17 +02002662 * Return FAIL when the command is not to be executed.
2663 * May set "errormsg" to an error message.
2664 */
2665 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002666parse_command_modifiers(exarg_T *eap, char **errormsg, int skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002667{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002668 char_u *p;
2669 int starts_with_colon = FALSE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002670
Bram Moolenaara80faa82020-04-12 19:37:17 +02002671 CLEAR_FIELD(cmdmod);
Bram Moolenaareffed932018-08-14 13:38:17 +02002672 eap->verbose_save = -1;
2673 eap->save_msg_silent = -1;
2674
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002675 // Repeat until no more command modifiers are found.
Bram Moolenaareffed932018-08-14 13:38:17 +02002676 for (;;)
2677 {
Bram Moolenaareffed932018-08-14 13:38:17 +02002678 while (*eap->cmd == ' ' || *eap->cmd == '\t' || *eap->cmd == ':')
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002679 {
2680 if (*eap->cmd == ':')
2681 starts_with_colon = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002682 ++eap->cmd;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002683 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002684
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002685 // in ex mode, an empty line works like :+
Bram Moolenaareffed932018-08-14 13:38:17 +02002686 if (*eap->cmd == NUL && exmode_active
2687 && (getline_equal(eap->getline, eap->cookie, getexmodeline)
2688 || getline_equal(eap->getline, eap->cookie, getexline))
2689 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2690 {
2691 eap->cmd = (char_u *)"+";
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002692 if (!skip_only)
2693 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002694 }
2695
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002696 // ignore comment and empty lines
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002697 if (comment_start(eap->cmd, starts_with_colon))
Bram Moolenaareffed932018-08-14 13:38:17 +02002698 return FAIL;
2699 if (*eap->cmd == NUL)
2700 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002701 if (!skip_only)
2702 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002703 return FAIL;
2704 }
2705
Bram Moolenaareffed932018-08-14 13:38:17 +02002706 p = skip_range(eap->cmd, NULL);
2707 switch (*p)
2708 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002709 // When adding an entry, also modify cmd_exists().
Bram Moolenaareffed932018-08-14 13:38:17 +02002710 case 'a': if (!checkforcmd(&eap->cmd, "aboveleft", 3))
2711 break;
2712 cmdmod.split |= WSP_ABOVE;
2713 continue;
2714
2715 case 'b': if (checkforcmd(&eap->cmd, "belowright", 3))
2716 {
2717 cmdmod.split |= WSP_BELOW;
2718 continue;
2719 }
2720 if (checkforcmd(&eap->cmd, "browse", 3))
2721 {
2722#ifdef FEAT_BROWSE_CMD
2723 cmdmod.browse = TRUE;
2724#endif
2725 continue;
2726 }
2727 if (!checkforcmd(&eap->cmd, "botright", 2))
2728 break;
2729 cmdmod.split |= WSP_BOT;
2730 continue;
2731
2732 case 'c': if (!checkforcmd(&eap->cmd, "confirm", 4))
2733 break;
2734#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2735 cmdmod.confirm = TRUE;
2736#endif
2737 continue;
2738
2739 case 'k': if (checkforcmd(&eap->cmd, "keepmarks", 3))
2740 {
2741 cmdmod.keepmarks = TRUE;
2742 continue;
2743 }
2744 if (checkforcmd(&eap->cmd, "keepalt", 5))
2745 {
2746 cmdmod.keepalt = TRUE;
2747 continue;
2748 }
2749 if (checkforcmd(&eap->cmd, "keeppatterns", 5))
2750 {
2751 cmdmod.keeppatterns = TRUE;
2752 continue;
2753 }
2754 if (!checkforcmd(&eap->cmd, "keepjumps", 5))
2755 break;
2756 cmdmod.keepjumps = TRUE;
2757 continue;
2758
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002759 case 'f': // only accept ":filter {pat} cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002760 {
2761 char_u *reg_pat;
2762
2763 if (!checkforcmd(&p, "filter", 4)
2764 || *p == NUL || ends_excmd(*p))
2765 break;
2766 if (*p == '!')
2767 {
2768 cmdmod.filter_force = TRUE;
2769 p = skipwhite(p + 1);
2770 if (*p == NUL || ends_excmd(*p))
2771 break;
2772 }
Bram Moolenaar1e624c92020-07-11 14:08:04 +02002773#ifdef FEAT_EVAL
Bram Moolenaarb074e8b2020-07-11 13:40:45 +02002774 // Avoid that "filter(arg)" is recognized.
2775 if (in_vim9script() && !VIM_ISWHITE(*p))
2776 break;
Bram Moolenaar1e624c92020-07-11 14:08:04 +02002777#endif
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002778 if (skip_only)
2779 p = skip_vimgrep_pat(p, NULL, NULL);
2780 else
2781 // NOTE: This puts a NUL after the pattern.
2782 p = skip_vimgrep_pat(p, &reg_pat, NULL);
Bram Moolenaareffed932018-08-14 13:38:17 +02002783 if (p == NULL || *p == NUL)
2784 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002785 if (!skip_only)
2786 {
2787 cmdmod.filter_regmatch.regprog =
Bram Moolenaareffed932018-08-14 13:38:17 +02002788 vim_regcomp(reg_pat, RE_MAGIC);
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002789 if (cmdmod.filter_regmatch.regprog == NULL)
2790 break;
2791 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002792 eap->cmd = p;
2793 continue;
2794 }
2795
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002796 // ":hide" and ":hide | cmd" are not modifiers
Bram Moolenaareffed932018-08-14 13:38:17 +02002797 case 'h': if (p != eap->cmd || !checkforcmd(&p, "hide", 3)
2798 || *p == NUL || ends_excmd(*p))
2799 break;
2800 eap->cmd = p;
2801 cmdmod.hide = TRUE;
2802 continue;
2803
2804 case 'l': if (checkforcmd(&eap->cmd, "lockmarks", 3))
2805 {
2806 cmdmod.lockmarks = TRUE;
2807 continue;
2808 }
2809
2810 if (!checkforcmd(&eap->cmd, "leftabove", 5))
2811 break;
2812 cmdmod.split |= WSP_ABOVE;
2813 continue;
2814
2815 case 'n': if (checkforcmd(&eap->cmd, "noautocmd", 3))
2816 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002817 if (cmdmod.save_ei == NULL && !skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002818 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002819 // Set 'eventignore' to "all". Restore the
2820 // existing option value later.
Bram Moolenaareffed932018-08-14 13:38:17 +02002821 cmdmod.save_ei = vim_strsave(p_ei);
2822 set_string_option_direct((char_u *)"ei", -1,
2823 (char_u *)"all", OPT_FREE, SID_NONE);
2824 }
2825 continue;
2826 }
2827 if (!checkforcmd(&eap->cmd, "noswapfile", 3))
2828 break;
2829 cmdmod.noswapfile = TRUE;
2830 continue;
2831
2832 case 'r': if (!checkforcmd(&eap->cmd, "rightbelow", 6))
2833 break;
2834 cmdmod.split |= WSP_BELOW;
2835 continue;
2836
2837 case 's': if (checkforcmd(&eap->cmd, "sandbox", 3))
2838 {
2839#ifdef HAVE_SANDBOX
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002840 if (!skip_only)
2841 {
2842 if (!eap->did_sandbox)
2843 ++sandbox;
2844 eap->did_sandbox = TRUE;
2845 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002846#endif
2847 continue;
2848 }
2849 if (!checkforcmd(&eap->cmd, "silent", 3))
2850 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002851 if (!skip_only)
2852 {
2853 if (eap->save_msg_silent == -1)
2854 eap->save_msg_silent = msg_silent;
2855 ++msg_silent;
2856 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002857 if (*eap->cmd == '!' && !VIM_ISWHITE(eap->cmd[-1]))
2858 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002859 // ":silent!", but not "silent !cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002860 eap->cmd = skipwhite(eap->cmd + 1);
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002861 if (!skip_only)
2862 {
2863 ++emsg_silent;
2864 ++eap->did_esilent;
2865 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002866 }
2867 continue;
2868
2869 case 't': if (checkforcmd(&p, "tab", 3))
2870 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002871 if (!skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002872 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002873 long tabnr = get_address(eap, &eap->cmd,
2874 ADDR_TABS, eap->skip,
2875 skip_only, FALSE, 1);
2876 if (tabnr == MAXLNUM)
2877 cmdmod.tab = tabpage_index(curtab) + 1;
2878 else
Bram Moolenaareffed932018-08-14 13:38:17 +02002879 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002880 if (tabnr < 0 || tabnr > LAST_TAB_NR)
2881 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002882 *errormsg = _(e_invrange);
Bram Moolenaar548e5982018-12-26 21:45:00 +01002883 return FAIL;
2884 }
2885 cmdmod.tab = tabnr + 1;
Bram Moolenaareffed932018-08-14 13:38:17 +02002886 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002887 }
2888 eap->cmd = p;
2889 continue;
2890 }
2891 if (!checkforcmd(&eap->cmd, "topleft", 2))
2892 break;
2893 cmdmod.split |= WSP_TOP;
2894 continue;
2895
2896 case 'u': if (!checkforcmd(&eap->cmd, "unsilent", 3))
2897 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002898 if (!skip_only)
2899 {
2900 if (eap->save_msg_silent == -1)
2901 eap->save_msg_silent = msg_silent;
2902 msg_silent = 0;
2903 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002904 continue;
2905
2906 case 'v': if (checkforcmd(&eap->cmd, "vertical", 4))
2907 {
2908 cmdmod.split |= WSP_VERT;
2909 continue;
2910 }
2911 if (!checkforcmd(&p, "verbose", 4))
2912 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002913 if (!skip_only)
2914 {
2915 if (eap->verbose_save < 0)
2916 eap->verbose_save = p_verbose;
2917 if (vim_isdigit(*eap->cmd))
2918 p_verbose = atoi((char *)eap->cmd);
2919 else
2920 p_verbose = 1;
2921 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002922 eap->cmd = p;
2923 continue;
2924 }
2925 break;
2926 }
2927
2928 return OK;
2929}
2930
2931/*
Bram Moolenaarb074e8b2020-07-11 13:40:45 +02002932 * Undo and free contents of "cmdmod".
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002933 */
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002934 void
2935undo_cmdmod(exarg_T *eap, int save_msg_scroll)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002936{
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002937 if (eap->verbose_save >= 0)
2938 p_verbose = eap->verbose_save;
2939
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002940 if (cmdmod.save_ei != NULL)
2941 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002942 // Restore 'eventignore' to the value before ":noautocmd".
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002943 set_string_option_direct((char_u *)"ei", -1, cmdmod.save_ei,
2944 OPT_FREE, SID_NONE);
2945 free_string_option(cmdmod.save_ei);
2946 }
2947
2948 if (cmdmod.filter_regmatch.regprog != NULL)
2949 vim_regfree(cmdmod.filter_regmatch.regprog);
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002950
2951 if (eap->save_msg_silent != -1)
2952 {
2953 // messages could be enabled for a serious error, need to check if the
2954 // counters don't become negative
2955 if (!did_emsg || msg_silent > eap->save_msg_silent)
2956 msg_silent = eap->save_msg_silent;
2957 emsg_silent -= eap->did_esilent;
2958 if (emsg_silent < 0)
2959 emsg_silent = 0;
2960 // Restore msg_scroll, it's set by file I/O commands, even when no
2961 // message is actually displayed.
2962 msg_scroll = save_msg_scroll;
2963
2964 // "silent reg" or "silent echo x" inside "redir" leaves msg_col
2965 // somewhere in the line. Put it back in the first column.
2966 if (redirecting())
2967 msg_col = 0;
2968 }
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002969}
2970
2971/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002972 * Parse the address range, if any, in "eap".
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02002973 * May set the last search pattern, unless "silent" is TRUE.
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002974 * Return FAIL and set "errormsg" or return OK.
2975 */
2976 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002977parse_cmd_address(exarg_T *eap, char **errormsg, int silent)
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002978{
2979 int address_count = 1;
2980 linenr_T lnum;
2981
2982 // Repeat for all ',' or ';' separated addresses.
2983 for (;;)
2984 {
2985 eap->line1 = eap->line2;
2986 switch (eap->addr_type)
2987 {
2988 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02002989 case ADDR_OTHER:
Bram Moolenaarb5383b12020-05-18 19:46:48 +02002990 // Default is the cursor line number. Avoid using an invalid
2991 // line number though.
2992 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
2993 eap->line2 = curbuf->b_ml.ml_line_count;
2994 else
2995 eap->line2 = curwin->w_cursor.lnum;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002996 break;
2997 case ADDR_WINDOWS:
2998 eap->line2 = CURRENT_WIN_NR;
2999 break;
3000 case ADDR_ARGUMENTS:
3001 eap->line2 = curwin->w_arg_idx + 1;
3002 if (eap->line2 > ARGCOUNT)
3003 eap->line2 = ARGCOUNT;
3004 break;
3005 case ADDR_LOADED_BUFFERS:
3006 case ADDR_BUFFERS:
3007 eap->line2 = curbuf->b_fnum;
3008 break;
3009 case ADDR_TABS:
3010 eap->line2 = CURRENT_TAB_NR;
3011 break;
3012 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003013 case ADDR_UNSIGNED:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003014 eap->line2 = 1;
3015 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003016 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003017#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02003018 eap->line2 = qf_get_cur_idx(eap);
3019#endif
3020 break;
3021 case ADDR_QUICKFIX_VALID:
3022#ifdef FEAT_QUICKFIX
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003023 eap->line2 = qf_get_cur_valid_idx(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003024#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003025 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003026 case ADDR_NONE:
3027 // Will give an error later if a range is found.
3028 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003029 }
3030 eap->cmd = skipwhite(eap->cmd);
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003031 lnum = get_address(eap, &eap->cmd, eap->addr_type, eap->skip, silent,
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003032 eap->addr_count == 0, address_count++);
3033 if (eap->cmd == NULL) // error detected
3034 return FAIL;
3035 if (lnum == MAXLNUM)
3036 {
3037 if (*eap->cmd == '%') // '%' - all lines
3038 {
3039 ++eap->cmd;
3040 switch (eap->addr_type)
3041 {
3042 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003043 case ADDR_OTHER:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003044 eap->line1 = 1;
3045 eap->line2 = curbuf->b_ml.ml_line_count;
3046 break;
3047 case ADDR_LOADED_BUFFERS:
3048 {
3049 buf_T *buf = firstbuf;
3050
3051 while (buf->b_next != NULL
3052 && buf->b_ml.ml_mfp == NULL)
3053 buf = buf->b_next;
3054 eap->line1 = buf->b_fnum;
3055 buf = lastbuf;
3056 while (buf->b_prev != NULL
3057 && buf->b_ml.ml_mfp == NULL)
3058 buf = buf->b_prev;
3059 eap->line2 = buf->b_fnum;
3060 break;
3061 }
3062 case ADDR_BUFFERS:
3063 eap->line1 = firstbuf->b_fnum;
3064 eap->line2 = lastbuf->b_fnum;
3065 break;
3066 case ADDR_WINDOWS:
3067 case ADDR_TABS:
3068 if (IS_USER_CMDIDX(eap->cmdidx))
3069 {
3070 eap->line1 = 1;
3071 eap->line2 = eap->addr_type == ADDR_WINDOWS
3072 ? LAST_WIN_NR : LAST_TAB_NR;
3073 }
3074 else
3075 {
3076 // there is no Vim command which uses '%' and
3077 // ADDR_WINDOWS or ADDR_TABS
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003078 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003079 return FAIL;
3080 }
3081 break;
3082 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003083 case ADDR_UNSIGNED:
3084 case ADDR_QUICKFIX:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003085 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003086 return FAIL;
3087 case ADDR_ARGUMENTS:
3088 if (ARGCOUNT == 0)
3089 eap->line1 = eap->line2 = 0;
3090 else
3091 {
3092 eap->line1 = 1;
3093 eap->line2 = ARGCOUNT;
3094 }
3095 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003096 case ADDR_QUICKFIX_VALID:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003097#ifdef FEAT_QUICKFIX
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003098 eap->line1 = 1;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003099 eap->line2 = qf_get_valid_size(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003100 if (eap->line2 == 0)
3101 eap->line2 = 1;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003102#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003103 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003104 case ADDR_NONE:
3105 // Will give an error later if a range is found.
3106 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003107 }
3108 ++eap->addr_count;
3109 }
3110 else if (*eap->cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
3111 {
3112 pos_T *fp;
3113
3114 // '*' - visual area
3115 if (eap->addr_type != ADDR_LINES)
3116 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003117 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003118 return FAIL;
3119 }
3120
3121 ++eap->cmd;
3122 if (!eap->skip)
3123 {
3124 fp = getmark('<', FALSE);
3125 if (check_mark(fp) == FAIL)
3126 return FAIL;
3127 eap->line1 = fp->lnum;
3128 fp = getmark('>', FALSE);
3129 if (check_mark(fp) == FAIL)
3130 return FAIL;
3131 eap->line2 = fp->lnum;
3132 ++eap->addr_count;
3133 }
3134 }
3135 }
3136 else
3137 eap->line2 = lnum;
3138 eap->addr_count++;
3139
3140 if (*eap->cmd == ';')
3141 {
3142 if (!eap->skip)
3143 {
3144 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar0e717042020-04-27 19:29:01 +02003145 // Don't leave the cursor on an illegal line or column, but do
3146 // accept zero as address, so 0;/PATTERN/ works correctly.
3147 if (eap->line2 > 0)
3148 check_cursor();
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003149 }
3150 }
3151 else if (*eap->cmd != ',')
3152 break;
3153 ++eap->cmd;
3154 }
3155
3156 // One address given: set start and end lines.
3157 if (eap->addr_count == 1)
3158 {
3159 eap->line1 = eap->line2;
3160 // ... but only implicit: really no address given
3161 if (lnum == MAXLNUM)
3162 eap->addr_count = 0;
3163 }
3164 return OK;
3165}
3166
3167/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003168 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169 * If there is a match advance "pp" to the argument and return TRUE.
3170 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003171 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003172checkforcmd(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003173 char_u **pp, // start of command
3174 char *cmd, // name of command
3175 int len) // required length
Bram Moolenaar071d4272004-06-13 20:20:40 +00003176{
3177 int i;
3178
3179 for (i = 0; cmd[i] != NUL; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003180 if (((char_u *)cmd)[i] != (*pp)[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003181 break;
3182 if (i >= len && !isalpha((*pp)[i]))
3183 {
3184 *pp = skipwhite(*pp + i);
3185 return TRUE;
3186 }
3187 return FALSE;
3188}
3189
3190/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003191 * Append "cmd" to the error message in IObuff.
3192 * Takes care of limiting the length and handling 0xa0, which would be
3193 * invisible otherwise.
3194 */
3195 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003196append_command(char_u *cmd)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003197{
3198 char_u *s = cmd;
3199 char_u *d;
3200
3201 STRCAT(IObuff, ": ");
3202 d = IObuff + STRLEN(IObuff);
3203 while (*s != NUL && d - IObuff < IOSIZE - 7)
3204 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003205 if (enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) : *s == 0xa0)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003206 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003207 s += enc_utf8 ? 2 : 1;
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003208 STRCPY(d, "<a0>");
3209 d += 4;
3210 }
3211 else
3212 MB_COPY_CHAR(s, d);
3213 }
3214 *d = NUL;
3215}
3216
3217/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003219 * Start of the name can be found at eap->cmd.
Bram Moolenaar25190db2019-05-04 15:05:28 +02003220 * Sets eap->cmdidx and returns a pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003221 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003222 *
3223 * If "lookup" is not NULL recognize expression without "eval" or "call" and
3224 * assignment without "let". Sets eap->cmdidx to the command while returning
3225 * "eap->cmd".
3226 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003227 * Returns NULL for an ambiguous user command.
3228 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003229 char_u *
3230find_ex_command(
3231 exarg_T *eap,
Bram Moolenaara494f562020-04-18 17:45:38 +02003232 int *full UNUSED,
Bram Moolenaarb84a3812020-05-01 15:44:29 +02003233 void *(*lookup)(char_u *, size_t, cctx_T *) UNUSED,
Bram Moolenaara494f562020-04-18 17:45:38 +02003234 cctx_T *cctx UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003235{
3236 int len;
3237 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003238 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003239
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003240#ifdef FEAT_EVAL
3241 /*
3242 * Recognize a Vim9 script function/method call and assignment:
3243 * "lvar = value", "lvar(arg)", "[1, 2 3]->Func()"
3244 */
Bram Moolenaar0c6ceaf2020-02-22 18:36:32 +01003245 p = eap->cmd;
Bram Moolenaar3d48e252020-07-15 14:15:52 +02003246 if (lookup != NULL && (vim_strchr((char_u *)"{('[", *p) != NULL
3247 || ((p = to_name_const_end(eap->cmd)) > eap->cmd
3248 && *p != NUL)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003249 {
3250 int oplen;
3251 int heredoc;
3252
Bram Moolenaar3d48e252020-07-15 14:15:52 +02003253 if (
3254 // "(..." is an expression.
3255 // "funcname(" is always a function call.
3256 *p == '('
3257 || (p == eap->cmd
3258 ? (
3259 // "{..." is an dict expression.
3260 *eap->cmd == '{'
3261 // "'string'->func()" is an expression.
3262 || *eap->cmd == '\''
3263 // "g:varname" is an expression.
3264 || eap->cmd[1] == ':'
3265 )
3266 : (
3267 // "varname[]" is an expression.
3268 *p == '['
3269 // "varname->func()" is an expression.
3270 || (*p == '-' && p[1] == '>')
3271 // "varname.expr" is an expression.
3272 || (*p == '.' && ASCII_ISALPHA(p[1]))
3273 )))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003274 {
3275 eap->cmdidx = CMD_eval;
3276 return eap->cmd;
3277 }
3278
Bram Moolenaard2ef6b32020-07-02 21:11:34 +02003279 // "[...]->Method()" is a list expression, but "[a, b] = Func()" is
3280 // an assignment.
3281 // If there is no line break inside the "[...]" then "p" is advanced to
3282 // after the "]" by to_name_const_end(): check if a "=" follows.
3283 // If "[...]" has a line break "p" still points at the "[" and it can't
3284 // be an assignment.
3285 if (*eap->cmd == '[' && (p == eap->cmd || *skipwhite(p) != '='))
3286 {
3287 eap->cmdidx = CMD_eval;
3288 return eap->cmd;
3289 }
3290
Bram Moolenaar5f195932020-07-01 20:07:14 +02003291 // Recognize an assignment if we recognize the variable name:
3292 // "g:var = expr"
3293 // "var = expr" where "var" is a local var name.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003294 oplen = assignment_len(skipwhite(p), &heredoc);
3295 if (oplen > 0)
3296 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003297 if (((p - eap->cmd) > 2 && eap->cmd[1] == ':')
Bram Moolenaarb84a3812020-05-01 15:44:29 +02003298 || lookup(eap->cmd, p - eap->cmd, cctx) != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003299 {
3300 eap->cmdidx = CMD_let;
3301 return eap->cmd;
3302 }
3303 }
3304 }
3305#endif
3306
Bram Moolenaar071d4272004-06-13 20:20:40 +00003307 /*
3308 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00003309 * Exceptions:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003310 * - the 'k' command can directly be followed by any character.
3311 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003312 * but :sre[wind] is another command, as are :scr[iptnames],
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00003314 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315 */
3316 p = eap->cmd;
3317 if (*p == 'k')
3318 {
3319 eap->cmdidx = CMD_k;
3320 ++p;
3321 }
3322 else if (p[0] == 's'
Bram Moolenaar204b93f2015-08-04 22:02:51 +02003323 && ((p[1] == 'c' && (p[2] == NUL || (p[2] != 's' && p[2] != 'r'
3324 && (p[3] == NUL || (p[3] != 'i' && p[4] != 'p')))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325 || p[1] == 'g'
3326 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
3327 || p[1] == 'I'
3328 || (p[1] == 'r' && p[2] != 'e')))
3329 {
3330 eap->cmdidx = CMD_substitute;
3331 ++p;
3332 }
3333 else
3334 {
3335 while (ASCII_ISALPHA(*p))
3336 ++p;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003337 // for python 3.x support ":py3", ":python3", ":py3file", etc.
Bram Moolenaar55d5c032010-07-17 23:52:29 +02003338 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003339 {
Bram Moolenaarb6590522010-07-21 16:00:43 +02003340 while (ASCII_ISALNUM(*p))
3341 ++p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003342 }
3343 else if (*p == '9' && STRNCMP("vim9", eap->cmd, 4) == 0)
3344 {
3345 // include "9" for "vim9script"
3346 ++p;
3347 while (ASCII_ISALPHA(*p))
3348 ++p;
3349 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02003350
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003351 // check for non-alpha command
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3353 ++p;
3354 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003355 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
3356 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003357 // Check for ":dl", ":dell", etc. to ":deletel": that's
3358 // :delete with the 'l' flag. Same for 'p'.
Bram Moolenaardf177f62005-02-22 08:39:57 +00003359 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003360 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00003361 break;
3362 if (i == len - 1)
3363 {
3364 --len;
3365 if (p[-1] == 'l')
3366 eap->flags |= EXFLAG_LIST;
3367 else
3368 eap->flags |= EXFLAG_PRINT;
3369 }
3370 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003372 if (ASCII_ISLOWER(eap->cmd[0]))
3373 {
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003374 int c1 = eap->cmd[0];
Bram Moolenaar94f82cb2019-07-24 22:30:27 +02003375 int c2 = len == 1 ? NUL : eap->cmd[1];
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003376
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003377 if (command_count != (int)CMD_SIZE)
3378 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003379 iemsg(_("E943: Command table needs to be updated, run 'make cmdidxs'"));
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003380 getout(1);
3381 }
3382
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003383 // Use a precomputed index for fast look-up in cmdnames[]
3384 // taking into account the first 2 letters of eap->cmd.
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003385 eap->cmdidx = cmdidxs1[CharOrdLow(c1)];
3386 if (ASCII_ISLOWER(c2))
3387 eap->cmdidx += cmdidxs2[CharOrdLow(c1)][CharOrdLow(c2)];
3388 }
Bram Moolenaara494f562020-04-18 17:45:38 +02003389 else if (ASCII_ISUPPER(eap->cmd[0]))
3390 eap->cmdidx = CMD_Next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003391 else
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003392 eap->cmdidx = CMD_bang;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393
3394 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
3395 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
3396 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
3397 (size_t)len) == 0)
3398 {
3399#ifdef FEAT_EVAL
3400 if (full != NULL
3401 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
3402 *full = TRUE;
3403#endif
3404 break;
3405 }
3406
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003407 // Look for a user defined command as a last resort. Let ":Print" be
3408 // overruled by a user defined command.
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01003409 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
3410 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411 {
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003412 // User defined commands may contain digits.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413 while (ASCII_ISALNUM(*p))
3414 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003415 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003417 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418 eap->cmdidx = CMD_SIZE;
3419 }
3420
3421 return p;
3422}
3423
3424#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003425static struct cmdmod
3426{
3427 char *name;
3428 int minlen;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003429 int has_count; // :123verbose :3tab
Bram Moolenaared53fb92007-11-24 20:50:24 +00003430} cmdmods[] = {
3431 {"aboveleft", 3, FALSE},
3432 {"belowright", 3, FALSE},
3433 {"botright", 2, FALSE},
3434 {"browse", 3, FALSE},
3435 {"confirm", 4, FALSE},
Bram Moolenaar7b668e82016-08-23 23:51:21 +02003436 {"filter", 4, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003437 {"hide", 3, FALSE},
3438 {"keepalt", 5, FALSE},
3439 {"keepjumps", 5, FALSE},
3440 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01003441 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003442 {"leftabove", 5, FALSE},
3443 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003444 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003445 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003446 {"rightbelow", 6, FALSE},
3447 {"sandbox", 3, FALSE},
3448 {"silent", 3, FALSE},
3449 {"tab", 3, TRUE},
3450 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003451 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003452 {"verbose", 4, TRUE},
3453 {"vertical", 4, FALSE},
3454};
3455
3456/*
3457 * Return length of a command modifier (including optional count).
3458 * Return zero when it's not a modifier.
3459 */
3460 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003461modifier_len(char_u *cmd)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003462{
3463 int i, j;
3464 char_u *p = cmd;
3465
3466 if (VIM_ISDIGIT(*cmd))
3467 p = skipwhite(skipdigits(cmd));
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003468 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003469 {
3470 for (j = 0; p[j] != NUL; ++j)
3471 if (p[j] != cmdmods[i].name[j])
3472 break;
Bram Moolenaar2d473ab2013-06-12 17:12:24 +02003473 if (!ASCII_ISALPHA(p[j]) && j >= cmdmods[i].minlen
Bram Moolenaared53fb92007-11-24 20:50:24 +00003474 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003475 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003476 }
3477 return 0;
3478}
3479
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480/*
3481 * Return > 0 if an Ex command "name" exists.
3482 * Return 2 if there is an exact match.
3483 * Return 3 if there is an ambiguous match.
3484 */
3485 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003486cmd_exists(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487{
3488 exarg_T ea;
3489 int full = FALSE;
3490 int i;
3491 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003492 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003494 // Check command modifiers.
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003495 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496 {
3497 for (j = 0; name[j] != NUL; ++j)
3498 if (name[j] != cmdmods[i].name[j])
3499 break;
3500 if (name[j] == NUL && j >= cmdmods[i].minlen)
3501 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3502 }
3503
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003504 // Check built-in commands and user defined commands.
3505 // For ":2match" and ":3match" we need to skip the number.
Bram Moolenaara9587612006-05-04 21:47:50 +00003506 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003508 p = find_ex_command(&ea, &full, NULL, NULL);
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003509 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003511 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3512 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003513 if (*skipwhite(p) != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003514 return 0; // trailing garbage
Bram Moolenaar071d4272004-06-13 20:20:40 +00003515 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3516}
3517#endif
3518
Bram Moolenaard0190392019-08-23 21:17:35 +02003519 cmdidx_T
3520excmd_get_cmdidx(char_u *cmd, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521{
Bram Moolenaard0190392019-08-23 21:17:35 +02003522 cmdidx_T idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523
Bram Moolenaard0190392019-08-23 21:17:35 +02003524 for (idx = (cmdidx_T)0; (int)idx < (int)CMD_SIZE;
3525 idx = (cmdidx_T)((int)idx + 1))
3526 if (STRNCMP(cmdnames[(int)idx].cmd_name, cmd, (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527 break;
3528
Bram Moolenaard0190392019-08-23 21:17:35 +02003529 return idx;
3530}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531
Bram Moolenaard0190392019-08-23 21:17:35 +02003532 long
3533excmd_get_argt(cmdidx_T idx)
3534{
3535 return (long)cmdnames[(int)idx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536}
3537
3538/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003539 * Skip a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540 *
3541 * Backslashed delimiters after / or ? will be skipped, and commands will
3542 * not be expanded between /'s and ?'s or after "'".
3543 *
Bram Moolenaardf069ee2020-06-22 23:02:51 +02003544 * Also skip white space and ":" characters after the range.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545 * Returns the "cmd" pointer advanced to beyond the range.
3546 */
3547 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003548skip_range(
3549 char_u *cmd,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003550 int *ctx) // pointer to xp_context or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003552 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01003554 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;\\", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555 {
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01003556 if (*cmd == '\\')
3557 {
3558 if (cmd[1] == '?' || cmd[1] == '/' || cmd[1] == '&')
3559 ++cmd;
3560 else
3561 break;
3562 }
3563 else if (*cmd == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564 {
3565 if (*++cmd == NUL && ctx != NULL)
3566 *ctx = EXPAND_NOTHING;
3567 }
3568 else if (*cmd == '/' || *cmd == '?')
3569 {
3570 delim = *cmd++;
3571 while (*cmd != NUL && *cmd != delim)
3572 if (*cmd++ == '\\' && *cmd != NUL)
3573 ++cmd;
3574 if (*cmd == NUL && ctx != NULL)
3575 *ctx = EXPAND_NOTHING;
3576 }
3577 if (*cmd != NUL)
3578 ++cmd;
3579 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00003580
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003581 // Skip ":" and white space.
Bram Moolenaardf177f62005-02-22 08:39:57 +00003582 while (*cmd == ':')
3583 cmd = skipwhite(cmd + 1);
3584
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585 return cmd;
3586}
3587
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003588 static void
3589addr_error(cmd_addr_T addr_type)
3590{
3591 if (addr_type == ADDR_NONE)
3592 emsg(_(e_norange));
3593 else
3594 emsg(_(e_invrange));
3595}
3596
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597/*
Bram Moolenaar198cb662018-09-06 21:44:17 +02003598 * Get a single EX address.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599 *
3600 * Set ptr to the next character after the part that was interpreted.
3601 * Set ptr to NULL when an error is encountered.
Bram Moolenaar198cb662018-09-06 21:44:17 +02003602 * This may set the last used search pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603 *
3604 * Return MAXLNUM when no Ex address was found.
3605 */
3606 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003607get_address(
3608 exarg_T *eap UNUSED,
3609 char_u **ptr,
Bram Moolenaar1b03a192019-12-08 17:08:29 +01003610 cmd_addr_T addr_type,
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003611 int skip, // only skip the address, don't use it
3612 int silent, // no errors or side effects
3613 int to_other_file, // flag: may jump to other file
3614 int address_count UNUSED) // 1 for first address, >1 after comma
Bram Moolenaar071d4272004-06-13 20:20:40 +00003615{
3616 int c;
3617 int i;
3618 long n;
3619 char_u *cmd;
3620 pos_T pos;
3621 pos_T *fp;
3622 linenr_T lnum;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003623 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624
3625 cmd = skipwhite(*ptr);
3626 lnum = MAXLNUM;
3627 do
3628 {
3629 switch (*cmd)
3630 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003631 case '.': // '.' - Cursor position
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003632 ++cmd;
3633 switch (addr_type)
3634 {
3635 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003636 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003637 lnum = curwin->w_cursor.lnum;
3638 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003639 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003640 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003641 break;
3642 case ADDR_ARGUMENTS:
3643 lnum = curwin->w_arg_idx + 1;
3644 break;
3645 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003646 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003647 lnum = curbuf->b_fnum;
3648 break;
3649 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003650 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003651 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003652 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003653 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003654 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003655 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003656 cmd = NULL;
3657 goto error;
3658 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003659 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003660#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02003661 lnum = qf_get_cur_idx(eap);
3662#endif
3663 break;
3664 case ADDR_QUICKFIX_VALID:
3665#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003666 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02003667#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003668 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003669 }
3670 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003671
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003672 case '$': // '$' - last line
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 = curbuf->b_ml.ml_line_count;
3679 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003680 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003681 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003682 break;
3683 case ADDR_ARGUMENTS:
3684 lnum = ARGCOUNT;
3685 break;
3686 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003687 buf = lastbuf;
3688 while (buf->b_ml.ml_mfp == NULL)
3689 {
3690 if (buf->b_prev == NULL)
3691 break;
3692 buf = buf->b_prev;
3693 }
3694 lnum = buf->b_fnum;
3695 break;
3696 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003697 lnum = lastbuf->b_fnum;
3698 break;
3699 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003700 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003701 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003702 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003703 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003704 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003705 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003706 cmd = NULL;
3707 goto error;
3708 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003709 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003710#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003711 lnum = qf_get_size(eap);
3712 if (lnum == 0)
3713 lnum = 1;
Bram Moolenaare906c502015-09-09 21:10:39 +02003714#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003715 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003716 case ADDR_QUICKFIX_VALID:
3717#ifdef FEAT_QUICKFIX
3718 lnum = qf_get_valid_size(eap);
3719 if (lnum == 0)
3720 lnum = 1;
3721#endif
3722 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003723 }
3724 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003725
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003726 case '\'': // ''' - mark
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003727 if (*++cmd == NUL)
3728 {
3729 cmd = NULL;
3730 goto error;
3731 }
3732 if (addr_type != ADDR_LINES)
3733 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003734 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003735 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003736 goto error;
3737 }
3738 if (skip)
3739 ++cmd;
3740 else
3741 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003742 // Only accept a mark in another file when it is
3743 // used by itself: ":'M".
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003744 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
3745 ++cmd;
3746 if (fp == (pos_T *)-1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003747 // Jumped to another file.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003748 lnum = curwin->w_cursor.lnum;
3749 else
3750 {
3751 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003752 {
3753 cmd = NULL;
3754 goto error;
3755 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003756 lnum = fp->lnum;
3757 }
3758 }
3759 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003760
3761 case '/':
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003762 case '?': // '/' or '?' - search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003763 c = *cmd++;
3764 if (addr_type != ADDR_LINES)
3765 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003766 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003767 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003768 goto error;
3769 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003770 if (skip) // skip "/pat/"
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003771 {
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02003772 cmd = skip_regexp(cmd, c, (int)p_magic);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003773 if (*cmd == c)
3774 ++cmd;
3775 }
3776 else
3777 {
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003778 int flags;
3779
3780 pos = curwin->w_cursor; // save curwin->w_cursor
3781
3782 // When '/' or '?' follows another address, start from
3783 // there.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003784 if (lnum != MAXLNUM)
3785 curwin->w_cursor.lnum = lnum;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003786
3787 // Start a forward search at the end of the line (unless
3788 // before the first line).
3789 // Start a backward search at the start of the line.
3790 // This makes sure we never match in the current
3791 // line, and can match anywhere in the
3792 // next/previous line.
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01003793 if (c == '/' && curwin->w_cursor.lnum > 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003794 curwin->w_cursor.col = MAXCOL;
3795 else
3796 curwin->w_cursor.col = 0;
3797 searchcmdlen = 0;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003798 flags = silent ? 0 : SEARCH_HIS | SEARCH_MSG;
Bram Moolenaarc036e872020-02-21 21:30:52 +01003799 if (!do_search(NULL, c, c, cmd, 1L, flags, NULL))
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003800 {
3801 curwin->w_cursor = pos;
3802 cmd = NULL;
3803 goto error;
3804 }
3805 lnum = curwin->w_cursor.lnum;
3806 curwin->w_cursor = pos;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003807 // adjust command string pointer
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003808 cmd += searchcmdlen;
3809 }
3810 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003812 case '\\': // "\?", "\/" or "\&", repeat search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003813 ++cmd;
3814 if (addr_type != ADDR_LINES)
3815 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003816 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003817 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003818 goto error;
3819 }
3820 if (*cmd == '&')
3821 i = RE_SUBST;
3822 else if (*cmd == '?' || *cmd == '/')
3823 i = RE_SEARCH;
3824 else
3825 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003826 emsg(_(e_backslash));
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003827 cmd = NULL;
3828 goto error;
3829 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003831 if (!skip)
3832 {
3833 /*
3834 * When search follows another address, start from
3835 * there.
3836 */
3837 if (lnum != MAXLNUM)
3838 pos.lnum = lnum;
3839 else
3840 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003842 /*
3843 * Start the search just like for the above
3844 * do_search().
3845 */
3846 if (*cmd != '?')
3847 pos.col = MAXCOL;
3848 else
3849 pos.col = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02003850 pos.coladd = 0;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01003851 if (searchit(curwin, curbuf, &pos, NULL,
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003852 *cmd == '?' ? BACKWARD : FORWARD,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02003853 (char_u *)"", 1L, SEARCH_MSG, i, NULL) != FAIL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003854 lnum = pos.lnum;
3855 else
3856 {
3857 cmd = NULL;
3858 goto error;
3859 }
3860 }
3861 ++cmd;
3862 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003863
3864 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003865 if (VIM_ISDIGIT(*cmd)) // absolute line number
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003866 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867 }
3868
3869 for (;;)
3870 {
3871 cmd = skipwhite(cmd);
3872 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
3873 break;
3874
3875 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003876 {
3877 switch (addr_type)
3878 {
3879 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003880 case ADDR_OTHER:
3881 // "+1" is same as ".+1"
Bram Moolenaarf240e182014-11-27 18:33:02 +01003882 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003883 break;
3884 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003885 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003886 break;
3887 case ADDR_ARGUMENTS:
3888 lnum = curwin->w_arg_idx + 1;
3889 break;
3890 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003891 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003892 lnum = curbuf->b_fnum;
3893 break;
3894 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003895 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003896 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003897 case ADDR_TABS_RELATIVE:
3898 lnum = 1;
3899 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003900 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003901#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02003902 lnum = qf_get_cur_idx(eap);
3903#endif
3904 break;
3905 case ADDR_QUICKFIX_VALID:
3906#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003907 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02003908#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003909 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003910 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003911 case ADDR_UNSIGNED:
3912 lnum = 0;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003913 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003914 }
3915 }
3916
Bram Moolenaar071d4272004-06-13 20:20:40 +00003917 if (VIM_ISDIGIT(*cmd))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003918 i = '+'; // "number" is same as "+number"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003919 else
3920 i = *cmd++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003921 if (!VIM_ISDIGIT(*cmd)) // '+' is '+1', but '+0' is not '+1'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003922 n = 1;
3923 else
3924 n = getdigits(&cmd);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003925
3926 if (addr_type == ADDR_TABS_RELATIVE)
3927 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003928 emsg(_(e_invrange));
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003929 cmd = NULL;
3930 goto error;
3931 }
3932 else if (addr_type == ADDR_LOADED_BUFFERS
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003933 || addr_type == ADDR_BUFFERS)
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003934 lnum = compute_buffer_local_count(
3935 addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003936 else
Bram Moolenaarded27822017-01-02 14:27:34 +01003937 {
3938#ifdef FEAT_FOLDING
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003939 // Relative line addressing, need to adjust for folded lines
3940 // now, but only do it after the first address.
Bram Moolenaarded27822017-01-02 14:27:34 +01003941 if (addr_type == ADDR_LINES && (i == '-' || i == '+')
3942 && address_count >= 2)
3943 (void)hasFolding(lnum, NULL, &lnum);
3944#endif
3945 if (i == '-')
3946 lnum -= n;
3947 else
3948 lnum += n;
3949 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003950 }
3951 } while (*cmd == '/' || *cmd == '?');
3952
3953error:
3954 *ptr = cmd;
3955 return lnum;
3956}
3957
3958/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00003959 * Get flags from an Ex command argument.
3960 */
3961 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003962get_flags(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00003963{
3964 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
3965 {
3966 if (*eap->arg == 'l')
3967 eap->flags |= EXFLAG_LIST;
3968 else if (*eap->arg == 'p')
3969 eap->flags |= EXFLAG_PRINT;
3970 else
3971 eap->flags |= EXFLAG_NR;
3972 eap->arg = skipwhite(eap->arg + 1);
3973 }
3974}
3975
3976/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977 * Function called for command which is Not Implemented. NI!
3978 */
3979 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003980ex_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003981{
3982 if (!eap->skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003983 eap->errmsg = N_("E319: Sorry, the command is not available in this version");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003984}
3985
Bram Moolenaar7bb75552007-07-16 18:39:49 +00003986#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003987/*
3988 * Function called for script command which is Not Implemented. NI!
3989 * Skips over ":perl <<EOF" constructs.
3990 */
3991 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003992ex_script_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993{
3994 if (!eap->skip)
3995 ex_ni(eap);
3996 else
3997 vim_free(script_get(eap, eap->arg));
3998}
3999#endif
4000
4001/*
4002 * Check range in Ex command for validity.
4003 * Return NULL when valid, error message when invalid.
4004 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004005 static char *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004006invalid_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004007{
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004008 buf_T *buf;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004009
Bram Moolenaar071d4272004-06-13 20:20:40 +00004010 if ( eap->line1 < 0
4011 || eap->line2 < 0
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004012 || eap->line1 > eap->line2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004013 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004014
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004015 if (eap->argt & EX_RANGE)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004016 {
Bram Moolenaarb7316892019-05-01 18:08:42 +02004017 switch (eap->addr_type)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004018 {
4019 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004020 if (eap->line2 > curbuf->b_ml.ml_line_count
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004021#ifdef FEAT_DIFF
4022 + (eap->cmdidx == CMD_diffget)
4023#endif
4024 )
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004025 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004026 break;
4027 case ADDR_ARGUMENTS:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004028 // add 1 if ARGCOUNT is 0
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004029 if (eap->line2 > ARGCOUNT + (!ARGCOUNT))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004030 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004031 break;
4032 case ADDR_BUFFERS:
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02004033 // Only a boundary check, not whether the buffers actually
4034 // exist.
4035 if (eap->line1 < 1 || eap->line2 > get_highest_fnum())
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004036 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004037 break;
4038 case ADDR_LOADED_BUFFERS:
4039 buf = firstbuf;
4040 while (buf->b_ml.ml_mfp == NULL)
4041 {
4042 if (buf->b_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004043 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004044 buf = buf->b_next;
4045 }
4046 if (eap->line1 < buf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004047 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004048 buf = lastbuf;
4049 while (buf->b_ml.ml_mfp == NULL)
4050 {
4051 if (buf->b_prev == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004052 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004053 buf = buf->b_prev;
4054 }
4055 if (eap->line2 > buf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004056 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004057 break;
4058 case ADDR_WINDOWS:
Bram Moolenaar8be63882015-01-14 11:25:05 +01004059 if (eap->line2 > LAST_WIN_NR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004060 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004061 break;
4062 case ADDR_TABS:
4063 if (eap->line2 > LAST_TAB_NR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004064 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004065 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004066 case ADDR_TABS_RELATIVE:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004067 case ADDR_OTHER:
4068 // Any range is OK.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004069 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004070 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004071#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004072 // No error for value that is too big, will use the last entry.
4073 if (eap->line2 <= 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004074 return _(e_invrange);
Bram Moolenaare906c502015-09-09 21:10:39 +02004075#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004076 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004077 case ADDR_QUICKFIX_VALID:
4078#ifdef FEAT_QUICKFIX
4079 if ((eap->line2 != 1 && eap->line2 > qf_get_valid_size(eap))
4080 || eap->line2 < 0)
4081 return _(e_invrange);
4082#endif
4083 break;
4084 case ADDR_UNSIGNED:
4085 if (eap->line2 < 0)
4086 return _(e_invrange);
4087 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004088 case ADDR_NONE:
4089 // Will give an error elsewhere.
4090 break;
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004091 }
4092 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004093 return NULL;
4094}
4095
4096/*
4097 * Correct the range for zero line number, if required.
4098 */
4099 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004100correct_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101{
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004102 if (!(eap->argt & EX_ZEROR)) // zero in range not allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004103 {
4104 if (eap->line1 == 0)
4105 eap->line1 = 1;
4106 if (eap->line2 == 0)
4107 eap->line2 = 1;
4108 }
4109}
4110
Bram Moolenaar748bf032005-02-02 23:04:36 +00004111#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004112/*
4113 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4114 * pattern. Otherwise return eap->arg.
4115 */
4116 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004117skip_grep_pat(exarg_T *eap)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004118{
4119 char_u *p = eap->arg;
4120
Bram Moolenaara37420f2006-02-04 22:37:47 +00004121 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4122 || eap->cmdidx == CMD_vimgrepadd
4123 || eap->cmdidx == CMD_lvimgrepadd
4124 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004125 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004126 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004127 if (p == NULL)
4128 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004129 }
4130 return p;
4131}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004132
4133/*
4134 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4135 * in the command line, so that things like % get expanded.
4136 */
4137 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004138replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004139{
4140 char_u *new_cmdline;
4141 char_u *program;
4142 char_u *pos;
4143 char_u *ptr;
4144 int len;
4145 int i;
4146
4147 /*
4148 * Don't do it when ":vimgrep" is used for ":grep".
4149 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004150 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4151 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4152 || eap->cmdidx == CMD_grepadd
4153 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004154 && !grep_internal(eap->cmdidx))
4155 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004156 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4157 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004158 {
4159 if (*curbuf->b_p_gp == NUL)
4160 program = p_gp;
4161 else
4162 program = curbuf->b_p_gp;
4163 }
4164 else
4165 {
4166 if (*curbuf->b_p_mp == NUL)
4167 program = p_mp;
4168 else
4169 program = curbuf->b_p_mp;
4170 }
4171
4172 p = skipwhite(p);
4173
4174 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4175 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004176 // replace $* by given arguments
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004177 i = 1;
4178 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4179 ++i;
4180 len = (int)STRLEN(p);
Bram Moolenaar51e14382019-05-25 20:21:28 +02004181 new_cmdline = alloc(STRLEN(program) + i * (len - 2) + 1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004182 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004183 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004184 ptr = new_cmdline;
4185 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4186 {
4187 i = (int)(pos - program);
4188 STRNCPY(ptr, program, i);
4189 STRCPY(ptr += i, p);
4190 ptr += len;
4191 program = pos + 2;
4192 }
4193 STRCPY(ptr, program);
4194 }
4195 else
4196 {
Bram Moolenaar51e14382019-05-25 20:21:28 +02004197 new_cmdline = alloc(STRLEN(program) + STRLEN(p) + 2);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004198 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004199 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004200 STRCPY(new_cmdline, program);
4201 STRCAT(new_cmdline, " ");
4202 STRCAT(new_cmdline, p);
4203 }
4204 msg_make(p);
4205
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004206 // 'eap->cmd' is not set here, because it is not used at CMD_make
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004207 vim_free(*cmdlinep);
4208 *cmdlinep = new_cmdline;
4209 p = new_cmdline;
4210 }
4211 return p;
4212}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004213#endif
4214
Bram Moolenaar071d4272004-06-13 20:20:40 +00004215/*
4216 * Expand file name in Ex command argument.
Bram Moolenaar0abb4272019-06-15 16:06:00 +02004217 * When an error is detected, "errormsgp" is set to a non-NULL pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004218 * Return FAIL for failure, OK otherwise.
4219 */
4220 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004221expand_filename(
4222 exarg_T *eap,
4223 char_u **cmdlinep,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004224 char **errormsgp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004226 int has_wildcards; // need to expand wildcards
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227 char_u *repl;
4228 int srclen;
4229 char_u *p;
4230 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004231 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232
Bram Moolenaar748bf032005-02-02 23:04:36 +00004233#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004234 // Skip a regexp pattern for ":vimgrep[add] pat file..."
Bram Moolenaar748bf032005-02-02 23:04:36 +00004235 p = skip_grep_pat(eap);
4236#else
4237 p = eap->arg;
4238#endif
4239
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240 /*
4241 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4242 * the file name contains a wildcard it should not cause expanding.
4243 * (it will be expanded anyway if there is a wildcard before replacing).
4244 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004245 has_wildcards = mch_has_wildcard(p);
4246 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004247 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004248#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004249 // Skip over `=expr`, wildcards in it are not expanded.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004250 if (p[0] == '`' && p[1] == '=')
4251 {
4252 p += 2;
4253 (void)skip_expr(&p);
4254 if (*p == '`')
4255 ++p;
4256 continue;
4257 }
4258#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004259 /*
4260 * Quick check if this cannot be the start of a special string.
4261 * Also removes backslash before '%', '#' and '<'.
4262 */
4263 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4264 {
4265 ++p;
4266 continue;
4267 }
4268
4269 /*
4270 * Try to find a match at this position.
4271 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00004272 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
4273 errormsgp, &escaped);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004274 if (*errormsgp != NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275 return FAIL;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004276 if (repl == NULL) // no match found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004277 {
4278 p += srclen;
4279 continue;
4280 }
4281
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004282 // Wildcards won't be expanded below, the replacement is taken
4283 // literally. But do expand "~/file", "~user/file" and "$HOME/file".
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004284 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4285 {
4286 char_u *l = repl;
4287
4288 repl = expand_env_save(repl);
4289 vim_free(l);
4290 }
4291
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004292 // Need to escape white space et al. with a backslash.
4293 // Don't do this for:
4294 // - replacement that already has been escaped: "##"
4295 // - shell commands (may have to use quotes instead).
4296 // - non-unix systems when there is a single argument (spaces don't
4297 // separate arguments then).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004298 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00004299 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00004300 && eap->cmdidx != CMD_bang
Bram Moolenaar071d4272004-06-13 20:20:40 +00004301 && eap->cmdidx != CMD_grep
4302 && eap->cmdidx != CMD_grepadd
Bram Moolenaarbf15b8d2017-06-04 20:43:48 +02004303 && eap->cmdidx != CMD_hardcopy
Bram Moolenaar67883b42017-07-27 22:57:00 +02004304 && eap->cmdidx != CMD_lgrep
4305 && eap->cmdidx != CMD_lgrepadd
4306 && eap->cmdidx != CMD_lmake
4307 && eap->cmdidx != CMD_make
4308 && eap->cmdidx != CMD_terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309#ifndef UNIX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004310 && !(eap->argt & EX_NOSPC)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004311#endif
4312 )
4313 {
4314 char_u *l;
4315#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004316 // Don't escape a backslash here, because rem_backslash() doesn't
4317 // remove it later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004318 static char_u *nobslash = (char_u *)" \t\"|";
4319# define ESCAPE_CHARS nobslash
4320#else
4321# define ESCAPE_CHARS escape_chars
4322#endif
4323
4324 for (l = repl; *l; ++l)
4325 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
4326 {
4327 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
4328 if (l != NULL)
4329 {
4330 vim_free(repl);
4331 repl = l;
4332 }
4333 break;
4334 }
4335 }
4336
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004337 // For a shell command a '!' must be escaped.
Bram Moolenaar67883b42017-07-27 22:57:00 +02004338 if ((eap->usefilter || eap->cmdidx == CMD_bang
4339 || eap->cmdidx == CMD_terminal)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004340 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004341 {
4342 char_u *l;
4343
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004344 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004345 if (l != NULL)
4346 {
4347 vim_free(repl);
4348 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004349 }
4350 }
4351
4352 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
4353 vim_free(repl);
4354 if (p == NULL)
4355 return FAIL;
4356 }
4357
4358 /*
4359 * One file argument: Expand wildcards.
4360 * Don't do this with ":r !command" or ":w !command".
4361 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004362 if ((eap->argt & EX_NOSPC) && !eap->usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004363 {
4364 /*
4365 * May do this twice:
4366 * 1. Replace environment variables.
4367 * 2. Replace any other wildcards, remove backslashes.
4368 */
4369 for (n = 1; n <= 2; ++n)
4370 {
4371 if (n == 2)
4372 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004373 /*
4374 * Halve the number of backslashes (this is Vi compatible).
4375 * For Unix and OS/2, when wildcards are expanded, this is
4376 * done by ExpandOne() below.
4377 */
Bram Moolenaare7fedb62015-12-31 19:07:19 +01004378#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004379 if (!has_wildcards)
4380#endif
4381 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004382 }
4383
4384 if (has_wildcards)
4385 {
4386 if (n == 1)
4387 {
4388 /*
4389 * First loop: May expand environment variables. This
4390 * can be done much faster with expand_env() than with
4391 * something else (e.g., calling a shell).
4392 * After expanding environment variables, check again
4393 * if there are still wildcards present.
4394 */
4395 if (vim_strchr(eap->arg, '$') != NULL
4396 || vim_strchr(eap->arg, '~') != NULL)
4397 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00004398 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004399 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004400 has_wildcards = mch_has_wildcard(NameBuff);
4401 p = NameBuff;
4402 }
4403 else
4404 p = NULL;
4405 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004406 else // n == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004407 {
4408 expand_T xpc;
Bram Moolenaarb40c2572019-10-19 21:01:05 +02004409 int options = WILD_LIST_NOTFOUND
4410 | WILD_NOERROR | WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004411
4412 ExpandInit(&xpc);
4413 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004414 if (p_wic)
4415 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004416 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01004417 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004418 if (p == NULL)
4419 return FAIL;
4420 }
4421 if (p != NULL)
4422 {
4423 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
4424 p, cmdlinep);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004425 if (n == 2) // p came from ExpandOne()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004426 vim_free(p);
4427 }
4428 }
4429 }
4430 }
4431 return OK;
4432}
4433
4434/*
4435 * Replace part of the command line, keeping eap->cmd, eap->arg and
4436 * eap->nextcmd correct.
4437 * "src" points to the part that is to be replaced, of length "srclen".
4438 * "repl" is the replacement string.
4439 * Returns a pointer to the character after the replaced string.
4440 * Returns NULL for failure.
4441 */
4442 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004443repl_cmdline(
4444 exarg_T *eap,
4445 char_u *src,
4446 int srclen,
4447 char_u *repl,
4448 char_u **cmdlinep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004449{
4450 int len;
4451 int i;
4452 char_u *new_cmdline;
4453
4454 /*
4455 * The new command line is build in new_cmdline[].
4456 * First allocate it.
4457 * Careful: a "+cmd" argument may have been NUL terminated.
4458 */
4459 len = (int)STRLEN(repl);
4460 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004461 if (eap->nextcmd != NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004462 i += (int)STRLEN(eap->nextcmd);// add space for next command
Bram Moolenaar964b3742019-05-24 18:54:09 +02004463 if ((new_cmdline = alloc(i)) == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004464 return NULL; // out of memory!
Bram Moolenaar071d4272004-06-13 20:20:40 +00004465
4466 /*
4467 * Copy the stuff before the expanded part.
4468 * Copy the expanded stuff.
4469 * Copy what came after the expanded part.
4470 * Copy the next commands, if there are any.
4471 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004472 i = (int)(src - *cmdlinep); // length of part before match
Bram Moolenaar071d4272004-06-13 20:20:40 +00004473 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00004474
Bram Moolenaar071d4272004-06-13 20:20:40 +00004475 mch_memmove(new_cmdline + i, repl, (size_t)len);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004476 i += len; // remember the end of the string
Bram Moolenaar071d4272004-06-13 20:20:40 +00004477 STRCPY(new_cmdline + i, src + srclen);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004478 src = new_cmdline + i; // remember where to continue
Bram Moolenaar071d4272004-06-13 20:20:40 +00004479
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004480 if (eap->nextcmd != NULL) // append next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00004481 {
4482 i = (int)STRLEN(new_cmdline) + 1;
4483 STRCPY(new_cmdline + i, eap->nextcmd);
4484 eap->nextcmd = new_cmdline + i;
4485 }
4486 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
4487 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
4488 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
4489 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
4490 vim_free(*cmdlinep);
4491 *cmdlinep = new_cmdline;
4492
4493 return src;
4494}
4495
4496/*
4497 * Check for '|' to separate commands and '"' to start comments.
4498 */
4499 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004500separate_nextcmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004501{
4502 char_u *p;
4503
Bram Moolenaar86b68352004-12-27 21:59:20 +00004504#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004505 p = skip_grep_pat(eap);
4506#else
4507 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004508#endif
4509
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004510 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004511 {
4512 if (*p == Ctrl_V)
4513 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004514 if (eap->argt & (EX_CTRLV | EX_XFILE))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004515 ++p; // skip CTRL-V and next char
Bram Moolenaar071d4272004-06-13 20:20:40 +00004516 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004517 // remove CTRL-V and skip next char
Bram Moolenaara7241f52008-06-24 20:39:31 +00004518 STRMOVE(p, p + 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004519 if (*p == NUL) // stop at NUL after CTRL-V
Bram Moolenaar071d4272004-06-13 20:20:40 +00004520 break;
4521 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004522
4523#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004524 // Skip over `=expr` when wildcards are expanded.
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004525 else if (p[0] == '`' && p[1] == '=' && (eap->argt & EX_XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004526 {
4527 p += 2;
4528 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004529 }
4530#endif
4531
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004532 // Check for '"': start of comment or '|': next command
4533 // :@" and :*" do not start a comment!
4534 // :redir @" doesn't either.
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004535 else if ((*p == '"'
Bram Moolenaara26b9702020-04-18 19:53:28 +02004536#ifdef FEAT_EVAL
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004537 && !in_vim9script()
4538#endif
4539 && !(eap->argt & EX_NOTRLCOM)
4540 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
4541 || p != eap->arg)
4542 && (eap->cmdidx != CMD_redir
4543 || p != eap->arg + 1 || p[-1] != '@'))
4544#ifdef FEAT_EVAL
4545 || (*p == '#'
4546 && in_vim9script()
4547 && p[1] != '{'
4548 && p > eap->cmd && VIM_ISWHITE(p[-1]))
Bram Moolenaara26b9702020-04-18 19:53:28 +02004549#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004550 || *p == '|' || *p == '\n')
4551 {
4552 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004553 * We remove the '\' before the '|', unless EX_CTRLV is used
Bram Moolenaar071d4272004-06-13 20:20:40 +00004554 * AND 'b' is present in 'cpoptions'.
4555 */
4556 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004557 || !(eap->argt & EX_CTRLV)) && *(p - 1) == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004558 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004559 STRMOVE(p - 1, p); // remove the '\'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004560 --p;
4561 }
4562 else
4563 {
4564 eap->nextcmd = check_nextcmd(p);
4565 *p = NUL;
4566 break;
4567 }
4568 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004569 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004570
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004571 if (!(eap->argt & EX_NOTRLCOM)) // remove trailing spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00004572 del_trailing_spaces(eap->arg);
4573}
4574
4575/*
4576 * get + command from ex argument
4577 */
4578 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004579getargcmd(char_u **argp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004580{
4581 char_u *arg = *argp;
4582 char_u *command = NULL;
4583
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004584 if (*arg == '+') // +[command]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004585 {
4586 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02004587 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004588 command = dollar_command;
4589 else
4590 {
4591 command = arg;
4592 arg = skip_cmd_arg(command, TRUE);
4593 if (*arg != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004594 *arg++ = NUL; // terminate command with NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004595 }
4596
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004597 arg = skipwhite(arg); // skip over spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00004598 *argp = arg;
4599 }
4600 return command;
4601}
4602
4603/*
4604 * Find end of "+command" argument. Skip over "\ " and "\\".
4605 */
Bram Moolenaard0190392019-08-23 21:17:35 +02004606 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004607skip_cmd_arg(
4608 char_u *p,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004609 int rembs) // TRUE to halve the number of backslashes
Bram Moolenaar071d4272004-06-13 20:20:40 +00004610{
4611 while (*p && !vim_isspace(*p))
4612 {
4613 if (*p == '\\' && p[1] != NUL)
4614 {
4615 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00004616 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617 else
4618 ++p;
4619 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004620 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004621 }
4622 return p;
4623}
4624
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004625 int
4626get_bad_opt(char_u *p, exarg_T *eap)
4627{
4628 if (STRICMP(p, "keep") == 0)
4629 eap->bad_char = BAD_KEEP;
4630 else if (STRICMP(p, "drop") == 0)
4631 eap->bad_char = BAD_DROP;
4632 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
4633 eap->bad_char = *p;
Bram Moolenaar75808492018-06-12 12:39:41 +02004634 else
4635 return FAIL;
4636 return OK;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004637}
4638
Bram Moolenaar071d4272004-06-13 20:20:40 +00004639/*
4640 * Get "++opt=arg" argument.
4641 * Return FAIL or OK.
4642 */
4643 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004644getargopt(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004645{
4646 char_u *arg = eap->arg + 2;
4647 int *pp = NULL;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004648 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004649 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004650
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004651 // ":edit ++[no]bin[ary] file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004652 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
4653 {
4654 if (*arg == 'n')
4655 {
4656 arg += 2;
4657 eap->force_bin = FORCE_NOBIN;
4658 }
4659 else
4660 eap->force_bin = FORCE_BIN;
4661 if (!checkforcmd(&arg, "binary", 3))
4662 return FAIL;
4663 eap->arg = skipwhite(arg);
4664 return OK;
4665 }
4666
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004667 // ":read ++edit file"
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004668 if (STRNCMP(arg, "edit", 4) == 0)
4669 {
4670 eap->read_edit = TRUE;
4671 eap->arg = skipwhite(arg + 4);
4672 return OK;
4673 }
4674
Bram Moolenaar071d4272004-06-13 20:20:40 +00004675 if (STRNCMP(arg, "ff", 2) == 0)
4676 {
4677 arg += 2;
4678 pp = &eap->force_ff;
4679 }
4680 else if (STRNCMP(arg, "fileformat", 10) == 0)
4681 {
4682 arg += 10;
4683 pp = &eap->force_ff;
4684 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004685 else if (STRNCMP(arg, "enc", 3) == 0)
4686 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01004687 if (STRNCMP(arg, "encoding", 8) == 0)
4688 arg += 8;
4689 else
4690 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004691 pp = &eap->force_enc;
4692 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004693 else if (STRNCMP(arg, "bad", 3) == 0)
4694 {
4695 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004696 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004697 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004698
4699 if (pp == NULL || *arg != '=')
4700 return FAIL;
4701
4702 ++arg;
4703 *pp = (int)(arg - eap->cmd);
4704 arg = skip_cmd_arg(arg, FALSE);
4705 eap->arg = skipwhite(arg);
4706 *arg = NUL;
4707
Bram Moolenaar071d4272004-06-13 20:20:40 +00004708 if (pp == &eap->force_ff)
4709 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004710 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
4711 return FAIL;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004712 eap->force_ff = eap->cmd[eap->force_ff];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004713 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004714 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004715 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004716 // Make 'fileencoding' lower case.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004717 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
4718 *p = TOLOWER_ASC(*p);
4719 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004720 else
4721 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004722 // Check ++bad= argument. Must be a single-byte character, "keep" or
4723 // "drop".
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004724 if (get_bad_opt(eap->cmd + bad_char_idx, eap) == FAIL)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004725 return FAIL;
4726 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004727
4728 return OK;
4729}
4730
Bram Moolenaar071d4272004-06-13 20:20:40 +00004731 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004732ex_autocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004733{
4734 /*
Bram Moolenaar26d4b892018-07-04 22:26:28 +02004735 * Disallow autocommands from .exrc and .vimrc in current
Bram Moolenaar071d4272004-06-13 20:20:40 +00004736 * directory for security reasons.
4737 */
4738 if (secure)
4739 {
4740 secure = 2;
4741 eap->errmsg = e_curdir;
4742 }
4743 else if (eap->cmdidx == CMD_autocmd)
4744 do_autocmd(eap->arg, eap->forceit);
4745 else
4746 do_augroup(eap->arg, eap->forceit);
4747}
4748
4749/*
4750 * ":doautocmd": Apply the automatic commands to the current buffer.
4751 */
4752 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004753ex_doautocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004754{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01004755 char_u *arg = eap->arg;
4756 int call_do_modelines = check_nomodeline(&arg);
Bram Moolenaar1610d052016-06-09 22:53:01 +02004757 int did_aucmd;
Bram Moolenaar60542ac2012-02-12 20:14:01 +01004758
Bram Moolenaar1610d052016-06-09 22:53:01 +02004759 (void)do_doautocmd(arg, TRUE, &did_aucmd);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004760 // Only when there is no <nomodeline>.
Bram Moolenaar1610d052016-06-09 22:53:01 +02004761 if (call_do_modelines && did_aucmd)
Bram Moolenaar60542ac2012-02-12 20:14:01 +01004762 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004763}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004764
Bram Moolenaar071d4272004-06-13 20:20:40 +00004765/*
4766 * :[N]bunload[!] [N] [bufname] unload buffer
4767 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
4768 * :[N]bwipeout[!] [N] [bufname] delete buffer really
4769 */
4770 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004771ex_bunload(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004772{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004773 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02004774 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004775 eap->errmsg = do_bufdel(
4776 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
4777 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
4778 : DOBUF_UNLOAD, eap->arg,
4779 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
4780}
4781
4782/*
4783 * :[N]buffer [N] to buffer N
4784 * :[N]sbuffer [N] to buffer N
4785 */
4786 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004787ex_buffer(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004788{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004789 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02004790 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004791 if (*eap->arg)
4792 eap->errmsg = e_trailing;
4793 else
4794 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004795 if (eap->addr_count == 0) // default is current buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00004796 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
4797 else
4798 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004799 if (eap->do_ecmd_cmd != NULL)
4800 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004801 }
4802}
4803
4804/*
4805 * :[N]bmodified [N] to next mod. buffer
4806 * :[N]sbmodified [N] to next mod. buffer
4807 */
4808 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004809ex_bmodified(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004810{
4811 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004812 if (eap->do_ecmd_cmd != NULL)
4813 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004814}
4815
4816/*
4817 * :[N]bnext [N] to next buffer
4818 * :[N]sbnext [N] split and to next buffer
4819 */
4820 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004821ex_bnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004822{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004823 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01004824 return;
4825
Bram Moolenaar071d4272004-06-13 20:20:40 +00004826 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004827 if (eap->do_ecmd_cmd != NULL)
4828 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004829}
4830
4831/*
4832 * :[N]bNext [N] to previous buffer
4833 * :[N]bprevious [N] to previous buffer
4834 * :[N]sbNext [N] split and to previous buffer
4835 * :[N]sbprevious [N] split and to previous buffer
4836 */
4837 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004838ex_bprevious(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004839{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004840 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01004841 return;
4842
Bram Moolenaar071d4272004-06-13 20:20:40 +00004843 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004844 if (eap->do_ecmd_cmd != NULL)
4845 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004846}
4847
4848/*
4849 * :brewind to first buffer
4850 * :bfirst to first buffer
4851 * :sbrewind split and to first buffer
4852 * :sbfirst split and to first buffer
4853 */
4854 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004855ex_brewind(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004856{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004857 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01004858 return;
4859
Bram Moolenaar071d4272004-06-13 20:20:40 +00004860 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004861 if (eap->do_ecmd_cmd != NULL)
4862 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004863}
4864
4865/*
4866 * :blast to last buffer
4867 * :sblast split and to last buffer
4868 */
4869 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004870ex_blast(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004871{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004872 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01004873 return;
4874
Bram Moolenaar071d4272004-06-13 20:20:40 +00004875 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004876 if (eap->do_ecmd_cmd != NULL)
4877 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004879
Bram Moolenaar7a092242020-04-16 22:10:49 +02004880/*
4881 * Check if "c" ends an Ex command.
Bram Moolenaara494f562020-04-18 17:45:38 +02004882 * In Vim9 script does not check for white space before # or #{.
Bram Moolenaar7a092242020-04-16 22:10:49 +02004883 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004884 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004885ends_excmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004886{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004887 int comment_char = '"';
4888
Bram Moolenaarfbda69b2020-04-13 15:06:53 +02004889#ifdef FEAT_EVAL
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004890 if (in_vim9script())
4891 comment_char = '#';
Bram Moolenaar7a092242020-04-16 22:10:49 +02004892#endif
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004893 return (c == NUL || c == '|' || c == comment_char || c == '\n');
Bram Moolenaar7a092242020-04-16 22:10:49 +02004894}
4895
4896/*
4897 * Like ends_excmd() but checks that a # in Vim9 script either has "cmd" equal
4898 * to "cmd_start" or has a white space character before it.
4899 */
4900 int
Bram Moolenaar21cfe502020-04-16 23:01:50 +02004901ends_excmd2(char_u *cmd_start UNUSED, char_u *cmd)
Bram Moolenaar7a092242020-04-16 22:10:49 +02004902{
4903 int c = *cmd;
4904
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004905 if (c == NUL || c == '|' || c == '\n')
4906 return TRUE;
Bram Moolenaar7a092242020-04-16 22:10:49 +02004907#ifdef FEAT_EVAL
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004908 if (in_vim9script())
4909 return c == '#' && cmd[1] != '{'
4910 && (cmd == cmd_start || VIM_ISWHITE(cmd[-1]));
Bram Moolenaarfbda69b2020-04-13 15:06:53 +02004911#endif
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004912 return c == '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004913}
4914
4915#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
4916 || defined(PROTO)
4917/*
4918 * Return the next command, after the first '|' or '\n'.
4919 * Return NULL if not found.
4920 */
4921 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004922find_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004923{
4924 while (*p != '|' && *p != '\n')
4925 {
4926 if (*p == NUL)
4927 return NULL;
4928 ++p;
4929 }
4930 return (p + 1);
4931}
4932#endif
4933
4934/*
Bram Moolenaar2256c992016-11-15 21:17:07 +01004935 * Check if *p is a separator between Ex commands, skipping over white space.
4936 * Return NULL if it isn't, the following character if it is.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004937 */
4938 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004939check_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004940{
Bram Moolenaar2256c992016-11-15 21:17:07 +01004941 char_u *s = skipwhite(p);
4942
4943 if (*s == '|' || *s == '\n')
4944 return (s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004945 else
4946 return NULL;
4947}
4948
4949/*
4950 * - if there are more files to edit
4951 * - and this is the last window
4952 * - and forceit not used
4953 * - and not repeated twice on a row
4954 * return FAIL and give error message if 'message' TRUE
4955 * return OK otherwise
4956 */
4957 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004958check_more(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004959 int message, // when FALSE check only, no messages
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004960 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004961{
4962 int n = ARGCOUNT - curwin->w_arg_idx - 1;
4963
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00004964 if (!forceit && only_one_window()
4965 && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004966 {
4967 if (message)
4968 {
4969#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
4970 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
4971 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02004972 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004973
Bram Moolenaarda6e8912018-08-21 15:12:14 +02004974 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
4975 NGETTEXT("%d more file to edit. Quit anyway?",
4976 "%d more files to edit. Quit anyway?", n), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004977 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
4978 return OK;
4979 return FAIL;
4980 }
4981#endif
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01004982 semsg(NGETTEXT("E173: %d more file to edit",
4983 "E173: %d more files to edit", n), n);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004984 quitmore = 2; // next try to quit is allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985 }
4986 return FAIL;
4987 }
4988 return OK;
4989}
4990
Bram Moolenaar071d4272004-06-13 20:20:40 +00004991/*
4992 * Function given to ExpandGeneric() to obtain the list of command names.
4993 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004994 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004995get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996{
4997 if (idx >= (int)CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004998 return get_user_command_name(idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004999 return cmdnames[idx].cmd_name;
5000}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005001
Bram Moolenaar071d4272004-06-13 20:20:40 +00005002 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005003ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005004{
Bram Moolenaare6850792010-05-14 15:28:44 +02005005 if (*eap->arg == NUL)
5006 {
5007#ifdef FEAT_EVAL
5008 char_u *expr = vim_strsave((char_u *)"g:colors_name");
5009 char_u *p = NULL;
5010
5011 if (expr != NULL)
5012 {
5013 ++emsg_off;
Bram Moolenaarb171fb12020-06-24 20:34:03 +02005014 p = eval_to_string(expr, FALSE);
Bram Moolenaare6850792010-05-14 15:28:44 +02005015 --emsg_off;
5016 vim_free(expr);
5017 }
5018 if (p != NULL)
5019 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005020 msg((char *)p);
Bram Moolenaare6850792010-05-14 15:28:44 +02005021 vim_free(p);
5022 }
5023 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005024 msg("default");
Bram Moolenaare6850792010-05-14 15:28:44 +02005025#else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005026 msg(_("unknown"));
Bram Moolenaare6850792010-05-14 15:28:44 +02005027#endif
5028 }
5029 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005030 semsg(_("E185: Cannot find color scheme '%s'"), eap->arg);
Bram Moolenaarf58d81a2019-01-28 20:19:05 +01005031
5032#ifdef FEAT_VTP
5033 else if (has_vtp_working())
5034 {
5035 // background color change requires clear + redraw
5036 update_screen(CLEAR);
5037 redrawcmd();
5038 }
5039#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005040}
5041
5042 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005043ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044{
5045 if (*eap->arg == NUL && eap->cmd[2] == '!')
Bram Moolenaar32526b32019-01-19 17:43:09 +01005046 msg(_("Greetings, Vim user!"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047 do_highlight(eap->arg, eap->forceit, FALSE);
5048}
5049
5050
5051/*
5052 * Call this function if we thought we were going to exit, but we won't
5053 * (because of an error). May need to restore the terminal mode.
5054 */
5055 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005056not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057{
5058 exiting = FALSE;
5059 settmode(TMODE_RAW);
5060}
5061
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005062 static int
5063before_quit_autocmds(win_T *wp, int quit_all, int forceit)
5064{
5065 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, wp->w_buffer);
5066
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02005067 // Bail out when autocommands closed the window.
5068 // Refuse to quit when the buffer in the last window is being closed (can
5069 // only happen in autocommands).
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005070 if (!win_valid(wp)
5071 || curbuf_locked()
5072 || (wp->w_buffer->b_nwindows == 1 && wp->w_buffer->b_locked > 0))
5073 return TRUE;
5074
5075 if (quit_all || (check_more(FALSE, forceit) == OK && only_one_window()))
5076 {
5077 apply_autocmds(EVENT_EXITPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02005078 // Refuse to quit when locked or when the window was closed or the
5079 // buffer in the last window is being closed (can only happen in
5080 // autocommands).
5081 if (!win_valid(wp) || curbuf_locked()
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005082 || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
5083 return TRUE;
5084 }
5085
5086 return FALSE;
5087}
5088
Bram Moolenaar071d4272004-06-13 20:20:40 +00005089/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01005090 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005091 * ":{nr}quit": quit window {nr}
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02005092 * Also used when closing a terminal window that's the last one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005093 */
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02005094 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005095ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005096{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005097 win_T *wp;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005098
Bram Moolenaar071d4272004-06-13 20:20:40 +00005099#ifdef FEAT_CMDWIN
5100 if (cmdwin_type != 0)
5101 {
5102 cmdwin_result = Ctrl_C;
5103 return;
5104 }
5105#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005106 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005107 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005108 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005109 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005110 return;
5111 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005112 if (eap->addr_count > 0)
5113 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01005114 int wnr = eap->line2;
5115
5116 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
5117 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005118 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005119 }
5120 else
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005121 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01005122
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005123 // Refuse to quit when locked.
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02005124 if (curbuf_locked())
5125 return;
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005126
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005127 // Trigger QuitPre and maybe ExitPre
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005128 if (before_quit_autocmds(wp, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005129 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005130
5131#ifdef FEAT_NETBEANS_INTG
5132 netbeansForcedQuit = eap->forceit;
5133#endif
5134
5135 /*
5136 * If there are more files or windows we won't exit.
5137 */
5138 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
5139 exiting = TRUE;
Bram Moolenaarff930ca2017-10-19 17:12:10 +02005140 if ((!buf_hide(wp->w_buffer)
5141 && check_changed(wp->w_buffer, (p_awa ? CCGD_AW : 0)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01005142 | (eap->forceit ? CCGD_FORCEIT : 0)
5143 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005144 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01005145 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005146 {
5147 not_exiting();
5148 }
5149 else
5150 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005151 // quit last window
5152 // Note: only_one_window() returns true, even so a help window is
5153 // still open. In that case only quit, if no address has been
5154 // specified. Example:
5155 // :h|wincmd w|1q - don't quit
5156 // :h|wincmd w|q - quit
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01005157 if (only_one_window() && (ONE_WINDOW || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005158 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02005159 not_exiting();
Bram Moolenaar4033c552017-09-16 20:54:51 +02005160#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005161 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005162#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005163 // close window; may free buffer
Bram Moolenaareb44a682017-08-03 22:44:55 +02005164 win_close(wp, !buf_hide(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005165 }
5166}
5167
5168/*
5169 * ":cquit".
5170 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005171 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005172ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005173{
Bram Moolenaar1860bde2020-01-06 21:47:21 +01005174 // this does not always pass on the exit code to the Manx compiler. why?
5175 getout(eap->addr_count > 0 ? (int)eap->line2 : EXIT_FAILURE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005176}
5177
5178/*
5179 * ":qall": try to quit all windows
5180 */
5181 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005182ex_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005183{
5184# ifdef FEAT_CMDWIN
5185 if (cmdwin_type != 0)
5186 {
5187 if (eap->forceit)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005188 cmdwin_result = K_XF1; // ex_window() takes care of this
Bram Moolenaar071d4272004-06-13 20:20:40 +00005189 else
5190 cmdwin_result = K_XF2;
5191 return;
5192 }
5193# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005194
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005195 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005196 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005197 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005198 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005199 return;
5200 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005201
5202 if (before_quit_autocmds(curwin, TRUE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005203 return;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005204
Bram Moolenaar071d4272004-06-13 20:20:40 +00005205 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01005206 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207 getout(0);
5208 not_exiting();
5209}
5210
Bram Moolenaar071d4272004-06-13 20:20:40 +00005211/*
5212 * ":close": close current window, unless it is the last one
5213 */
5214 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005215ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005216{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005217 win_T *win;
5218 int winnr = 0;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005219#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00005220 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02005221 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005222 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005223#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005224 if (!text_locked() && !curbuf_locked())
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005225 {
5226 if (eap->addr_count == 0)
5227 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar6d41c782018-06-06 09:11:12 +02005228 else
5229 {
Bram Moolenaar29323592016-07-24 22:04:11 +02005230 FOR_ALL_WINDOWS(win)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005231 {
5232 winnr++;
5233 if (winnr == eap->line2)
5234 break;
5235 }
5236 if (win == NULL)
5237 win = lastwin;
5238 ex_win_close(eap->forceit, win, NULL);
5239 }
5240 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005241}
5242
Bram Moolenaar4033c552017-09-16 20:54:51 +02005243#ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005244/*
5245 * ":pclose": Close any preview window.
5246 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005247 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005248ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005249{
5250 win_T *win;
5251
Bram Moolenaar79648732019-07-18 21:43:07 +02005252 // First close any normal window.
Bram Moolenaar29323592016-07-24 22:04:11 +02005253 FOR_ALL_WINDOWS(win)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005254 if (win->w_p_pvw)
5255 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005256 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar79648732019-07-18 21:43:07 +02005257 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005258 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005259# ifdef FEAT_PROP_POPUP
Bram Moolenaar79648732019-07-18 21:43:07 +02005260 // Also when 'previewpopup' is empty, it might have been cleared.
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02005261 popup_close_preview();
Bram Moolenaar79648732019-07-18 21:43:07 +02005262# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005263}
Bram Moolenaar4033c552017-09-16 20:54:51 +02005264#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005265
Bram Moolenaarf740b292006-02-16 22:11:02 +00005266/*
5267 * Close window "win" and take care of handling closing the last window for a
5268 * modified buffer.
5269 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005270 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005271ex_win_close(
5272 int forceit,
5273 win_T *win,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005274 tabpage_T *tp) // NULL or the tab page "win" is in
Bram Moolenaar071d4272004-06-13 20:20:40 +00005275{
5276 int need_hide;
5277 buf_T *buf = win->w_buffer;
5278
Bram Moolenaarcf844172020-06-26 19:44:06 +02005279 // Never close the autocommand window.
5280 if (win == aucmd_win)
5281 {
5282 emsg(_(e_autocmd_close));
5283 return;
5284 }
5285
Bram Moolenaar071d4272004-06-13 20:20:40 +00005286 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02005287 if (need_hide && !buf_hide(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005288 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02005289#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005290 if ((p_confirm || cmdmod.confirm) && p_write)
5291 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005292 bufref_T bufref;
5293
5294 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005295 dialog_changed(buf, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005296 if (bufref_valid(&bufref) && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005297 return;
5298 need_hide = FALSE;
5299 }
5300 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005301#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005302 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02005303 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005304 return;
5305 }
5306 }
5307
Bram Moolenaar4033c552017-09-16 20:54:51 +02005308#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005309 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005310#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005311
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005312 // free buffer when not hiding it or when it's a scratch buffer
Bram Moolenaarf740b292006-02-16 22:11:02 +00005313 if (tp == NULL)
Bram Moolenaareb44a682017-08-03 22:44:55 +02005314 win_close(win, !need_hide && !buf_hide(buf));
Bram Moolenaarf740b292006-02-16 22:11:02 +00005315 else
Bram Moolenaareb44a682017-08-03 22:44:55 +02005316 win_close_othertab(win, !need_hide && !buf_hide(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005317}
5318
Bram Moolenaar071d4272004-06-13 20:20:40 +00005319/*
Bram Moolenaardea25702017-01-29 15:18:10 +01005320 * Handle the argument for a tabpage related ex command.
5321 * Returns a tabpage number.
5322 * When an error is encountered then eap->errmsg is set.
5323 */
5324 static int
5325get_tabpage_arg(exarg_T *eap)
5326{
5327 int tab_number;
5328 int unaccept_arg0 = (eap->cmdidx == CMD_tabmove) ? 0 : 1;
5329
5330 if (eap->arg && *eap->arg != NUL)
5331 {
5332 char_u *p = eap->arg;
5333 char_u *p_save;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005334 int relative = 0; // argument +N/-N means: go to N places to the
5335 // right/left relative to the current position.
Bram Moolenaardea25702017-01-29 15:18:10 +01005336
5337 if (*p == '-')
5338 {
5339 relative = -1;
5340 p++;
5341 }
5342 else if (*p == '+')
5343 {
5344 relative = 1;
5345 p++;
5346 }
5347
5348 p_save = p;
5349 tab_number = getdigits(&p);
5350
5351 if (relative == 0)
5352 {
5353 if (STRCMP(p, "$") == 0)
5354 tab_number = LAST_TAB_NR;
5355 else if (p == p_save || *p_save == '-' || *p != NUL
5356 || tab_number > LAST_TAB_NR)
5357 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005358 // No numbers as argument.
Bram Moolenaardea25702017-01-29 15:18:10 +01005359 eap->errmsg = e_invarg;
5360 goto theend;
5361 }
5362 }
5363 else
5364 {
5365 if (*p_save == NUL)
5366 tab_number = 1;
5367 else if (p == p_save || *p_save == '-' || *p != NUL
5368 || tab_number == 0)
5369 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005370 // No numbers as argument.
Bram Moolenaardea25702017-01-29 15:18:10 +01005371 eap->errmsg = e_invarg;
5372 goto theend;
5373 }
5374 tab_number = tab_number * relative + tabpage_index(curtab);
5375 if (!unaccept_arg0 && relative == -1)
5376 --tab_number;
5377 }
5378 if (tab_number < unaccept_arg0 || tab_number > LAST_TAB_NR)
5379 eap->errmsg = e_invarg;
5380 }
5381 else if (eap->addr_count > 0)
5382 {
5383 if (unaccept_arg0 && eap->line2 == 0)
Bram Moolenaarc6251552017-01-29 21:42:20 +01005384 {
Bram Moolenaardea25702017-01-29 15:18:10 +01005385 eap->errmsg = e_invrange;
Bram Moolenaarc6251552017-01-29 21:42:20 +01005386 tab_number = 0;
5387 }
Bram Moolenaardea25702017-01-29 15:18:10 +01005388 else
5389 {
5390 tab_number = eap->line2;
Bram Moolenaar82a12462019-01-22 22:41:42 +01005391 if (!unaccept_arg0 && *skipwhite(*eap->cmdlinep) == '-')
Bram Moolenaardea25702017-01-29 15:18:10 +01005392 {
5393 --tab_number;
5394 if (tab_number < unaccept_arg0)
5395 eap->errmsg = e_invarg;
5396 }
5397 }
5398 }
5399 else
5400 {
5401 switch (eap->cmdidx)
5402 {
5403 case CMD_tabnext:
5404 tab_number = tabpage_index(curtab) + 1;
5405 if (tab_number > LAST_TAB_NR)
5406 tab_number = 1;
5407 break;
5408 case CMD_tabmove:
5409 tab_number = LAST_TAB_NR;
5410 break;
5411 default:
5412 tab_number = tabpage_index(curtab);
5413 }
5414 }
5415
5416theend:
5417 return tab_number;
5418}
5419
5420/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00005421 * ":tabclose": close current tab page, unless it is the last one.
5422 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005423 */
5424 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005425ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005426{
Bram Moolenaarf740b292006-02-16 22:11:02 +00005427 tabpage_T *tp;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005428 int tab_number;
Bram Moolenaarf740b292006-02-16 22:11:02 +00005429
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005430# ifdef FEAT_CMDWIN
5431 if (cmdwin_type != 0)
5432 cmdwin_result = K_IGNORE;
5433 else
5434# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005435 if (first_tabpage->tp_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005436 emsg(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00005437 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005438 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005439 tab_number = get_tabpage_arg(eap);
5440 if (eap->errmsg == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005441 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005442 tp = find_tabpage(tab_number);
Bram Moolenaarf740b292006-02-16 22:11:02 +00005443 if (tp == NULL)
5444 {
5445 beep_flush();
5446 return;
5447 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005448 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005449 {
5450 tabpage_close_other(tp, eap->forceit);
5451 return;
5452 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005453 else if (!text_locked() && !curbuf_locked())
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005454 tabpage_close(eap->forceit);
5455 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005456 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005457}
5458
5459/*
5460 * ":tabonly": close all tab pages except the current one
5461 */
5462 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005463ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005464{
5465 tabpage_T *tp;
5466 int done;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005467 int tab_number;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005468
5469# ifdef FEAT_CMDWIN
5470 if (cmdwin_type != 0)
5471 cmdwin_result = K_IGNORE;
5472 else
5473# endif
5474 if (first_tabpage->tp_next == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005475 msg(_("Already only one tab page"));
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005476 else
5477 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005478 tab_number = get_tabpage_arg(eap);
5479 if (eap->errmsg == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005480 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005481 goto_tabpage(tab_number);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005482 // Repeat this up to a 1000 times, because autocommands may
5483 // mess up the lists.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005484 for (done = 0; done < 1000; ++done)
5485 {
5486 FOR_ALL_TABPAGES(tp)
5487 if (tp->tp_topframe != topframe)
5488 {
5489 tabpage_close_other(tp, eap->forceit);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005490 // if we failed to close it quit
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005491 if (valid_tabpage(tp))
5492 done = 1000;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005493 // start over, "tp" is now invalid
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005494 break;
5495 }
5496 if (first_tabpage->tp_next == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005497 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005498 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005499 }
5500 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005501}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005502
5503/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00005504 * Close the current tab page.
5505 */
5506 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005507tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005508{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005509 // First close all the windows but the current one. If that worked then
5510 // close the last window in this tab, that will close it.
Bram Moolenaar459ca562016-11-10 18:16:33 +01005511 if (!ONE_WINDOW)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005512 close_others(TRUE, forceit);
Bram Moolenaar459ca562016-11-10 18:16:33 +01005513 if (ONE_WINDOW)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005514 ex_win_close(forceit, curwin, NULL);
5515# ifdef FEAT_GUI
5516 need_mouse_correct = TRUE;
5517# endif
5518}
5519
5520/*
5521 * Close tab page "tp", which is not the current tab page.
5522 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005523 * Also takes care of the tab pages line disappearing when closing the
5524 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00005525 */
5526 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005527tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005528{
5529 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005530 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005531 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00005532
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005533 // Limit to 1000 windows, autocommands may add a window while we close
5534 // one. OK, so I'm paranoid...
Bram Moolenaarf740b292006-02-16 22:11:02 +00005535 while (++done < 1000)
5536 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005537 wp = tp->tp_firstwin;
5538 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00005539
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005540 // Autocommands may delete the tab page under our fingers and we may
5541 // fail to close a window with a modified buffer.
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005542 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005543 break;
5544 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005545
Bram Moolenaar29323592016-07-24 22:04:11 +02005546 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar29323592016-07-24 22:04:11 +02005547
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005548 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005549 if (h != tabline_height())
5550 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00005551}
5552
5553/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005554 * ":only".
5555 */
5556 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005557ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005558{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005559 win_T *wp;
5560 int wnr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005561# ifdef FEAT_GUI
5562 need_mouse_correct = TRUE;
5563# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005564 if (eap->addr_count > 0)
5565 {
5566 wnr = eap->line2;
5567 for (wp = firstwin; --wnr > 0; )
5568 {
5569 if (wp->w_next == NULL)
5570 break;
5571 else
5572 wp = wp->w_next;
5573 }
5574 win_goto(wp);
5575 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005576 close_others(TRUE, eap->forceit);
5577}
5578
Bram Moolenaar071d4272004-06-13 20:20:40 +00005579 static void
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01005580ex_hide(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005581{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005582 // ":hide" or ":hide | cmd": hide current window
Bram Moolenaar2256c992016-11-15 21:17:07 +01005583 if (!eap->skip)
5584 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02005585#ifdef FEAT_GUI
Bram Moolenaar2256c992016-11-15 21:17:07 +01005586 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005587#endif
Bram Moolenaar2256c992016-11-15 21:17:07 +01005588 if (eap->addr_count == 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005589 win_close(curwin, FALSE); // don't free buffer
Bram Moolenaar2256c992016-11-15 21:17:07 +01005590 else
5591 {
5592 int winnr = 0;
5593 win_T *win;
Bram Moolenaarf240e182014-11-27 18:33:02 +01005594
Bram Moolenaar2256c992016-11-15 21:17:07 +01005595 FOR_ALL_WINDOWS(win)
5596 {
5597 winnr++;
5598 if (winnr == eap->line2)
5599 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005600 }
Bram Moolenaar2256c992016-11-15 21:17:07 +01005601 if (win == NULL)
5602 win = lastwin;
5603 win_close(win, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005604 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005605 }
5606}
5607
5608/*
5609 * ":stop" and ":suspend": Suspend Vim.
5610 */
5611 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005612ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005613{
5614 /*
5615 * Disallow suspending for "rvim".
5616 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005617 if (!check_restricted())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005618 {
5619 if (!eap->forceit)
5620 autowrite_all();
5621 windgoto((int)Rows - 1, 0);
5622 out_char('\n');
5623 out_flush();
5624 stoptermcap();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005625 out_flush(); // needed for SUN to restore xterm buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00005626#ifdef FEAT_TITLE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005627 mch_restore_title(SAVE_RESTORE_BOTH); // restore window titles
Bram Moolenaar071d4272004-06-13 20:20:40 +00005628#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005629 ui_suspend(); // call machine specific function
Bram Moolenaar071d4272004-06-13 20:20:40 +00005630#ifdef FEAT_TITLE
5631 maketitle();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005632 resettitle(); // force updating the title
Bram Moolenaar071d4272004-06-13 20:20:40 +00005633#endif
5634 starttermcap();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005635 scroll_start(); // scroll screen before redrawing
Bram Moolenaar071d4272004-06-13 20:20:40 +00005636 redraw_later_clear();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005637 shell_resized(); // may have resized window
Bram Moolenaar071d4272004-06-13 20:20:40 +00005638 }
5639}
5640
5641/*
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005642 * ":exit", ":xit" and ":wq": Write file and quite the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005643 */
5644 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005645ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005646{
5647#ifdef FEAT_CMDWIN
5648 if (cmdwin_type != 0)
5649 {
5650 cmdwin_result = Ctrl_C;
5651 return;
5652 }
5653#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005654 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005655 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005656 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005657 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005658 return;
5659 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005660
5661 if (before_quit_autocmds(curwin, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005662 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005663
5664 /*
5665 * if more files or windows we won't exit
5666 */
5667 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
5668 exiting = TRUE;
5669 if ( ((eap->cmdidx == CMD_wq
5670 || curbufIsChanged())
5671 && do_write(eap) == FAIL)
5672 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01005673 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005674 {
5675 not_exiting();
5676 }
5677 else
5678 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005679 if (only_one_window()) // quit last window, exit Vim
Bram Moolenaar071d4272004-06-13 20:20:40 +00005680 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02005681 not_exiting();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005682# ifdef FEAT_GUI
5683 need_mouse_correct = TRUE;
5684# endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005685 // Quit current window, may free the buffer.
Bram Moolenaareb44a682017-08-03 22:44:55 +02005686 win_close(curwin, !buf_hide(curwin->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005687 }
5688}
5689
5690/*
5691 * ":print", ":list", ":number".
5692 */
5693 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005694ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005695{
Bram Moolenaardf177f62005-02-22 08:39:57 +00005696 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005697 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +00005698 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005699 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00005700 for ( ;!got_int; ui_breakcheck())
5701 {
5702 print_line(eap->line1,
5703 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
5704 || (eap->flags & EXFLAG_NR)),
5705 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
5706 if (++eap->line1 > eap->line2)
5707 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005708 out_flush(); // show one line at a time
Bram Moolenaardf177f62005-02-22 08:39:57 +00005709 }
5710 setpcmark();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005711 // put cursor at last line
Bram Moolenaardf177f62005-02-22 08:39:57 +00005712 curwin->w_cursor.lnum = eap->line2;
5713 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005714 }
5715
Bram Moolenaar071d4272004-06-13 20:20:40 +00005716 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005717}
5718
5719#ifdef FEAT_BYTEOFF
5720 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005721ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005722{
5723 goto_byte(eap->line2);
5724}
5725#endif
5726
5727/*
5728 * ":shell".
5729 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005730 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005731ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005732{
5733 do_shell(NULL, 0);
5734}
5735
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005736#if defined(HAVE_DROP_FILE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005737
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005738static int drop_busy = FALSE;
5739static int drop_filec;
5740static char_u **drop_filev = NULL;
5741static int drop_split;
5742static void (*drop_callback)(void *);
5743static void *drop_cookie;
5744
5745 static void
5746handle_drop_internal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005747{
5748 exarg_T ea;
5749 int save_msg_scroll = msg_scroll;
5750
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005751 // Setting the argument list may cause screen updates and being called
5752 // recursively. Avoid that by setting drop_busy.
5753 drop_busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005754
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005755 // Check whether the current buffer is changed. If so, we will need
5756 // to split the current window or data could be lost.
5757 // We don't need to check if the 'hidden' option is set, as in this
5758 // case the buffer won't be lost.
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005759 if (!buf_hide(curbuf) && !drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005760 {
5761 ++emsg_off;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005762 drop_split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005763 --emsg_off;
5764 }
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005765 if (drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005766 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005767 if (win_split(0, 0) == FAIL)
5768 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02005769 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005770
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005771 // When splitting the window, create a new alist. Otherwise the
5772 // existing one is overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005773 alist_unlink(curwin->w_alist);
5774 alist_new();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005775 }
5776
5777 /*
5778 * Set up the new argument list.
5779 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005780 alist_set(ALIST(curwin), drop_filec, drop_filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005781
5782 /*
5783 * Move to the first file.
5784 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005785 // Fake up a minimal "next" command for do_argfile()
Bram Moolenaara80faa82020-04-12 19:37:17 +02005786 CLEAR_FIELD(ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005787 ea.cmd = (char_u *)"next";
5788 do_argfile(&ea, 0);
5789
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005790 // do_ecmd() may set need_start_insertmode, but since we never left Insert
5791 // mode that is not needed here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005792 need_start_insertmode = FALSE;
5793
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005794 // Restore msg_scroll, otherwise a following command may cause scrolling
5795 // unexpectedly. The screen will be redrawn by the caller, thus
5796 // msg_scroll being set by displaying a message is irrelevant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005797 msg_scroll = save_msg_scroll;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005798
5799 if (drop_callback != NULL)
5800 drop_callback(drop_cookie);
5801
5802 drop_filev = NULL;
5803 drop_busy = FALSE;
5804}
5805
5806/*
5807 * Handle a file drop. The code is here because a drop is *nearly* like an
5808 * :args command, but not quite (we have a list of exact filenames, so we
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01005809 * don't want to (a) parse a command line, or (b) expand wildcards). So the
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005810 * code is very similar to :args and hence needs access to a lot of the static
5811 * functions in this file.
5812 *
5813 * The "filev" list must have been allocated using alloc(), as should each item
5814 * in the list. This function takes over responsibility for freeing the "filev"
5815 * list.
5816 */
5817 void
5818handle_drop(
5819 int filec, // the number of files dropped
5820 char_u **filev, // the list of files dropped
5821 int split, // force splitting the window
5822 void (*callback)(void *), // to be called after setting the argument
5823 // list
5824 void *cookie) // argument for "callback" (allocated)
5825{
5826 // Cannot handle recursive drops, finish the pending one.
5827 if (drop_busy)
5828 {
5829 FreeWild(filec, filev);
5830 vim_free(cookie);
5831 return;
5832 }
5833
5834 // When calling handle_drop() more than once in a row we only use the last
5835 // one.
5836 if (drop_filev != NULL)
5837 {
5838 FreeWild(drop_filec, drop_filev);
5839 vim_free(drop_cookie);
5840 }
5841
5842 drop_filec = filec;
5843 drop_filev = filev;
5844 drop_split = split;
5845 drop_callback = callback;
5846 drop_cookie = cookie;
5847
5848 // Postpone this when:
5849 // - editing the command line
5850 // - not possible to change the current buffer
5851 // - updating the screen
5852 // As it may change buffers and window structures that are in use and cause
5853 // freed memory to be used.
5854 if (text_locked() || curbuf_locked() || updating_screen)
5855 return;
5856
5857 handle_drop_internal();
5858}
5859
5860/*
5861 * To be called when text is unlocked, curbuf is unlocked or updating_screen is
5862 * reset: Handle a postponed drop.
5863 */
5864 void
5865handle_any_postponed_drop(void)
5866{
5867 if (!drop_busy && drop_filev != NULL
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02005868 && !text_locked() && !curbuf_locked() && !updating_screen)
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005869 handle_drop_internal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005870}
5871#endif
5872
Bram Moolenaar071d4272004-06-13 20:20:40 +00005873/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005874 * ":preserve".
5875 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005876 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005877ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005878{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005879 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005880 ml_preserve(curbuf, TRUE);
5881}
5882
5883/*
5884 * ":recover".
5885 */
5886 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005887ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005888{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005889 // Set recoverymode right away to avoid the ATTENTION prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005890 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01005891 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
5892 | CCGD_MULTWIN
5893 | (eap->forceit ? CCGD_FORCEIT : 0)
5894 | CCGD_EXCMD)
5895
Bram Moolenaar071d4272004-06-13 20:20:40 +00005896 && (*eap->arg == NUL
5897 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
Bram Moolenaar99499b12019-05-23 21:35:48 +02005898 ml_recover(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005899 recoverymode = FALSE;
5900}
5901
5902/*
5903 * Command modifier used in a wrong way.
5904 */
5905 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005906ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005907{
5908 eap->errmsg = e_invcmd;
5909}
5910
Bram Moolenaar071d4272004-06-13 20:20:40 +00005911/*
5912 * :sview [+command] file split window with new file, read-only
5913 * :split [[+command] file] split window with current or new file
5914 * :vsplit [[+command] file] split window vertically with current or new file
5915 * :new [[+command] file] split window with no or new file
5916 * :vnew [[+command] file] split vertically window with no or new file
5917 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005918 *
5919 * :tabedit open new Tab page with empty window
5920 * :tabedit [+command] file open new Tab page and edit "file"
5921 * :tabnew [[+command] file] just like :tabedit
5922 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005923 */
5924 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005925ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005926{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005927 win_T *old_curwin = curwin;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005928#if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005929 char_u *fname = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005930#endif
5931#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005932 int browse_flag = cmdmod.browse;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005933#endif
Bram Moolenaar39902a02018-06-21 22:10:08 +02005934 int use_tab = eap->cmdidx == CMD_tabedit
5935 || eap->cmdidx == CMD_tabfind
5936 || eap->cmdidx == CMD_tabnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005937
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005938 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005939 return;
5940
Bram Moolenaar4033c552017-09-16 20:54:51 +02005941#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005942 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005943#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005944
Bram Moolenaar4033c552017-09-16 20:54:51 +02005945#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005946 // A ":split" in the quickfix window works like ":new". Don't want two
5947 // quickfix windows. But it's OK when doing ":tab split".
Bram Moolenaar05bb9532008-07-04 09:44:11 +00005948 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005949 {
5950 if (eap->cmdidx == CMD_split)
5951 eap->cmdidx = CMD_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005952 if (eap->cmdidx == CMD_vsplit)
5953 eap->cmdidx = CMD_vnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005954 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02005955#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005956
Bram Moolenaar4033c552017-09-16 20:54:51 +02005957#ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005958 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005959 {
5960 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
5961 FNAME_MESS, TRUE, curbuf->b_ffname);
5962 if (fname == NULL)
5963 goto theend;
5964 eap->arg = fname;
5965 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005966# ifdef FEAT_BROWSE
Bram Moolenaar4033c552017-09-16 20:54:51 +02005967 else
5968# endif
5969#endif
5970#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005971 if (cmdmod.browse
Bram Moolenaar071d4272004-06-13 20:20:40 +00005972 && eap->cmdidx != CMD_vnew
Bram Moolenaar071d4272004-06-13 20:20:40 +00005973 && eap->cmdidx != CMD_new)
5974 {
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005975 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005976# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005977 !gui.in_use &&
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005978# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005979 au_has_group((char_u *)"FileExplorer"))
5980 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005981 // No browsing supported but we do have the file explorer:
5982 // Edit the directory.
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005983 if (*eap->arg == NUL || !mch_isdir(eap->arg))
5984 eap->arg = (char_u *)".";
5985 }
5986 else
5987 {
Bram Moolenaar39902a02018-06-21 22:10:08 +02005988 fname = do_browse(0, (char_u *)(use_tab
5989 ? _("Edit File in new tab page")
5990 : _("Edit File in new window")),
Bram Moolenaar071d4272004-06-13 20:20:40 +00005991 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005992 if (fname == NULL)
5993 goto theend;
5994 eap->arg = fname;
5995 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005996 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005997 cmdmod.browse = FALSE; // Don't browse again in do_ecmd().
Bram Moolenaar4033c552017-09-16 20:54:51 +02005998#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005999
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006000 /*
6001 * Either open new tab page or split the window.
6002 */
Bram Moolenaar39902a02018-06-21 22:10:08 +02006003 if (use_tab)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006004 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006005 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
6006 : eap->addr_count == 0 ? 0
6007 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006008 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00006009 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006010
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006011 // set the alternate buffer for the window we came from
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006012 if (curwin != old_curwin
6013 && win_valid(old_curwin)
6014 && old_curwin->w_buffer != curbuf
6015 && !cmdmod.keepalt)
6016 old_curwin->w_alt_fnum = curbuf->b_fnum;
6017 }
6018 }
6019 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006020 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
6021 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006022 // Reset 'scrollbind' when editing another file, but keep it when
6023 // doing ":split" without arguments.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006024 if (*eap->arg != NUL
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01006025# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006026 || cmdmod.browse
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01006027# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006028 )
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006029 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006030 else
6031 do_check_scrollbind(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006032 do_exedit(eap, old_curwin);
6033 }
6034
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006035# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006036 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006037# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006038
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006039# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006040theend:
6041 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006042# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006043}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006044
6045/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006046 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006047 */
6048 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006049tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006050{
6051 exarg_T ea;
6052
Bram Moolenaara80faa82020-04-12 19:37:17 +02006053 CLEAR_FIELD(ea);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006054 ea.cmdidx = CMD_tabnew;
6055 ea.cmd = (char_u *)"tabn";
6056 ea.arg = (char_u *)"";
6057 ex_splitview(&ea);
6058}
6059
6060/*
6061 * :tabnext command
6062 */
6063 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006064ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006065{
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006066 int tab_number;
6067
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02006068 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006069 return;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006070 switch (eap->cmdidx)
6071 {
6072 case CMD_tabfirst:
6073 case CMD_tabrewind:
6074 goto_tabpage(1);
6075 break;
6076 case CMD_tablast:
6077 goto_tabpage(9999);
6078 break;
6079 case CMD_tabprevious:
6080 case CMD_tabNext:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006081 if (eap->arg && *eap->arg != NUL)
6082 {
6083 char_u *p = eap->arg;
6084 char_u *p_save = p;
6085
6086 tab_number = getdigits(&p);
6087 if (p == p_save || *p_save == '-' || *p != NUL
6088 || tab_number == 0)
6089 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006090 // No numbers as argument.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006091 eap->errmsg = e_invarg;
6092 return;
6093 }
6094 }
6095 else
6096 {
6097 if (eap->addr_count == 0)
6098 tab_number = 1;
6099 else
6100 {
6101 tab_number = eap->line2;
6102 if (tab_number < 1)
6103 {
6104 eap->errmsg = e_invrange;
6105 return;
6106 }
6107 }
6108 }
6109 goto_tabpage(-tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006110 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006111 default: // CMD_tabnext
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006112 tab_number = get_tabpage_arg(eap);
6113 if (eap->errmsg == NULL)
6114 goto_tabpage(tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006115 break;
6116 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006117}
6118
6119/*
6120 * :tabmove command
6121 */
6122 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006123ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006124{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02006125 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02006126
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006127 tab_number = get_tabpage_arg(eap);
6128 if (eap->errmsg == NULL)
6129 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006130}
6131
6132/*
6133 * :tabs command: List tabs and their contents.
6134 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006135 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006136ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006137{
6138 tabpage_T *tp;
6139 win_T *wp;
6140 int tabcount = 1;
6141
6142 msg_start();
6143 msg_scroll = TRUE;
6144 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
6145 {
6146 msg_putchar('\n');
6147 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
Bram Moolenaar8820b482017-03-16 17:23:31 +01006148 msg_outtrans_attr(IObuff, HL_ATTR(HLF_T));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006149 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006150 ui_breakcheck();
6151
Bram Moolenaar030f0df2006-02-21 22:02:53 +00006152 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006153 wp = firstwin;
6154 else
6155 wp = tp->tp_firstwin;
6156 for ( ; wp != NULL && !got_int; wp = wp->w_next)
6157 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006158 msg_putchar('\n');
6159 msg_putchar(wp == curwin ? '>' : ' ');
6160 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006161 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
6162 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006163 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02006164 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006165 else
6166 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
6167 IObuff, IOSIZE, TRUE);
6168 msg_outtrans(IObuff);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006169 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006170 ui_breakcheck();
6171 }
6172 }
6173}
6174
Bram Moolenaar071d4272004-06-13 20:20:40 +00006175/*
6176 * ":mode": Set screen mode.
6177 * If no argument given, just get the screen size and redraw.
6178 */
6179 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006180ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006181{
6182 if (*eap->arg == NUL)
6183 shell_resized();
6184 else
Bram Moolenaar3c71aec2020-01-17 19:32:20 +01006185 emsg(_(e_screenmode));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006186}
6187
Bram Moolenaar071d4272004-06-13 20:20:40 +00006188/*
6189 * ":resize".
6190 * set, increment or decrement current window height
6191 */
6192 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006193ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006194{
6195 int n;
6196 win_T *wp = curwin;
6197
6198 if (eap->addr_count > 0)
6199 {
6200 n = eap->line2;
6201 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
6202 ;
6203 }
6204
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006205# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006206 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006207# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006208 n = atol((char *)eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006209 if (cmdmod.split & WSP_VERT)
6210 {
6211 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar02631462017-09-22 15:20:32 +02006212 n += curwin->w_width;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006213 else if (n == 0 && eap->arg[0] == NUL) // default is very wide
Bram Moolenaar071d4272004-06-13 20:20:40 +00006214 n = 9999;
6215 win_setwidth_win((int)n, wp);
6216 }
6217 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006218 {
6219 if (*eap->arg == '-' || *eap->arg == '+')
6220 n += curwin->w_height;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006221 else if (n == 0 && eap->arg[0] == NUL) // default is very high
Bram Moolenaar071d4272004-06-13 20:20:40 +00006222 n = 9999;
6223 win_setheight_win((int)n, wp);
6224 }
6225}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006226
6227/*
6228 * ":find [+command] <file>" command.
6229 */
6230 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006231ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006232{
6233#ifdef FEAT_SEARCHPATH
6234 char_u *fname;
6235 int count;
6236
6237 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
6238 TRUE, curbuf->b_ffname);
6239 if (eap->addr_count > 0)
6240 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006241 // Repeat finding the file "count" times. This matters when it
6242 // appears several times in the path.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006243 count = eap->line2;
6244 while (fname != NULL && --count > 0)
6245 {
6246 vim_free(fname);
6247 fname = find_file_in_path(NULL, 0, FNAME_MESS,
6248 FALSE, curbuf->b_ffname);
6249 }
6250 }
6251
6252 if (fname != NULL)
6253 {
6254 eap->arg = fname;
6255#endif
6256 do_exedit(eap, NULL);
6257#ifdef FEAT_SEARCHPATH
6258 vim_free(fname);
6259 }
6260#endif
6261}
6262
6263/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00006264 * ":open" simulation: for now just work like ":visual".
6265 */
6266 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006267ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006268{
6269 regmatch_T regmatch;
6270 char_u *p;
6271
6272 curwin->w_cursor.lnum = eap->line2;
6273 beginline(BL_SOL | BL_FIX);
6274 if (*eap->arg == '/')
6275 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006276 // ":open /pattern/": put cursor in column found with pattern
Bram Moolenaardf177f62005-02-22 08:39:57 +00006277 ++eap->arg;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006278 p = skip_regexp(eap->arg, '/', p_magic);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006279 *p = NUL;
6280 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
6281 if (regmatch.regprog != NULL)
6282 {
6283 regmatch.rm_ic = p_ic;
6284 p = ml_get_curline();
6285 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006286 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006287 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006288 emsg(_(e_nomatch));
Bram Moolenaar473de612013-06-08 18:19:48 +02006289 vim_regfree(regmatch.regprog);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006290 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006291 // Move to the NUL, ignore any other arguments.
Bram Moolenaardf177f62005-02-22 08:39:57 +00006292 eap->arg += STRLEN(eap->arg);
6293 }
6294 check_cursor();
6295
6296 eap->cmdidx = CMD_visual;
6297 do_exedit(eap, NULL);
6298}
6299
6300/*
6301 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006302 */
6303 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006304ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006305{
6306 do_exedit(eap, NULL);
6307}
6308
6309/*
Bram Moolenaar4b96df52020-01-26 22:00:26 +01006310 * ":edit <file>" command and alike.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006311 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006312 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006313do_exedit(
6314 exarg_T *eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006315 win_T *old_curwin) // curwin before doing a split or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006316{
6317 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006318 int need_hide;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006319 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006320
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01006321 if ((eap->cmdidx != CMD_pedit && ERROR_IF_POPUP_WINDOW)
6322 || ERROR_IF_TERM_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006323 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006324 /*
6325 * ":vi" command ends Ex mode.
6326 */
6327 if (exmode_active && (eap->cmdidx == CMD_visual
6328 || eap->cmdidx == CMD_view))
6329 {
6330 exmode_active = FALSE;
Bram Moolenaar158ea172020-06-18 17:28:39 +02006331 ex_pressedreturn = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006332 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006333 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006334 // Special case: ":global/pat/visual\NLvi-commands"
Bram Moolenaardf177f62005-02-22 08:39:57 +00006335 if (global_busy)
6336 {
6337 int rd = RedrawingDisabled;
6338 int nwr = no_wait_return;
6339 int ms = msg_scroll;
6340#ifdef FEAT_GUI
6341 int he = hold_gui_events;
6342#endif
6343
6344 if (eap->nextcmd != NULL)
6345 {
6346 stuffReadbuff(eap->nextcmd);
6347 eap->nextcmd = NULL;
6348 }
6349
6350 if (exmode_was != EXMODE_VIM)
6351 settmode(TMODE_RAW);
6352 RedrawingDisabled = 0;
6353 no_wait_return = 0;
6354 need_wait_return = FALSE;
6355 msg_scroll = 0;
6356#ifdef FEAT_GUI
6357 hold_gui_events = 0;
6358#endif
6359 must_redraw = CLEAR;
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01006360 pending_exmode_active = TRUE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006361
6362 main_loop(FALSE, TRUE);
6363
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01006364 pending_exmode_active = FALSE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006365 RedrawingDisabled = rd;
6366 no_wait_return = nwr;
6367 msg_scroll = ms;
6368#ifdef FEAT_GUI
6369 hold_gui_events = he;
6370#endif
6371 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006372 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006373 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006374 }
6375
6376 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006377 || eap->cmdidx == CMD_tabnew
6378 || eap->cmdidx == CMD_tabedit
Bram Moolenaar4033c552017-09-16 20:54:51 +02006379 || eap->cmdidx == CMD_vnew) && *eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006380 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006381 // ":new" or ":tabnew" without argument: edit an new empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006382 setpcmark();
6383 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006384 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
6385 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006386 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006387 else if ((eap->cmdidx != CMD_split && eap->cmdidx != CMD_vsplit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006388 || *eap->arg != NUL
6389#ifdef FEAT_BROWSE
6390 || cmdmod.browse
6391#endif
6392 )
6393 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006394 // Can't edit another file when "curbuf_lock" is set. Only ":edit"
6395 // can bring us here, others are stopped earlier.
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006396 if (*eap->arg != NUL && curbuf_locked())
6397 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006398
Bram Moolenaar071d4272004-06-13 20:20:40 +00006399 n = readonlymode;
6400 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
6401 readonlymode = TRUE;
6402 else if (eap->cmdidx == CMD_enew)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006403 readonlymode = FALSE; // 'readonly' doesn't make sense in an
6404 // empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006405 setpcmark();
6406 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
6407 NULL, eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006408 // ":edit" goes to first line if Vi compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00006409 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
6410 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
6411 ? ECMD_ONE : eap->do_ecmd_lnum,
Bram Moolenaareb44a682017-08-03 22:44:55 +02006412 (buf_hide(curbuf) ? ECMD_HIDE : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006413 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006414 // after a split we can use an existing buffer
Bram Moolenaar7b449342014-03-25 13:03:48 +01006415 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006416 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006417 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006418 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006419 // Editing the file failed. If the window was split, close it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006420 if (old_curwin != NULL)
6421 {
6422 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02006423 if (!need_hide || buf_hide(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006424 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006425#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006426 cleanup_T cs;
6427
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006428 // Reset the error/interrupt/exception state here so that
6429 // aborting() returns FALSE when closing a window.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006430 enter_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02006431#endif
6432#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006433 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006434#endif
Bram Moolenaareb44a682017-08-03 22:44:55 +02006435 win_close(curwin, !need_hide && !buf_hide(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006436
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006437#if defined(FEAT_EVAL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006438 // Restore the error/interrupt/exception state if not
6439 // discarded by a new aborting error, interrupt, or
6440 // uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006441 leave_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02006442#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006443 }
6444 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006445 }
6446 else if (readonlymode && curbuf->b_nwindows == 1)
6447 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006448 // When editing an already visited buffer, 'readonly' won't be set
6449 // but the previous value is kept. With ":view" and ":sview" we
6450 // want the file to be readonly, except when another window is
6451 // editing the same buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006452 curbuf->b_p_ro = TRUE;
6453 }
6454 readonlymode = n;
6455 }
6456 else
6457 {
6458 if (eap->do_ecmd_cmd != NULL)
6459 do_cmdline_cmd(eap->do_ecmd_cmd);
6460#ifdef FEAT_TITLE
6461 n = curwin->w_arg_idx_invalid;
6462#endif
6463 check_arg_idx(curwin);
6464#ifdef FEAT_TITLE
6465 if (n != curwin->w_arg_idx_invalid)
6466 maketitle();
6467#endif
6468 }
6469
Bram Moolenaar071d4272004-06-13 20:20:40 +00006470 /*
6471 * if ":split file" worked, set alternate file name in old window to new
6472 * file
6473 */
6474 if (old_curwin != NULL
6475 && *eap->arg != NUL
6476 && curwin != old_curwin
6477 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00006478 && old_curwin->w_buffer != curbuf
6479 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006480 old_curwin->w_alt_fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006481
6482 ex_no_reprint = TRUE;
6483}
6484
6485#ifndef FEAT_GUI
6486/*
6487 * ":gui" and ":gvim" when there is no GUI.
6488 */
6489 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006490ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006491{
6492 eap->errmsg = e_nogvim;
6493}
6494#endif
6495
Bram Moolenaar4f974752019-02-17 17:44:42 +01006496#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006497 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006498ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006499{
6500 gui_make_tearoff(eap->arg);
6501}
6502#endif
6503
Bram Moolenaar29a2c082018-03-05 21:06:23 +01006504#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
6505 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006506 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006507ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006508{
Bram Moolenaar29a2c082018-03-05 21:06:23 +01006509# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
6510 if (gui.in_use)
6511 gui_make_popup(eap->arg, eap->forceit);
6512# ifdef FEAT_TERM_POPUP_MENU
6513 else
6514# endif
6515# endif
6516# ifdef FEAT_TERM_POPUP_MENU
6517 pum_make_popup(eap->arg, eap->forceit);
6518# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006519}
6520#endif
6521
Bram Moolenaar071d4272004-06-13 20:20:40 +00006522 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006523ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006524{
6525 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006526 msg(_("No swap file"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006527 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01006528 msg((char *)curbuf->b_ml.ml_mfp->mf_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006529}
6530
6531/*
6532 * ":syncbind" forces all 'scrollbind' windows to have the same relative
6533 * offset.
6534 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
6535 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006536 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006537ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006538{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006539 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006540 win_T *save_curwin = curwin;
6541 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006542 long topline;
6543 long y;
6544 linenr_T old_linenr = curwin->w_cursor.lnum;
6545
6546 setpcmark();
6547
6548 /*
6549 * determine max topline
6550 */
6551 if (curwin->w_p_scb)
6552 {
6553 topline = curwin->w_topline;
Bram Moolenaar29323592016-07-24 22:04:11 +02006554 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006555 {
6556 if (wp->w_p_scb && wp->w_buffer)
6557 {
Bram Moolenaar375e3392019-01-31 18:26:10 +01006558 y = wp->w_buffer->b_ml.ml_line_count - get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006559 if (topline > y)
6560 topline = y;
6561 }
6562 }
6563 if (topline < 1)
6564 topline = 1;
6565 }
6566 else
6567 {
6568 topline = 1;
6569 }
6570
6571
6572 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006573 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006574 */
Bram Moolenaar29323592016-07-24 22:04:11 +02006575 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006576 {
6577 if (curwin->w_p_scb)
6578 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006579 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006580 y = topline - curwin->w_topline;
6581 if (y > 0)
6582 scrollup(y, TRUE);
6583 else
6584 scrolldown(-y, TRUE);
6585 curwin->w_scbind_pos = topline;
6586 redraw_later(VALID);
6587 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006588 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006589 }
6590 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006591 curwin = save_curwin;
6592 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006593 if (curwin->w_p_scb)
6594 {
6595 did_syncbind = TRUE;
6596 checkpcmark();
6597 if (old_linenr != curwin->w_cursor.lnum)
6598 {
6599 char_u ctrl_o[2];
6600
6601 ctrl_o[0] = Ctrl_O;
6602 ctrl_o[1] = 0;
6603 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
6604 }
6605 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006606}
6607
6608
6609 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006610ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006611{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006612 int i;
6613 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
6614 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006615
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006616 if (eap->usefilter) // :r!cmd
Bram Moolenaar071d4272004-06-13 20:20:40 +00006617 do_bang(1, eap, FALSE, FALSE, TRUE);
6618 else
6619 {
6620 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
6621 return;
6622
6623#ifdef FEAT_BROWSE
6624 if (cmdmod.browse)
6625 {
6626 char_u *browseFile;
6627
Bram Moolenaar7171abe2004-10-11 10:06:20 +00006628 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006629 NULL, NULL, NULL, curbuf);
6630 if (browseFile != NULL)
6631 {
6632 i = readfile(browseFile, NULL,
6633 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
6634 vim_free(browseFile);
6635 }
6636 else
6637 i = OK;
6638 }
6639 else
6640#endif
6641 if (*eap->arg == NUL)
6642 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006643 if (check_fname() == FAIL) // check for no file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00006644 return;
6645 i = readfile(curbuf->b_ffname, curbuf->b_fname,
6646 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
6647 }
6648 else
6649 {
6650 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
6651 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
6652 i = readfile(eap->arg, NULL,
6653 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
6654
6655 }
Bram Moolenaare13b9af2017-01-13 22:01:02 +01006656 if (i != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006657 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006658#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006659 if (!aborting())
6660#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006661 semsg(_(e_notopen), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006662 }
6663 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00006664 {
6665 if (empty && exmode_active)
6666 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006667 // Delete the empty line that remains. Historically ex does
6668 // this but vi doesn't.
Bram Moolenaardf177f62005-02-22 08:39:57 +00006669 if (eap->line2 == 0)
6670 lnum = curbuf->b_ml.ml_line_count;
6671 else
6672 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00006673 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006674 {
Bram Moolenaarca70c072020-05-30 20:30:46 +02006675 ml_delete(lnum);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00006676 if (curwin->w_cursor.lnum > 1
6677 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006678 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00006679 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006680 }
6681 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006682 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006683 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006684 }
6685}
6686
Bram Moolenaarea408852005-06-25 22:49:46 +00006687static char_u *prev_dir = NULL;
6688
6689#if defined(EXITFREE) || defined(PROTO)
6690 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006691free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00006692{
Bram Moolenaard23a8232018-02-10 18:45:26 +01006693 VIM_CLEAR(prev_dir);
6694 VIM_CLEAR(globaldir);
Bram Moolenaarea408852005-06-25 22:49:46 +00006695}
6696#endif
6697
Bram Moolenaarf4258302013-06-02 18:20:17 +02006698/*
Bram Moolenaar002bc792020-06-05 22:33:42 +02006699 * Get the previous directory for the given chdir scope.
6700 */
6701 static char_u *
6702get_prevdir(cdscope_T scope)
6703{
6704 if (scope == CDSCOPE_WINDOW)
6705 return curwin->w_prevdir;
6706 else if (scope == CDSCOPE_TABPAGE)
6707 return curtab->tp_prevdir;
6708 return prev_dir;
6709}
6710
6711/*
Bram Moolenaarf4258302013-06-02 18:20:17 +02006712 * Deal with the side effects of changing the current directory.
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006713 * When 'scope' is CDSCOPE_TABPAGE then this was after an ":tcd" command.
6714 * When 'scope' is CDSCOPE_WINDOW then this was after an ":lcd" command.
Bram Moolenaarf4258302013-06-02 18:20:17 +02006715 */
6716 void
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006717post_chdir(cdscope_T scope)
Bram Moolenaarf4258302013-06-02 18:20:17 +02006718{
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006719 if (scope != CDSCOPE_WINDOW)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006720 // Clear tab local directory for both :cd and :tcd
6721 VIM_CLEAR(curtab->tp_localdir);
Bram Moolenaard23a8232018-02-10 18:45:26 +01006722 VIM_CLEAR(curwin->w_localdir);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006723 if (scope != CDSCOPE_GLOBAL)
Bram Moolenaarf4258302013-06-02 18:20:17 +02006724 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02006725 char_u *pdir = get_prevdir(scope);
6726
6727 // If still in the global directory, need to remember current
6728 // directory as the global directory.
6729 if (globaldir == NULL && pdir != NULL)
6730 globaldir = vim_strsave(pdir);
6731
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006732 // Remember this local directory for the window.
Bram Moolenaarf4258302013-06-02 18:20:17 +02006733 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006734 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006735 if (scope == CDSCOPE_TABPAGE)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006736 curtab->tp_localdir = vim_strsave(NameBuff);
6737 else
6738 curwin->w_localdir = vim_strsave(NameBuff);
6739 }
Bram Moolenaarf4258302013-06-02 18:20:17 +02006740 }
6741 else
6742 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02006743 // We are now in the global directory, no need to remember its name.
Bram Moolenaard23a8232018-02-10 18:45:26 +01006744 VIM_CLEAR(globaldir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006745 }
6746
6747 shorten_fnames(TRUE);
6748}
6749
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006750/*
6751 * Change directory function used by :cd/:tcd/:lcd Ex commands and the
Bram Moolenaar95058722020-06-01 16:26:19 +02006752 * chdir() function.
6753 * scope == CDSCOPE_WINDOW: changes the window-local directory
6754 * scope == CDSCOPE_TABPAGE: changes the tab-local directory
6755 * Otherwise: changes the global directory
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006756 * Returns TRUE if the directory is successfully changed.
6757 */
6758 int
6759changedir_func(
6760 char_u *new_dir,
6761 int forceit,
6762 cdscope_T scope)
6763{
6764 char_u *tofree;
Bram Moolenaar002bc792020-06-05 22:33:42 +02006765 char_u *pdir = NULL;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006766 int dir_differs;
6767 int retval = FALSE;
6768
Bram Moolenaar7cc96922020-01-31 22:41:38 +01006769 if (new_dir == NULL || allbuf_locked())
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006770 return FALSE;
6771
6772 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged() && !forceit)
6773 {
6774 emsg(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
6775 return FALSE;
6776 }
6777
6778 // ":cd -": Change to previous directory
6779 if (STRCMP(new_dir, "-") == 0)
6780 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02006781 pdir = get_prevdir(scope);
6782 if (pdir == NULL)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006783 {
6784 emsg(_("E186: No previous directory"));
6785 return FALSE;
6786 }
Bram Moolenaar002bc792020-06-05 22:33:42 +02006787 new_dir = pdir;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006788 }
6789
Bram Moolenaar002bc792020-06-05 22:33:42 +02006790 // Free the previous directory
6791 tofree = get_prevdir(scope);
6792
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006793 // Save current directory for next ":cd -"
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006794 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar002bc792020-06-05 22:33:42 +02006795 pdir = vim_strsave(NameBuff);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006796 else
Bram Moolenaar002bc792020-06-05 22:33:42 +02006797 pdir = NULL;
6798 if (scope == CDSCOPE_WINDOW)
6799 curwin->w_prevdir = pdir;
6800 else if (scope == CDSCOPE_TABPAGE)
6801 curtab->tp_prevdir = pdir;
6802 else
6803 prev_dir = pdir;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006804
6805#if defined(UNIX) || defined(VMS)
6806 // for UNIX ":cd" means: go to home directory
6807 if (*new_dir == NUL)
6808 {
6809 // use NameBuff for home directory name
6810# ifdef VMS
6811 char_u *p;
6812
6813 p = mch_getenv((char_u *)"SYS$LOGIN");
6814 if (p == NULL || *p == NUL) // empty is the same as not set
6815 NameBuff[0] = NUL;
6816 else
6817 vim_strncpy(NameBuff, p, MAXPATHL - 1);
6818# else
6819 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
6820# endif
6821 new_dir = NameBuff;
6822 }
6823#endif
Bram Moolenaar002bc792020-06-05 22:33:42 +02006824 dir_differs = new_dir == NULL || pdir == NULL
6825 || pathcmp((char *)pdir, (char *)new_dir, -1) != 0;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006826 if (new_dir == NULL || (dir_differs && vim_chdir(new_dir)))
6827 emsg(_(e_failed));
6828 else
6829 {
6830 char_u *acmd_fname;
6831
6832 post_chdir(scope);
6833
6834 if (dir_differs)
6835 {
6836 if (scope == CDSCOPE_WINDOW)
6837 acmd_fname = (char_u *)"window";
6838 else if (scope == CDSCOPE_TABPAGE)
6839 acmd_fname = (char_u *)"tabpage";
6840 else
6841 acmd_fname = (char_u *)"global";
6842 apply_autocmds(EVENT_DIRCHANGED, acmd_fname, new_dir, FALSE,
6843 curbuf);
6844 }
6845 retval = TRUE;
6846 }
6847 vim_free(tofree);
6848
6849 return retval;
6850}
Bram Moolenaarea408852005-06-25 22:49:46 +00006851
Bram Moolenaar071d4272004-06-13 20:20:40 +00006852/*
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006853 * ":cd", ":tcd", ":lcd", ":chdir" ":tchdir" and ":lchdir".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006854 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00006855 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006856ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006857{
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01006858 char_u *new_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006859
6860 new_dir = eap->arg;
6861#if !defined(UNIX) && !defined(VMS)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006862 // for non-UNIX ":cd" means: print current directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006863 if (*new_dir == NUL)
6864 ex_pwd(NULL);
6865 else
6866#endif
6867 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006868 cdscope_T scope = CDSCOPE_GLOBAL;
6869
6870 if (eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir)
6871 scope = CDSCOPE_WINDOW;
6872 else if (eap->cmdidx == CMD_tcd || eap->cmdidx == CMD_tchdir)
6873 scope = CDSCOPE_TABPAGE;
6874
6875 if (changedir_func(new_dir, eap->forceit, scope))
Bram Moolenaardf177f62005-02-22 08:39:57 +00006876 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006877 // Echo the new current directory if the command was typed.
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00006878 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006879 ex_pwd(eap);
6880 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006881 }
6882}
6883
6884/*
6885 * ":pwd".
6886 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006887 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006888ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006889{
6890 if (mch_dirname(NameBuff, MAXPATHL) == OK)
6891 {
6892#ifdef BACKSLASH_IN_FILENAME
6893 slash_adjust(NameBuff);
6894#endif
Bram Moolenaar95058722020-06-01 16:26:19 +02006895 if (p_verbose > 0)
6896 {
6897 char *context = "global";
6898
6899 if (curwin->w_localdir != NULL)
6900 context = "window";
6901 else if (curtab->tp_localdir != NULL)
6902 context = "tabpage";
6903 smsg("[%s] %s", context, (char *)NameBuff);
6904 }
6905 else
6906 msg((char *)NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006907 }
6908 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006909 emsg(_("E187: Unknown"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006910}
6911
6912/*
6913 * ":=".
6914 */
6915 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006916ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006917{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006918 smsg("%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006919 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006920}
6921
6922 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006923ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006924{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006925 int n;
6926 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006927
6928 if (cursor_valid())
6929 {
6930 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
6931 if (n >= 0)
Bram Moolenaar53f81742017-09-22 14:35:51 +02006932 windgoto((int)n, curwin->w_wincol + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006933 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006934
6935 len = eap->line2;
6936 switch (*eap->arg)
6937 {
6938 case 'm': break;
6939 case NUL: len *= 1000L; break;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006940 default: semsg(_(e_invarg2), eap->arg); return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006941 }
6942 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006943}
6944
6945/*
6946 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
6947 */
6948 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006949do_sleep(long msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006950{
Bram Moolenaar52953192019-08-16 10:27:13 +02006951 long done = 0;
Bram Moolenaar975b5272016-03-15 23:10:59 +01006952 long wait_now;
Bram Moolenaar52953192019-08-16 10:27:13 +02006953# ifdef ELAPSED_FUNC
6954 elapsed_T start_tv;
6955
6956 // Remember at what time we started, so that we know how much longer we
6957 // should wait after waiting for a bit.
6958 ELAPSED_INIT(start_tv);
6959# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006960
6961 cursor_on();
Bram Moolenaarf45d7472018-09-30 18:22:26 +02006962 out_flush_cursor(FALSE, FALSE);
Bram Moolenaar52953192019-08-16 10:27:13 +02006963 while (!got_int && done < msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006964 {
Bram Moolenaar975b5272016-03-15 23:10:59 +01006965 wait_now = msec - done > 1000L ? 1000L : msec - done;
6966#ifdef FEAT_TIMERS
6967 {
6968 long due_time = check_due_timer();
6969
6970 if (due_time > 0 && due_time < wait_now)
6971 wait_now = due_time;
6972 }
6973#endif
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02006974#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar28e67e02019-08-15 23:05:49 +02006975 if (has_any_channel() && wait_now > 20L)
6976 wait_now = 20L;
6977#endif
6978#ifdef FEAT_SOUND
6979 if (has_any_sound_callback() && wait_now > 20L)
6980 wait_now = 20L;
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02006981#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +01006982 ui_delay(wait_now, TRUE);
Bram Moolenaar52953192019-08-16 10:27:13 +02006983
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02006984#ifdef FEAT_JOB_CHANNEL
6985 if (has_any_channel())
6986 ui_breakcheck_force(TRUE);
6987 else
6988#endif
6989 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006990#ifdef MESSAGE_QUEUE
Bram Moolenaar52953192019-08-16 10:27:13 +02006991 // Process the netbeans and clientserver messages that may have been
6992 // received in the call to ui_breakcheck() when the GUI is in use. This
6993 // may occur when running a test case.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006994 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02006995#endif
Bram Moolenaar52953192019-08-16 10:27:13 +02006996
6997# ifdef ELAPSED_FUNC
6998 // actual time passed
6999 done = ELAPSED_FUNC(start_tv);
7000# else
7001 // guestimate time passed (will actually be more)
7002 done += wait_now;
7003# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007004 }
Bram Moolenaar1ebff3d2018-07-07 16:18:13 +02007005
7006 // If CTRL-C was typed to interrupt the sleep, drop the CTRL-C from the
7007 // input buffer, otherwise a following call to input() fails.
7008 if (got_int)
7009 (void)vpeekc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007010}
7011
Bram Moolenaar071d4272004-06-13 20:20:40 +00007012/*
7013 * ":winsize" command (obsolete).
7014 */
7015 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007016ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007017{
7018 int w, h;
7019 char_u *arg = eap->arg;
7020 char_u *p;
7021
7022 w = getdigits(&arg);
7023 arg = skipwhite(arg);
7024 p = arg;
7025 h = getdigits(&arg);
7026 if (*p != NUL && *arg == NUL)
7027 set_shellsize(w, h, TRUE);
7028 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007029 emsg(_("E465: :winsize requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007030}
7031
Bram Moolenaar071d4272004-06-13 20:20:40 +00007032 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007033ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007034{
7035 int xchar = NUL;
7036 char_u *p;
7037
7038 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
7039 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007040 // CTRL-W g and CTRL-W CTRL-G have an extra command character
Bram Moolenaar071d4272004-06-13 20:20:40 +00007041 if (eap->arg[1] == NUL)
7042 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007043 emsg(_(e_invarg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007044 return;
7045 }
7046 xchar = eap->arg[1];
7047 p = eap->arg + 2;
7048 }
7049 else
7050 p = eap->arg + 1;
7051
7052 eap->nextcmd = check_nextcmd(p);
7053 p = skipwhite(p);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02007054 if (*p != NUL && *p != (
7055#ifdef FEAT_EVAL
7056 in_vim9script() ? '#' :
7057#endif
7058 '"')
7059 && eap->nextcmd == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007060 emsg(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02007061 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007062 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007063 // Pass flags on for ":vertical wincmd ]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007064 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00007065 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007066 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
7067 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00007068 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007069 }
7070}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007071
Bram Moolenaar843ee412004-06-30 16:16:41 +00007072#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007073/*
7074 * ":winpos".
7075 */
7076 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007077ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007078{
7079 int x, y;
7080 char_u *arg = eap->arg;
7081 char_u *p;
7082
7083 if (*arg == NUL)
7084 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00007085# if defined(FEAT_GUI) || defined(MSWIN)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007086# ifdef VIMDLL
7087 if (gui.in_use ? gui_mch_get_winpos(&x, &y) != FAIL :
7088 mch_get_winpos(&x, &y) != FAIL)
7089# elif defined(FEAT_GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007090 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00007091# else
7092 if (mch_get_winpos(&x, &y) != FAIL)
7093# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007094 {
7095 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
Bram Moolenaar32526b32019-01-19 17:43:09 +01007096 msg((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007097 }
7098 else
7099# endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007100 emsg(_("E188: Obtaining window position not implemented for this platform"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007101 }
7102 else
7103 {
7104 x = getdigits(&arg);
7105 arg = skipwhite(arg);
7106 p = arg;
7107 y = getdigits(&arg);
7108 if (*p == NUL || *arg != NUL)
7109 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007110 emsg(_("E466: :winpos requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007111 return;
7112 }
7113# ifdef FEAT_GUI
7114 if (gui.in_use)
7115 gui_mch_set_winpos(x, y);
7116 else if (gui.starting)
7117 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007118 // Remember the coordinates for when the window is opened.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007119 gui_win_x = x;
7120 gui_win_y = y;
7121 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007122# if defined(HAVE_TGETENT) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007123 else
7124# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007125# endif
7126# if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar843ee412004-06-30 16:16:41 +00007127 mch_set_winpos(x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007128# endif
7129# ifdef HAVE_TGETENT
7130 if (*T_CWP)
7131 term_set_winpos(x, y);
7132# endif
7133 }
7134}
7135#endif
7136
7137/*
7138 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
7139 */
7140 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007141ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007142{
7143 oparg_T oa;
7144
7145 clear_oparg(&oa);
7146 oa.regname = eap->regname;
7147 oa.start.lnum = eap->line1;
7148 oa.end.lnum = eap->line2;
7149 oa.line_count = eap->line2 - eap->line1 + 1;
7150 oa.motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007151 virtual_op = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007152 if (eap->cmdidx != CMD_yank) // position cursor for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00007153 {
7154 setpcmark();
7155 curwin->w_cursor.lnum = eap->line1;
7156 beginline(BL_SOL | BL_FIX);
7157 }
7158
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007159 if (VIsual_active)
7160 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007161
Bram Moolenaar071d4272004-06-13 20:20:40 +00007162 switch (eap->cmdidx)
7163 {
7164 case CMD_delete:
7165 oa.op_type = OP_DELETE;
7166 op_delete(&oa);
7167 break;
7168
7169 case CMD_yank:
7170 oa.op_type = OP_YANK;
7171 (void)op_yank(&oa, FALSE, TRUE);
7172 break;
7173
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007174 default: // CMD_rshift or CMD_lshift
Bram Moolenaar6e707362013-06-14 19:15:58 +02007175 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007176#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02007177 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
7178#else
7179 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00007180#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02007181 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007182 oa.op_type = OP_RSHIFT;
7183 else
7184 oa.op_type = OP_LSHIFT;
7185 op_shift(&oa, FALSE, eap->amount);
7186 break;
7187 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007188 virtual_op = MAYBE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007189 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007190}
7191
7192/*
7193 * ":put".
7194 */
7195 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007196ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007197{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007198 // ":0put" works like ":1put!".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007199 if (eap->line2 == 0)
7200 {
7201 eap->line2 = 1;
7202 eap->forceit = TRUE;
7203 }
7204 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007205 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
7206 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007207}
7208
7209/*
7210 * Handle ":copy" and ":move".
7211 */
7212 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007213ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007214{
7215 long n;
7216
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02007217 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE, FALSE, 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007218 if (eap->arg == NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00007219 {
7220 eap->nextcmd = NULL;
7221 return;
7222 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00007223 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007224
7225 /*
7226 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
7227 */
7228 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
7229 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02007230 emsg(_(e_invrange));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007231 return;
7232 }
7233
7234 if (eap->cmdidx == CMD_move)
7235 {
7236 if (do_move(eap->line1, eap->line2, n) == FAIL)
7237 return;
7238 }
7239 else
7240 ex_copy(eap->line1, eap->line2, n);
7241 u_clearline();
7242 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007243 ex_may_print(eap);
7244}
7245
7246/*
7247 * Print the current line if flags were given to the Ex command.
7248 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02007249 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007250ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007251{
7252 if (eap->flags != 0)
7253 {
7254 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
7255 (eap->flags & EXFLAG_LIST));
7256 ex_no_reprint = TRUE;
7257 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007258}
7259
7260/*
7261 * ":smagic" and ":snomagic".
7262 */
7263 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007264ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007265{
7266 int magic_save = p_magic;
7267
7268 p_magic = (eap->cmdidx == CMD_smagic);
7269 do_sub(eap);
7270 p_magic = magic_save;
7271}
7272
7273/*
7274 * ":join".
7275 */
7276 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007277ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007278{
7279 curwin->w_cursor.lnum = eap->line1;
7280 if (eap->line1 == eap->line2)
7281 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007282 if (eap->addr_count >= 2) // :2,2join does nothing
Bram Moolenaar071d4272004-06-13 20:20:40 +00007283 return;
7284 if (eap->line2 == curbuf->b_ml.ml_line_count)
7285 {
7286 beep_flush();
7287 return;
7288 }
7289 ++eap->line2;
7290 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02007291 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007292 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007293 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007294}
7295
7296/*
7297 * ":[addr]@r" or ":[addr]*r": execute register
7298 */
7299 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007300ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007301{
7302 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00007303 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007304
7305 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4930a762016-09-11 14:39:53 +02007306 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007307
7308#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007309 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00007310#endif
7311
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007312 // get the register name. No name means to use the previous one
Bram Moolenaar071d4272004-06-13 20:20:40 +00007313 c = *eap->arg;
7314 if (c == NUL || (c == '*' && *eap->cmd == '*'))
7315 c = '@';
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007316 // Put the register in the typeahead buffer with the "silent" flag.
Bram Moolenaard333d1e2006-11-07 17:43:47 +00007317 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
7318 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007319 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007320 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00007321 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007322 else
7323 {
7324 int save_efr = exec_from_reg;
7325
7326 exec_from_reg = TRUE;
7327
7328 /*
7329 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00007330 * Continue until the stuff buffer is empty and all added characters
7331 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007332 */
Bram Moolenaar60462872009-11-03 11:40:19 +00007333 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007334 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
7335
7336 exec_from_reg = save_efr;
7337 }
7338}
7339
7340/*
7341 * ":!".
7342 */
7343 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007344ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007345{
7346 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
7347}
7348
7349/*
7350 * ":undo".
7351 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007352 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01007353ex_undo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007354{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007355 if (eap->addr_count == 1) // :undo 123
Bram Moolenaar730cde92010-06-27 05:18:54 +02007356 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00007357 else
7358 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007359}
7360
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007361#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02007362 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007363ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007364{
7365 char_u hash[UNDO_HASH_SIZE];
7366
7367 u_compute_hash(hash);
7368 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
7369}
7370
Bram Moolenaar6df6f472010-07-18 18:04:50 +02007371 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007372ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007373{
7374 char_u hash[UNDO_HASH_SIZE];
7375
7376 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02007377 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007378}
7379#endif
7380
Bram Moolenaar071d4272004-06-13 20:20:40 +00007381/*
7382 * ":redo".
7383 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007384 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007385ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007386{
7387 u_redo(1);
7388}
7389
7390/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007391 * ":earlier" and ":later".
7392 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007393 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007394ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007395{
7396 long count = 0;
7397 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02007398 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007399 char_u *p = eap->arg;
7400
7401 if (*p == NUL)
7402 count = 1;
7403 else if (isdigit(*p))
7404 {
7405 count = getdigits(&p);
7406 switch (*p)
7407 {
7408 case 's': ++p; sec = TRUE; break;
7409 case 'm': ++p; sec = TRUE; count *= 60; break;
7410 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02007411 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
7412 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007413 }
7414 }
7415
7416 if (*p != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007417 semsg(_(e_invarg2), eap->arg);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007418 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02007419 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
7420 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007421}
7422
7423/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007424 * ":redir": start/stop redirection.
7425 */
7426 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007427ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007428{
7429 char *mode;
7430 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00007431 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007432
Bram Moolenaarba768492016-07-08 15:32:54 +02007433#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02007434 if (redir_execute)
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007435 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007436 emsg(_("E930: Cannot use :redir inside execute()"));
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007437 return;
7438 }
Bram Moolenaarba768492016-07-08 15:32:54 +02007439#endif
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007440
Bram Moolenaar071d4272004-06-13 20:20:40 +00007441 if (STRICMP(eap->arg, "END") == 0)
7442 close_redir();
7443 else
7444 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007445 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007446 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007447 ++arg;
7448 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007449 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007450 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007451 mode = "a";
7452 }
7453 else
7454 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00007455 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007456
7457 close_redir();
7458
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007459 // Expand environment variables and "~/".
Bram Moolenaarca472992005-01-21 11:46:23 +00007460 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007461 if (fname == NULL)
7462 return;
7463#ifdef FEAT_BROWSE
7464 if (cmdmod.browse)
7465 {
7466 char_u *browseFile;
7467
Bram Moolenaar7171abe2004-10-11 10:06:20 +00007468 browseFile = do_browse(BROWSE_SAVE,
7469 (char_u *)_("Save Redirection"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +02007470 fname, NULL, NULL,
7471 (char_u *)_(BROWSE_FILTER_ALL_FILES), curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007472 if (browseFile == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007473 return; // operation cancelled
Bram Moolenaar071d4272004-06-13 20:20:40 +00007474 vim_free(fname);
7475 fname = browseFile;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007476 eap->forceit = TRUE; // since dialog already asked
Bram Moolenaar071d4272004-06-13 20:20:40 +00007477 }
7478#endif
7479
7480 redir_fd = open_exfile(fname, eap->forceit, mode);
7481 vim_free(fname);
7482 }
7483#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00007484 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007485 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007486 // redirect to a register a-z (resp. A-Z for appending)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007487 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00007488 ++arg;
7489 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007490# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00007491 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00007492 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00007493# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00007494 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007495 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007496 redir_reg = *arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007497 if (*arg == '>' && arg[1] == '>') // append
Bram Moolenaard9d30582005-05-18 22:10:28 +00007498 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00007499 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007500 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007501 // Can use both "@a" and "@a>".
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00007502 if (*arg == '>')
7503 arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007504 // Make register empty when not using @A-@Z and the
7505 // command is valid.
Bram Moolenaarc188b882007-10-19 14:20:54 +00007506 if (*arg == NUL && !isupper(redir_reg))
7507 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007508 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00007509 }
7510 if (*arg != NUL)
7511 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007512 redir_reg = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007513 semsg(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007514 }
7515 }
7516 else if (*arg == '=' && arg[1] == '>')
7517 {
7518 int append;
7519
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007520 // redirect to a variable
Bram Moolenaardf177f62005-02-22 08:39:57 +00007521 close_redir();
7522 arg += 2;
7523
7524 if (*arg == '>')
7525 {
7526 ++arg;
7527 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007528 }
7529 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00007530 append = FALSE;
7531
7532 if (var_redir_start(skipwhite(arg), append) == OK)
7533 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007534 }
7535#endif
7536
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007537 // TODO: redirect to a buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00007538
Bram Moolenaar071d4272004-06-13 20:20:40 +00007539 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007540 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007541 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00007542
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007543 // Make sure redirection is not off. Can happen for cmdline completion
7544 // that indirectly invokes a command to catch its output.
Bram Moolenaar29b2d262006-09-10 19:07:28 +00007545 if (redir_fd != NULL
7546#ifdef FEAT_EVAL
7547 || redir_reg || redir_vname
7548#endif
7549 )
7550 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007551}
7552
7553/*
7554 * ":redraw": force redraw
7555 */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01007556 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007557ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007558{
7559 int r = RedrawingDisabled;
7560 int p = p_lz;
7561
7562 RedrawingDisabled = 0;
7563 p_lz = FALSE;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01007564 validate_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007565 update_topline();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007566 update_screen(eap->forceit ? CLEAR : VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007567#ifdef FEAT_TITLE
7568 if (need_maketitle)
7569 maketitle();
7570#endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007571#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
7572# ifdef VIMDLL
7573 if (!gui.in_use)
7574# endif
7575 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01007576#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007577 RedrawingDisabled = r;
7578 p_lz = p;
7579
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007580 // Reset msg_didout, so that a message that's there is overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007581 msg_didout = FALSE;
7582 msg_col = 0;
7583
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007584 // No need to wait after an intentional redraw.
Bram Moolenaar56667a52013-07-17 11:54:28 +02007585 need_wait_return = FALSE;
7586
Bram Moolenaar071d4272004-06-13 20:20:40 +00007587 out_flush();
7588}
7589
7590/*
7591 * ":redrawstatus": force redraw of status line(s)
7592 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007593 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007594ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007595{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007596 int r = RedrawingDisabled;
7597 int p = p_lz;
7598
7599 RedrawingDisabled = 0;
7600 p_lz = FALSE;
7601 if (eap->forceit)
7602 status_redraw_all();
7603 else
7604 status_redraw_curbuf();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007605 update_screen(VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007606 RedrawingDisabled = r;
7607 p_lz = p;
7608 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007609}
7610
Bram Moolenaare12bab32019-01-08 22:02:56 +01007611/*
7612 * ":redrawtabline": force redraw of the tabline
7613 */
7614 static void
7615ex_redrawtabline(exarg_T *eap UNUSED)
7616{
7617 int r = RedrawingDisabled;
7618 int p = p_lz;
7619
7620 RedrawingDisabled = 0;
7621 p_lz = FALSE;
7622
7623 draw_tabline();
7624
7625 RedrawingDisabled = r;
7626 p_lz = p;
7627 out_flush();
7628}
7629
Bram Moolenaar071d4272004-06-13 20:20:40 +00007630 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007631close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007632{
7633 if (redir_fd != NULL)
7634 {
7635 fclose(redir_fd);
7636 redir_fd = NULL;
7637 }
7638#ifdef FEAT_EVAL
7639 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007640 if (redir_vname)
7641 {
7642 var_redir_stop();
7643 redir_vname = 0;
7644 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007645#endif
7646}
7647
Bram Moolenaarf96ae0b2019-07-28 15:21:55 +02007648#if (defined(FEAT_SESSION) || defined(FEAT_EVAL)) || defined(PROTO)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007649 int
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02007650vim_mkdir_emsg(char_u *name, int prot UNUSED)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007651{
7652 if (vim_mkdir(name, prot) != 0)
7653 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007654 semsg(_("E739: Cannot create directory: %s"), name);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007655 return FAIL;
7656 }
7657 return OK;
7658}
7659#endif
7660
Bram Moolenaar071d4272004-06-13 20:20:40 +00007661/*
7662 * Open a file for writing for an Ex command, with some checks.
7663 * Return file descriptor, or NULL on failure.
7664 */
7665 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007666open_exfile(
7667 char_u *fname,
7668 int forceit,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007669 char *mode) // "w" for create new file or "a" for append
Bram Moolenaar071d4272004-06-13 20:20:40 +00007670{
7671 FILE *fd;
7672
7673#ifdef UNIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007674 // with Unix it is possible to open a directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007675 if (mch_isdir(fname))
7676 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007677 semsg(_(e_isadir2), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007678 return NULL;
7679 }
7680#endif
7681 if (!forceit && *mode != 'a' && vim_fexists(fname))
7682 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007683 semsg(_("E189: \"%s\" exists (add ! to override)"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007684 return NULL;
7685 }
7686
7687 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007688 semsg(_("E190: Cannot open \"%s\" for writing"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007689
7690 return fd;
7691}
7692
7693/*
7694 * ":mark" and ":k".
7695 */
7696 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007697ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007698{
7699 pos_T pos;
7700
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007701 if (*eap->arg == NUL) // No argument?
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007702 emsg(_(e_argreq));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007703 else if (eap->arg[1] != NUL) // more than one character?
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007704 emsg(_(e_trailing));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007705 else
7706 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007707 pos = curwin->w_cursor; // save curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00007708 curwin->w_cursor.lnum = eap->line2;
7709 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007710 if (setmark(*eap->arg) == FAIL) // set mark
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007711 emsg(_("E191: Argument must be a letter or forward/backward quote"));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007712 curwin->w_cursor = pos; // restore curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00007713 }
7714}
7715
7716/*
7717 * Update w_topline, w_leftcol and the cursor position.
7718 */
7719 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007720update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007721{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007722 check_cursor(); // put cursor on valid line
Bram Moolenaar071d4272004-06-13 20:20:40 +00007723 update_topline();
7724 if (!curwin->w_p_wrap)
7725 validate_cursor();
7726 update_curswant();
7727}
7728
Bram Moolenaar071d4272004-06-13 20:20:40 +00007729/*
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007730 * Save the current State and go to Normal mode.
7731 * Return TRUE if the typeahead could be saved.
7732 */
7733 int
7734save_current_state(save_state_T *sst)
7735{
7736 sst->save_msg_scroll = msg_scroll;
7737 sst->save_restart_edit = restart_edit;
7738 sst->save_msg_didout = msg_didout;
7739 sst->save_State = State;
7740 sst->save_insertmode = p_im;
7741 sst->save_finish_op = finish_op;
7742 sst->save_opcount = opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01007743 sst->save_reg_executing = reg_executing;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007744
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007745 msg_scroll = FALSE; // no msg scrolling in Normal mode
7746 restart_edit = 0; // don't go to Insert mode
7747 p_im = FALSE; // don't use 'insertmode'
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007748
7749 /*
7750 * Save the current typeahead. This is required to allow using ":normal"
7751 * from an event handler and makes sure we don't hang when the argument
7752 * ends with half a command.
7753 */
7754 save_typeahead(&sst->tabuf);
7755 return sst->tabuf.typebuf_valid;
7756}
7757
7758 void
7759restore_current_state(save_state_T *sst)
7760{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007761 // Restore the previous typeahead.
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007762 restore_typeahead(&sst->tabuf);
7763
7764 msg_scroll = sst->save_msg_scroll;
7765 restart_edit = sst->save_restart_edit;
7766 p_im = sst->save_insertmode;
7767 finish_op = sst->save_finish_op;
7768 opcount = sst->save_opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01007769 reg_executing = sst->save_reg_executing;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007770 msg_didout |= sst->save_msg_didout; // don't reset msg_didout now
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007771
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007772 // Restore the state (needed when called from a function executed for
7773 // 'indentexpr'). Update the mouse and cursor, they may have changed.
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007774 State = sst->save_State;
7775#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007776 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007777#endif
7778}
7779
7780/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007781 * ":normal[!] {commands}": Execute normal mode commands.
7782 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01007783 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007784ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007785{
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007786 save_state_T save_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007787 char_u *arg = NULL;
7788 int l;
7789 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007790
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00007791 if (ex_normal_lock > 0)
7792 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007793 emsg(_(e_secure));
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00007794 return;
7795 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007796 if (ex_normal_busy >= p_mmd)
7797 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007798 emsg(_("E192: Recursive use of :normal too deep"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007799 return;
7800 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007801
Bram Moolenaar071d4272004-06-13 20:20:40 +00007802 /*
7803 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
7804 * this for the K_SPECIAL leading byte, otherwise special keys will not
7805 * work.
7806 */
7807 if (has_mbyte)
7808 {
7809 int len = 0;
7810
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007811 // Count the number of characters to be escaped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007812 for (p = eap->arg; *p != NUL; ++p)
7813 {
Bram Moolenaar13505972019-01-24 15:04:48 +01007814#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007815 if (*p == CSI) // leadbyte CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007816 len += 2;
Bram Moolenaar13505972019-01-24 15:04:48 +01007817#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007818 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007819 if (*++p == K_SPECIAL // trailbyte K_SPECIAL or CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01007820#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007821 || *p == CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01007822#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007823 )
7824 len += 2;
7825 }
7826 if (len > 0)
7827 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02007828 arg = alloc(STRLEN(eap->arg) + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007829 if (arg != NULL)
7830 {
7831 len = 0;
7832 for (p = eap->arg; *p != NUL; ++p)
7833 {
7834 arg[len++] = *p;
Bram Moolenaar13505972019-01-24 15:04:48 +01007835#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007836 if (*p == CSI)
7837 {
7838 arg[len++] = KS_EXTRA;
7839 arg[len++] = (int)KE_CSI;
7840 }
Bram Moolenaar13505972019-01-24 15:04:48 +01007841#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007842 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007843 {
7844 arg[len++] = *++p;
7845 if (*p == K_SPECIAL)
7846 {
7847 arg[len++] = KS_SPECIAL;
7848 arg[len++] = KE_FILLER;
7849 }
Bram Moolenaar13505972019-01-24 15:04:48 +01007850#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007851 else if (*p == CSI)
7852 {
7853 arg[len++] = KS_EXTRA;
7854 arg[len++] = (int)KE_CSI;
7855 }
Bram Moolenaar13505972019-01-24 15:04:48 +01007856#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007857 }
7858 arg[len] = NUL;
7859 }
7860 }
7861 }
7862 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007863
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007864 ++ex_normal_busy;
7865 if (save_current_state(&save_state))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007866 {
7867 /*
7868 * Repeat the :normal command for each line in the range. When no
7869 * range given, execute it just once, without positioning the cursor
7870 * first.
7871 */
7872 do
7873 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007874 if (eap->addr_count != 0)
7875 {
7876 curwin->w_cursor.lnum = eap->line1++;
7877 curwin->w_cursor.col = 0;
Bram Moolenaard5d37532017-03-27 23:02:07 +02007878 check_cursor_moved(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007879 }
7880
Bram Moolenaar13505972019-01-24 15:04:48 +01007881 exec_normal_cmd(arg != NULL
7882 ? arg
7883 : eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007884 }
7885 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
7886 }
7887
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007888 // Might not return to the main loop when in an event handler.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007889 update_topline_cursor();
7890
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007891 restore_current_state(&save_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007892 --ex_normal_busy;
Bram Moolenaareda73602014-11-05 09:53:23 +01007893 setmouse();
Bram Moolenaareda73602014-11-05 09:53:23 +01007894#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007895 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaareda73602014-11-05 09:53:23 +01007896#endif
7897
Bram Moolenaar071d4272004-06-13 20:20:40 +00007898 vim_free(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007899}
7900
7901/*
Bram Moolenaar64969662005-12-14 21:59:55 +00007902 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007903 */
7904 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007905ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007906{
Bram Moolenaarfd371682005-01-14 21:42:54 +00007907 if (eap->forceit)
7908 {
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007909 // cursor line can be zero on startup
Bram Moolenaar09ca9322017-09-26 17:40:45 +02007910 if (!curwin->w_cursor.lnum)
7911 curwin->w_cursor.lnum = 1;
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007912 set_cursor_for_append_to_line();
Bram Moolenaarfd371682005-01-14 21:42:54 +00007913 }
Bram Moolenaareb58a242020-04-19 18:13:19 +02007914#ifdef FEAT_TERMINAL
7915 // Ignore this when running in an active terminal.
7916 if (term_job_running(curbuf->b_term))
7917 return;
7918#endif
Bram Moolenaarfd371682005-01-14 21:42:54 +00007919
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007920 // Ignore the command when already in Insert mode. Inserting an
7921 // expression register that invokes a function can do this.
Bram Moolenaara40c5002005-01-09 21:16:21 +00007922 if (State & INSERT)
7923 return;
7924
Bram Moolenaar64969662005-12-14 21:59:55 +00007925 if (eap->cmdidx == CMD_startinsert)
7926 restart_edit = 'a';
7927 else if (eap->cmdidx == CMD_startreplace)
7928 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00007929 else
Bram Moolenaar64969662005-12-14 21:59:55 +00007930 restart_edit = 'V';
7931
7932 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007933 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00007934 if (eap->cmdidx == CMD_startinsert)
7935 restart_edit = 'i';
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007936 curwin->w_curswant = 0; // avoid MAXCOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007937 }
7938}
7939
7940/*
7941 * ":stopinsert"
7942 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007943 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007944ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007945{
7946 restart_edit = 0;
7947 stop_insert_mode = TRUE;
Bram Moolenaarfd773e92016-04-02 19:39:16 +02007948 clearmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007949}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007950
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007951/*
7952 * Execute normal mode command "cmd".
7953 * "remap" can be REMAP_NONE or REMAP_YES.
7954 */
7955 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007956exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007957{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007958 // Stuff the argument into the typeahead buffer.
Bram Moolenaar25281632016-01-21 23:32:32 +01007959 ins_typebuf(cmd, remap, 0, TRUE, silent);
Bram Moolenaar586c70c2018-10-02 16:23:58 +02007960 exec_normal(FALSE, FALSE, FALSE);
Bram Moolenaar25281632016-01-21 23:32:32 +01007961}
Bram Moolenaar25281632016-01-21 23:32:32 +01007962
Bram Moolenaar25281632016-01-21 23:32:32 +01007963/*
7964 * Execute normal_cmd() until there is no typeahead left.
Bram Moolenaar586c70c2018-10-02 16:23:58 +02007965 * When "use_vpeekc" is TRUE use vpeekc() to check for available chars.
Bram Moolenaar25281632016-01-21 23:32:32 +01007966 */
7967 void
Bram Moolenaar586c70c2018-10-02 16:23:58 +02007968exec_normal(int was_typed, int use_vpeekc, int may_use_terminal_loop UNUSED)
Bram Moolenaar25281632016-01-21 23:32:32 +01007969{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007970 oparg_T oa;
Bram Moolenaar905dd902019-04-07 14:21:47 +02007971 int c;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007972
Bram Moolenaar905dd902019-04-07 14:21:47 +02007973 // When calling vpeekc() from feedkeys() it will return Ctrl_C when there
7974 // is nothing to get, so also check for Ctrl_C.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007975 clear_oparg(&oa);
7976 finish_op = FALSE;
Bram Moolenaar586c70c2018-10-02 16:23:58 +02007977 while ((!stuff_empty()
7978 || ((was_typed || !typebuf_typed()) && typebuf.tb_len > 0)
Bram Moolenaar905dd902019-04-07 14:21:47 +02007979 || (use_vpeekc && (c = vpeekc()) != NUL && c != Ctrl_C))
Bram Moolenaar586c70c2018-10-02 16:23:58 +02007980 && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007981 {
7982 update_topline_cursor();
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02007983#ifdef FEAT_TERMINAL
Bram Moolenaar655a82a2018-05-08 22:01:07 +02007984 if (may_use_terminal_loop && term_use_loop()
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02007985 && oa.op_type == OP_NOP && oa.regname == NUL
7986 && !VIsual_active)
7987 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007988 // If terminal_loop() returns OK we got a key that is handled
7989 // in Normal model. With FAIL we first need to position the
7990 // cursor and the screen needs to be redrawn.
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02007991 if (terminal_loop(TRUE) == OK)
7992 normal_cmd(&oa, TRUE);
7993 }
7994 else
7995#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007996 // execute a Normal mode cmd
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02007997 normal_cmd(&oa, TRUE);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007998 }
7999}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008000
Bram Moolenaar071d4272004-06-13 20:20:40 +00008001#ifdef FEAT_FIND_ID
8002 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008003ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008004{
8005 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
8006 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
8007 (linenr_T)1, (linenr_T)MAXLNUM);
8008}
8009
Bram Moolenaar4033c552017-09-16 20:54:51 +02008010#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008011/*
8012 * ":psearch"
8013 */
8014 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008015ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008016{
8017 g_do_tagpreview = p_pvh;
8018 ex_findpat(eap);
8019 g_do_tagpreview = 0;
8020}
8021#endif
8022
8023 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008024ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008025{
8026 int whole = TRUE;
8027 long n;
8028 char_u *p;
8029 int action;
8030
8031 switch (cmdnames[eap->cmdidx].cmd_name[2])
8032 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008033 case 'e': // ":psearch", ":isearch" and ":dsearch"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008034 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
8035 action = ACTION_GOTO;
8036 else
8037 action = ACTION_SHOW;
8038 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008039 case 'i': // ":ilist" and ":dlist"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008040 action = ACTION_SHOW_ALL;
8041 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008042 case 'u': // ":ijump" and ":djump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008043 action = ACTION_GOTO;
8044 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008045 default: // ":isplit" and ":dsplit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008046 action = ACTION_SPLIT;
8047 break;
8048 }
8049
8050 n = 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008051 if (vim_isdigit(*eap->arg)) // get count
Bram Moolenaar071d4272004-06-13 20:20:40 +00008052 {
8053 n = getdigits(&eap->arg);
8054 eap->arg = skipwhite(eap->arg);
8055 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008056 if (*eap->arg == '/') // Match regexp, not just whole words
Bram Moolenaar071d4272004-06-13 20:20:40 +00008057 {
8058 whole = FALSE;
8059 ++eap->arg;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02008060 p = skip_regexp(eap->arg, '/', p_magic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008061 if (*p)
8062 {
8063 *p++ = NUL;
8064 p = skipwhite(p);
8065
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008066 // Check for trailing illegal characters
Bram Moolenaarfaac4102020-04-20 17:46:14 +02008067 if (!ends_excmd2(eap->arg, p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008068 eap->errmsg = e_trailing;
8069 else
8070 eap->nextcmd = check_nextcmd(p);
8071 }
8072 }
8073 if (!eap->skip)
8074 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
8075 whole, !eap->forceit,
8076 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
8077 n, action, eap->line1, eap->line2);
8078}
8079#endif
8080
Bram Moolenaar071d4272004-06-13 20:20:40 +00008081
Bram Moolenaar4033c552017-09-16 20:54:51 +02008082#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00008083/*
8084 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
8085 */
8086 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008087ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008088{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008089 g_do_tagpreview = p_pvh; // will be reset to 0 in ex_tag_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00008090 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
8091}
8092
8093/*
8094 * ":pedit"
8095 */
8096 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008097ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008098{
8099 win_T *curwin_save = curwin;
8100
Bram Moolenaar3a2505c2020-03-09 16:40:41 +01008101 if (ERROR_IF_ANY_POPUP_WINDOW)
8102 return;
8103
Bram Moolenaar026587b2019-08-17 15:08:00 +02008104 // Open the preview window or popup and make it the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008105 g_do_tagpreview = p_pvh;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02008106 prepare_tagpreview(TRUE, TRUE, FALSE);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008107
Bram Moolenaar026587b2019-08-17 15:08:00 +02008108 // Edit the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008109 do_exedit(eap, NULL);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008110
Bram Moolenaar071d4272004-06-13 20:20:40 +00008111 if (curwin != curwin_save && win_valid(curwin_save))
8112 {
Bram Moolenaar026587b2019-08-17 15:08:00 +02008113 // Return cursor to where we were
Bram Moolenaar071d4272004-06-13 20:20:40 +00008114 validate_cursor();
8115 redraw_later(VALID);
8116 win_enter(curwin_save, TRUE);
8117 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01008118# ifdef FEAT_PROP_POPUP
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008119 else if (WIN_IS_POPUP(curwin))
8120 {
8121 // can't keep focus in popup window
8122 win_enter(firstwin, TRUE);
8123 }
8124# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008125 g_do_tagpreview = 0;
8126}
Bram Moolenaar4033c552017-09-16 20:54:51 +02008127#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008128
8129/*
8130 * ":stag", ":stselect" and ":stjump".
8131 */
8132 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008133ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008134{
8135 postponed_split = -1;
8136 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008137 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008138 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
8139 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008140 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008141}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008142
8143/*
8144 * ":tag", ":tselect", ":tjump", ":tnext", etc.
8145 */
8146 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008147ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008148{
8149 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
8150}
8151
8152 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008153ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008154{
8155 int cmd;
8156
8157 switch (name[1])
8158 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008159 case 'j': cmd = DT_JUMP; // ":tjump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008160 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008161 case 's': cmd = DT_SELECT; // ":tselect"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008162 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008163 case 'p': cmd = DT_PREV; // ":tprevious"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008164 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008165 case 'N': cmd = DT_PREV; // ":tNext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008166 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008167 case 'n': cmd = DT_NEXT; // ":tnext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008168 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008169 case 'o': cmd = DT_POP; // ":pop"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008170 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008171 case 'f': // ":tfirst"
8172 case 'r': cmd = DT_FIRST; // ":trewind"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008173 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008174 case 'l': cmd = DT_LAST; // ":tlast"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008175 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008176 default: // ":tag"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008177#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +00008178 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008179 {
Bram Moolenaard4db7712016-11-12 19:16:46 +01008180 ex_cstag(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008181 return;
8182 }
8183#endif
8184 cmd = DT_TAG;
8185 break;
8186 }
8187
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00008188 if (name[0] == 'l')
8189 {
8190#ifndef FEAT_QUICKFIX
8191 ex_ni(eap);
8192 return;
8193#else
8194 cmd = DT_LTAG;
8195#endif
8196 }
8197
Bram Moolenaar071d4272004-06-13 20:20:40 +00008198 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
8199 eap->forceit, TRUE);
8200}
8201
8202/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008203 * Check "str" for starting with a special cmdline variable.
8204 * If found return one of the SPEC_ values and set "*usedlen" to the length of
8205 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
8206 */
8207 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008208find_cmdline_var(char_u *src, int *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008209{
8210 int len;
8211 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +00008212 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008213 "%",
8214#define SPEC_PERC 0
8215 "#",
Bram Moolenaar65f08472017-09-10 18:16:20 +02008216#define SPEC_HASH (SPEC_PERC + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008217 "<cword>", // cursor word
Bram Moolenaar65f08472017-09-10 18:16:20 +02008218#define SPEC_CWORD (SPEC_HASH + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008219 "<cWORD>", // cursor WORD
Bram Moolenaar65f08472017-09-10 18:16:20 +02008220#define SPEC_CCWORD (SPEC_CWORD + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008221 "<cexpr>", // expr under cursor
Bram Moolenaar65f08472017-09-10 18:16:20 +02008222#define SPEC_CEXPR (SPEC_CCWORD + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008223 "<cfile>", // cursor path name
Bram Moolenaar65f08472017-09-10 18:16:20 +02008224#define SPEC_CFILE (SPEC_CEXPR + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008225 "<sfile>", // ":so" file name
Bram Moolenaar65f08472017-09-10 18:16:20 +02008226#define SPEC_SFILE (SPEC_CFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008227 "<slnum>", // ":so" file line number
Bram Moolenaar65f08472017-09-10 18:16:20 +02008228#define SPEC_SLNUM (SPEC_SFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008229 "<afile>", // autocommand file name
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008230#define SPEC_AFILE (SPEC_SLNUM + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008231 "<abuf>", // autocommand buffer number
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008232#define SPEC_ABUF (SPEC_AFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008233 "<amatch>", // autocommand match name
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008234#define SPEC_AMATCH (SPEC_ABUF + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008235 "<sflnum>", // script file line number
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008236#define SPEC_SFLNUM (SPEC_AMATCH + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008237#ifdef FEAT_CLIENTSERVER
8238 "<client>"
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008239# define SPEC_CLIENT (SPEC_SFLNUM + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008240#endif
8241 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008242
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008243 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008244 {
8245 len = (int)STRLEN(spec_str[i]);
8246 if (STRNCMP(src, spec_str[i], len) == 0)
8247 {
8248 *usedlen = len;
8249 return i;
8250 }
8251 }
8252 return -1;
8253}
8254
8255/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008256 * Evaluate cmdline variables.
8257 *
8258 * change '%' to curbuf->b_ffname
8259 * '#' to curwin->w_altfile
8260 * '<cword>' to word under the cursor
8261 * '<cWORD>' to WORD under the cursor
Bram Moolenaar8071cb22019-07-12 17:58:01 +02008262 * '<cexpr>' to C-expression under the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00008263 * '<cfile>' to path name under the cursor
8264 * '<sfile>' to sourced file name
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008265 * '<slnum>' to sourced file line number
Bram Moolenaar071d4272004-06-13 20:20:40 +00008266 * '<afile>' to file name for autocommand
8267 * '<abuf>' to buffer number for autocommand
8268 * '<amatch>' to matching name for autocommand
8269 *
8270 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
8271 * "" for error without a message) and NULL is returned.
8272 * Returns an allocated string if a valid match was found.
8273 * Returns NULL if no match was found. "usedlen" then still contains the
8274 * number of characters to skip.
8275 */
8276 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008277eval_vars(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008278 char_u *src, // pointer into commandline
8279 char_u *srcstart, // beginning of valid memory for src
8280 int *usedlen, // characters after src that are used
8281 linenr_T *lnump, // line number for :e command, or NULL
8282 char **errormsg, // pointer to error message
8283 int *escaped) // return value has escaped white space (can
8284 // be NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008285{
8286 int i;
8287 char_u *s;
8288 char_u *result;
8289 char_u *resultbuf = NULL;
8290 int resultlen;
8291 buf_T *buf;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008292 int valid = VALID_HEAD + VALID_PATH; // assume valid result
Bram Moolenaar071d4272004-06-13 20:20:40 +00008293 int spec_idx;
Bram Moolenaarfd249462018-07-28 16:14:30 +02008294 int tilde_file = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008295 int skip_mod = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008296 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008297
8298 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +00008299 if (escaped != NULL)
8300 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008301
8302 /*
8303 * Check if there is something to do.
8304 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008305 spec_idx = find_cmdline_var(src, usedlen);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008306 if (spec_idx < 0) // no match
Bram Moolenaar071d4272004-06-13 20:20:40 +00008307 {
8308 *usedlen = 1;
8309 return NULL;
8310 }
8311
8312 /*
8313 * Skip when preceded with a backslash "\%" and "\#".
8314 * Note: In "\\%" the % is also not recognized!
8315 */
8316 if (src > srcstart && src[-1] == '\\')
8317 {
8318 *usedlen = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008319 STRMOVE(src - 1, src); // remove backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00008320 return NULL;
8321 }
8322
8323 /*
8324 * word or WORD under cursor
8325 */
Bram Moolenaar65f08472017-09-10 18:16:20 +02008326 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD
8327 || spec_idx == SPEC_CEXPR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008328 {
Bram Moolenaar65f08472017-09-10 18:16:20 +02008329 resultlen = find_ident_under_cursor(&result,
8330 spec_idx == SPEC_CWORD ? (FIND_IDENT | FIND_STRING)
8331 : spec_idx == SPEC_CEXPR ? (FIND_IDENT | FIND_STRING | FIND_EVAL)
8332 : FIND_STRING);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008333 if (resultlen == 0)
8334 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008335 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008336 return NULL;
8337 }
8338 }
8339
8340 /*
8341 * '#': Alternate file name
8342 * '%': Current file name
8343 * File name under the cursor
8344 * File name for autocommand
8345 * and following modifiers
8346 */
8347 else
8348 {
8349 switch (spec_idx)
8350 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008351 case SPEC_PERC: // '%': current file
Bram Moolenaar071d4272004-06-13 20:20:40 +00008352 if (curbuf->b_fname == NULL)
8353 {
8354 result = (char_u *)"";
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008355 valid = 0; // Must have ":p:h" to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00008356 }
8357 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008358 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008359 result = curbuf->b_fname;
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008360 tilde_file = STRCMP(result, "~") == 0;
8361 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008362 break;
8363
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008364 case SPEC_HASH: // '#' or "#99": alternate file
8365 if (src[1] == '#') // "##": the argument list
Bram Moolenaar071d4272004-06-13 20:20:40 +00008366 {
8367 result = arg_all();
8368 resultbuf = result;
8369 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +00008370 if (escaped != NULL)
8371 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008372 skip_mod = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008373 break;
8374 }
8375 s = src + 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008376 if (*s == '<') // "#<99" uses v:oldfiles
Bram Moolenaard812df62008-11-09 12:46:09 +00008377 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008378 i = (int)getdigits(&s);
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008379 if (s == src + 2 && src[1] == '-')
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008380 // just a minus sign, don't skip over it
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008381 s--;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008382 *usedlen = (int)(s - src); // length of what we expand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008383
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008384 if (src[1] == '<' && i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008385 {
Bram Moolenaard812df62008-11-09 12:46:09 +00008386 if (*usedlen < 2)
8387 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008388 // Should we give an error message for #<text?
Bram Moolenaard812df62008-11-09 12:46:09 +00008389 *usedlen = 1;
8390 return NULL;
8391 }
8392#ifdef FEAT_EVAL
8393 result = list_find_str(get_vim_var_list(VV_OLDFILES),
8394 (long)i);
8395 if (result == NULL)
8396 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008397 *errormsg = "";
Bram Moolenaard812df62008-11-09 12:46:09 +00008398 return NULL;
8399 }
8400#else
Bram Moolenaar8e481e82019-01-15 20:07:48 +01008401 *errormsg = _("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008402 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00008403#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008404 }
8405 else
Bram Moolenaard812df62008-11-09 12:46:09 +00008406 {
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008407 if (i == 0 && src[1] == '<' && *usedlen > 1)
8408 *usedlen = 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00008409 buf = buflist_findnr(i);
8410 if (buf == NULL)
8411 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008412 *errormsg = _("E194: No alternate file name to substitute for '#'");
Bram Moolenaard812df62008-11-09 12:46:09 +00008413 return NULL;
8414 }
8415 if (lnump != NULL)
8416 *lnump = ECMD_LAST;
8417 if (buf->b_fname == NULL)
8418 {
8419 result = (char_u *)"";
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008420 valid = 0; // Must have ":p:h" to be valid
Bram Moolenaard812df62008-11-09 12:46:09 +00008421 }
8422 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008423 {
Bram Moolenaard812df62008-11-09 12:46:09 +00008424 result = buf->b_fname;
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008425 tilde_file = STRCMP(result, "~") == 0;
8426 }
Bram Moolenaard812df62008-11-09 12:46:09 +00008427 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008428 break;
8429
8430#ifdef FEAT_SEARCHPATH
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008431 case SPEC_CFILE: // file name under cursor
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008432 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008433 if (result == NULL)
8434 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008435 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008436 return NULL;
8437 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008438 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00008439 break;
8440#endif
8441
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008442 case SPEC_AFILE: // file name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008443 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +00008444 if (result != NULL && !autocmd_fname_full)
8445 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008446 // Still need to turn the fname into a full path. It is
8447 // postponed to avoid a delay when <afile> is not used.
Bram Moolenaarf6dad432008-09-18 19:29:58 +00008448 autocmd_fname_full = TRUE;
8449 result = FullName_save(autocmd_fname, FALSE);
8450 vim_free(autocmd_fname);
8451 autocmd_fname = result;
8452 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008453 if (result == NULL)
8454 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008455 *errormsg = _("E495: no autocommand file name to substitute for \"<afile>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008456 return NULL;
8457 }
Bram Moolenaara0174af2008-01-02 20:08:25 +00008458 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008459 break;
8460
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008461 case SPEC_ABUF: // buffer number for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008462 if (autocmd_bufnr <= 0)
8463 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008464 *errormsg = _("E496: no autocommand buffer number to substitute for \"<abuf>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008465 return NULL;
8466 }
8467 sprintf((char *)strbuf, "%d", autocmd_bufnr);
8468 result = strbuf;
8469 break;
8470
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008471 case SPEC_AMATCH: // match name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008472 result = autocmd_match;
8473 if (result == NULL)
8474 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008475 *errormsg = _("E497: no autocommand match name to substitute for \"<amatch>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008476 return NULL;
8477 }
8478 break;
8479
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008480 case SPEC_SFILE: // file name for ":so" command
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008481 result = estack_sfile();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008482 if (result == NULL)
8483 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008484 *errormsg = _("E498: no :source file name to substitute for \"<sfile>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008485 return NULL;
8486 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008487 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00008488 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008489
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008490 case SPEC_SLNUM: // line in file for ":so" command
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008491 if (SOURCING_NAME == NULL || SOURCING_LNUM == 0)
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008492 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008493 *errormsg = _("E842: no line number to use for \"<slnum>\"");
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008494 return NULL;
8495 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008496 sprintf((char *)strbuf, "%ld", SOURCING_LNUM);
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008497 result = strbuf;
8498 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008499
8500#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008501 case SPEC_SFLNUM: // line in script file
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008502 if (current_sctx.sc_lnum + SOURCING_LNUM == 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008503 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008504 *errormsg = _("E961: no line number to use for \"<sflnum>\"");
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008505 return NULL;
8506 }
8507 sprintf((char *)strbuf, "%ld",
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008508 (long)(current_sctx.sc_lnum + SOURCING_LNUM));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008509 result = strbuf;
8510 break;
8511#endif
8512
8513#ifdef FEAT_CLIENTSERVER
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008514 case SPEC_CLIENT: // Source of last submitted input
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008515 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
8516 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008517 result = strbuf;
8518 break;
8519#endif
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008520
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02008521 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008522 result = (char_u *)""; // avoid gcc warning
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02008523 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008524 }
8525
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008526 resultlen = (int)STRLEN(result); // length of new string
8527 if (src[*usedlen] == '<') // remove the file name extension
Bram Moolenaar071d4272004-06-13 20:20:40 +00008528 {
8529 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008530 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008531 resultlen = (int)(s - result);
8532 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008533 else if (!skip_mod)
8534 {
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008535 valid |= modify_fname(src, tilde_file, usedlen, &result, &resultbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008536 &resultlen);
8537 if (result == NULL)
8538 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008539 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008540 return NULL;
8541 }
8542 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008543 }
8544
8545 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
8546 {
8547 if (valid != VALID_HEAD + VALID_PATH)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008548 // xgettext:no-c-format
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008549 *errormsg = _("E499: Empty file name for '%' or '#', only works with \":p:h\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008550 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008551 *errormsg = _("E500: Evaluates to an empty string");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008552 result = NULL;
8553 }
8554 else
8555 result = vim_strnsave(result, resultlen);
8556 vim_free(resultbuf);
8557 return result;
8558}
8559
8560/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008561 * Expand the <sfile> string in "arg".
8562 *
8563 * Returns an allocated string, or NULL for any error.
8564 */
8565 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008566expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008567{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008568 char *errormsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008569 int len;
8570 char_u *result;
8571 char_u *newres;
8572 char_u *repl;
8573 int srclen;
8574 char_u *p;
8575
8576 result = vim_strsave(arg);
8577 if (result == NULL)
8578 return NULL;
8579
8580 for (p = result; *p; )
8581 {
8582 if (STRNCMP(p, "<sfile>", 7) != 0)
8583 ++p;
8584 else
8585 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008586 // replace "<sfile>" with the sourced file name, and do ":" stuff
Bram Moolenaar63b92542007-03-27 14:57:09 +00008587 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008588 if (errormsg != NULL)
8589 {
8590 if (*errormsg)
8591 emsg(errormsg);
8592 vim_free(result);
8593 return NULL;
8594 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008595 if (repl == NULL) // no match (cannot happen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008596 {
8597 p += srclen;
8598 continue;
8599 }
8600 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
8601 newres = alloc(len);
8602 if (newres == NULL)
8603 {
8604 vim_free(repl);
8605 vim_free(result);
8606 return NULL;
8607 }
8608 mch_memmove(newres, result, (size_t)(p - result));
8609 STRCPY(newres + (p - result), repl);
8610 len = (int)STRLEN(newres);
8611 STRCAT(newres, p + srclen);
8612 vim_free(repl);
8613 vim_free(result);
8614 result = newres;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008615 p = newres + len; // continue after the match
Bram Moolenaar071d4272004-06-13 20:20:40 +00008616 }
8617 }
8618
8619 return result;
8620}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008621
Bram Moolenaar071d4272004-06-13 20:20:40 +00008622#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00008623/*
Bram Moolenaard9462e32011-04-11 21:35:11 +02008624 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +00008625 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +00008626 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008627 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008628dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008629{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008630 if (fname == NULL)
8631 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +02008632 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008633}
8634#endif
8635
8636/*
8637 * ":behave {mswin,xterm}"
8638 */
8639 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008640ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008641{
8642 if (STRCMP(eap->arg, "mswin") == 0)
8643 {
8644 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
8645 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
8646 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
8647 set_option_value((char_u *)"keymodel", 0L,
8648 (char_u *)"startsel,stopsel", 0);
8649 }
8650 else if (STRCMP(eap->arg, "xterm") == 0)
8651 {
8652 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
8653 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
8654 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
8655 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
8656 }
8657 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008658 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008659}
8660
Bram Moolenaar071d4272004-06-13 20:20:40 +00008661static int filetype_detect = FALSE;
8662static int filetype_plugin = FALSE;
8663static int filetype_indent = FALSE;
8664
8665/*
8666 * ":filetype [plugin] [indent] {on,off,detect}"
8667 * on: Load the filetype.vim file to install autocommands for file types.
8668 * off: Load the ftoff.vim file to remove all autocommands for file types.
8669 * plugin on: load filetype.vim and ftplugin.vim
8670 * plugin off: load ftplugof.vim
8671 * indent on: load filetype.vim and indent.vim
8672 * indent off: load indoff.vim
8673 */
8674 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008675ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008676{
8677 char_u *arg = eap->arg;
8678 int plugin = FALSE;
8679 int indent = FALSE;
8680
8681 if (*eap->arg == NUL)
8682 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008683 // Print current status.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008684 smsg("filetype detection:%s plugin:%s indent:%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00008685 filetype_detect ? "ON" : "OFF",
8686 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
8687 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
8688 return;
8689 }
8690
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008691 // Accept "plugin" and "indent" in any order.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008692 for (;;)
8693 {
8694 if (STRNCMP(arg, "plugin", 6) == 0)
8695 {
8696 plugin = TRUE;
8697 arg = skipwhite(arg + 6);
8698 continue;
8699 }
8700 if (STRNCMP(arg, "indent", 6) == 0)
8701 {
8702 indent = TRUE;
8703 arg = skipwhite(arg + 6);
8704 continue;
8705 }
8706 break;
8707 }
8708 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
8709 {
8710 if (*arg == 'o' || !filetype_detect)
8711 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008712 source_runtime((char_u *)FILETYPE_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008713 filetype_detect = TRUE;
8714 if (plugin)
8715 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008716 source_runtime((char_u *)FTPLUGIN_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008717 filetype_plugin = TRUE;
8718 }
8719 if (indent)
8720 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008721 source_runtime((char_u *)INDENT_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008722 filetype_indent = TRUE;
8723 }
8724 }
8725 if (*arg == 'd')
8726 {
Bram Moolenaar1610d052016-06-09 22:53:01 +02008727 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +00008728 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008729 }
8730 }
8731 else if (STRCMP(arg, "off") == 0)
8732 {
8733 if (plugin || indent)
8734 {
8735 if (plugin)
8736 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008737 source_runtime((char_u *)FTPLUGOF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008738 filetype_plugin = FALSE;
8739 }
8740 if (indent)
8741 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008742 source_runtime((char_u *)INDOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008743 filetype_indent = FALSE;
8744 }
8745 }
8746 else
8747 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008748 source_runtime((char_u *)FTOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008749 filetype_detect = FALSE;
8750 }
8751 }
8752 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008753 semsg(_(e_invarg2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008754}
8755
8756/*
Bram Moolenaar3e545692017-06-04 19:00:32 +02008757 * ":setfiletype [FALLBACK] {name}"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008758 */
8759 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008760ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008761{
8762 if (!did_filetype)
Bram Moolenaar3e545692017-06-04 19:00:32 +02008763 {
8764 char_u *arg = eap->arg;
8765
8766 if (STRNCMP(arg, "FALLBACK ", 9) == 0)
8767 arg += 9;
8768
8769 set_option_value((char_u *)"filetype", 0L, arg, OPT_LOCAL);
8770 if (arg != eap->arg)
8771 did_filetype = FALSE;
8772 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008773}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008774
Bram Moolenaar071d4272004-06-13 20:20:40 +00008775 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008776ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008777{
8778#ifdef FEAT_DIGRAPHS
8779 if (*eap->arg != NUL)
8780 putdigraph(eap->arg);
8781 else
Bram Moolenaareae8ae12018-12-14 18:53:02 +01008782 listdigraphs(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008783#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008784 emsg(_("E196: No digraphs in this version"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008785#endif
8786}
8787
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02008788#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
8789 void
8790set_no_hlsearch(int flag)
8791{
8792 no_hlsearch = flag;
8793# ifdef FEAT_EVAL
8794 set_vim_var_nr(VV_HLSEARCH, !no_hlsearch && p_hls);
8795# endif
8796}
8797
Bram Moolenaar071d4272004-06-13 20:20:40 +00008798/*
8799 * ":nohlsearch"
8800 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008801 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008802ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008803{
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02008804 set_no_hlsearch(TRUE);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00008805 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008806}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008807#endif
8808
8809#ifdef FEAT_CRYPT
8810/*
8811 * ":X": Get crypt key
8812 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008813 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008814ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008815{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +01008816 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02008817 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008818}
8819#endif
8820
8821#ifdef FEAT_FOLDING
8822 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008823ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008824{
8825 if (foldManualAllowed(TRUE))
8826 foldCreate(eap->line1, eap->line2);
8827}
8828
8829 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008830ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008831{
8832 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
8833 eap->forceit, FALSE);
8834}
8835
8836 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008837ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008838{
8839 linenr_T lnum;
8840
Bram Moolenaar4facea32019-10-12 20:17:40 +02008841# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02008842 start_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +02008843# endif
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02008844
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008845 // First set the marks for all lines closed/open.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008846 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
8847 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
8848 ml_setmarked(lnum);
8849
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008850 // Execute the command on the marked lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008851 global_exe(eap->arg);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008852 ml_clearmarked(); // clear rest of the marks
Bram Moolenaar4facea32019-10-12 20:17:40 +02008853# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02008854 end_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +02008855# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008856}
8857#endif
Bram Moolenaarf5291f32017-09-14 22:55:37 +02008858
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008859#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar39665952018-08-15 20:59:48 +02008860/*
8861 * Returns TRUE if the supplied Ex cmdidx is for a location list command
8862 * instead of a quickfix command.
8863 */
8864 int
8865is_loclist_cmd(int cmdidx)
8866{
Bram Moolenaar74c8be22018-08-23 22:51:40 +02008867 if (cmdidx < 0 || cmdidx >= CMD_SIZE)
Bram Moolenaar39665952018-08-15 20:59:48 +02008868 return FALSE;
8869 return cmdnames[cmdidx].cmd_name[0] == 'l';
8870}
8871#endif
8872
Bram Moolenaar4facea32019-10-12 20:17:40 +02008873#if defined(FEAT_TIMERS) || defined(PROTO)
Bram Moolenaarf5291f32017-09-14 22:55:37 +02008874 int
8875get_pressedreturn(void)
8876{
8877 return ex_pressedreturn;
8878}
8879
8880 void
8881set_pressedreturn(int val)
8882{
8883 ex_pressedreturn = val;
8884}
8885#endif