blob: 0573897b7c7f03f0d63defd770b97b8bec9641ca [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * ex_docmd.c: functions for executing an Ex command line.
12 */
13
14#include "vim.h"
15
Bram Moolenaar071d4272004-06-13 20:20:40 +000016static int quitmore = 0;
17static int ex_pressedreturn = FALSE;
18#ifndef FEAT_PRINTER
19# define ex_hardcopy ex_ni
20#endif
21
Bram Moolenaar071d4272004-06-13 20:20:40 +000022#ifdef FEAT_EVAL
Bram Moolenaarddef1292019-12-16 17:10:33 +010023static char_u *do_one_cmd(char_u **, int, cstack_T *, char_u *(*fgetline)(int, void *, int, int), void *cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024#else
Bram Moolenaare96a2492019-06-25 04:12:16 +020025static char_u *do_one_cmd(char_u **, int, char_u *(*fgetline)(int, void *, int, int), void *cookie);
Bram Moolenaar217e1b82019-12-01 21:41:28 +010026static int if_level = 0; // depth in :if
Bram Moolenaar071d4272004-06-13 20:20:40 +000027#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010028static void append_command(char_u *cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000029
Bram Moolenaar071d4272004-06-13 20:20:40 +000030#ifndef FEAT_MENU
31# define ex_emenu ex_ni
32# define ex_menu ex_ni
33# define ex_menutranslate ex_ni
34#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010035static void ex_autocmd(exarg_T *eap);
36static void ex_doautocmd(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010037static void ex_bunload(exarg_T *eap);
38static void ex_buffer(exarg_T *eap);
39static void ex_bmodified(exarg_T *eap);
40static void ex_bnext(exarg_T *eap);
41static void ex_bprevious(exarg_T *eap);
42static void ex_brewind(exarg_T *eap);
43static void ex_blast(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010044static char_u *getargcmd(char_u **);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010045static int getargopt(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000046#ifndef FEAT_QUICKFIX
47# define ex_make ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +000048# define ex_cbuffer ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000049# define ex_cc ex_ni
50# define ex_cnext ex_ni
Bram Moolenaar3ff33112019-05-03 21:56:35 +020051# define ex_cbelow ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000052# define ex_cfile ex_ni
53# define qf_list ex_ni
54# define qf_age ex_ni
Bram Moolenaarf6acffb2016-07-16 16:54:24 +020055# define qf_history ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000056# define ex_helpgrep ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +000057# define ex_vimgrep ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000058#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +020059#if !defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +000060# define ex_cclose ex_ni
61# define ex_copen ex_ni
62# define ex_cwindow ex_ni
Bram Moolenaardcb17002016-07-07 18:58:59 +020063# define ex_cbottom ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000064#endif
Bram Moolenaar1e015462005-09-25 22:16:38 +000065#if !defined(FEAT_QUICKFIX) || !defined(FEAT_EVAL)
66# define ex_cexpr ex_ni
67#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000068
Bram Moolenaarb7316892019-05-01 18:08:42 +020069static linenr_T get_address(exarg_T *, char_u **, cmd_addr_T addr_type, int skip, int silent, int to_other_file, int address_count);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010070static void get_flags(exarg_T *eap);
Bram Moolenaar85363ab2010-07-18 13:58:26 +020071#if !defined(FEAT_PERL) \
72 || !defined(FEAT_PYTHON) || !defined(FEAT_PYTHON3) \
73 || !defined(FEAT_TCL) \
74 || !defined(FEAT_RUBY) \
75 || !defined(FEAT_LUA) \
76 || !defined(FEAT_MZSCHEME)
Bram Moolenaar7bb75552007-07-16 18:39:49 +000077# define HAVE_EX_SCRIPT_NI
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010078static void ex_script_ni(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000079#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010080static char *invalid_range(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010081static void correct_range(exarg_T *eap);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000082#ifdef FEAT_QUICKFIX
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010083static char_u *replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000084#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010085static char_u *repl_cmdline(exarg_T *eap, char_u *src, int srclen, char_u *repl, char_u **cmdlinep);
86static void ex_highlight(exarg_T *eap);
87static void ex_colorscheme(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010088static void ex_cquit(exarg_T *eap);
89static void ex_quit_all(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010090static void ex_close(exarg_T *eap);
91static void ex_win_close(int forceit, win_T *win, tabpage_T *tp);
92static void ex_only(exarg_T *eap);
93static void ex_resize(exarg_T *eap);
94static void ex_stag(exarg_T *eap);
95static void ex_tabclose(exarg_T *eap);
96static void ex_tabonly(exarg_T *eap);
97static void ex_tabnext(exarg_T *eap);
98static void ex_tabmove(exarg_T *eap);
99static void ex_tabs(exarg_T *eap);
Bram Moolenaar4033c552017-09-16 20:54:51 +0200100#if defined(FEAT_QUICKFIX)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100101static void ex_pclose(exarg_T *eap);
102static void ex_ptag(exarg_T *eap);
103static void ex_pedit(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000104#else
105# define ex_pclose ex_ni
106# define ex_ptag ex_ni
107# define ex_pedit ex_ni
108#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100109static void ex_hide(exarg_T *eap);
110static void ex_stop(exarg_T *eap);
111static void ex_exit(exarg_T *eap);
112static void ex_print(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000113#ifdef FEAT_BYTEOFF
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100114static void ex_goto(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000115#else
116# define ex_goto ex_ni
117#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100118static void ex_shell(exarg_T *eap);
119static void ex_preserve(exarg_T *eap);
120static void ex_recover(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100121static void ex_mode(exarg_T *eap);
122static void ex_wrongmodifier(exarg_T *eap);
123static void ex_find(exarg_T *eap);
124static void ex_open(exarg_T *eap);
125static void ex_edit(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000126#ifndef FEAT_GUI
127# define ex_gui ex_nogui
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100128static void ex_nogui(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000129#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +0100130#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100131static void ex_tearoff(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000132#else
133# define ex_tearoff ex_ni
134#endif
Bram Moolenaar29a2c082018-03-05 21:06:23 +0100135#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
136 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100137static void ex_popup(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000138#else
139# define ex_popup ex_ni
140#endif
141#ifndef FEAT_GUI_MSWIN
142# define ex_simalt ex_ni
143#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000144#if !defined(FEAT_GUI_MSWIN) && !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000145# define gui_mch_find_dialog ex_ni
146# define gui_mch_replace_dialog ex_ni
147#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000148#if !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000149# define ex_helpfind ex_ni
150#endif
151#ifndef FEAT_CSCOPE
Bram Moolenaard4db7712016-11-12 19:16:46 +0100152# define ex_cscope ex_ni
153# define ex_scscope ex_ni
154# define ex_cstag ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000155#endif
156#ifndef FEAT_SYN_HL
157# define ex_syntax ex_ni
Bram Moolenaar860cae12010-06-05 23:22:07 +0200158# define ex_ownsyntax ex_ni
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000159#endif
Bram Moolenaarf7512552013-06-06 14:55:19 +0200160#if !defined(FEAT_SYN_HL) || !defined(FEAT_PROFILE)
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +0200161# define ex_syntime ex_ni
162#endif
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000163#ifndef FEAT_SPELL
Bram Moolenaarb765d632005-06-07 21:00:02 +0000164# define ex_spell ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000165# define ex_mkspell ex_ni
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000166# define ex_spelldump ex_ni
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000167# define ex_spellinfo ex_ni
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000168# define ex_spellrepall ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000169#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200170#ifndef FEAT_PERSISTENT_UNDO
171# define ex_rundo ex_ni
172# define ex_wundo ex_ni
173#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200174#ifndef FEAT_LUA
175# define ex_lua ex_script_ni
176# define ex_luado ex_ni
177# define ex_luafile ex_ni
178#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000179#ifndef FEAT_MZSCHEME
180# define ex_mzscheme ex_script_ni
181# define ex_mzfile ex_ni
182#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000183#ifndef FEAT_PERL
184# define ex_perl ex_script_ni
185# define ex_perldo ex_ni
186#endif
187#ifndef FEAT_PYTHON
188# define ex_python ex_script_ni
Bram Moolenaard620aa92013-05-17 16:40:06 +0200189# define ex_pydo ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000190# define ex_pyfile ex_ni
191#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200192#ifndef FEAT_PYTHON3
Bram Moolenaar368373e2010-07-19 20:46:22 +0200193# define ex_py3 ex_script_ni
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200194# define ex_py3do ex_ni
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200195# define ex_py3file ex_ni
196#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100197#if !defined(FEAT_PYTHON) && !defined(FEAT_PYTHON3)
198# define ex_pyx ex_script_ni
199# define ex_pyxdo ex_ni
200# define ex_pyxfile ex_ni
201#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000202#ifndef FEAT_TCL
203# define ex_tcl ex_script_ni
204# define ex_tcldo ex_ni
205# define ex_tclfile ex_ni
206#endif
207#ifndef FEAT_RUBY
208# define ex_ruby ex_script_ni
209# define ex_rubydo ex_ni
210# define ex_rubyfile ex_ni
211#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000212#ifndef FEAT_KEYMAP
213# define ex_loadkeymap ex_ni
214#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100215static void ex_swapname(exarg_T *eap);
216static void ex_syncbind(exarg_T *eap);
217static void ex_read(exarg_T *eap);
218static void ex_pwd(exarg_T *eap);
219static void ex_equal(exarg_T *eap);
220static void ex_sleep(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100221static void ex_winsize(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100222static void ex_wincmd(exarg_T *eap);
Bram Moolenaar843ee412004-06-30 16:16:41 +0000223#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100224static void ex_winpos(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000225#else
226# define ex_winpos ex_ni
227#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100228static void ex_operators(exarg_T *eap);
229static void ex_put(exarg_T *eap);
230static void ex_copymove(exarg_T *eap);
231static void ex_submagic(exarg_T *eap);
232static void ex_join(exarg_T *eap);
233static void ex_at(exarg_T *eap);
234static void ex_bang(exarg_T *eap);
235static void ex_undo(exarg_T *eap);
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200236#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100237static void ex_wundo(exarg_T *eap);
238static void ex_rundo(exarg_T *eap);
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200239#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100240static void ex_redo(exarg_T *eap);
241static void ex_later(exarg_T *eap);
242static void ex_redir(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100243static void ex_redrawstatus(exarg_T *eap);
Bram Moolenaare12bab32019-01-08 22:02:56 +0100244static void ex_redrawtabline(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100245static void close_redir(void);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100246static void ex_mark(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100247static void ex_startinsert(exarg_T *eap);
248static void ex_stopinsert(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000249#ifdef FEAT_FIND_ID
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100250static void ex_checkpath(exarg_T *eap);
251static void ex_findpat(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000252#else
253# define ex_findpat ex_ni
254# define ex_checkpath ex_ni
255#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +0200256#if defined(FEAT_FIND_ID) && defined(FEAT_QUICKFIX)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100257static void ex_psearch(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000258#else
259# define ex_psearch ex_ni
260#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100261static void ex_tag(exarg_T *eap);
262static void ex_tag_cmd(exarg_T *eap, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000263#ifndef FEAT_EVAL
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200264# define ex_break ex_ni
265# define ex_breakadd ex_ni
266# define ex_breakdel ex_ni
267# define ex_breaklist ex_ni
268# define ex_call ex_ni
269# define ex_catch ex_ni
270# define ex_compiler ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200271# define ex_continue ex_ni
272# define ex_debug ex_ni
273# define ex_debuggreedy ex_ni
Bram Moolenaar822ba242020-05-24 23:00:18 +0200274# define ex_def ex_ni
275# define ex_defcompile ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200276# define ex_delfunction ex_ni
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100277# define ex_disassemble ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000278# define ex_echo ex_ni
279# define ex_echohl ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000280# define ex_else ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200281# define ex_endfunction ex_ni
282# define ex_endif ex_ni
283# define ex_endtry ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000284# define ex_endwhile ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200285# define ex_eval ex_ni
286# define ex_execute ex_ni
287# define ex_finally ex_ni
288# define ex_finish ex_ni
289# define ex_function ex_ni
290# define ex_if ex_ni
291# define ex_let ex_ni
292# define ex_lockvar ex_ni
293# define ex_oldfiles ex_ni
294# define ex_options ex_ni
295# define ex_packadd ex_ni
296# define ex_packloadall ex_ni
297# define ex_return ex_ni
298# define ex_scriptnames ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000299# define ex_throw ex_ni
300# define ex_try ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000301# define ex_unlet ex_ni
Bram Moolenaar65c1b012005-01-31 19:02:28 +0000302# define ex_unlockvar ex_ni
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100303# define ex_vim9script ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200304# define ex_while ex_ni
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100305# define ex_import ex_ni
306# define ex_export ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000307#endif
Bram Moolenaar84538072019-07-28 14:15:42 +0200308#ifndef FEAT_SESSION
Bram Moolenaar071d4272004-06-13 20:20:40 +0000309# define ex_loadview ex_ni
310#endif
Bram Moolenaardefa0672019-07-21 19:25:37 +0200311#ifndef FEAT_VIMINFO
Bram Moolenaar071d4272004-06-13 20:20:40 +0000312# define ex_viminfo ex_ni
313#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100314static void ex_behave(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100315static void ex_filetype(exarg_T *eap);
316static void ex_setfiletype(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000317#ifndef FEAT_DIFF
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000318# define ex_diffoff ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000319# define ex_diffpatch ex_ni
320# define ex_diffgetput ex_ni
321# define ex_diffsplit ex_ni
322# define ex_diffthis ex_ni
323# define ex_diffupdate ex_ni
324#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100325static void ex_digraphs(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000326#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100327static void ex_nohlsearch(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000328#else
329# define ex_nohlsearch ex_ni
330# define ex_match ex_ni
331#endif
332#ifdef FEAT_CRYPT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100333static void ex_X(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000334#else
335# define ex_X ex_ni
336#endif
337#ifdef FEAT_FOLDING
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100338static void ex_fold(exarg_T *eap);
339static void ex_foldopen(exarg_T *eap);
340static void ex_folddo(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000341#else
342# define ex_fold ex_ni
343# define ex_foldopen ex_ni
344# define ex_folddo ex_ni
345#endif
Bram Moolenaar13505972019-01-24 15:04:48 +0100346#if !(defined(HAVE_LOCALE_H) || defined(X_LOCALE))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000347# define ex_language ex_ni
348#endif
349#ifndef FEAT_SIGNS
350# define ex_sign ex_ni
351#endif
Bram Moolenaar009b2592004-10-24 19:18:58 +0000352#ifndef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200353# define ex_nbclose ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000354# define ex_nbkey ex_ni
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200355# define ex_nbstart ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000356#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000357
Bram Moolenaar071d4272004-06-13 20:20:40 +0000358#ifndef FEAT_JUMPLIST
359# define ex_jumps ex_ni
Bram Moolenaar2d358992016-06-12 21:20:54 +0200360# define ex_clearjumps ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000361# define ex_changes ex_ni
362#endif
363
Bram Moolenaar05159a02005-02-26 23:04:13 +0000364#ifndef FEAT_PROFILE
365# define ex_profile ex_ni
366#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200367#ifndef FEAT_TERMINAL
368# define ex_terminal ex_ni
369#endif
Bram Moolenaard4aa83a2019-05-09 18:59:31 +0200370#if !defined(FEAT_X11) || !defined(FEAT_XCLIPBOARD)
371# define ex_xrestore ex_ni
372#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100373#if !defined(FEAT_PROP_POPUP)
Bram Moolenaar682725c2019-05-25 20:10:37 +0200374# define ex_popupclear ex_ni
375#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +0000376
Bram Moolenaar071d4272004-06-13 20:20:40 +0000377/*
378 * Declare cmdnames[].
379 */
380#define DO_DECLARE_EXCMD
381#include "ex_cmds.h"
Bram Moolenaar6de5e122017-04-20 21:55:44 +0200382#include "ex_cmdidxs.h"
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +0100383
Bram Moolenaar071d4272004-06-13 20:20:40 +0000384static char_u dollar_command[2] = {'$', 0};
385
386
387#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100388// Struct for storing a line inside a while/for loop
Bram Moolenaar071d4272004-06-13 20:20:40 +0000389typedef struct
390{
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100391 char_u *line; // command line
392 linenr_T lnum; // sourcing_lnum of the line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000393} wcmd_T;
394
395/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000396 * Structure used to store info for line position in a while or for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000397 * This is required, because do_one_cmd() may invoke ex_function(), which
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000398 * reads more lines that may come from the while/for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000399 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000400struct loop_cookie
Bram Moolenaar071d4272004-06-13 20:20:40 +0000401{
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100402 garray_T *lines_gap; // growarray with line info
403 int current_line; // last read line from growarray
404 int repeating; // TRUE when looping a second time
405 // When "repeating" is FALSE use "getline" and "cookie" to get lines
Bram Moolenaare96a2492019-06-25 04:12:16 +0200406 char_u *(*getline)(int, void *, int, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000407 void *cookie;
408};
409
Bram Moolenaare96a2492019-06-25 04:12:16 +0200410static char_u *get_loop_line(int c, void *cookie, int indent, int do_concat);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100411static int store_loop_line(garray_T *gap, char_u *line);
412static void free_cmdlines(garray_T *gap);
Bram Moolenaared203462004-06-16 11:19:22 +0000413
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100414// Struct to save a few things while debugging. Used in do_cmdline() only.
Bram Moolenaared203462004-06-16 11:19:22 +0000415struct dbg_stuff
416{
417 int trylevel;
418 int force_abort;
419 except_T *caught_stack;
420 char_u *vv_exception;
421 char_u *vv_throwpoint;
422 int did_emsg;
423 int got_int;
424 int did_throw;
425 int need_rethrow;
426 int check_cstack;
427 except_T *current_exception;
428};
429
Bram Moolenaared203462004-06-16 11:19:22 +0000430 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100431save_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000432{
433 dsp->trylevel = trylevel; trylevel = 0;
434 dsp->force_abort = force_abort; force_abort = FALSE;
435 dsp->caught_stack = caught_stack; caught_stack = NULL;
436 dsp->vv_exception = v_exception(NULL);
437 dsp->vv_throwpoint = v_throwpoint(NULL);
438
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100439 // Necessary for debugging an inactive ":catch", ":finally", ":endtry"
Bram Moolenaared203462004-06-16 11:19:22 +0000440 dsp->did_emsg = did_emsg; did_emsg = FALSE;
441 dsp->got_int = got_int; got_int = FALSE;
442 dsp->did_throw = did_throw; did_throw = FALSE;
443 dsp->need_rethrow = need_rethrow; need_rethrow = FALSE;
444 dsp->check_cstack = check_cstack; check_cstack = FALSE;
445 dsp->current_exception = current_exception; current_exception = NULL;
446}
447
448 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100449restore_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000450{
451 suppress_errthrow = FALSE;
452 trylevel = dsp->trylevel;
453 force_abort = dsp->force_abort;
454 caught_stack = dsp->caught_stack;
455 (void)v_exception(dsp->vv_exception);
456 (void)v_throwpoint(dsp->vv_throwpoint);
457 did_emsg = dsp->did_emsg;
458 got_int = dsp->got_int;
459 did_throw = dsp->did_throw;
460 need_rethrow = dsp->need_rethrow;
461 check_cstack = dsp->check_cstack;
462 current_exception = dsp->current_exception;
463}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000464#endif
465
Bram Moolenaar071d4272004-06-13 20:20:40 +0000466/*
467 * do_exmode(): Repeatedly get commands for the "Ex" mode, until the ":vi"
468 * command is given.
469 */
470 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100471do_exmode(
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100472 int improved) // TRUE for "improved Ex" mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000473{
474 int save_msg_scroll;
475 int prev_msg_row;
476 linenr_T prev_line;
Bram Moolenaar79518e22017-02-17 16:31:35 +0100477 varnumber_T changedtick;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000478
479 if (improved)
480 exmode_active = EXMODE_VIM;
481 else
482 exmode_active = EXMODE_NORMAL;
483 State = NORMAL;
484
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100485 // When using ":global /pat/ visual" and then "Q" we return to continue
486 // the :global command.
Bram Moolenaardf177f62005-02-22 08:39:57 +0000487 if (global_busy)
488 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000489
490 save_msg_scroll = msg_scroll;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100491 ++RedrawingDisabled; // don't redisplay the window
492 ++no_wait_return; // don't wait for return
Bram Moolenaar071d4272004-06-13 20:20:40 +0000493#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100494 // Ignore scrollbar and mouse events in Ex mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000495 ++hold_gui_events;
496#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000497
Bram Moolenaar32526b32019-01-19 17:43:09 +0100498 msg(_("Entering Ex mode. Type \"visual\" to go to Normal mode."));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000499 while (exmode_active)
500 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100501 // Check for a ":normal" command and no more characters left.
Bram Moolenaar7c626922005-02-07 22:01:03 +0000502 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
503 {
504 exmode_active = FALSE;
505 break;
506 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000507 msg_scroll = TRUE;
508 need_wait_return = FALSE;
509 ex_pressedreturn = FALSE;
510 ex_no_reprint = FALSE;
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100511 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000512 prev_msg_row = msg_row;
513 prev_line = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000514 if (improved)
515 {
516 cmdline_row = msg_row;
517 do_cmdline(NULL, getexline, NULL, 0);
518 }
519 else
520 do_cmdline(NULL, getexmodeline, NULL, DOCMD_NOWAIT);
521 lines_left = Rows - 1;
522
Bram Moolenaardf177f62005-02-22 08:39:57 +0000523 if ((prev_line != curwin->w_cursor.lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100524 || changedtick != CHANGEDTICK(curbuf)) && !ex_no_reprint)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000525 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000526 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100527 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000528 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000529 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000530 if (ex_pressedreturn)
531 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100532 // go up one line, to overwrite the ":<CR>" line, so the
533 // output doesn't contain empty lines.
Bram Moolenaardf177f62005-02-22 08:39:57 +0000534 msg_row = prev_msg_row;
535 if (prev_msg_row == Rows - 1)
536 msg_row--;
537 }
538 msg_col = 0;
539 print_line_no_prefix(curwin->w_cursor.lnum, FALSE, FALSE);
540 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000541 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000542 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100543 else if (ex_pressedreturn && !ex_no_reprint) // must be at EOF
Bram Moolenaardf177f62005-02-22 08:39:57 +0000544 {
545 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100546 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000547 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100548 emsg(_("E501: At end-of-file"));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000549 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000550 }
551
552#ifdef FEAT_GUI
553 --hold_gui_events;
554#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000555 --RedrawingDisabled;
556 --no_wait_return;
557 update_screen(CLEAR);
558 need_wait_return = FALSE;
559 msg_scroll = save_msg_scroll;
560}
561
562/*
Bram Moolenaar4facea32019-10-12 20:17:40 +0200563 * Print the executed command for when 'verbose' is set.
564 * When "lnum" is 0 only print the command.
565 */
566 static void
567msg_verbose_cmd(linenr_T lnum, char_u *cmd)
568{
569 ++no_wait_return;
570 verbose_enter_scroll();
571
572 if (lnum == 0)
573 smsg(_("Executing: %s"), cmd);
574 else
575 smsg(_("line %ld: %s"), (long)lnum, cmd);
576 if (msg_silent == 0)
577 msg_puts("\n"); // don't overwrite this
578
579 verbose_leave_scroll();
580 --no_wait_return;
581}
582
583/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000584 * Execute a simple command line. Used for translated commands like "*".
585 */
586 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100587do_cmdline_cmd(char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000588{
589 return do_cmdline(cmd, NULL, NULL,
590 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
591}
592
593/*
594 * do_cmdline(): execute one Ex command line
595 *
596 * 1. Execute "cmdline" when it is not NULL.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100597 * If "cmdline" is NULL, or more lines are needed, fgetline() is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000598 * 2. Split up in parts separated with '|'.
599 *
600 * This function can be called recursively!
601 *
602 * flags:
603 * DOCMD_VERBOSE - The command will be included in the error message.
604 * DOCMD_NOWAIT - Don't call wait_return() and friends.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100605 * DOCMD_REPEAT - Repeat execution until fgetline() returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000606 * DOCMD_KEYTYPED - Don't reset KeyTyped.
607 * DOCMD_EXCRESET - Reset the exception environment (used for debugging).
608 * DOCMD_KEEPLINE - Store first typed line (for repeating with ".").
609 *
610 * return FAIL if cmdline could not be executed, OK otherwise
611 */
612 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100613do_cmdline(
614 char_u *cmdline,
Bram Moolenaare96a2492019-06-25 04:12:16 +0200615 char_u *(*fgetline)(int, void *, int, int),
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100616 void *cookie, // argument for fgetline()
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100617 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000618{
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100619 char_u *next_cmdline; // next cmd to execute
620 char_u *cmdline_copy = NULL; // copy of cmd line
621 int used_getline = FALSE; // used "fgetline" to obtain command
622 static int recursive = 0; // recursive depth
Bram Moolenaar071d4272004-06-13 20:20:40 +0000623 int msg_didout_before_start = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100624 int count = 0; // line number count
625 int did_inc = FALSE; // incremented RedrawingDisabled
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626 int retval = OK;
627#ifdef FEAT_EVAL
Bram Moolenaarddef1292019-12-16 17:10:33 +0100628 cstack_T cstack; // conditional stack
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100629 garray_T lines_ga; // keep lines for ":while"/":for"
630 int current_line = 0; // active line in lines_ga
Bram Moolenaard5053d02020-06-28 15:51:16 +0200631 int current_line_before = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100632 char_u *fname = NULL; // function or script name
633 linenr_T *breakpoint = NULL; // ptr to breakpoint field in cookie
634 int *dbg_tick = NULL; // ptr to dbg_tick field in cookie
635 struct dbg_stuff debug_saved; // saved things for debug mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000636 int initial_trylevel;
Bram Moolenaar25e0f582020-05-25 22:36:50 +0200637 msglist_T **saved_msg_list = NULL;
638 msglist_T *private_msg_list;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000639
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100640 // "fgetline" and "cookie" passed to do_one_cmd()
Bram Moolenaare96a2492019-06-25 04:12:16 +0200641 char_u *(*cmd_getline)(int, void *, int, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642 void *cmd_cookie;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000643 struct loop_cookie cmd_loop_cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000644 void *real_cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000645 int getline_is_func;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000646#else
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100647# define cmd_getline fgetline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000648# define cmd_cookie cookie
649#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100650 static int call_depth = 0; // recursiveness
Bram Moolenaar2196bce2020-04-12 20:01:11 +0200651#ifdef FEAT_EVAL
Bram Moolenaare31ee862020-01-07 20:59:34 +0100652 ESTACK_CHECK_DECLARATION
Bram Moolenaar071d4272004-06-13 20:20:40 +0000653
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100654 // For every pair of do_cmdline()/do_one_cmd() calls, use an extra memory
655 // location for storing error messages to be converted to an exception.
656 // This ensures that the do_errthrow() call in do_one_cmd() does not
657 // combine the messages stored by an earlier invocation of do_one_cmd()
658 // with the command name of the later one. This would happen when
659 // BufWritePost autocommands are executed after a write error.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000660 saved_msg_list = msg_list;
661 msg_list = &private_msg_list;
662 private_msg_list = NULL;
663#endif
664
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100665 // It's possible to create an endless loop with ":execute", catch that
666 // here. The value of 200 allows nested function calls, ":source", etc.
667 // Allow 200 or 'maxfuncdepth', whatever is larger.
Bram Moolenaarb094ff42017-01-02 16:16:39 +0100668 if (call_depth >= 200
669#ifdef FEAT_EVAL
670 && call_depth >= p_mfd
671#endif
672 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000673 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100674 emsg(_("E169: Command too recursive"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100676 // When converting to an exception, we do not include the command name
677 // since this is not an error of the specific command.
Bram Moolenaarddef1292019-12-16 17:10:33 +0100678 do_errthrow((cstack_T *)NULL, (char_u *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679 msg_list = saved_msg_list;
680#endif
681 return FAIL;
682 }
683 ++call_depth;
684
685#ifdef FEAT_EVAL
686 cstack.cs_idx = -1;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000687 cstack.cs_looplevel = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688 cstack.cs_trylevel = 0;
689 cstack.cs_emsg_silent_list = NULL;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000690 cstack.cs_lflags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691 ga_init2(&lines_ga, (int)sizeof(wcmd_T), 10);
692
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100693 real_cookie = getline_cookie(fgetline, cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100695 // Inside a function use a higher nesting level.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100696 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000697 if (getline_is_func && ex_nesting_level == func_level(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698 ++ex_nesting_level;
699
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100700 // Get the function or script name and the address where the next breakpoint
701 // line and the debug tick for a function or script are stored.
Bram Moolenaar05159a02005-02-26 23:04:13 +0000702 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000703 {
704 fname = func_name(real_cookie);
705 breakpoint = func_breakpoint(real_cookie);
706 dbg_tick = func_dbg_tick(real_cookie);
707 }
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100708 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100710 fname = SOURCING_NAME;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000711 breakpoint = source_breakpoint(real_cookie);
712 dbg_tick = source_dbg_tick(real_cookie);
713 }
714
715 /*
716 * Initialize "force_abort" and "suppress_errthrow" at the top level.
717 */
718 if (!recursive)
719 {
720 force_abort = FALSE;
721 suppress_errthrow = FALSE;
722 }
723
724 /*
725 * If requested, store and reset the global values controlling the
Bram Moolenaar89d40322006-08-29 15:30:07 +0000726 * exception handling (used when debugging). Otherwise clear it to avoid
727 * a bogus compiler warning when the optimizer uses inline functions...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000728 */
Bram Moolenaarb4872942006-05-13 10:32:52 +0000729 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +0000730 save_dbg_stuff(&debug_saved);
Bram Moolenaar89d40322006-08-29 15:30:07 +0000731 else
Bram Moolenaara80faa82020-04-12 19:37:17 +0200732 CLEAR_FIELD(debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000733
734 initial_trylevel = trylevel;
735
736 /*
737 * "did_throw" will be set to TRUE when an exception is being thrown.
738 */
739 did_throw = FALSE;
740#endif
741 /*
742 * "did_emsg" will be set to TRUE when emsg() is used, in which case we
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000743 * cancel the whole command line, and any if/endif or loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000744 * If force_abort is set, we cancel everything.
745 */
746 did_emsg = FALSE;
747
748 /*
749 * KeyTyped is only set when calling vgetc(). Reset it here when not
750 * calling vgetc() (sourced command lines).
751 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100752 if (!(flags & DOCMD_KEYTYPED)
753 && !getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754 KeyTyped = FALSE;
755
756 /*
757 * Continue executing command lines:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000758 * - when inside an ":if", ":while" or ":for"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000759 * - for multiple commands on one line, separated with '|'
760 * - when repeating until there are no more lines (for ":source")
761 */
762 next_cmdline = cmdline;
763 do
764 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000765#ifdef FEAT_EVAL
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100766 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000767#endif
768
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100769 // stop skipping cmds for an error msg after all endif/while/for
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770 if (next_cmdline == NULL
771#ifdef FEAT_EVAL
772 && !force_abort
773 && cstack.cs_idx < 0
Bram Moolenaar05159a02005-02-26 23:04:13 +0000774 && !(getline_is_func && func_has_abort(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775#endif
776 )
777 did_emsg = FALSE;
778
779 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000780 * 1. If repeating a line in a loop, get a line from lines_ga.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100781 * 2. If no line given: Get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782 * 3. If a line is given: Make a copy, so we can mess with it.
783 */
784
785#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100786 // 1. If repeating, get a previous line from lines_ga.
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000787 if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100789 // Each '|' separated command is stored separately in lines_ga, to
790 // be able to jump to it. Don't use next_cmdline now.
Bram Moolenaard23a8232018-02-10 18:45:26 +0100791 VIM_CLEAR(cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100793 // Check if a function has returned or, unless it has an unclosed
794 // try conditional, aborted.
Bram Moolenaar05159a02005-02-26 23:04:13 +0000795 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000797# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000798 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000799 func_line_end(real_cookie);
800# endif
801 if (func_has_ended(real_cookie))
802 {
803 retval = FAIL;
804 break;
805 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000807#ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000808 else if (do_profiling == PROF_YES
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100809 && getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000810 script_line_end();
811#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000812
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100813 // Check if a sourced file hit a ":finish" command.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100814 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815 {
816 retval = FAIL;
817 break;
818 }
819
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100820 // If breakpoints have been added/deleted need to check for it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000821 if (breakpoint != NULL && dbg_tick != NULL
822 && *dbg_tick != debug_tick)
823 {
824 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100825 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100826 fname, SOURCING_LNUM);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827 *dbg_tick = debug_tick;
828 }
829
830 next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100831 SOURCING_LNUM = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100833 // Did we encounter a breakpoint?
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834 if (breakpoint != NULL && *breakpoint != 0
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100835 && *breakpoint <= SOURCING_LNUM)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000836 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100837 dbg_breakpoint(fname, SOURCING_LNUM);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100838 // Find next breakpoint.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100840 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100841 fname, SOURCING_LNUM);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842 *dbg_tick = debug_tick;
843 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000844# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000845 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000846 {
847 if (getline_is_func)
848 func_line_start(real_cookie);
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100849 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000850 script_line_start();
851 }
852# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000853 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854#endif
855
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100856 // 2. If no line given, get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000857 if (next_cmdline == NULL)
858 {
859 /*
860 * Need to set msg_didout for the first line after an ":if",
861 * otherwise the ":if" will be overwritten.
862 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100863 if (count == 1 && getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000864 msg_didout = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100865 if (fgetline == NULL || (next_cmdline = fgetline(':', cookie,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000866#ifdef FEAT_EVAL
867 cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2
868#else
869 0
870#endif
Bram Moolenaare96a2492019-06-25 04:12:16 +0200871 , TRUE)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000872 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100873 // Don't call wait_return for aborted command line. The NULL
874 // returned for the end of a sourced file or executed function
875 // doesn't do this.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876 if (KeyTyped && !(flags & DOCMD_REPEAT))
877 need_wait_return = FALSE;
878 retval = FAIL;
879 break;
880 }
881 used_getline = TRUE;
882
883 /*
884 * Keep the first typed line. Clear it when more lines are typed.
885 */
886 if (flags & DOCMD_KEEPLINE)
887 {
888 vim_free(repeat_cmdline);
889 if (count == 0)
890 repeat_cmdline = vim_strsave(next_cmdline);
891 else
892 repeat_cmdline = NULL;
893 }
894 }
895
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100896 // 3. Make a copy of the command so we can mess with it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000897 else if (cmdline_copy == NULL)
898 {
899 next_cmdline = vim_strsave(next_cmdline);
900 if (next_cmdline == NULL)
901 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100902 emsg(_(e_outofmem));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000903 retval = FAIL;
904 break;
905 }
906 }
907 cmdline_copy = next_cmdline;
908
909#ifdef FEAT_EVAL
910 /*
Bram Moolenaard5053d02020-06-28 15:51:16 +0200911 * Inside a while/for loop, and when the command looks like a ":while"
912 * or ":for", the line is stored, because we may need it later when
913 * looping.
914 *
915 * When there is a '|' and another command, it is stored separately,
916 * because we need to be able to jump back to it from an
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000917 * :endwhile/:endfor.
Bram Moolenaard5053d02020-06-28 15:51:16 +0200918 *
919 * Pass a different "fgetline" function to do_one_cmd() below,
920 * that it stores lines in or reads them from "lines_ga". Makes it
921 * possible to define a function inside a while/for loop and handles
922 * line continuation.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000923 */
Bram Moolenaard5053d02020-06-28 15:51:16 +0200924 if ((cstack.cs_looplevel > 0 || has_loop_cmd(next_cmdline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000925 {
Bram Moolenaard5053d02020-06-28 15:51:16 +0200926 cmd_getline = get_loop_line;
927 cmd_cookie = (void *)&cmd_loop_cookie;
928 cmd_loop_cookie.lines_gap = &lines_ga;
929 cmd_loop_cookie.current_line = current_line;
930 cmd_loop_cookie.getline = fgetline;
931 cmd_loop_cookie.cookie = cookie;
932 cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len);
933
934 // Save the current line when encountering it the first time.
935 if (current_line == lines_ga.ga_len
936 && store_loop_line(&lines_ga, next_cmdline) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937 {
938 retval = FAIL;
939 break;
940 }
Bram Moolenaard5053d02020-06-28 15:51:16 +0200941 current_line_before = current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000942 }
Bram Moolenaard5053d02020-06-28 15:51:16 +0200943 else
944 {
945 cmd_getline = fgetline;
946 cmd_cookie = cookie;
947 }
948
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949 did_endif = FALSE;
950#endif
951
952 if (count++ == 0)
953 {
954 /*
955 * All output from the commands is put below each other, without
956 * waiting for a return. Don't do this when executing commands
957 * from a script or when being called recursive (e.g. for ":e
958 * +command file").
959 */
960 if (!(flags & DOCMD_NOWAIT) && !recursive)
961 {
962 msg_didout_before_start = msg_didout;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100963 msg_didany = FALSE; // no output yet
Bram Moolenaar071d4272004-06-13 20:20:40 +0000964 msg_start();
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100965 msg_scroll = TRUE; // put messages below each other
966 ++no_wait_return; // don't wait for return until finished
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967 ++RedrawingDisabled;
968 did_inc = TRUE;
969 }
970 }
971
Bram Moolenaar823654b2020-05-29 23:03:09 +0200972 if ((p_verbose >= 15 && SOURCING_NAME != NULL) || p_verbose >= 16)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100973 msg_verbose_cmd(SOURCING_LNUM, cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000974
975 /*
976 * 2. Execute one '|' separated command.
977 * do_one_cmd() will return NULL if there is no trailing '|'.
978 * "cmdline_copy" can change, e.g. for '%' and '#' expansion.
979 */
980 ++recursive;
981 next_cmdline = do_one_cmd(&cmdline_copy, flags & DOCMD_VERBOSE,
982#ifdef FEAT_EVAL
983 &cstack,
984#endif
985 cmd_getline, cmd_cookie);
986 --recursive;
987
988#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000989 if (cmd_cookie == (void *)&cmd_loop_cookie)
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100990 // Use "current_line" from "cmd_loop_cookie", it may have been
991 // incremented when defining a function.
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000992 current_line = cmd_loop_cookie.current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993#endif
994
995 if (next_cmdline == NULL)
996 {
Bram Moolenaard23a8232018-02-10 18:45:26 +0100997 VIM_CLEAR(cmdline_copy);
Bram Moolenaard7663c22019-08-06 21:59:57 +0200998
Bram Moolenaar071d4272004-06-13 20:20:40 +0000999 /*
1000 * If the command was typed, remember it for the ':' register.
1001 * Do this AFTER executing the command to make :@: work.
1002 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001003 if (getline_equal(fgetline, cookie, getexline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001004 && new_last_cmdline != NULL)
1005 {
1006 vim_free(last_cmdline);
1007 last_cmdline = new_last_cmdline;
1008 new_last_cmdline = NULL;
1009 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010 }
1011 else
1012 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001013 // need to copy the command after the '|' to cmdline_copy, for the
1014 // next do_one_cmd()
Bram Moolenaara7241f52008-06-24 20:39:31 +00001015 STRMOVE(cmdline_copy, next_cmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016 next_cmdline = cmdline_copy;
1017 }
1018
1019
1020#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001021 // reset did_emsg for a function that is not aborted by an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022 if (did_emsg && !force_abort
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001023 && getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001024 && !func_has_abort(real_cookie))
1025 did_emsg = FALSE;
1026
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001027 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028 {
1029 ++current_line;
1030
1031 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001032 * An ":endwhile", ":endfor" and ":continue" is handled here.
1033 * If we were executing commands, jump back to the ":while" or
1034 * ":for".
1035 * If we were not executing commands, decrement cs_looplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001037 if (cstack.cs_lflags & (CSL_HAD_CONT | CSL_HAD_ENDLOOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001039 cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001041 // Jump back to the matching ":while" or ":for". Be careful
1042 // not to use a cs_line[] from an entry that isn't a ":while"
1043 // or ":for": It would make "current_line" invalid and can
1044 // cause a crash.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045 if (!did_emsg && !got_int && !did_throw
1046 && cstack.cs_idx >= 0
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001047 && (cstack.cs_flags[cstack.cs_idx]
1048 & (CSF_WHILE | CSF_FOR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049 && cstack.cs_line[cstack.cs_idx] >= 0
1050 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE))
1051 {
1052 current_line = cstack.cs_line[cstack.cs_idx];
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001053 // remember we jumped there
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001054 cstack.cs_lflags |= CSL_HAD_LOOP;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001055 line_breakcheck(); // check if CTRL-C typed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001057 // Check for the next breakpoint at or after the ":while"
1058 // or ":for".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059 if (breakpoint != NULL)
1060 {
1061 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001062 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063 fname,
1064 ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1);
1065 *dbg_tick = debug_tick;
1066 }
1067 }
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001068 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001070 // can only get here with ":endwhile" or ":endfor"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071 if (cstack.cs_idx >= 0)
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001072 rewind_conditionals(&cstack, cstack.cs_idx - 1,
1073 CSF_WHILE | CSF_FOR, &cstack.cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074 }
1075 }
1076
1077 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001078 * For a ":while" or ":for" we need to remember the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001080 else if (cstack.cs_lflags & CSL_HAD_LOOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001082 cstack.cs_lflags &= ~CSL_HAD_LOOP;
Bram Moolenaard5053d02020-06-28 15:51:16 +02001083 cstack.cs_line[cstack.cs_idx] = current_line_before;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084 }
1085 }
1086
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001087 // Check for the next breakpoint after a watchexpression
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01001088 if (breakpoint != NULL && has_watchexpr())
1089 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001090 *breakpoint = dbg_find_breakpoint(FALSE, fname, SOURCING_LNUM);
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01001091 *dbg_tick = debug_tick;
1092 }
1093
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094 /*
1095 * When not inside any ":while" loop, clear remembered lines.
1096 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001097 if (cstack.cs_looplevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098 {
1099 if (lines_ga.ga_len > 0)
1100 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001101 SOURCING_LNUM =
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102 ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum;
1103 free_cmdlines(&lines_ga);
1104 }
1105 current_line = 0;
1106 }
1107
1108 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001109 * A ":finally" makes did_emsg, got_int, and did_throw pending for
1110 * being restored at the ":endtry". Reset them here and set the
1111 * ACTIVE and FINALLY flags, so that the finally clause gets executed.
1112 * This includes the case where a missing ":endif", ":endwhile" or
1113 * ":endfor" was detected by the ":finally" itself.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001115 if (cstack.cs_lflags & CSL_HAD_FINA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001116 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001117 cstack.cs_lflags &= ~CSL_HAD_FINA;
1118 report_make_pending(cstack.cs_pending[cstack.cs_idx]
1119 & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001120 did_throw ? (void *)current_exception : NULL);
1121 did_emsg = got_int = did_throw = FALSE;
1122 cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY;
1123 }
1124
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001125 // Update global "trylevel" for recursive calls to do_cmdline() from
1126 // within this loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001127 trylevel = initial_trylevel + cstack.cs_trylevel;
1128
1129 /*
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001130 * If the outermost try conditional (across function calls and sourced
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131 * files) is aborted because of an error, an interrupt, or an uncaught
1132 * exception, cancel everything. If it is left normally, reset
1133 * force_abort to get the non-EH compatible abortion behavior for
1134 * the rest of the script.
1135 */
1136 if (trylevel == 0 && !did_emsg && !got_int && !did_throw)
1137 force_abort = FALSE;
1138
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001139 // Convert an interrupt to an exception if appropriate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001140 (void)do_intthrow(&cstack);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001141#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142
1143 }
1144 /*
1145 * Continue executing command lines when:
1146 * - no CTRL-C typed, no aborting error, no exception thrown or try
1147 * conditionals need to be checked for executing finally clauses or
1148 * catching an interrupt exception
1149 * - didn't get an error message or lines are not typed
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001150 * - there is a command after '|', inside a :if, :while, :for or :try, or
Bram Moolenaar071d4272004-06-13 20:20:40 +00001151 * looping for ":source" command or function call.
1152 */
1153 while (!((got_int
1154#ifdef FEAT_EVAL
1155 || (did_emsg && force_abort) || did_throw
1156#endif
1157 )
1158#ifdef FEAT_EVAL
1159 && cstack.cs_trylevel == 0
1160#endif
1161 )
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001162 && !(did_emsg
1163#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001164 // Keep going when inside try/catch, so that the error can be
1165 // deal with, except when it is a syntax error, it may cause
1166 // the :endtry to be missed.
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001167 && (cstack.cs_trylevel == 0 || did_emsg_syntax)
1168#endif
1169 && used_getline
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001170 && (getline_equal(fgetline, cookie, getexmodeline)
1171 || getline_equal(fgetline, cookie, getexline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172 && (next_cmdline != NULL
1173#ifdef FEAT_EVAL
1174 || cstack.cs_idx >= 0
1175#endif
1176 || (flags & DOCMD_REPEAT)));
1177
1178 vim_free(cmdline_copy);
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001179 did_emsg_syntax = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180#ifdef FEAT_EVAL
1181 free_cmdlines(&lines_ga);
1182 ga_clear(&lines_ga);
1183
1184 if (cstack.cs_idx >= 0)
1185 {
1186 /*
1187 * If a sourced file or executed function ran to its end, report the
1188 * unclosed conditional.
1189 */
1190 if (!got_int && !did_throw
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001191 && ((getline_equal(fgetline, cookie, getsourceline)
1192 && !source_finished(fgetline, cookie))
1193 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194 && !func_has_ended(real_cookie))))
1195 {
1196 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001197 emsg(_(e_endtry));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001199 emsg(_(e_endwhile));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001200 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001201 emsg(_(e_endfor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001203 emsg(_(e_endif));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204 }
1205
1206 /*
1207 * Reset "trylevel" in case of a ":finish" or ":return" or a missing
1208 * ":endtry" in a sourced file or executed function. If the try
1209 * conditional is in its finally clause, ignore anything pending.
1210 * If it is in a catch clause, finish the caught exception.
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001211 * Also cleanup any "cs_forinfo" structures.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212 */
1213 do
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001214 {
1215 int idx = cleanup_conditionals(&cstack, 0, TRUE);
1216
Bram Moolenaar89e5d682005-01-17 22:06:23 +00001217 if (idx >= 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001218 --idx; // remove try block not in its finally clause
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001219 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
1220 &cstack.cs_looplevel);
1221 }
1222 while (cstack.cs_idx >= 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223 trylevel = initial_trylevel;
1224 }
1225
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001226 // If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
1227 // lack was reported above and the error message is to be converted to an
1228 // exception, do this now after rewinding the cstack.
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001229 do_errthrow(&cstack, getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001230 ? (char_u *)"endfunction" : (char_u *)NULL);
1231
1232 if (trylevel == 0)
1233 {
1234 /*
1235 * When an exception is being thrown out of the outermost try
1236 * conditional, discard the uncaught exception, disable the conversion
1237 * of interrupts or errors to exceptions, and ensure that no more
1238 * commands are executed.
1239 */
1240 if (did_throw)
1241 {
1242 void *p = NULL;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02001243 msglist_T *messages = NULL, *next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244
1245 /*
1246 * If the uncaught exception is a user exception, report it as an
1247 * error. If it is an error exception, display the saved error
1248 * message now. For an interrupt exception, do nothing; the
1249 * interrupt message is given elsewhere.
1250 */
1251 switch (current_exception->type)
1252 {
1253 case ET_USER:
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001254 vim_snprintf((char *)IObuff, IOSIZE,
1255 _("E605: Exception not caught: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256 current_exception->value);
1257 p = vim_strsave(IObuff);
1258 break;
1259 case ET_ERROR:
1260 messages = current_exception->messages;
1261 current_exception->messages = NULL;
1262 break;
1263 case ET_INTERRUPT:
1264 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265 }
1266
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001267 estack_push(ETYPE_EXCEPT, current_exception->throw_name,
1268 current_exception->throw_lnum);
Bram Moolenaare31ee862020-01-07 20:59:34 +01001269 ESTACK_CHECK_SETUP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270 current_exception->throw_name = NULL;
1271
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001272 discard_current_exception(); // uses IObuff if 'verbose'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273 suppress_errthrow = TRUE;
1274 force_abort = TRUE;
1275
1276 if (messages != NULL)
1277 {
1278 do
1279 {
1280 next = messages->next;
1281 emsg(messages->msg);
1282 vim_free(messages->msg);
Bram Moolenaar5fbf3bc2020-06-01 21:13:11 +02001283 vim_free(messages->sfile);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284 vim_free(messages);
1285 messages = next;
1286 }
1287 while (messages != NULL);
1288 }
1289 else if (p != NULL)
1290 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01001291 emsg(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292 vim_free(p);
1293 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001294 vim_free(SOURCING_NAME);
Bram Moolenaare31ee862020-01-07 20:59:34 +01001295 ESTACK_CHECK_NOW
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001296 estack_pop();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297 }
1298
1299 /*
1300 * On an interrupt or an aborting error not converted to an exception,
1301 * disable the conversion of errors to exceptions. (Interrupts are not
Bram Moolenaar2196bce2020-04-12 20:01:11 +02001302 * converted anymore, here.) This enables also the interrupt message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303 * when force_abort is set and did_emsg unset in case of an interrupt
1304 * from a finally clause after an error.
1305 */
1306 else if (got_int || (did_emsg && force_abort))
1307 suppress_errthrow = TRUE;
1308 }
1309
1310 /*
1311 * The current cstack will be freed when do_cmdline() returns. An uncaught
1312 * exception will have to be rethrown in the previous cstack. If a function
1313 * has just returned or a script file was just finished and the previous
1314 * cstack belongs to the same function or, respectively, script file, it
1315 * will have to be checked for finally clauses to be executed due to the
1316 * ":return" or ":finish". This is done in do_one_cmd().
1317 */
1318 if (did_throw)
1319 need_rethrow = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001320 if ((getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321 && ex_nesting_level > source_level(real_cookie))
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001322 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323 && ex_nesting_level > func_level(real_cookie) + 1))
1324 {
1325 if (!did_throw)
1326 check_cstack = TRUE;
1327 }
1328 else
1329 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001330 // When leaving a function, reduce nesting level.
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001331 if (getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332 --ex_nesting_level;
1333 /*
1334 * Go to debug mode when returning from a function in which we are
1335 * single-stepping.
1336 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001337 if ((getline_equal(fgetline, cookie, getsourceline)
1338 || getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339 && ex_nesting_level + 1 <= debug_break_level)
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001340 do_debug(getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341 ? (char_u *)_("End of sourced file")
1342 : (char_u *)_("End of function"));
1343 }
1344
1345 /*
1346 * Restore the exception environment (done after returning from the
1347 * debugger).
1348 */
1349 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001350 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351
1352 msg_list = saved_msg_list;
Bram Moolenaar292b90d2020-03-18 15:23:16 +01001353
1354 // Cleanup if "cs_emsg_silent_list" remains.
1355 if (cstack.cs_emsg_silent_list != NULL)
1356 {
1357 eslist_T *elem, *temp;
1358
1359 for (elem = cstack.cs_emsg_silent_list; elem != NULL; elem = temp)
1360 {
1361 temp = elem->next;
1362 vim_free(elem);
1363 }
1364 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001365#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366
1367 /*
1368 * If there was too much output to fit on the command line, ask the user to
1369 * hit return before redrawing the screen. With the ":global" command we do
1370 * this only once after the command is finished.
1371 */
1372 if (did_inc)
1373 {
1374 --RedrawingDisabled;
1375 --no_wait_return;
1376 msg_scroll = FALSE;
1377
1378 /*
1379 * When just finished an ":if"-":else" which was typed, no need to
1380 * wait for hit-return. Also for an error situation.
1381 */
1382 if (retval == FAIL
1383#ifdef FEAT_EVAL
1384 || (did_endif && KeyTyped && !did_emsg)
1385#endif
1386 )
1387 {
1388 need_wait_return = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001389 msg_didany = FALSE; // don't wait when restarting edit
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390 }
1391 else if (need_wait_return)
1392 {
1393 /*
1394 * The msg_start() above clears msg_didout. The wait_return we do
1395 * here should not overwrite the command that may be shown before
1396 * doing that.
1397 */
1398 msg_didout |= msg_didout_before_start;
1399 wait_return(FALSE);
1400 }
1401 }
1402
Bram Moolenaar2a942252012-11-28 23:03:07 +01001403#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001404 did_endif = FALSE; // in case do_cmdline used recursively
Bram Moolenaar2a942252012-11-28 23:03:07 +01001405#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406 /*
1407 * Reset if_level, in case a sourced script file contains more ":if" than
1408 * ":endif" (could be ":if x | foo | endif").
1409 */
1410 if_level = 0;
Bram Moolenaar2e18a122012-11-28 19:10:54 +01001411#endif
Bram Moolenaard4ad0d42012-11-28 17:34:48 +01001412
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413 --call_depth;
1414 return retval;
1415}
1416
1417#ifdef FEAT_EVAL
1418/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001419 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420 */
1421 static char_u *
Bram Moolenaare96a2492019-06-25 04:12:16 +02001422get_loop_line(int c, void *cookie, int indent, int do_concat)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001424 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425 wcmd_T *wp;
1426 char_u *line;
1427
1428 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1429 {
1430 if (cp->repeating)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001431 return NULL; // trying to read past ":endwhile"/":endfor"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001433 // First time inside the ":while"/":for": get line normally.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001434 if (cp->getline == NULL)
Bram Moolenaare96a2492019-06-25 04:12:16 +02001435 line = getcmdline(c, 0L, indent, do_concat);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001436 else
Bram Moolenaare96a2492019-06-25 04:12:16 +02001437 line = cp->getline(c, cp->cookie, indent, do_concat);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001438 if (line != NULL && store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001439 ++cp->current_line;
1440
1441 return line;
1442 }
1443
1444 KeyTyped = FALSE;
1445 ++cp->current_line;
1446 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001447 SOURCING_LNUM = wp->lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001448 return vim_strsave(wp->line);
1449}
1450
1451/*
1452 * Store a line in "gap" so that a ":while" loop can execute it again.
1453 */
1454 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001455store_loop_line(garray_T *gap, char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001456{
1457 if (ga_grow(gap, 1) == FAIL)
1458 return FAIL;
1459 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001460 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = SOURCING_LNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001461 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001462 return OK;
1463}
1464
1465/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001466 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001467 */
1468 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001469free_cmdlines(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470{
1471 while (gap->ga_len > 0)
1472 {
1473 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1474 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001475 }
1476}
1477#endif
1478
1479/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001480 * If "fgetline" is get_loop_line(), return TRUE if the getline it uses equals
1481 * "func". * Otherwise return TRUE when "fgetline" equals "func".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001484getline_equal(
Bram Moolenaare96a2492019-06-25 04:12:16 +02001485 char_u *(*fgetline)(int, void *, int, int),
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001486 void *cookie UNUSED, // argument for fgetline()
Bram Moolenaare96a2492019-06-25 04:12:16 +02001487 char_u *(*func)(int, void *, int, int))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488{
1489#ifdef FEAT_EVAL
Bram Moolenaare96a2492019-06-25 04:12:16 +02001490 char_u *(*gp)(int, void *, int, int);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001491 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001492
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001493 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1494 // function that's originally used to obtain the lines. This may be
1495 // nested several levels.
Bram Moolenaar89d40322006-08-29 15:30:07 +00001496 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001497 cp = (struct loop_cookie *)cookie;
1498 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001499 {
1500 gp = cp->getline;
1501 cp = cp->cookie;
1502 }
1503 return gp == func;
1504#else
Bram Moolenaar89d40322006-08-29 15:30:07 +00001505 return fgetline == func;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001506#endif
1507}
1508
Bram Moolenaar071d4272004-06-13 20:20:40 +00001509/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001510 * If "fgetline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511 * getline function. Otherwise return "cookie".
1512 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513 void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001514getline_cookie(
Bram Moolenaare96a2492019-06-25 04:12:16 +02001515 char_u *(*fgetline)(int, void *, int, int) UNUSED,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001516 void *cookie) // argument for fgetline()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517{
Bram Moolenaar13505972019-01-24 15:04:48 +01001518#ifdef FEAT_EVAL
Bram Moolenaare96a2492019-06-25 04:12:16 +02001519 char_u *(*gp)(int, void *, int, int);
Bram Moolenaard5053d02020-06-28 15:51:16 +02001520 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001521
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001522 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1523 // cookie that's originally used to obtain the lines. This may be nested
1524 // several levels.
Bram Moolenaar89d40322006-08-29 15:30:07 +00001525 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001526 cp = (struct loop_cookie *)cookie;
1527 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001528 {
1529 gp = cp->getline;
1530 cp = cp->cookie;
1531 }
1532 return cp;
Bram Moolenaar13505972019-01-24 15:04:48 +01001533#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001534 return cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001535#endif
Bram Moolenaar13505972019-01-24 15:04:48 +01001536}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537
Bram Moolenaard5053d02020-06-28 15:51:16 +02001538#if defined(FEAT_EVAL) || defined(PROT)
1539/*
1540 * Get the next line source line without advancing.
1541 */
1542 char_u *
1543getline_peek(
1544 char_u *(*fgetline)(int, void *, int, int) UNUSED,
1545 void *cookie) // argument for fgetline()
1546{
1547 char_u *(*gp)(int, void *, int, int);
1548 struct loop_cookie *cp;
1549 wcmd_T *wp;
1550
1551 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1552 // cookie that's originally used to obtain the lines. This may be nested
1553 // several levels.
1554 gp = fgetline;
1555 cp = (struct loop_cookie *)cookie;
1556 while (gp == get_loop_line)
1557 {
1558 if (cp->current_line + 1 < cp->lines_gap->ga_len)
1559 {
1560 // executing lines a second time, use the stored copy
1561 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line + 1;
1562 return wp->line;
1563 }
1564 gp = cp->getline;
1565 cp = cp->cookie;
1566 }
1567 if (gp == getsourceline)
1568 return source_nextline(cp);
1569 return NULL;
1570}
1571#endif
1572
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001573
1574/*
1575 * Helper function to apply an offset for buffer commands, i.e. ":bdelete",
1576 * ":bwipeout", etc.
1577 * Returns the buffer number.
1578 */
1579 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001580compute_buffer_local_count(int addr_type, int lnum, int offset)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001581{
1582 buf_T *buf;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001583 buf_T *nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001584 int count = offset;
1585
1586 buf = firstbuf;
1587 while (buf->b_next != NULL && buf->b_fnum < lnum)
1588 buf = buf->b_next;
1589 while (count != 0)
1590 {
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001591 count += (offset < 0) ? 1 : -1;
1592 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1593 if (nextbuf == NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001594 break;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001595 buf = nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001596 if (addr_type == ADDR_LOADED_BUFFERS)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001597 // skip over unloaded buffers
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001598 while (buf->b_ml.ml_mfp == NULL)
1599 {
1600 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1601 if (nextbuf == NULL)
1602 break;
1603 buf = nextbuf;
1604 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001605 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001606 // we might have gone too far, last buffer is not loadedd
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001607 if (addr_type == ADDR_LOADED_BUFFERS)
1608 while (buf->b_ml.ml_mfp == NULL)
1609 {
1610 nextbuf = (offset >= 0) ? buf->b_prev : buf->b_next;
1611 if (nextbuf == NULL)
1612 break;
1613 buf = nextbuf;
1614 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001615 return buf->b_fnum;
1616}
1617
Bram Moolenaarb7316892019-05-01 18:08:42 +02001618/*
1619 * Return the window number of "win".
1620 * When "win" is NULL return the number of windows.
1621 */
Bram Moolenaarf240e182014-11-27 18:33:02 +01001622 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001623current_win_nr(win_T *win)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001624{
1625 win_T *wp;
1626 int nr = 0;
1627
Bram Moolenaar29323592016-07-24 22:04:11 +02001628 FOR_ALL_WINDOWS(wp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001629 {
1630 ++nr;
1631 if (wp == win)
1632 break;
1633 }
1634 return nr;
1635}
1636
1637 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001638current_tab_nr(tabpage_T *tab)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001639{
1640 tabpage_T *tp;
1641 int nr = 0;
1642
Bram Moolenaar29323592016-07-24 22:04:11 +02001643 FOR_ALL_TABPAGES(tp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001644 {
1645 ++nr;
1646 if (tp == tab)
1647 break;
1648 }
1649 return nr;
1650}
1651
1652# define CURRENT_WIN_NR current_win_nr(curwin)
1653# define LAST_WIN_NR current_win_nr(NULL)
1654# define CURRENT_TAB_NR current_tab_nr(curtab)
1655# define LAST_TAB_NR current_tab_nr(NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001656
Bram Moolenaar071d4272004-06-13 20:20:40 +00001657/*
1658 * Execute one Ex command.
1659 *
1660 * If 'sourcing' is TRUE, the command will be included in the error message.
1661 *
1662 * 1. skip comment lines and leading space
1663 * 2. handle command modifiers
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001664 * 3. find the command
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001665 * 4. parse range
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001666 * 5. Parse the command.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001667 * 6. parse arguments
1668 * 7. switch on command name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669 *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001670 * Note: "fgetline" can be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001671 *
1672 * This function may be called recursively!
1673 */
1674#if (_MSC_VER == 1200)
1675/*
Bram Moolenaared203462004-06-16 11:19:22 +00001676 * Avoid optimisation bug in VC++ version 6.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001677 */
Bram Moolenaar281bdce2005-01-25 21:53:18 +00001678 #pragma optimize( "g", off )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001679#endif
1680 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001681do_one_cmd(
Bram Moolenaarddef1292019-12-16 17:10:33 +01001682 char_u **cmdlinep,
1683 int sourcing,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684#ifdef FEAT_EVAL
Bram Moolenaarddef1292019-12-16 17:10:33 +01001685 cstack_T *cstack,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001686#endif
Bram Moolenaarddef1292019-12-16 17:10:33 +01001687 char_u *(*fgetline)(int, void *, int, int),
1688 void *cookie) // argument for fgetline()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689{
Bram Moolenaarddef1292019-12-16 17:10:33 +01001690 char_u *p;
1691 linenr_T lnum;
1692 long n;
1693 char *errormsg = NULL; // error message
1694 char_u *after_modifier = NULL;
1695 exarg_T ea; // Ex command arguments
1696 int save_msg_scroll = msg_scroll;
1697 cmdmod_T save_cmdmod;
1698 int save_reg_executing = reg_executing;
1699 int ni; // set when Not Implemented
1700 char_u *cmd;
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001701#ifdef FEAT_EVAL
1702 int starts_with_colon;
1703#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001704
Bram Moolenaara80faa82020-04-12 19:37:17 +02001705 CLEAR_FIELD(ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001706 ea.line1 = 1;
1707 ea.line2 = 1;
1708#ifdef FEAT_EVAL
1709 ++ex_nesting_level;
1710#endif
1711
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001712 // When the last file has not been edited :q has to be typed twice.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713 if (quitmore
1714#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001715 // avoid that a function call in 'statusline' does this
Bram Moolenaar89d40322006-08-29 15:30:07 +00001716 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001717#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001718 // avoid that an autocommand, e.g. QuitPre, does this
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001719 && !getline_equal(fgetline, cookie, getnextac))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001720 --quitmore;
1721
1722 /*
1723 * Reset browse, confirm, etc.. They are restored when returning, for
1724 * recursive calls.
1725 */
1726 save_cmdmod = cmdmod;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001728 // "#!anything" is handled like a comment.
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001729 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1730 goto doend;
1731
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02001732/*
1733 * 1. Skip comment lines and leading white space and colons.
1734 * 2. Handle command modifiers.
1735 */
1736 // The "ea" structure holds the arguments that can be used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001737 ea.cmd = *cmdlinep;
Bram Moolenaareffed932018-08-14 13:38:17 +02001738 ea.cmdlinep = cmdlinep;
1739 ea.getline = fgetline;
1740 ea.cookie = cookie;
1741#ifdef FEAT_EVAL
1742 ea.cstack = cstack;
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001743 starts_with_colon = *skipwhite(ea.cmd) == ':';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001744#endif
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02001745 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaareffed932018-08-14 13:38:17 +02001746 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001747
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001748 after_modifier = ea.cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749
1750#ifdef FEAT_EVAL
1751 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
1752 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
1753#else
1754 ea.skip = (if_level > 0);
1755#endif
1756
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001758 * 3. Skip over the range to find the command. Let "p" point to after it.
1759 *
1760 * We need the command to know what kind of range it uses.
1761 */
1762 cmd = ea.cmd;
1763 ea.cmd = skip_range(ea.cmd, NULL);
1764 if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
1765 ea.cmd = skipwhite(ea.cmd + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001766
1767#ifdef FEAT_EVAL
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001768 if (current_sctx.sc_version == SCRIPT_VERSION_VIM9 && !starts_with_colon)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02001769 {
1770 if (ea.cmd > cmd)
1771 {
1772 emsg(_(e_colon_required));
1773 goto doend;
1774 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001775 p = find_ex_command(&ea, NULL, lookup_scriptvar, NULL);
Bram Moolenaardf069ee2020-06-22 23:02:51 +02001776 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001777 else
1778#endif
1779 p = find_ex_command(&ea, NULL, NULL, NULL);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001780
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001781#ifdef FEAT_EVAL
1782# ifdef FEAT_PROFILE
1783 // Count this line for profiling if skip is TRUE.
1784 if (do_profiling == PROF_YES
1785 && (!ea.skip || cstack->cs_idx == 0 || (cstack->cs_idx > 0
1786 && (cstack->cs_flags[cstack->cs_idx - 1] & CSF_ACTIVE))))
1787 {
1788 int skip = did_emsg || got_int || did_throw;
1789
1790 if (ea.cmdidx == CMD_catch)
1791 skip = !skip && !(cstack->cs_idx >= 0
1792 && (cstack->cs_flags[cstack->cs_idx] & CSF_THROWN)
1793 && !(cstack->cs_flags[cstack->cs_idx] & CSF_CAUGHT));
1794 else if (ea.cmdidx == CMD_else || ea.cmdidx == CMD_elseif)
1795 skip = skip || !(cstack->cs_idx >= 0
1796 && !(cstack->cs_flags[cstack->cs_idx]
1797 & (CSF_ACTIVE | CSF_TRUE)));
1798 else if (ea.cmdidx == CMD_finally)
1799 skip = FALSE;
1800 else if (ea.cmdidx != CMD_endif
1801 && ea.cmdidx != CMD_endfor
1802 && ea.cmdidx != CMD_endtry
1803 && ea.cmdidx != CMD_endwhile)
1804 skip = ea.skip;
1805
1806 if (!skip)
1807 {
1808 if (getline_equal(fgetline, cookie, get_func_line))
1809 func_line_exec(getline_cookie(fgetline, cookie));
1810 else if (getline_equal(fgetline, cookie, getsourceline))
1811 script_line_exec();
1812 }
1813 }
1814# endif
1815
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001816 // May go to debug mode. If this happens and the ">quit" debug command is
1817 // used, throw an interrupt exception and skip the next command.
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001818 dbg_check_breakpoint(&ea);
1819 if (!ea.skip && got_int)
1820 {
1821 ea.skip = TRUE;
1822 (void)do_intthrow(cstack);
1823 }
1824#endif
1825
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001826/*
1827 * 4. parse a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828 *
1829 * where 'addr' is:
1830 *
1831 * % (entire file)
1832 * $ [+-NUM]
1833 * 'x [+-NUM] (where x denotes a currently defined mark)
1834 * . [+-NUM]
1835 * [+-NUM]..
1836 * NUM
1837 *
1838 * The ea.cmd pointer is updated to point to the first character following the
1839 * range spec. If an initial address is found, but no second, the upper bound
1840 * is equal to the lower.
1841 */
1842
Bram Moolenaar25190db2019-05-04 15:05:28 +02001843 // ea.addr_type for user commands is set by find_ucmd
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001844 if (!IS_USER_CMDIDX(ea.cmdidx))
1845 {
1846 if (ea.cmdidx != CMD_SIZE)
1847 ea.addr_type = cmdnames[(int)ea.cmdidx].cmd_addr_type;
1848 else
1849 ea.addr_type = ADDR_LINES;
1850
Bram Moolenaar25190db2019-05-04 15:05:28 +02001851 // :wincmd range depends on the argument.
Bram Moolenaar164f3262015-01-14 21:22:01 +01001852 if (ea.cmdidx == CMD_wincmd && p != NULL)
1853 get_wincmd_addr_type(skipwhite(p), &ea);
Bram Moolenaar25190db2019-05-04 15:05:28 +02001854#ifdef FEAT_QUICKFIX
1855 // :.cc in quickfix window uses line number
1856 if ((ea.cmdidx == CMD_cc || ea.cmdidx == CMD_ll) && bt_quickfix(curbuf))
1857 ea.addr_type = ADDR_OTHER;
1858#endif
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001859 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001860
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001861 ea.cmd = cmd;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02001862 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaaree8415b2018-08-10 23:13:12 +02001863 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001864
Bram Moolenaar071d4272004-06-13 20:20:40 +00001865/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001866 * 5. Parse the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867 */
1868
1869 /*
1870 * Skip ':' and any white space
1871 */
1872 ea.cmd = skipwhite(ea.cmd);
1873 while (*ea.cmd == ':')
1874 ea.cmd = skipwhite(ea.cmd + 1);
1875
1876 /*
1877 * If we got a line, but no command, then go to the line.
1878 * If we find a '|' or '\n' we set ea.nextcmd.
1879 */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001880 if (*ea.cmd == NUL || *ea.cmd == '"'
Bram Moolenaar7a092242020-04-16 22:10:49 +02001881#ifdef FEAT_EVAL
Bram Moolenaara26b9702020-04-18 19:53:28 +02001882 || (*ea.cmd == '#' && ea.cmd[1] != '{'
1883 && !starts_with_colon && in_vim9script())
Bram Moolenaar7a092242020-04-16 22:10:49 +02001884#endif
1885 || (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886 {
1887 /*
1888 * strange vi behaviour:
1889 * ":3" jumps to line 3
1890 * ":3|..." prints line 3
1891 * ":|" prints current line
1892 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001893 if (ea.skip) // skip this if inside :if
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001895 if (*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896 {
1897 ea.cmdidx = CMD_print;
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001898 ea.argt = EX_RANGE+EX_COUNT+EX_TRLBAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001899 if ((errormsg = invalid_range(&ea)) == NULL)
1900 {
1901 correct_range(&ea);
1902 ex_print(&ea);
1903 }
1904 }
1905 else if (ea.addr_count != 0)
1906 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001907 if (ea.line2 > curbuf->b_ml.ml_line_count)
1908 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001909 // With '-' in 'cpoptions' a line number past the file is an
1910 // error, otherwise put it at the end of the file.
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001911 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
1912 ea.line2 = -1;
1913 else
1914 ea.line2 = curbuf->b_ml.ml_line_count;
1915 }
1916
1917 if (ea.line2 < 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001918 errormsg = _(e_invrange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001919 else
1920 {
1921 if (ea.line2 == 0)
1922 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923 else
1924 curwin->w_cursor.lnum = ea.line2;
1925 beginline(BL_SOL | BL_FIX);
1926 }
1927 }
1928 goto doend;
1929 }
1930
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001931 // If this looks like an undefined user command and there are CmdUndefined
1932 // autocommands defined, trigger the matching autocommands.
Bram Moolenaard5005162014-08-22 23:05:54 +02001933 if (p != NULL && ea.cmdidx == CMD_SIZE && !ea.skip
1934 && ASCII_ISUPPER(*ea.cmd)
1935 && has_cmdundefined())
1936 {
Bram Moolenaard5005162014-08-22 23:05:54 +02001937 int ret;
1938
Bram Moolenaar5a31b462014-08-23 14:16:20 +02001939 p = ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02001940 while (ASCII_ISALNUM(*p))
1941 ++p;
Bram Moolenaar158ea172020-06-18 17:28:39 +02001942 p = vim_strnsave(ea.cmd, p - ea.cmd);
Bram Moolenaard5005162014-08-22 23:05:54 +02001943 ret = apply_autocmds(EVENT_CMDUNDEFINED, p, p, TRUE, NULL);
1944 vim_free(p);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001945 // If the autocommands did something and didn't cause an error, try
1946 // finding the command again.
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001947 p = (ret
1948#ifdef FEAT_EVAL
1949 && !aborting()
Bram Moolenaard5005162014-08-22 23:05:54 +02001950#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001951 ) ? find_ex_command(&ea, NULL, NULL, NULL) : ea.cmd;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001952 }
Bram Moolenaard5005162014-08-22 23:05:54 +02001953
Bram Moolenaar071d4272004-06-13 20:20:40 +00001954 if (p == NULL)
1955 {
1956 if (!ea.skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001957 errormsg = _("E464: Ambiguous use of user-defined command");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001958 goto doend;
1959 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001960 // Check for wrong commands.
Bram Moolenaar6f9a4762017-06-22 20:39:17 +02001961 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78
1962 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001963 {
1964 errormsg = uc_fun_cmd();
1965 goto doend;
1966 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001967
Bram Moolenaar071d4272004-06-13 20:20:40 +00001968 if (ea.cmdidx == CMD_SIZE)
1969 {
1970 if (!ea.skip)
1971 {
1972 STRCPY(IObuff, _("E492: Not an editor command"));
1973 if (!sourcing)
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001974 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001975 // If the modifier was parsed OK the error must be in the
1976 // following command
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001977 if (after_modifier != NULL)
1978 append_command(after_modifier);
1979 else
1980 append_command(*cmdlinep);
1981 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001982 errormsg = (char *)IObuff;
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001983 did_emsg_syntax = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001984 }
1985 goto doend;
1986 }
1987
Bram Moolenaar958636c2014-10-21 20:01:58 +02001988 ni = (!IS_USER_CMDIDX(ea.cmdidx)
1989 && (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00001990#ifdef HAVE_EX_SCRIPT_NI
1991 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
1992#endif
1993 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001994
1995#ifndef FEAT_EVAL
1996 /*
1997 * When the expression evaluation is disabled, recognize the ":if" and
1998 * ":endif" commands and ignore everything in between it.
1999 */
2000 if (ea.cmdidx == CMD_if)
2001 ++if_level;
2002 if (if_level)
2003 {
2004 if (ea.cmdidx == CMD_endif)
2005 --if_level;
2006 goto doend;
2007 }
2008
2009#endif
2010
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002011 // forced commands
Bram Moolenaar196b3b02008-06-20 16:51:41 +00002012 if (*p == '!' && ea.cmdidx != CMD_substitute
2013 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002014 {
2015 ++p;
2016 ea.forceit = TRUE;
2017 }
2018 else
2019 ea.forceit = FALSE;
2020
2021/*
Bram Moolenaarb7316892019-05-01 18:08:42 +02002022 * 6. Parse arguments. Then check for errors.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002023 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02002024 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002025 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002026
2027 if (!ea.skip)
2028 {
2029#ifdef HAVE_SANDBOX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002030 if (sandbox != 0 && !(ea.argt & EX_SBOXOK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002031 {
Bram Moolenaar8c62a082019-02-08 14:34:10 +01002032 // Command not allowed in sandbox.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002033 errormsg = _(e_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002034 goto doend;
2035 }
2036#endif
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002037 if (restricted != 0 && (ea.argt & EX_RESTRICT))
Bram Moolenaar8c62a082019-02-08 14:34:10 +01002038 {
2039 errormsg = _("E981: Command not allowed in rvim");
2040 goto doend;
2041 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002042 if (!curbuf->b_p_ma && (ea.argt & EX_MODIFY))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002043 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002044 // Command not allowed in non-'modifiable' buffer
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002045 errormsg = _(e_modifiable);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046 goto doend;
2047 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002048
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002049 if (text_locked() && !(ea.argt & EX_CMDWIN)
Bram Moolenaar958636c2014-10-21 20:01:58 +02002050 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002051 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002052 // Command not allowed when editing the command line.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002053 errormsg = _(get_text_locked_msg());
Bram Moolenaar071d4272004-06-13 20:20:40 +00002054 goto doend;
2055 }
Bram Moolenaar379fb762018-08-30 15:58:28 +02002056
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002057 // Disallow editing another buffer when "curbuf_lock" is set.
2058 // Do allow ":checktime" (it is postponed).
2059 // Do allow ":edit" (check for an argument later).
2060 // Do allow ":file" with no arguments (check for an argument later).
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002061 if (!(ea.argt & EX_CMDWIN)
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002062 && ea.cmdidx != CMD_checktime
Bram Moolenaar379fb762018-08-30 15:58:28 +02002063 && ea.cmdidx != CMD_edit
2064 && ea.cmdidx != CMD_file
Bram Moolenaar958636c2014-10-21 20:01:58 +02002065 && !IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002066 && curbuf_locked())
2067 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002068
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002069 if (!ni && !(ea.argt & EX_RANGE) && ea.addr_count > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002070 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002071 // no range allowed
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002072 errormsg = _(e_norange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002073 goto doend;
2074 }
2075 }
2076
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002077 if (!ni && !(ea.argt & EX_BANG) && ea.forceit) // no <!> allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002078 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002079 errormsg = _(e_nobang);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002080 goto doend;
2081 }
2082
2083 /*
2084 * Don't complain about the range if it is not used
2085 * (could happen if line_count is accidentally set to 0).
2086 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002087 if (!ea.skip && !ni && (ea.argt & EX_RANGE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002088 {
2089 /*
2090 * If the range is backwards, ask for confirmation and, if given, swap
2091 * ea.line1 & ea.line2 so it's forwards again.
2092 * When global command is busy, don't ask, will fail below.
2093 */
2094 if (!global_busy && ea.line1 > ea.line2)
2095 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002096 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002097 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002098 if (sourcing || exmode_active)
2099 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002100 errormsg = _("E493: Backwards range given");
Bram Moolenaara5792f52005-11-23 21:25:05 +00002101 goto doend;
2102 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002103 if (ask_yesno((char_u *)
2104 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002105 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002106 }
2107 lnum = ea.line1;
2108 ea.line1 = ea.line2;
2109 ea.line2 = lnum;
2110 }
2111 if ((errormsg = invalid_range(&ea)) != NULL)
2112 goto doend;
2113 }
2114
Bram Moolenaarb7316892019-05-01 18:08:42 +02002115 if ((ea.addr_type == ADDR_OTHER) && ea.addr_count == 0)
2116 // default is 1, not cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002117 ea.line2 = 1;
2118
2119 correct_range(&ea);
2120
2121#ifdef FEAT_FOLDING
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002122 if (((ea.argt & EX_WHOLEFOLD) || ea.addr_count >= 2) && !global_busy
Bram Moolenaara3306952016-01-02 21:41:06 +01002123 && ea.addr_type == ADDR_LINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002125 // Put the first line at the start of a closed fold, put the last line
2126 // at the end of a closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002127 (void)hasFolding(ea.line1, &ea.line1, NULL);
2128 (void)hasFolding(ea.line2, NULL, &ea.line2);
2129 }
2130#endif
2131
2132#ifdef FEAT_QUICKFIX
2133 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002134 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135 * option here, so things like % get expanded.
2136 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002137 p = replace_makeprg(&ea, p, cmdlinep);
2138 if (p == NULL)
2139 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002140#endif
2141
2142 /*
2143 * Skip to start of argument.
2144 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2145 */
2146 if (ea.cmdidx == CMD_bang)
2147 ea.arg = p;
2148 else
2149 ea.arg = skipwhite(p);
2150
Bram Moolenaar379fb762018-08-30 15:58:28 +02002151 // ":file" cannot be run with an argument when "curbuf_lock" is set
2152 if (ea.cmdidx == CMD_file && *ea.arg != NUL && curbuf_locked())
2153 goto doend;
2154
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155 /*
2156 * Check for "++opt=val" argument.
2157 * Must be first, allow ":w ++enc=utf8 !cmd"
2158 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002159 if (ea.argt & EX_ARGOPT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2161 if (getargopt(&ea) == FAIL && !ni)
2162 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002163 errormsg = _(e_invarg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164 goto doend;
2165 }
2166
2167 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2168 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002169 if (*ea.arg == '>') // append
Bram Moolenaar071d4272004-06-13 20:20:40 +00002170 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002171 if (*++ea.arg != '>') // typed wrong
Bram Moolenaar071d4272004-06-13 20:20:40 +00002172 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002173 errormsg = _("E494: Use w or w>>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002174 goto doend;
2175 }
2176 ea.arg = skipwhite(ea.arg + 1);
2177 ea.append = TRUE;
2178 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002179 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) // :w !filter
Bram Moolenaar071d4272004-06-13 20:20:40 +00002180 {
2181 ++ea.arg;
2182 ea.usefilter = TRUE;
2183 }
2184 }
2185
2186 if (ea.cmdidx == CMD_read)
2187 {
2188 if (ea.forceit)
2189 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002190 ea.usefilter = TRUE; // :r! filter if ea.forceit
Bram Moolenaar071d4272004-06-13 20:20:40 +00002191 ea.forceit = FALSE;
2192 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002193 else if (*ea.arg == '!') // :r !filter
Bram Moolenaar071d4272004-06-13 20:20:40 +00002194 {
2195 ++ea.arg;
2196 ea.usefilter = TRUE;
2197 }
2198 }
2199
2200 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2201 {
2202 ea.amount = 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002203 while (*ea.arg == *ea.cmd) // count number of '>' or '<'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002204 {
2205 ++ea.arg;
2206 ++ea.amount;
2207 }
2208 ea.arg = skipwhite(ea.arg);
2209 }
2210
2211 /*
2212 * Check for "+command" argument, before checking for next command.
2213 * Don't do this for ":read !cmd" and ":write !cmd".
2214 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002215 if ((ea.argt & EX_CMDARG) && !ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002216 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2217
2218 /*
2219 * Check for '|' to separate commands and '"' to start comments.
2220 * Don't do this for ":read !cmd" and ":write !cmd".
2221 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002222 if ((ea.argt & EX_TRLBAR) && !ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223 separate_nextcmd(&ea);
2224
2225 /*
2226 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002227 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2228 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002229 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002230 else if (ea.cmdidx == CMD_bang
Bram Moolenaar67883b42017-07-27 22:57:00 +02002231 || ea.cmdidx == CMD_terminal
Bram Moolenaardf177f62005-02-22 08:39:57 +00002232 || ea.cmdidx == CMD_global
2233 || ea.cmdidx == CMD_vglobal
2234 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002235 {
2236 for (p = ea.arg; *p; ++p)
2237 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002238 // Remove one backslash before a newline, so that it's possible to
2239 // pass a newline to the shell and also a newline that is preceded
2240 // with a backslash. This makes it impossible to end a shell
2241 // command in a backslash, but that doesn't appear useful.
2242 // Halving the number of backslashes is incompatible with previous
2243 // versions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002244 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002245 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002246 else if (*p == '\n')
2247 {
2248 ea.nextcmd = p + 1;
2249 *p = NUL;
2250 break;
2251 }
2252 }
2253 }
2254
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002255 if ((ea.argt & EX_DFLALL) && ea.addr_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002256 {
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002257 buf_T *buf;
2258
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259 ea.line1 = 1;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002260 switch (ea.addr_type)
2261 {
2262 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02002263 case ADDR_OTHER:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002264 ea.line2 = curbuf->b_ml.ml_line_count;
2265 break;
2266 case ADDR_LOADED_BUFFERS:
2267 buf = firstbuf;
2268 while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL)
2269 buf = buf->b_next;
2270 ea.line1 = buf->b_fnum;
2271 buf = lastbuf;
2272 while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL)
2273 buf = buf->b_prev;
2274 ea.line2 = buf->b_fnum;
2275 break;
2276 case ADDR_BUFFERS:
2277 ea.line1 = firstbuf->b_fnum;
2278 ea.line2 = lastbuf->b_fnum;
2279 break;
2280 case ADDR_WINDOWS:
2281 ea.line2 = LAST_WIN_NR;
2282 break;
2283 case ADDR_TABS:
2284 ea.line2 = LAST_TAB_NR;
2285 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01002286 case ADDR_TABS_RELATIVE:
2287 ea.line2 = 1;
2288 break;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002289 case ADDR_ARGUMENTS:
2290 if (ARGCOUNT == 0)
2291 ea.line1 = ea.line2 = 0;
2292 else
2293 ea.line2 = ARGCOUNT;
2294 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02002295 case ADDR_QUICKFIX_VALID:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002296#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02002297 ea.line2 = qf_get_valid_size(&ea);
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002298 if (ea.line2 == 0)
2299 ea.line2 = 1;
Bram Moolenaare906c502015-09-09 21:10:39 +02002300#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002301 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02002302 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02002303 case ADDR_UNSIGNED:
2304 case ADDR_QUICKFIX:
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002305 iemsg(_("INTERNAL: Cannot use EX_DFLALL with ADDR_NONE, ADDR_UNSIGNED or ADDR_QUICKFIX"));
Bram Moolenaarb7316892019-05-01 18:08:42 +02002306 break;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002307 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308 }
2309
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002310 // accept numbered register only when no count allowed (:put)
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002311 if ( (ea.argt & EX_REGSTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312 && *ea.arg != NUL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002313 // Do not allow register = for user commands
Bram Moolenaar958636c2014-10-21 20:01:58 +02002314 && (!IS_USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002315 && !((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002316 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002317#ifndef FEAT_CLIPBOARD
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002318 // check these explicitly for a more specific error message
Bram Moolenaar85de2062011-05-05 14:26:41 +02002319 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002320 {
Bram Moolenaar99b12722019-01-14 20:16:40 +01002321 errormsg = _(e_invalidreg);
Bram Moolenaar85de2062011-05-05 14:26:41 +02002322 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323 }
2324#endif
Bram Moolenaar958636c2014-10-21 20:01:58 +02002325 if (valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2326 && !IS_USER_CMDIDX(ea.cmdidx))))
Bram Moolenaar85de2062011-05-05 14:26:41 +02002327 {
2328 ea.regname = *ea.arg++;
2329#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002330 // for '=' register: accept the rest of the line as an expression
Bram Moolenaar85de2062011-05-05 14:26:41 +02002331 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2332 {
2333 set_expr_line(vim_strsave(ea.arg));
2334 ea.arg += STRLEN(ea.arg);
2335 }
2336#endif
2337 ea.arg = skipwhite(ea.arg);
2338 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002339 }
2340
2341 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002342 * Check for a count. When accepting a EX_BUFNAME, don't use "123foo" as a
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343 * count, it's a buffer name.
2344 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002345 if ((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)
2346 && (!(ea.argt & EX_BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01002347 || VIM_ISWHITE(*p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348 {
2349 n = getdigits(&ea.arg);
2350 ea.arg = skipwhite(ea.arg);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002351 if (n <= 0 && !ni && (ea.argt & EX_ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002353 errormsg = _(e_zerocount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002354 goto doend;
2355 }
Bram Moolenaarb7316892019-05-01 18:08:42 +02002356 if (ea.addr_type != ADDR_LINES) // e.g. :buffer 2, :sleep 3
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357 {
2358 ea.line2 = n;
2359 if (ea.addr_count == 0)
2360 ea.addr_count = 1;
2361 }
2362 else
2363 {
2364 ea.line1 = ea.line2;
2365 ea.line2 += n - 1;
2366 ++ea.addr_count;
2367 /*
2368 * Be vi compatible: no error message for out of range.
2369 */
Bram Moolenaarb7316892019-05-01 18:08:42 +02002370 if (ea.line2 > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371 ea.line2 = curbuf->b_ml.ml_line_count;
2372 }
2373 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002374
2375 /*
2376 * Check for flags: 'l', 'p' and '#'.
2377 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002378 if (ea.argt & EX_FLAGS)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002379 get_flags(&ea);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002380 if (!ni && !(ea.argt & EX_EXTRA) && *ea.arg != NUL
2381 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & EX_TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002382 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002383 // no arguments allowed but there is something
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002384 errormsg = _(e_trailing);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385 goto doend;
2386 }
2387
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002388 if (!ni && (ea.argt & EX_NEEDARG) && *ea.arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002390 errormsg = _(e_argreq);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391 goto doend;
2392 }
2393
2394#ifdef FEAT_EVAL
2395 /*
2396 * Skip the command when it's not going to be executed.
2397 * The commands like :if, :endif, etc. always need to be executed.
2398 * Also make an exception for commands that handle a trailing command
2399 * themselves.
2400 */
2401 if (ea.skip)
2402 {
2403 switch (ea.cmdidx)
2404 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002405 // commands that need evaluation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406 case CMD_while:
2407 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002408 case CMD_for:
2409 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410 case CMD_if:
2411 case CMD_elseif:
2412 case CMD_else:
2413 case CMD_endif:
2414 case CMD_try:
2415 case CMD_catch:
2416 case CMD_finally:
2417 case CMD_endtry:
2418 case CMD_function:
Bram Moolenaarab55c682020-03-01 19:41:43 +01002419 case CMD_def:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420 break;
2421
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002422 // Commands that handle '|' themselves. Check: A command should
2423 // either have the EX_TRLBAR flag, appear in this list or appear in
2424 // the list at ":help :bar".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425 case CMD_aboveleft:
2426 case CMD_and:
2427 case CMD_belowright:
2428 case CMD_botright:
2429 case CMD_browse:
2430 case CMD_call:
2431 case CMD_confirm:
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01002432 case CMD_const:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433 case CMD_delfunction:
2434 case CMD_djump:
2435 case CMD_dlist:
2436 case CMD_dsearch:
2437 case CMD_dsplit:
2438 case CMD_echo:
2439 case CMD_echoerr:
2440 case CMD_echomsg:
2441 case CMD_echon:
Bram Moolenaara76b3152020-02-16 16:20:21 +01002442 case CMD_eval:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443 case CMD_execute:
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002444 case CMD_filter:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002445 case CMD_help:
2446 case CMD_hide:
2447 case CMD_ijump:
2448 case CMD_ilist:
2449 case CMD_isearch:
2450 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002451 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002452 case CMD_keepjumps:
2453 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01002454 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002455 case CMD_leftabove:
2456 case CMD_let:
2457 case CMD_lockmarks:
Bram Moolenaarcc4423a2019-11-26 17:05:00 +01002458 case CMD_lockvar:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002459 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002460 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002461 case CMD_mzscheme:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01002462 case CMD_noautocmd:
2463 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002464 case CMD_perl:
2465 case CMD_psearch:
2466 case CMD_python:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002467 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002468 case CMD_python3:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469 case CMD_return:
2470 case CMD_rightbelow:
2471 case CMD_ruby:
2472 case CMD_silent:
2473 case CMD_smagic:
2474 case CMD_snomagic:
2475 case CMD_substitute:
2476 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002477 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002478 case CMD_tcl:
2479 case CMD_throw:
2480 case CMD_tilde:
2481 case CMD_topleft:
2482 case CMD_unlet:
Bram Moolenaarcc4423a2019-11-26 17:05:00 +01002483 case CMD_unlockvar:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484 case CMD_verbose:
2485 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002486 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487 break;
2488
2489 default: goto doend;
2490 }
2491 }
2492#endif
2493
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002494 if (ea.argt & EX_XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495 {
2496 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2497 goto doend;
2498 }
2499
Bram Moolenaar071d4272004-06-13 20:20:40 +00002500 /*
2501 * Accept buffer name. Cannot be used at the same time with a buffer
2502 * number. Don't do this for a user command.
2503 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002504 if ((ea.argt & EX_BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
Bram Moolenaar958636c2014-10-21 20:01:58 +02002505 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002506 {
2507 /*
2508 * :bdelete, :bwipeout and :bunload take several arguments, separated
2509 * by spaces: find next space (skipping over escaped characters).
2510 * The others take one argument: ignore trailing spaces.
2511 */
2512 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2513 || ea.cmdidx == CMD_bunload)
2514 p = skiptowhite_esc(ea.arg);
2515 else
2516 {
2517 p = ea.arg + STRLEN(ea.arg);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002518 while (p > ea.arg && VIM_ISWHITE(p[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002519 --p;
2520 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002521 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & EX_BUFUNL) != 0,
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002522 FALSE, FALSE);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002523 if (ea.line2 < 0) // failed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524 goto doend;
2525 ea.addr_count = 1;
2526 ea.arg = skipwhite(p);
2527 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002528
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002529 // The :try command saves the emsg_silent flag, reset it here when
2530 // ":silent! try" was used, it should only apply to :try itself.
Bram Moolenaareffed932018-08-14 13:38:17 +02002531 if (ea.cmdidx == CMD_try && ea.did_esilent > 0)
Bram Moolenaar2be57332018-02-13 18:05:18 +01002532 {
Bram Moolenaareffed932018-08-14 13:38:17 +02002533 emsg_silent -= ea.did_esilent;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002534 if (emsg_silent < 0)
2535 emsg_silent = 0;
Bram Moolenaareffed932018-08-14 13:38:17 +02002536 ea.did_esilent = 0;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002537 }
2538
Bram Moolenaar071d4272004-06-13 20:20:40 +00002539/*
Bram Moolenaar2be57332018-02-13 18:05:18 +01002540 * 7. Execute the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002541 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542
Bram Moolenaar958636c2014-10-21 20:01:58 +02002543 if (IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544 {
2545 /*
2546 * Execute a user-defined command.
2547 */
2548 do_ucmd(&ea);
2549 }
2550 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002551 {
2552 /*
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002553 * Call the function to execute the builtin command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554 */
2555 ea.errmsg = NULL;
2556 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2557 if (ea.errmsg != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002558 errormsg = _(ea.errmsg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002559 }
2560
2561#ifdef FEAT_EVAL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002562 // Set flag that any command was executed, used by ex_vim9script().
Bram Moolenaar16531552020-02-08 16:00:46 +01002563 if (getline_equal(ea.getline, ea.cookie, getsourceline)
2564 && current_sctx.sc_sid > 0)
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002565 SCRIPT_ITEM(current_sctx.sc_sid)->sn_had_command = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002566
Bram Moolenaar071d4272004-06-13 20:20:40 +00002567 /*
2568 * If the command just executed called do_cmdline(), any throw or ":return"
2569 * or ":finish" encountered there must also check the cstack of the still
2570 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2571 * exception, or reanimate a returned function or finished script file and
2572 * return or finish it again.
2573 */
2574 if (need_rethrow)
2575 do_throw(cstack);
2576 else if (check_cstack)
2577 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002578 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002579 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002580 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002581 && current_func_returned())
2582 do_return(&ea, TRUE, FALSE, NULL);
2583 }
2584 need_rethrow = check_cstack = FALSE;
2585#endif
2586
2587doend:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002588 if (curwin->w_cursor.lnum == 0) // can happen with zero line number
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002589 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002590 curwin->w_cursor.lnum = 1;
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002591 curwin->w_cursor.col = 0;
2592 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593
2594 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2595 {
2596 if (sourcing)
2597 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002598 if (errormsg != (char *)IObuff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599 {
2600 STRCPY(IObuff, errormsg);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002601 errormsg = (char *)IObuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002603 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604 }
2605 emsg(errormsg);
2606 }
2607#ifdef FEAT_EVAL
2608 do_errthrow(cstack,
Bram Moolenaar958636c2014-10-21 20:01:58 +02002609 (ea.cmdidx != CMD_SIZE && !IS_USER_CMDIDX(ea.cmdidx))
2610 ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002611#endif
2612
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002613 undo_cmdmod(&ea, save_msg_scroll);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002614 cmdmod = save_cmdmod;
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01002615 reg_executing = save_reg_executing;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002616
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002617#ifdef HAVE_SANDBOX
Bram Moolenaareffed932018-08-14 13:38:17 +02002618 if (ea.did_sandbox)
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002619 --sandbox;
2620#endif
2621
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002622 if (ea.nextcmd && *ea.nextcmd == NUL) // not really a next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00002623 ea.nextcmd = NULL;
2624
2625#ifdef FEAT_EVAL
2626 --ex_nesting_level;
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002627 vim_free(ea.cmdline_tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002628#endif
2629
2630 return ea.nextcmd;
2631}
2632#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00002633 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002634#endif
2635
2636/*
Bram Moolenaareffed932018-08-14 13:38:17 +02002637 * Parse and skip over command modifiers:
2638 * - update eap->cmd
2639 * - store flags in "cmdmod".
2640 * - Set ex_pressedreturn for an empty command line.
2641 * - set msg_silent for ":silent"
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002642 * - set 'eventignore' to "all" for ":noautocmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002643 * - set p_verbose for ":verbose"
2644 * - Increment "sandbox" for ":sandbox"
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002645 * When "skip_only" is TRUE the global variables are not changed, except for
2646 * "cmdmod".
Bram Moolenaareffed932018-08-14 13:38:17 +02002647 * Return FAIL when the command is not to be executed.
2648 * May set "errormsg" to an error message.
2649 */
2650 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002651parse_command_modifiers(exarg_T *eap, char **errormsg, int skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002652{
2653 char_u *p;
2654
Bram Moolenaara80faa82020-04-12 19:37:17 +02002655 CLEAR_FIELD(cmdmod);
Bram Moolenaareffed932018-08-14 13:38:17 +02002656 eap->verbose_save = -1;
2657 eap->save_msg_silent = -1;
2658
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002659 // Repeat until no more command modifiers are found.
Bram Moolenaareffed932018-08-14 13:38:17 +02002660 for (;;)
2661 {
Bram Moolenaareffed932018-08-14 13:38:17 +02002662 while (*eap->cmd == ' ' || *eap->cmd == '\t' || *eap->cmd == ':')
2663 ++eap->cmd;
2664
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002665 // in ex mode, an empty line works like :+
Bram Moolenaareffed932018-08-14 13:38:17 +02002666 if (*eap->cmd == NUL && exmode_active
2667 && (getline_equal(eap->getline, eap->cookie, getexmodeline)
2668 || getline_equal(eap->getline, eap->cookie, getexline))
2669 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2670 {
2671 eap->cmd = (char_u *)"+";
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002672 if (!skip_only)
2673 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002674 }
2675
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002676 // ignore comment and empty lines
Bram Moolenaareffed932018-08-14 13:38:17 +02002677 if (*eap->cmd == '"')
2678 return FAIL;
2679 if (*eap->cmd == NUL)
2680 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002681 if (!skip_only)
2682 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002683 return FAIL;
2684 }
2685
Bram Moolenaareffed932018-08-14 13:38:17 +02002686 p = skip_range(eap->cmd, NULL);
2687 switch (*p)
2688 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002689 // When adding an entry, also modify cmd_exists().
Bram Moolenaareffed932018-08-14 13:38:17 +02002690 case 'a': if (!checkforcmd(&eap->cmd, "aboveleft", 3))
2691 break;
2692 cmdmod.split |= WSP_ABOVE;
2693 continue;
2694
2695 case 'b': if (checkforcmd(&eap->cmd, "belowright", 3))
2696 {
2697 cmdmod.split |= WSP_BELOW;
2698 continue;
2699 }
2700 if (checkforcmd(&eap->cmd, "browse", 3))
2701 {
2702#ifdef FEAT_BROWSE_CMD
2703 cmdmod.browse = TRUE;
2704#endif
2705 continue;
2706 }
2707 if (!checkforcmd(&eap->cmd, "botright", 2))
2708 break;
2709 cmdmod.split |= WSP_BOT;
2710 continue;
2711
2712 case 'c': if (!checkforcmd(&eap->cmd, "confirm", 4))
2713 break;
2714#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2715 cmdmod.confirm = TRUE;
2716#endif
2717 continue;
2718
2719 case 'k': if (checkforcmd(&eap->cmd, "keepmarks", 3))
2720 {
2721 cmdmod.keepmarks = TRUE;
2722 continue;
2723 }
2724 if (checkforcmd(&eap->cmd, "keepalt", 5))
2725 {
2726 cmdmod.keepalt = TRUE;
2727 continue;
2728 }
2729 if (checkforcmd(&eap->cmd, "keeppatterns", 5))
2730 {
2731 cmdmod.keeppatterns = TRUE;
2732 continue;
2733 }
2734 if (!checkforcmd(&eap->cmd, "keepjumps", 5))
2735 break;
2736 cmdmod.keepjumps = TRUE;
2737 continue;
2738
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002739 case 'f': // only accept ":filter {pat} cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002740 {
2741 char_u *reg_pat;
2742
2743 if (!checkforcmd(&p, "filter", 4)
2744 || *p == NUL || ends_excmd(*p))
2745 break;
2746 if (*p == '!')
2747 {
2748 cmdmod.filter_force = TRUE;
2749 p = skipwhite(p + 1);
2750 if (*p == NUL || ends_excmd(*p))
2751 break;
2752 }
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002753 if (skip_only)
2754 p = skip_vimgrep_pat(p, NULL, NULL);
2755 else
2756 // NOTE: This puts a NUL after the pattern.
2757 p = skip_vimgrep_pat(p, &reg_pat, NULL);
Bram Moolenaareffed932018-08-14 13:38:17 +02002758 if (p == NULL || *p == NUL)
2759 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002760 if (!skip_only)
2761 {
2762 cmdmod.filter_regmatch.regprog =
Bram Moolenaareffed932018-08-14 13:38:17 +02002763 vim_regcomp(reg_pat, RE_MAGIC);
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002764 if (cmdmod.filter_regmatch.regprog == NULL)
2765 break;
2766 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002767 eap->cmd = p;
2768 continue;
2769 }
2770
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002771 // ":hide" and ":hide | cmd" are not modifiers
Bram Moolenaareffed932018-08-14 13:38:17 +02002772 case 'h': if (p != eap->cmd || !checkforcmd(&p, "hide", 3)
2773 || *p == NUL || ends_excmd(*p))
2774 break;
2775 eap->cmd = p;
2776 cmdmod.hide = TRUE;
2777 continue;
2778
2779 case 'l': if (checkforcmd(&eap->cmd, "lockmarks", 3))
2780 {
2781 cmdmod.lockmarks = TRUE;
2782 continue;
2783 }
2784
2785 if (!checkforcmd(&eap->cmd, "leftabove", 5))
2786 break;
2787 cmdmod.split |= WSP_ABOVE;
2788 continue;
2789
2790 case 'n': if (checkforcmd(&eap->cmd, "noautocmd", 3))
2791 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002792 if (cmdmod.save_ei == NULL && !skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002793 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002794 // Set 'eventignore' to "all". Restore the
2795 // existing option value later.
Bram Moolenaareffed932018-08-14 13:38:17 +02002796 cmdmod.save_ei = vim_strsave(p_ei);
2797 set_string_option_direct((char_u *)"ei", -1,
2798 (char_u *)"all", OPT_FREE, SID_NONE);
2799 }
2800 continue;
2801 }
2802 if (!checkforcmd(&eap->cmd, "noswapfile", 3))
2803 break;
2804 cmdmod.noswapfile = TRUE;
2805 continue;
2806
2807 case 'r': if (!checkforcmd(&eap->cmd, "rightbelow", 6))
2808 break;
2809 cmdmod.split |= WSP_BELOW;
2810 continue;
2811
2812 case 's': if (checkforcmd(&eap->cmd, "sandbox", 3))
2813 {
2814#ifdef HAVE_SANDBOX
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002815 if (!skip_only)
2816 {
2817 if (!eap->did_sandbox)
2818 ++sandbox;
2819 eap->did_sandbox = TRUE;
2820 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002821#endif
2822 continue;
2823 }
2824 if (!checkforcmd(&eap->cmd, "silent", 3))
2825 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002826 if (!skip_only)
2827 {
2828 if (eap->save_msg_silent == -1)
2829 eap->save_msg_silent = msg_silent;
2830 ++msg_silent;
2831 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002832 if (*eap->cmd == '!' && !VIM_ISWHITE(eap->cmd[-1]))
2833 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002834 // ":silent!", but not "silent !cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002835 eap->cmd = skipwhite(eap->cmd + 1);
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002836 if (!skip_only)
2837 {
2838 ++emsg_silent;
2839 ++eap->did_esilent;
2840 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002841 }
2842 continue;
2843
2844 case 't': if (checkforcmd(&p, "tab", 3))
2845 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002846 if (!skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002847 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002848 long tabnr = get_address(eap, &eap->cmd,
2849 ADDR_TABS, eap->skip,
2850 skip_only, FALSE, 1);
2851 if (tabnr == MAXLNUM)
2852 cmdmod.tab = tabpage_index(curtab) + 1;
2853 else
Bram Moolenaareffed932018-08-14 13:38:17 +02002854 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002855 if (tabnr < 0 || tabnr > LAST_TAB_NR)
2856 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002857 *errormsg = _(e_invrange);
Bram Moolenaar548e5982018-12-26 21:45:00 +01002858 return FAIL;
2859 }
2860 cmdmod.tab = tabnr + 1;
Bram Moolenaareffed932018-08-14 13:38:17 +02002861 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002862 }
2863 eap->cmd = p;
2864 continue;
2865 }
2866 if (!checkforcmd(&eap->cmd, "topleft", 2))
2867 break;
2868 cmdmod.split |= WSP_TOP;
2869 continue;
2870
2871 case 'u': if (!checkforcmd(&eap->cmd, "unsilent", 3))
2872 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002873 if (!skip_only)
2874 {
2875 if (eap->save_msg_silent == -1)
2876 eap->save_msg_silent = msg_silent;
2877 msg_silent = 0;
2878 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002879 continue;
2880
2881 case 'v': if (checkforcmd(&eap->cmd, "vertical", 4))
2882 {
2883 cmdmod.split |= WSP_VERT;
2884 continue;
2885 }
2886 if (!checkforcmd(&p, "verbose", 4))
2887 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002888 if (!skip_only)
2889 {
2890 if (eap->verbose_save < 0)
2891 eap->verbose_save = p_verbose;
2892 if (vim_isdigit(*eap->cmd))
2893 p_verbose = atoi((char *)eap->cmd);
2894 else
2895 p_verbose = 1;
2896 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002897 eap->cmd = p;
2898 continue;
2899 }
2900 break;
2901 }
2902
2903 return OK;
2904}
2905
2906/*
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002907 * Unod and free contents of "cmdmod".
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002908 */
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002909 void
2910undo_cmdmod(exarg_T *eap, int save_msg_scroll)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002911{
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002912 if (eap->verbose_save >= 0)
2913 p_verbose = eap->verbose_save;
2914
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002915 if (cmdmod.save_ei != NULL)
2916 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002917 // Restore 'eventignore' to the value before ":noautocmd".
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002918 set_string_option_direct((char_u *)"ei", -1, cmdmod.save_ei,
2919 OPT_FREE, SID_NONE);
2920 free_string_option(cmdmod.save_ei);
2921 }
2922
2923 if (cmdmod.filter_regmatch.regprog != NULL)
2924 vim_regfree(cmdmod.filter_regmatch.regprog);
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002925
2926 if (eap->save_msg_silent != -1)
2927 {
2928 // messages could be enabled for a serious error, need to check if the
2929 // counters don't become negative
2930 if (!did_emsg || msg_silent > eap->save_msg_silent)
2931 msg_silent = eap->save_msg_silent;
2932 emsg_silent -= eap->did_esilent;
2933 if (emsg_silent < 0)
2934 emsg_silent = 0;
2935 // Restore msg_scroll, it's set by file I/O commands, even when no
2936 // message is actually displayed.
2937 msg_scroll = save_msg_scroll;
2938
2939 // "silent reg" or "silent echo x" inside "redir" leaves msg_col
2940 // somewhere in the line. Put it back in the first column.
2941 if (redirecting())
2942 msg_col = 0;
2943 }
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002944}
2945
2946/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002947 * Parse the address range, if any, in "eap".
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02002948 * May set the last search pattern, unless "silent" is TRUE.
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002949 * Return FAIL and set "errormsg" or return OK.
2950 */
2951 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002952parse_cmd_address(exarg_T *eap, char **errormsg, int silent)
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002953{
2954 int address_count = 1;
2955 linenr_T lnum;
2956
2957 // Repeat for all ',' or ';' separated addresses.
2958 for (;;)
2959 {
2960 eap->line1 = eap->line2;
2961 switch (eap->addr_type)
2962 {
2963 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02002964 case ADDR_OTHER:
Bram Moolenaarb5383b12020-05-18 19:46:48 +02002965 // Default is the cursor line number. Avoid using an invalid
2966 // line number though.
2967 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
2968 eap->line2 = curbuf->b_ml.ml_line_count;
2969 else
2970 eap->line2 = curwin->w_cursor.lnum;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002971 break;
2972 case ADDR_WINDOWS:
2973 eap->line2 = CURRENT_WIN_NR;
2974 break;
2975 case ADDR_ARGUMENTS:
2976 eap->line2 = curwin->w_arg_idx + 1;
2977 if (eap->line2 > ARGCOUNT)
2978 eap->line2 = ARGCOUNT;
2979 break;
2980 case ADDR_LOADED_BUFFERS:
2981 case ADDR_BUFFERS:
2982 eap->line2 = curbuf->b_fnum;
2983 break;
2984 case ADDR_TABS:
2985 eap->line2 = CURRENT_TAB_NR;
2986 break;
2987 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02002988 case ADDR_UNSIGNED:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002989 eap->line2 = 1;
2990 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002991 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002992#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02002993 eap->line2 = qf_get_cur_idx(eap);
2994#endif
2995 break;
2996 case ADDR_QUICKFIX_VALID:
2997#ifdef FEAT_QUICKFIX
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002998 eap->line2 = qf_get_cur_valid_idx(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002999#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003000 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003001 case ADDR_NONE:
3002 // Will give an error later if a range is found.
3003 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003004 }
3005 eap->cmd = skipwhite(eap->cmd);
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003006 lnum = get_address(eap, &eap->cmd, eap->addr_type, eap->skip, silent,
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003007 eap->addr_count == 0, address_count++);
3008 if (eap->cmd == NULL) // error detected
3009 return FAIL;
3010 if (lnum == MAXLNUM)
3011 {
3012 if (*eap->cmd == '%') // '%' - all lines
3013 {
3014 ++eap->cmd;
3015 switch (eap->addr_type)
3016 {
3017 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003018 case ADDR_OTHER:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003019 eap->line1 = 1;
3020 eap->line2 = curbuf->b_ml.ml_line_count;
3021 break;
3022 case ADDR_LOADED_BUFFERS:
3023 {
3024 buf_T *buf = firstbuf;
3025
3026 while (buf->b_next != NULL
3027 && buf->b_ml.ml_mfp == NULL)
3028 buf = buf->b_next;
3029 eap->line1 = buf->b_fnum;
3030 buf = lastbuf;
3031 while (buf->b_prev != NULL
3032 && buf->b_ml.ml_mfp == NULL)
3033 buf = buf->b_prev;
3034 eap->line2 = buf->b_fnum;
3035 break;
3036 }
3037 case ADDR_BUFFERS:
3038 eap->line1 = firstbuf->b_fnum;
3039 eap->line2 = lastbuf->b_fnum;
3040 break;
3041 case ADDR_WINDOWS:
3042 case ADDR_TABS:
3043 if (IS_USER_CMDIDX(eap->cmdidx))
3044 {
3045 eap->line1 = 1;
3046 eap->line2 = eap->addr_type == ADDR_WINDOWS
3047 ? LAST_WIN_NR : LAST_TAB_NR;
3048 }
3049 else
3050 {
3051 // there is no Vim command which uses '%' and
3052 // ADDR_WINDOWS or ADDR_TABS
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003053 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003054 return FAIL;
3055 }
3056 break;
3057 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003058 case ADDR_UNSIGNED:
3059 case ADDR_QUICKFIX:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003060 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003061 return FAIL;
3062 case ADDR_ARGUMENTS:
3063 if (ARGCOUNT == 0)
3064 eap->line1 = eap->line2 = 0;
3065 else
3066 {
3067 eap->line1 = 1;
3068 eap->line2 = ARGCOUNT;
3069 }
3070 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003071 case ADDR_QUICKFIX_VALID:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003072#ifdef FEAT_QUICKFIX
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003073 eap->line1 = 1;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003074 eap->line2 = qf_get_valid_size(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003075 if (eap->line2 == 0)
3076 eap->line2 = 1;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003077#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003078 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003079 case ADDR_NONE:
3080 // Will give an error later if a range is found.
3081 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003082 }
3083 ++eap->addr_count;
3084 }
3085 else if (*eap->cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
3086 {
3087 pos_T *fp;
3088
3089 // '*' - visual area
3090 if (eap->addr_type != ADDR_LINES)
3091 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003092 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003093 return FAIL;
3094 }
3095
3096 ++eap->cmd;
3097 if (!eap->skip)
3098 {
3099 fp = getmark('<', FALSE);
3100 if (check_mark(fp) == FAIL)
3101 return FAIL;
3102 eap->line1 = fp->lnum;
3103 fp = getmark('>', FALSE);
3104 if (check_mark(fp) == FAIL)
3105 return FAIL;
3106 eap->line2 = fp->lnum;
3107 ++eap->addr_count;
3108 }
3109 }
3110 }
3111 else
3112 eap->line2 = lnum;
3113 eap->addr_count++;
3114
3115 if (*eap->cmd == ';')
3116 {
3117 if (!eap->skip)
3118 {
3119 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar0e717042020-04-27 19:29:01 +02003120 // Don't leave the cursor on an illegal line or column, but do
3121 // accept zero as address, so 0;/PATTERN/ works correctly.
3122 if (eap->line2 > 0)
3123 check_cursor();
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003124 }
3125 }
3126 else if (*eap->cmd != ',')
3127 break;
3128 ++eap->cmd;
3129 }
3130
3131 // One address given: set start and end lines.
3132 if (eap->addr_count == 1)
3133 {
3134 eap->line1 = eap->line2;
3135 // ... but only implicit: really no address given
3136 if (lnum == MAXLNUM)
3137 eap->addr_count = 0;
3138 }
3139 return OK;
3140}
3141
3142/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003143 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003144 * If there is a match advance "pp" to the argument and return TRUE.
3145 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003146 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003147checkforcmd(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003148 char_u **pp, // start of command
3149 char *cmd, // name of command
3150 int len) // required length
Bram Moolenaar071d4272004-06-13 20:20:40 +00003151{
3152 int i;
3153
3154 for (i = 0; cmd[i] != NUL; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003155 if (((char_u *)cmd)[i] != (*pp)[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156 break;
3157 if (i >= len && !isalpha((*pp)[i]))
3158 {
3159 *pp = skipwhite(*pp + i);
3160 return TRUE;
3161 }
3162 return FALSE;
3163}
3164
3165/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003166 * Append "cmd" to the error message in IObuff.
3167 * Takes care of limiting the length and handling 0xa0, which would be
3168 * invisible otherwise.
3169 */
3170 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003171append_command(char_u *cmd)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003172{
3173 char_u *s = cmd;
3174 char_u *d;
3175
3176 STRCAT(IObuff, ": ");
3177 d = IObuff + STRLEN(IObuff);
3178 while (*s != NUL && d - IObuff < IOSIZE - 7)
3179 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003180 if (enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) : *s == 0xa0)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003181 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003182 s += enc_utf8 ? 2 : 1;
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003183 STRCPY(d, "<a0>");
3184 d += 4;
3185 }
3186 else
3187 MB_COPY_CHAR(s, d);
3188 }
3189 *d = NUL;
3190}
3191
3192/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003194 * Start of the name can be found at eap->cmd.
Bram Moolenaar25190db2019-05-04 15:05:28 +02003195 * Sets eap->cmdidx and returns a pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003196 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003197 *
3198 * If "lookup" is not NULL recognize expression without "eval" or "call" and
3199 * assignment without "let". Sets eap->cmdidx to the command while returning
3200 * "eap->cmd".
3201 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003202 * Returns NULL for an ambiguous user command.
3203 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003204 char_u *
3205find_ex_command(
3206 exarg_T *eap,
Bram Moolenaara494f562020-04-18 17:45:38 +02003207 int *full UNUSED,
Bram Moolenaarb84a3812020-05-01 15:44:29 +02003208 void *(*lookup)(char_u *, size_t, cctx_T *) UNUSED,
Bram Moolenaara494f562020-04-18 17:45:38 +02003209 cctx_T *cctx UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003210{
3211 int len;
3212 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003213 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003214
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003215#ifdef FEAT_EVAL
3216 /*
3217 * Recognize a Vim9 script function/method call and assignment:
3218 * "lvar = value", "lvar(arg)", "[1, 2 3]->Func()"
3219 */
Bram Moolenaar0c6ceaf2020-02-22 18:36:32 +01003220 p = eap->cmd;
Bram Moolenaard2ef6b32020-07-02 21:11:34 +02003221 if (lookup != NULL && (*p == '(' || *p == '{'
3222 || ((p = to_name_const_end(eap->cmd)) > eap->cmd && *p != NUL)
3223 || *p == '['))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003224 {
3225 int oplen;
3226 int heredoc;
3227
3228 // "funcname(" is always a function call.
3229 // "varname[]" is an expression.
3230 // "g:varname" is an expression.
3231 // "varname->expr" is an expression.
Bram Moolenaar0a47e092020-07-08 18:30:06 +02003232 // "varname.expr" is an expression.
Bram Moolenaar0c6ceaf2020-02-22 18:36:32 +01003233 // "(..." is an expression.
Bram Moolenaar5f195932020-07-01 20:07:14 +02003234 // "{..." is an dict expression.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003235 if (*p == '('
Bram Moolenaar5f195932020-07-01 20:07:14 +02003236 || *p == '{'
Bram Moolenaard2ef6b32020-07-02 21:11:34 +02003237 || (*p == '[' && p > eap->cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003238 || p[1] == ':'
Bram Moolenaar0a47e092020-07-08 18:30:06 +02003239 || (*p == '-' && p[1] == '>')
3240 || (*p == '.' && ASCII_ISALPHA(p[1])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003241 {
3242 eap->cmdidx = CMD_eval;
3243 return eap->cmd;
3244 }
3245
Bram Moolenaard2ef6b32020-07-02 21:11:34 +02003246 // "[...]->Method()" is a list expression, but "[a, b] = Func()" is
3247 // an assignment.
3248 // If there is no line break inside the "[...]" then "p" is advanced to
3249 // after the "]" by to_name_const_end(): check if a "=" follows.
3250 // If "[...]" has a line break "p" still points at the "[" and it can't
3251 // be an assignment.
3252 if (*eap->cmd == '[' && (p == eap->cmd || *skipwhite(p) != '='))
3253 {
3254 eap->cmdidx = CMD_eval;
3255 return eap->cmd;
3256 }
3257
Bram Moolenaar5f195932020-07-01 20:07:14 +02003258 // Recognize an assignment if we recognize the variable name:
3259 // "g:var = expr"
3260 // "var = expr" where "var" is a local var name.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003261 oplen = assignment_len(skipwhite(p), &heredoc);
3262 if (oplen > 0)
3263 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003264 if (((p - eap->cmd) > 2 && eap->cmd[1] == ':')
Bram Moolenaarb84a3812020-05-01 15:44:29 +02003265 || lookup(eap->cmd, p - eap->cmd, cctx) != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003266 {
3267 eap->cmdidx = CMD_let;
3268 return eap->cmd;
3269 }
3270 }
3271 }
3272#endif
3273
Bram Moolenaar071d4272004-06-13 20:20:40 +00003274 /*
3275 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00003276 * Exceptions:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003277 * - the 'k' command can directly be followed by any character.
3278 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003279 * but :sre[wind] is another command, as are :scr[iptnames],
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00003281 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282 */
3283 p = eap->cmd;
3284 if (*p == 'k')
3285 {
3286 eap->cmdidx = CMD_k;
3287 ++p;
3288 }
3289 else if (p[0] == 's'
Bram Moolenaar204b93f2015-08-04 22:02:51 +02003290 && ((p[1] == 'c' && (p[2] == NUL || (p[2] != 's' && p[2] != 'r'
3291 && (p[3] == NUL || (p[3] != 'i' && p[4] != 'p')))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292 || p[1] == 'g'
3293 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
3294 || p[1] == 'I'
3295 || (p[1] == 'r' && p[2] != 'e')))
3296 {
3297 eap->cmdidx = CMD_substitute;
3298 ++p;
3299 }
3300 else
3301 {
3302 while (ASCII_ISALPHA(*p))
3303 ++p;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003304 // for python 3.x support ":py3", ":python3", ":py3file", etc.
Bram Moolenaar55d5c032010-07-17 23:52:29 +02003305 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003306 {
Bram Moolenaarb6590522010-07-21 16:00:43 +02003307 while (ASCII_ISALNUM(*p))
3308 ++p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003309 }
3310 else if (*p == '9' && STRNCMP("vim9", eap->cmd, 4) == 0)
3311 {
3312 // include "9" for "vim9script"
3313 ++p;
3314 while (ASCII_ISALPHA(*p))
3315 ++p;
3316 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02003317
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003318 // check for non-alpha command
Bram Moolenaar071d4272004-06-13 20:20:40 +00003319 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3320 ++p;
3321 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003322 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
3323 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003324 // Check for ":dl", ":dell", etc. to ":deletel": that's
3325 // :delete with the 'l' flag. Same for 'p'.
Bram Moolenaardf177f62005-02-22 08:39:57 +00003326 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003327 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00003328 break;
3329 if (i == len - 1)
3330 {
3331 --len;
3332 if (p[-1] == 'l')
3333 eap->flags |= EXFLAG_LIST;
3334 else
3335 eap->flags |= EXFLAG_PRINT;
3336 }
3337 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003339 if (ASCII_ISLOWER(eap->cmd[0]))
3340 {
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003341 int c1 = eap->cmd[0];
Bram Moolenaar94f82cb2019-07-24 22:30:27 +02003342 int c2 = len == 1 ? NUL : eap->cmd[1];
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003343
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003344 if (command_count != (int)CMD_SIZE)
3345 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003346 iemsg(_("E943: Command table needs to be updated, run 'make cmdidxs'"));
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003347 getout(1);
3348 }
3349
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003350 // Use a precomputed index for fast look-up in cmdnames[]
3351 // taking into account the first 2 letters of eap->cmd.
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003352 eap->cmdidx = cmdidxs1[CharOrdLow(c1)];
3353 if (ASCII_ISLOWER(c2))
3354 eap->cmdidx += cmdidxs2[CharOrdLow(c1)][CharOrdLow(c2)];
3355 }
Bram Moolenaara494f562020-04-18 17:45:38 +02003356 else if (ASCII_ISUPPER(eap->cmd[0]))
3357 eap->cmdidx = CMD_Next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358 else
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003359 eap->cmdidx = CMD_bang;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360
3361 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
3362 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
3363 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
3364 (size_t)len) == 0)
3365 {
3366#ifdef FEAT_EVAL
3367 if (full != NULL
3368 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
3369 *full = TRUE;
3370#endif
3371 break;
3372 }
3373
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003374 // Look for a user defined command as a last resort. Let ":Print" be
3375 // overruled by a user defined command.
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01003376 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
3377 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378 {
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003379 // User defined commands may contain digits.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380 while (ASCII_ISALNUM(*p))
3381 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003382 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003384 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385 eap->cmdidx = CMD_SIZE;
3386 }
3387
3388 return p;
3389}
3390
3391#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003392static struct cmdmod
3393{
3394 char *name;
3395 int minlen;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003396 int has_count; // :123verbose :3tab
Bram Moolenaared53fb92007-11-24 20:50:24 +00003397} cmdmods[] = {
3398 {"aboveleft", 3, FALSE},
3399 {"belowright", 3, FALSE},
3400 {"botright", 2, FALSE},
3401 {"browse", 3, FALSE},
3402 {"confirm", 4, FALSE},
Bram Moolenaar7b668e82016-08-23 23:51:21 +02003403 {"filter", 4, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003404 {"hide", 3, FALSE},
3405 {"keepalt", 5, FALSE},
3406 {"keepjumps", 5, FALSE},
3407 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01003408 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003409 {"leftabove", 5, FALSE},
3410 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003411 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003412 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003413 {"rightbelow", 6, FALSE},
3414 {"sandbox", 3, FALSE},
3415 {"silent", 3, FALSE},
3416 {"tab", 3, TRUE},
3417 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003418 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003419 {"verbose", 4, TRUE},
3420 {"vertical", 4, FALSE},
3421};
3422
3423/*
3424 * Return length of a command modifier (including optional count).
3425 * Return zero when it's not a modifier.
3426 */
3427 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003428modifier_len(char_u *cmd)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003429{
3430 int i, j;
3431 char_u *p = cmd;
3432
3433 if (VIM_ISDIGIT(*cmd))
3434 p = skipwhite(skipdigits(cmd));
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003435 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003436 {
3437 for (j = 0; p[j] != NUL; ++j)
3438 if (p[j] != cmdmods[i].name[j])
3439 break;
Bram Moolenaar2d473ab2013-06-12 17:12:24 +02003440 if (!ASCII_ISALPHA(p[j]) && j >= cmdmods[i].minlen
Bram Moolenaared53fb92007-11-24 20:50:24 +00003441 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003442 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003443 }
3444 return 0;
3445}
3446
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447/*
3448 * Return > 0 if an Ex command "name" exists.
3449 * Return 2 if there is an exact match.
3450 * Return 3 if there is an ambiguous match.
3451 */
3452 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003453cmd_exists(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454{
3455 exarg_T ea;
3456 int full = FALSE;
3457 int i;
3458 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003459 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003461 // Check command modifiers.
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003462 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463 {
3464 for (j = 0; name[j] != NUL; ++j)
3465 if (name[j] != cmdmods[i].name[j])
3466 break;
3467 if (name[j] == NUL && j >= cmdmods[i].minlen)
3468 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3469 }
3470
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003471 // Check built-in commands and user defined commands.
3472 // For ":2match" and ":3match" we need to skip the number.
Bram Moolenaara9587612006-05-04 21:47:50 +00003473 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003475 p = find_ex_command(&ea, &full, NULL, NULL);
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003476 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003477 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003478 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3479 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003480 if (*skipwhite(p) != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003481 return 0; // trailing garbage
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3483}
3484#endif
3485
Bram Moolenaard0190392019-08-23 21:17:35 +02003486 cmdidx_T
3487excmd_get_cmdidx(char_u *cmd, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488{
Bram Moolenaard0190392019-08-23 21:17:35 +02003489 cmdidx_T idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490
Bram Moolenaard0190392019-08-23 21:17:35 +02003491 for (idx = (cmdidx_T)0; (int)idx < (int)CMD_SIZE;
3492 idx = (cmdidx_T)((int)idx + 1))
3493 if (STRNCMP(cmdnames[(int)idx].cmd_name, cmd, (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494 break;
3495
Bram Moolenaard0190392019-08-23 21:17:35 +02003496 return idx;
3497}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498
Bram Moolenaard0190392019-08-23 21:17:35 +02003499 long
3500excmd_get_argt(cmdidx_T idx)
3501{
3502 return (long)cmdnames[(int)idx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503}
3504
3505/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003506 * Skip a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507 *
3508 * Backslashed delimiters after / or ? will be skipped, and commands will
3509 * not be expanded between /'s and ?'s or after "'".
3510 *
Bram Moolenaardf069ee2020-06-22 23:02:51 +02003511 * Also skip white space and ":" characters after the range.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512 * Returns the "cmd" pointer advanced to beyond the range.
3513 */
3514 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003515skip_range(
3516 char_u *cmd,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003517 int *ctx) // pointer to xp_context or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003519 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01003521 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;\\", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522 {
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01003523 if (*cmd == '\\')
3524 {
3525 if (cmd[1] == '?' || cmd[1] == '/' || cmd[1] == '&')
3526 ++cmd;
3527 else
3528 break;
3529 }
3530 else if (*cmd == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531 {
3532 if (*++cmd == NUL && ctx != NULL)
3533 *ctx = EXPAND_NOTHING;
3534 }
3535 else if (*cmd == '/' || *cmd == '?')
3536 {
3537 delim = *cmd++;
3538 while (*cmd != NUL && *cmd != delim)
3539 if (*cmd++ == '\\' && *cmd != NUL)
3540 ++cmd;
3541 if (*cmd == NUL && ctx != NULL)
3542 *ctx = EXPAND_NOTHING;
3543 }
3544 if (*cmd != NUL)
3545 ++cmd;
3546 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00003547
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003548 // Skip ":" and white space.
Bram Moolenaardf177f62005-02-22 08:39:57 +00003549 while (*cmd == ':')
3550 cmd = skipwhite(cmd + 1);
3551
Bram Moolenaar071d4272004-06-13 20:20:40 +00003552 return cmd;
3553}
3554
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003555 static void
3556addr_error(cmd_addr_T addr_type)
3557{
3558 if (addr_type == ADDR_NONE)
3559 emsg(_(e_norange));
3560 else
3561 emsg(_(e_invrange));
3562}
3563
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564/*
Bram Moolenaar198cb662018-09-06 21:44:17 +02003565 * Get a single EX address.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566 *
3567 * Set ptr to the next character after the part that was interpreted.
3568 * Set ptr to NULL when an error is encountered.
Bram Moolenaar198cb662018-09-06 21:44:17 +02003569 * This may set the last used search pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570 *
3571 * Return MAXLNUM when no Ex address was found.
3572 */
3573 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003574get_address(
3575 exarg_T *eap UNUSED,
3576 char_u **ptr,
Bram Moolenaar1b03a192019-12-08 17:08:29 +01003577 cmd_addr_T addr_type,
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003578 int skip, // only skip the address, don't use it
3579 int silent, // no errors or side effects
3580 int to_other_file, // flag: may jump to other file
3581 int address_count UNUSED) // 1 for first address, >1 after comma
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582{
3583 int c;
3584 int i;
3585 long n;
3586 char_u *cmd;
3587 pos_T pos;
3588 pos_T *fp;
3589 linenr_T lnum;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003590 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591
3592 cmd = skipwhite(*ptr);
3593 lnum = MAXLNUM;
3594 do
3595 {
3596 switch (*cmd)
3597 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003598 case '.': // '.' - Cursor position
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003599 ++cmd;
3600 switch (addr_type)
3601 {
3602 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003603 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003604 lnum = curwin->w_cursor.lnum;
3605 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003606 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003607 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003608 break;
3609 case ADDR_ARGUMENTS:
3610 lnum = curwin->w_arg_idx + 1;
3611 break;
3612 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003613 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003614 lnum = curbuf->b_fnum;
3615 break;
3616 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003617 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003618 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003619 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003620 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003621 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003622 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003623 cmd = NULL;
3624 goto error;
3625 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003626 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003627#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02003628 lnum = qf_get_cur_idx(eap);
3629#endif
3630 break;
3631 case ADDR_QUICKFIX_VALID:
3632#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003633 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02003634#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003635 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003636 }
3637 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003638
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003639 case '$': // '$' - last line
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003640 ++cmd;
3641 switch (addr_type)
3642 {
3643 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003644 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645 lnum = curbuf->b_ml.ml_line_count;
3646 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003647 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003648 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003649 break;
3650 case ADDR_ARGUMENTS:
3651 lnum = ARGCOUNT;
3652 break;
3653 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003654 buf = lastbuf;
3655 while (buf->b_ml.ml_mfp == NULL)
3656 {
3657 if (buf->b_prev == NULL)
3658 break;
3659 buf = buf->b_prev;
3660 }
3661 lnum = buf->b_fnum;
3662 break;
3663 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003664 lnum = lastbuf->b_fnum;
3665 break;
3666 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003667 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003668 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003669 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003670 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003671 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003672 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003673 cmd = NULL;
3674 goto error;
3675 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003676 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003677#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003678 lnum = qf_get_size(eap);
3679 if (lnum == 0)
3680 lnum = 1;
Bram Moolenaare906c502015-09-09 21:10:39 +02003681#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003682 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003683 case ADDR_QUICKFIX_VALID:
3684#ifdef FEAT_QUICKFIX
3685 lnum = qf_get_valid_size(eap);
3686 if (lnum == 0)
3687 lnum = 1;
3688#endif
3689 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003690 }
3691 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003692
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003693 case '\'': // ''' - mark
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003694 if (*++cmd == NUL)
3695 {
3696 cmd = NULL;
3697 goto error;
3698 }
3699 if (addr_type != ADDR_LINES)
3700 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003701 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003702 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003703 goto error;
3704 }
3705 if (skip)
3706 ++cmd;
3707 else
3708 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003709 // Only accept a mark in another file when it is
3710 // used by itself: ":'M".
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003711 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
3712 ++cmd;
3713 if (fp == (pos_T *)-1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003714 // Jumped to another file.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003715 lnum = curwin->w_cursor.lnum;
3716 else
3717 {
3718 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719 {
3720 cmd = NULL;
3721 goto error;
3722 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003723 lnum = fp->lnum;
3724 }
3725 }
3726 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003727
3728 case '/':
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003729 case '?': // '/' or '?' - search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003730 c = *cmd++;
3731 if (addr_type != ADDR_LINES)
3732 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003733 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003734 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003735 goto error;
3736 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003737 if (skip) // skip "/pat/"
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003738 {
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02003739 cmd = skip_regexp(cmd, c, (int)p_magic);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003740 if (*cmd == c)
3741 ++cmd;
3742 }
3743 else
3744 {
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003745 int flags;
3746
3747 pos = curwin->w_cursor; // save curwin->w_cursor
3748
3749 // When '/' or '?' follows another address, start from
3750 // there.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003751 if (lnum != MAXLNUM)
3752 curwin->w_cursor.lnum = lnum;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003753
3754 // Start a forward search at the end of the line (unless
3755 // before the first line).
3756 // Start a backward search at the start of the line.
3757 // This makes sure we never match in the current
3758 // line, and can match anywhere in the
3759 // next/previous line.
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01003760 if (c == '/' && curwin->w_cursor.lnum > 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003761 curwin->w_cursor.col = MAXCOL;
3762 else
3763 curwin->w_cursor.col = 0;
3764 searchcmdlen = 0;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003765 flags = silent ? 0 : SEARCH_HIS | SEARCH_MSG;
Bram Moolenaarc036e872020-02-21 21:30:52 +01003766 if (!do_search(NULL, c, c, cmd, 1L, flags, NULL))
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003767 {
3768 curwin->w_cursor = pos;
3769 cmd = NULL;
3770 goto error;
3771 }
3772 lnum = curwin->w_cursor.lnum;
3773 curwin->w_cursor = pos;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003774 // adjust command string pointer
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003775 cmd += searchcmdlen;
3776 }
3777 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003779 case '\\': // "\?", "\/" or "\&", repeat search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003780 ++cmd;
3781 if (addr_type != ADDR_LINES)
3782 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003783 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003784 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003785 goto error;
3786 }
3787 if (*cmd == '&')
3788 i = RE_SUBST;
3789 else if (*cmd == '?' || *cmd == '/')
3790 i = RE_SEARCH;
3791 else
3792 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003793 emsg(_(e_backslash));
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003794 cmd = NULL;
3795 goto error;
3796 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003797
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003798 if (!skip)
3799 {
3800 /*
3801 * When search follows another address, start from
3802 * there.
3803 */
3804 if (lnum != MAXLNUM)
3805 pos.lnum = lnum;
3806 else
3807 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003808
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003809 /*
3810 * Start the search just like for the above
3811 * do_search().
3812 */
3813 if (*cmd != '?')
3814 pos.col = MAXCOL;
3815 else
3816 pos.col = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02003817 pos.coladd = 0;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01003818 if (searchit(curwin, curbuf, &pos, NULL,
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003819 *cmd == '?' ? BACKWARD : FORWARD,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02003820 (char_u *)"", 1L, SEARCH_MSG, i, NULL) != FAIL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003821 lnum = pos.lnum;
3822 else
3823 {
3824 cmd = NULL;
3825 goto error;
3826 }
3827 }
3828 ++cmd;
3829 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830
3831 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003832 if (VIM_ISDIGIT(*cmd)) // absolute line number
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003833 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003834 }
3835
3836 for (;;)
3837 {
3838 cmd = skipwhite(cmd);
3839 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
3840 break;
3841
3842 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003843 {
3844 switch (addr_type)
3845 {
3846 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003847 case ADDR_OTHER:
3848 // "+1" is same as ".+1"
Bram Moolenaarf240e182014-11-27 18:33:02 +01003849 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003850 break;
3851 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003852 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003853 break;
3854 case ADDR_ARGUMENTS:
3855 lnum = curwin->w_arg_idx + 1;
3856 break;
3857 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003858 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003859 lnum = curbuf->b_fnum;
3860 break;
3861 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003862 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003863 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003864 case ADDR_TABS_RELATIVE:
3865 lnum = 1;
3866 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003867 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003868#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02003869 lnum = qf_get_cur_idx(eap);
3870#endif
3871 break;
3872 case ADDR_QUICKFIX_VALID:
3873#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003874 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02003875#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003876 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003877 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003878 case ADDR_UNSIGNED:
3879 lnum = 0;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003880 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003881 }
3882 }
3883
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884 if (VIM_ISDIGIT(*cmd))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003885 i = '+'; // "number" is same as "+number"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886 else
3887 i = *cmd++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003888 if (!VIM_ISDIGIT(*cmd)) // '+' is '+1', but '+0' is not '+1'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889 n = 1;
3890 else
3891 n = getdigits(&cmd);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003892
3893 if (addr_type == ADDR_TABS_RELATIVE)
3894 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003895 emsg(_(e_invrange));
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003896 cmd = NULL;
3897 goto error;
3898 }
3899 else if (addr_type == ADDR_LOADED_BUFFERS
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003900 || addr_type == ADDR_BUFFERS)
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003901 lnum = compute_buffer_local_count(
3902 addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003903 else
Bram Moolenaarded27822017-01-02 14:27:34 +01003904 {
3905#ifdef FEAT_FOLDING
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003906 // Relative line addressing, need to adjust for folded lines
3907 // now, but only do it after the first address.
Bram Moolenaarded27822017-01-02 14:27:34 +01003908 if (addr_type == ADDR_LINES && (i == '-' || i == '+')
3909 && address_count >= 2)
3910 (void)hasFolding(lnum, NULL, &lnum);
3911#endif
3912 if (i == '-')
3913 lnum -= n;
3914 else
3915 lnum += n;
3916 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003917 }
3918 } while (*cmd == '/' || *cmd == '?');
3919
3920error:
3921 *ptr = cmd;
3922 return lnum;
3923}
3924
3925/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00003926 * Get flags from an Ex command argument.
3927 */
3928 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003929get_flags(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00003930{
3931 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
3932 {
3933 if (*eap->arg == 'l')
3934 eap->flags |= EXFLAG_LIST;
3935 else if (*eap->arg == 'p')
3936 eap->flags |= EXFLAG_PRINT;
3937 else
3938 eap->flags |= EXFLAG_NR;
3939 eap->arg = skipwhite(eap->arg + 1);
3940 }
3941}
3942
3943/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003944 * Function called for command which is Not Implemented. NI!
3945 */
3946 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003947ex_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003948{
3949 if (!eap->skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003950 eap->errmsg = N_("E319: Sorry, the command is not available in this version");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003951}
3952
Bram Moolenaar7bb75552007-07-16 18:39:49 +00003953#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003954/*
3955 * Function called for script command which is Not Implemented. NI!
3956 * Skips over ":perl <<EOF" constructs.
3957 */
3958 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003959ex_script_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003960{
3961 if (!eap->skip)
3962 ex_ni(eap);
3963 else
3964 vim_free(script_get(eap, eap->arg));
3965}
3966#endif
3967
3968/*
3969 * Check range in Ex command for validity.
3970 * Return NULL when valid, error message when invalid.
3971 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003972 static char *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003973invalid_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974{
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003975 buf_T *buf;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003976
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977 if ( eap->line1 < 0
3978 || eap->line2 < 0
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003979 || eap->line1 > eap->line2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003980 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003981
Bram Moolenaar8071cb22019-07-12 17:58:01 +02003982 if (eap->argt & EX_RANGE)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003983 {
Bram Moolenaarb7316892019-05-01 18:08:42 +02003984 switch (eap->addr_type)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003985 {
3986 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003987 if (eap->line2 > curbuf->b_ml.ml_line_count
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003988#ifdef FEAT_DIFF
3989 + (eap->cmdidx == CMD_diffget)
3990#endif
3991 )
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003992 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003993 break;
3994 case ADDR_ARGUMENTS:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003995 // add 1 if ARGCOUNT is 0
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003996 if (eap->line2 > ARGCOUNT + (!ARGCOUNT))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003997 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003998 break;
3999 case ADDR_BUFFERS:
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02004000 // Only a boundary check, not whether the buffers actually
4001 // exist.
4002 if (eap->line1 < 1 || eap->line2 > get_highest_fnum())
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004003 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004004 break;
4005 case ADDR_LOADED_BUFFERS:
4006 buf = firstbuf;
4007 while (buf->b_ml.ml_mfp == NULL)
4008 {
4009 if (buf->b_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004010 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004011 buf = buf->b_next;
4012 }
4013 if (eap->line1 < buf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004014 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004015 buf = lastbuf;
4016 while (buf->b_ml.ml_mfp == NULL)
4017 {
4018 if (buf->b_prev == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004019 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004020 buf = buf->b_prev;
4021 }
4022 if (eap->line2 > buf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004023 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004024 break;
4025 case ADDR_WINDOWS:
Bram Moolenaar8be63882015-01-14 11:25:05 +01004026 if (eap->line2 > LAST_WIN_NR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004027 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004028 break;
4029 case ADDR_TABS:
4030 if (eap->line2 > LAST_TAB_NR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004031 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004032 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004033 case ADDR_TABS_RELATIVE:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004034 case ADDR_OTHER:
4035 // Any range is OK.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004036 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004037 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004038#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004039 // No error for value that is too big, will use the last entry.
4040 if (eap->line2 <= 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004041 return _(e_invrange);
Bram Moolenaare906c502015-09-09 21:10:39 +02004042#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004043 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004044 case ADDR_QUICKFIX_VALID:
4045#ifdef FEAT_QUICKFIX
4046 if ((eap->line2 != 1 && eap->line2 > qf_get_valid_size(eap))
4047 || eap->line2 < 0)
4048 return _(e_invrange);
4049#endif
4050 break;
4051 case ADDR_UNSIGNED:
4052 if (eap->line2 < 0)
4053 return _(e_invrange);
4054 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004055 case ADDR_NONE:
4056 // Will give an error elsewhere.
4057 break;
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004058 }
4059 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060 return NULL;
4061}
4062
4063/*
4064 * Correct the range for zero line number, if required.
4065 */
4066 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004067correct_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068{
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004069 if (!(eap->argt & EX_ZEROR)) // zero in range not allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070 {
4071 if (eap->line1 == 0)
4072 eap->line1 = 1;
4073 if (eap->line2 == 0)
4074 eap->line2 = 1;
4075 }
4076}
4077
Bram Moolenaar748bf032005-02-02 23:04:36 +00004078#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004079/*
4080 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4081 * pattern. Otherwise return eap->arg.
4082 */
4083 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004084skip_grep_pat(exarg_T *eap)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004085{
4086 char_u *p = eap->arg;
4087
Bram Moolenaara37420f2006-02-04 22:37:47 +00004088 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4089 || eap->cmdidx == CMD_vimgrepadd
4090 || eap->cmdidx == CMD_lvimgrepadd
4091 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004092 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004093 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004094 if (p == NULL)
4095 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004096 }
4097 return p;
4098}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004099
4100/*
4101 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4102 * in the command line, so that things like % get expanded.
4103 */
4104 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004105replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004106{
4107 char_u *new_cmdline;
4108 char_u *program;
4109 char_u *pos;
4110 char_u *ptr;
4111 int len;
4112 int i;
4113
4114 /*
4115 * Don't do it when ":vimgrep" is used for ":grep".
4116 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004117 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4118 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4119 || eap->cmdidx == CMD_grepadd
4120 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004121 && !grep_internal(eap->cmdidx))
4122 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004123 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4124 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004125 {
4126 if (*curbuf->b_p_gp == NUL)
4127 program = p_gp;
4128 else
4129 program = curbuf->b_p_gp;
4130 }
4131 else
4132 {
4133 if (*curbuf->b_p_mp == NUL)
4134 program = p_mp;
4135 else
4136 program = curbuf->b_p_mp;
4137 }
4138
4139 p = skipwhite(p);
4140
4141 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4142 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004143 // replace $* by given arguments
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004144 i = 1;
4145 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4146 ++i;
4147 len = (int)STRLEN(p);
Bram Moolenaar51e14382019-05-25 20:21:28 +02004148 new_cmdline = alloc(STRLEN(program) + i * (len - 2) + 1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004149 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004150 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004151 ptr = new_cmdline;
4152 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4153 {
4154 i = (int)(pos - program);
4155 STRNCPY(ptr, program, i);
4156 STRCPY(ptr += i, p);
4157 ptr += len;
4158 program = pos + 2;
4159 }
4160 STRCPY(ptr, program);
4161 }
4162 else
4163 {
Bram Moolenaar51e14382019-05-25 20:21:28 +02004164 new_cmdline = alloc(STRLEN(program) + STRLEN(p) + 2);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004165 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004166 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004167 STRCPY(new_cmdline, program);
4168 STRCAT(new_cmdline, " ");
4169 STRCAT(new_cmdline, p);
4170 }
4171 msg_make(p);
4172
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004173 // 'eap->cmd' is not set here, because it is not used at CMD_make
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004174 vim_free(*cmdlinep);
4175 *cmdlinep = new_cmdline;
4176 p = new_cmdline;
4177 }
4178 return p;
4179}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004180#endif
4181
Bram Moolenaar071d4272004-06-13 20:20:40 +00004182/*
4183 * Expand file name in Ex command argument.
Bram Moolenaar0abb4272019-06-15 16:06:00 +02004184 * When an error is detected, "errormsgp" is set to a non-NULL pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004185 * Return FAIL for failure, OK otherwise.
4186 */
4187 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004188expand_filename(
4189 exarg_T *eap,
4190 char_u **cmdlinep,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004191 char **errormsgp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004193 int has_wildcards; // need to expand wildcards
Bram Moolenaar071d4272004-06-13 20:20:40 +00004194 char_u *repl;
4195 int srclen;
4196 char_u *p;
4197 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004198 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004199
Bram Moolenaar748bf032005-02-02 23:04:36 +00004200#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004201 // Skip a regexp pattern for ":vimgrep[add] pat file..."
Bram Moolenaar748bf032005-02-02 23:04:36 +00004202 p = skip_grep_pat(eap);
4203#else
4204 p = eap->arg;
4205#endif
4206
Bram Moolenaar071d4272004-06-13 20:20:40 +00004207 /*
4208 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4209 * the file name contains a wildcard it should not cause expanding.
4210 * (it will be expanded anyway if there is a wildcard before replacing).
4211 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004212 has_wildcards = mch_has_wildcard(p);
4213 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004215#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004216 // Skip over `=expr`, wildcards in it are not expanded.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004217 if (p[0] == '`' && p[1] == '=')
4218 {
4219 p += 2;
4220 (void)skip_expr(&p);
4221 if (*p == '`')
4222 ++p;
4223 continue;
4224 }
4225#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004226 /*
4227 * Quick check if this cannot be the start of a special string.
4228 * Also removes backslash before '%', '#' and '<'.
4229 */
4230 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4231 {
4232 ++p;
4233 continue;
4234 }
4235
4236 /*
4237 * Try to find a match at this position.
4238 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00004239 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
4240 errormsgp, &escaped);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004241 if (*errormsgp != NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242 return FAIL;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004243 if (repl == NULL) // no match found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244 {
4245 p += srclen;
4246 continue;
4247 }
4248
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004249 // Wildcards won't be expanded below, the replacement is taken
4250 // literally. But do expand "~/file", "~user/file" and "$HOME/file".
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004251 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4252 {
4253 char_u *l = repl;
4254
4255 repl = expand_env_save(repl);
4256 vim_free(l);
4257 }
4258
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004259 // Need to escape white space et al. with a backslash.
4260 // Don't do this for:
4261 // - replacement that already has been escaped: "##"
4262 // - shell commands (may have to use quotes instead).
4263 // - non-unix systems when there is a single argument (spaces don't
4264 // separate arguments then).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004265 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00004266 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00004267 && eap->cmdidx != CMD_bang
Bram Moolenaar071d4272004-06-13 20:20:40 +00004268 && eap->cmdidx != CMD_grep
4269 && eap->cmdidx != CMD_grepadd
Bram Moolenaarbf15b8d2017-06-04 20:43:48 +02004270 && eap->cmdidx != CMD_hardcopy
Bram Moolenaar67883b42017-07-27 22:57:00 +02004271 && eap->cmdidx != CMD_lgrep
4272 && eap->cmdidx != CMD_lgrepadd
4273 && eap->cmdidx != CMD_lmake
4274 && eap->cmdidx != CMD_make
4275 && eap->cmdidx != CMD_terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00004276#ifndef UNIX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004277 && !(eap->argt & EX_NOSPC)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004278#endif
4279 )
4280 {
4281 char_u *l;
4282#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004283 // Don't escape a backslash here, because rem_backslash() doesn't
4284 // remove it later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004285 static char_u *nobslash = (char_u *)" \t\"|";
4286# define ESCAPE_CHARS nobslash
4287#else
4288# define ESCAPE_CHARS escape_chars
4289#endif
4290
4291 for (l = repl; *l; ++l)
4292 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
4293 {
4294 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
4295 if (l != NULL)
4296 {
4297 vim_free(repl);
4298 repl = l;
4299 }
4300 break;
4301 }
4302 }
4303
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004304 // For a shell command a '!' must be escaped.
Bram Moolenaar67883b42017-07-27 22:57:00 +02004305 if ((eap->usefilter || eap->cmdidx == CMD_bang
4306 || eap->cmdidx == CMD_terminal)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004307 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004308 {
4309 char_u *l;
4310
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004311 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004312 if (l != NULL)
4313 {
4314 vim_free(repl);
4315 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004316 }
4317 }
4318
4319 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
4320 vim_free(repl);
4321 if (p == NULL)
4322 return FAIL;
4323 }
4324
4325 /*
4326 * One file argument: Expand wildcards.
4327 * Don't do this with ":r !command" or ":w !command".
4328 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004329 if ((eap->argt & EX_NOSPC) && !eap->usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004330 {
4331 /*
4332 * May do this twice:
4333 * 1. Replace environment variables.
4334 * 2. Replace any other wildcards, remove backslashes.
4335 */
4336 for (n = 1; n <= 2; ++n)
4337 {
4338 if (n == 2)
4339 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004340 /*
4341 * Halve the number of backslashes (this is Vi compatible).
4342 * For Unix and OS/2, when wildcards are expanded, this is
4343 * done by ExpandOne() below.
4344 */
Bram Moolenaare7fedb62015-12-31 19:07:19 +01004345#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004346 if (!has_wildcards)
4347#endif
4348 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004349 }
4350
4351 if (has_wildcards)
4352 {
4353 if (n == 1)
4354 {
4355 /*
4356 * First loop: May expand environment variables. This
4357 * can be done much faster with expand_env() than with
4358 * something else (e.g., calling a shell).
4359 * After expanding environment variables, check again
4360 * if there are still wildcards present.
4361 */
4362 if (vim_strchr(eap->arg, '$') != NULL
4363 || vim_strchr(eap->arg, '~') != NULL)
4364 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00004365 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004366 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004367 has_wildcards = mch_has_wildcard(NameBuff);
4368 p = NameBuff;
4369 }
4370 else
4371 p = NULL;
4372 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004373 else // n == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004374 {
4375 expand_T xpc;
Bram Moolenaarb40c2572019-10-19 21:01:05 +02004376 int options = WILD_LIST_NOTFOUND
4377 | WILD_NOERROR | WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004378
4379 ExpandInit(&xpc);
4380 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004381 if (p_wic)
4382 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01004384 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385 if (p == NULL)
4386 return FAIL;
4387 }
4388 if (p != NULL)
4389 {
4390 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
4391 p, cmdlinep);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004392 if (n == 2) // p came from ExpandOne()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004393 vim_free(p);
4394 }
4395 }
4396 }
4397 }
4398 return OK;
4399}
4400
4401/*
4402 * Replace part of the command line, keeping eap->cmd, eap->arg and
4403 * eap->nextcmd correct.
4404 * "src" points to the part that is to be replaced, of length "srclen".
4405 * "repl" is the replacement string.
4406 * Returns a pointer to the character after the replaced string.
4407 * Returns NULL for failure.
4408 */
4409 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004410repl_cmdline(
4411 exarg_T *eap,
4412 char_u *src,
4413 int srclen,
4414 char_u *repl,
4415 char_u **cmdlinep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004416{
4417 int len;
4418 int i;
4419 char_u *new_cmdline;
4420
4421 /*
4422 * The new command line is build in new_cmdline[].
4423 * First allocate it.
4424 * Careful: a "+cmd" argument may have been NUL terminated.
4425 */
4426 len = (int)STRLEN(repl);
4427 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004428 if (eap->nextcmd != NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004429 i += (int)STRLEN(eap->nextcmd);// add space for next command
Bram Moolenaar964b3742019-05-24 18:54:09 +02004430 if ((new_cmdline = alloc(i)) == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004431 return NULL; // out of memory!
Bram Moolenaar071d4272004-06-13 20:20:40 +00004432
4433 /*
4434 * Copy the stuff before the expanded part.
4435 * Copy the expanded stuff.
4436 * Copy what came after the expanded part.
4437 * Copy the next commands, if there are any.
4438 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004439 i = (int)(src - *cmdlinep); // length of part before match
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00004441
Bram Moolenaar071d4272004-06-13 20:20:40 +00004442 mch_memmove(new_cmdline + i, repl, (size_t)len);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004443 i += len; // remember the end of the string
Bram Moolenaar071d4272004-06-13 20:20:40 +00004444 STRCPY(new_cmdline + i, src + srclen);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004445 src = new_cmdline + i; // remember where to continue
Bram Moolenaar071d4272004-06-13 20:20:40 +00004446
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004447 if (eap->nextcmd != NULL) // append next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00004448 {
4449 i = (int)STRLEN(new_cmdline) + 1;
4450 STRCPY(new_cmdline + i, eap->nextcmd);
4451 eap->nextcmd = new_cmdline + i;
4452 }
4453 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
4454 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
4455 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
4456 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
4457 vim_free(*cmdlinep);
4458 *cmdlinep = new_cmdline;
4459
4460 return src;
4461}
4462
4463/*
4464 * Check for '|' to separate commands and '"' to start comments.
4465 */
4466 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004467separate_nextcmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004468{
4469 char_u *p;
4470
Bram Moolenaar86b68352004-12-27 21:59:20 +00004471#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004472 p = skip_grep_pat(eap);
4473#else
4474 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004475#endif
4476
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004477 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004478 {
4479 if (*p == Ctrl_V)
4480 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004481 if (eap->argt & (EX_CTRLV | EX_XFILE))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004482 ++p; // skip CTRL-V and next char
Bram Moolenaar071d4272004-06-13 20:20:40 +00004483 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004484 // remove CTRL-V and skip next char
Bram Moolenaara7241f52008-06-24 20:39:31 +00004485 STRMOVE(p, p + 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004486 if (*p == NUL) // stop at NUL after CTRL-V
Bram Moolenaar071d4272004-06-13 20:20:40 +00004487 break;
4488 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004489
4490#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004491 // Skip over `=expr` when wildcards are expanded.
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004492 else if (p[0] == '`' && p[1] == '=' && (eap->argt & EX_XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004493 {
4494 p += 2;
4495 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004496 }
4497#endif
4498
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004499 // Check for '"': start of comment or '|': next command
4500 // :@" and :*" do not start a comment!
4501 // :redir @" doesn't either.
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004502 else if ((*p == '"' && !(eap->argt & EX_NOTRLCOM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004503 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
4504 || p != eap->arg)
4505 && (eap->cmdidx != CMD_redir
4506 || p != eap->arg + 1 || p[-1] != '@'))
Bram Moolenaara26b9702020-04-18 19:53:28 +02004507#ifdef FEAT_EVAL
4508 || (*p == '#' && in_vim9script()
4509 && p[1] != '{' && p > eap->cmd && VIM_ISWHITE(p[-1]))
4510#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004511 || *p == '|' || *p == '\n')
4512 {
4513 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004514 * We remove the '\' before the '|', unless EX_CTRLV is used
Bram Moolenaar071d4272004-06-13 20:20:40 +00004515 * AND 'b' is present in 'cpoptions'.
4516 */
4517 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004518 || !(eap->argt & EX_CTRLV)) && *(p - 1) == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004519 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004520 STRMOVE(p - 1, p); // remove the '\'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004521 --p;
4522 }
4523 else
4524 {
4525 eap->nextcmd = check_nextcmd(p);
4526 *p = NUL;
4527 break;
4528 }
4529 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004530 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004531
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004532 if (!(eap->argt & EX_NOTRLCOM)) // remove trailing spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00004533 del_trailing_spaces(eap->arg);
4534}
4535
4536/*
4537 * get + command from ex argument
4538 */
4539 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004540getargcmd(char_u **argp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004541{
4542 char_u *arg = *argp;
4543 char_u *command = NULL;
4544
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004545 if (*arg == '+') // +[command]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004546 {
4547 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02004548 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004549 command = dollar_command;
4550 else
4551 {
4552 command = arg;
4553 arg = skip_cmd_arg(command, TRUE);
4554 if (*arg != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004555 *arg++ = NUL; // terminate command with NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004556 }
4557
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004558 arg = skipwhite(arg); // skip over spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00004559 *argp = arg;
4560 }
4561 return command;
4562}
4563
4564/*
4565 * Find end of "+command" argument. Skip over "\ " and "\\".
4566 */
Bram Moolenaard0190392019-08-23 21:17:35 +02004567 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004568skip_cmd_arg(
4569 char_u *p,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004570 int rembs) // TRUE to halve the number of backslashes
Bram Moolenaar071d4272004-06-13 20:20:40 +00004571{
4572 while (*p && !vim_isspace(*p))
4573 {
4574 if (*p == '\\' && p[1] != NUL)
4575 {
4576 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00004577 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004578 else
4579 ++p;
4580 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004581 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004582 }
4583 return p;
4584}
4585
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004586 int
4587get_bad_opt(char_u *p, exarg_T *eap)
4588{
4589 if (STRICMP(p, "keep") == 0)
4590 eap->bad_char = BAD_KEEP;
4591 else if (STRICMP(p, "drop") == 0)
4592 eap->bad_char = BAD_DROP;
4593 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
4594 eap->bad_char = *p;
Bram Moolenaar75808492018-06-12 12:39:41 +02004595 else
4596 return FAIL;
4597 return OK;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004598}
4599
Bram Moolenaar071d4272004-06-13 20:20:40 +00004600/*
4601 * Get "++opt=arg" argument.
4602 * Return FAIL or OK.
4603 */
4604 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004605getargopt(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004606{
4607 char_u *arg = eap->arg + 2;
4608 int *pp = NULL;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004609 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004610 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004611
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004612 // ":edit ++[no]bin[ary] file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004613 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
4614 {
4615 if (*arg == 'n')
4616 {
4617 arg += 2;
4618 eap->force_bin = FORCE_NOBIN;
4619 }
4620 else
4621 eap->force_bin = FORCE_BIN;
4622 if (!checkforcmd(&arg, "binary", 3))
4623 return FAIL;
4624 eap->arg = skipwhite(arg);
4625 return OK;
4626 }
4627
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004628 // ":read ++edit file"
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004629 if (STRNCMP(arg, "edit", 4) == 0)
4630 {
4631 eap->read_edit = TRUE;
4632 eap->arg = skipwhite(arg + 4);
4633 return OK;
4634 }
4635
Bram Moolenaar071d4272004-06-13 20:20:40 +00004636 if (STRNCMP(arg, "ff", 2) == 0)
4637 {
4638 arg += 2;
4639 pp = &eap->force_ff;
4640 }
4641 else if (STRNCMP(arg, "fileformat", 10) == 0)
4642 {
4643 arg += 10;
4644 pp = &eap->force_ff;
4645 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004646 else if (STRNCMP(arg, "enc", 3) == 0)
4647 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01004648 if (STRNCMP(arg, "encoding", 8) == 0)
4649 arg += 8;
4650 else
4651 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004652 pp = &eap->force_enc;
4653 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004654 else if (STRNCMP(arg, "bad", 3) == 0)
4655 {
4656 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004657 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004658 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004659
4660 if (pp == NULL || *arg != '=')
4661 return FAIL;
4662
4663 ++arg;
4664 *pp = (int)(arg - eap->cmd);
4665 arg = skip_cmd_arg(arg, FALSE);
4666 eap->arg = skipwhite(arg);
4667 *arg = NUL;
4668
Bram Moolenaar071d4272004-06-13 20:20:40 +00004669 if (pp == &eap->force_ff)
4670 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004671 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
4672 return FAIL;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004673 eap->force_ff = eap->cmd[eap->force_ff];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004674 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004675 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004676 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004677 // Make 'fileencoding' lower case.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004678 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
4679 *p = TOLOWER_ASC(*p);
4680 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004681 else
4682 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004683 // Check ++bad= argument. Must be a single-byte character, "keep" or
4684 // "drop".
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004685 if (get_bad_opt(eap->cmd + bad_char_idx, eap) == FAIL)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004686 return FAIL;
4687 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004688
4689 return OK;
4690}
4691
Bram Moolenaar071d4272004-06-13 20:20:40 +00004692 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004693ex_autocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694{
4695 /*
Bram Moolenaar26d4b892018-07-04 22:26:28 +02004696 * Disallow autocommands from .exrc and .vimrc in current
Bram Moolenaar071d4272004-06-13 20:20:40 +00004697 * directory for security reasons.
4698 */
4699 if (secure)
4700 {
4701 secure = 2;
4702 eap->errmsg = e_curdir;
4703 }
4704 else if (eap->cmdidx == CMD_autocmd)
4705 do_autocmd(eap->arg, eap->forceit);
4706 else
4707 do_augroup(eap->arg, eap->forceit);
4708}
4709
4710/*
4711 * ":doautocmd": Apply the automatic commands to the current buffer.
4712 */
4713 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004714ex_doautocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004715{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01004716 char_u *arg = eap->arg;
4717 int call_do_modelines = check_nomodeline(&arg);
Bram Moolenaar1610d052016-06-09 22:53:01 +02004718 int did_aucmd;
Bram Moolenaar60542ac2012-02-12 20:14:01 +01004719
Bram Moolenaar1610d052016-06-09 22:53:01 +02004720 (void)do_doautocmd(arg, TRUE, &did_aucmd);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004721 // Only when there is no <nomodeline>.
Bram Moolenaar1610d052016-06-09 22:53:01 +02004722 if (call_do_modelines && did_aucmd)
Bram Moolenaar60542ac2012-02-12 20:14:01 +01004723 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004724}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004725
Bram Moolenaar071d4272004-06-13 20:20:40 +00004726/*
4727 * :[N]bunload[!] [N] [bufname] unload buffer
4728 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
4729 * :[N]bwipeout[!] [N] [bufname] delete buffer really
4730 */
4731 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004732ex_bunload(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004733{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004734 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02004735 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004736 eap->errmsg = do_bufdel(
4737 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
4738 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
4739 : DOBUF_UNLOAD, eap->arg,
4740 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
4741}
4742
4743/*
4744 * :[N]buffer [N] to buffer N
4745 * :[N]sbuffer [N] to buffer N
4746 */
4747 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004748ex_buffer(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004749{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004750 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02004751 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004752 if (*eap->arg)
4753 eap->errmsg = e_trailing;
4754 else
4755 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004756 if (eap->addr_count == 0) // default is current buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00004757 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
4758 else
4759 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004760 if (eap->do_ecmd_cmd != NULL)
4761 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004762 }
4763}
4764
4765/*
4766 * :[N]bmodified [N] to next mod. buffer
4767 * :[N]sbmodified [N] to next mod. buffer
4768 */
4769 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004770ex_bmodified(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004771{
4772 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004773 if (eap->do_ecmd_cmd != NULL)
4774 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004775}
4776
4777/*
4778 * :[N]bnext [N] to next buffer
4779 * :[N]sbnext [N] split and to next buffer
4780 */
4781 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004782ex_bnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004783{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004784 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01004785 return;
4786
Bram Moolenaar071d4272004-06-13 20:20:40 +00004787 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004788 if (eap->do_ecmd_cmd != NULL)
4789 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004790}
4791
4792/*
4793 * :[N]bNext [N] to previous buffer
4794 * :[N]bprevious [N] to previous buffer
4795 * :[N]sbNext [N] split and to previous buffer
4796 * :[N]sbprevious [N] split and to previous buffer
4797 */
4798 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004799ex_bprevious(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004800{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004801 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01004802 return;
4803
Bram Moolenaar071d4272004-06-13 20:20:40 +00004804 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004805 if (eap->do_ecmd_cmd != NULL)
4806 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004807}
4808
4809/*
4810 * :brewind to first buffer
4811 * :bfirst to first buffer
4812 * :sbrewind split and to first buffer
4813 * :sbfirst split and to first buffer
4814 */
4815 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004816ex_brewind(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004817{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004818 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01004819 return;
4820
Bram Moolenaar071d4272004-06-13 20:20:40 +00004821 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004822 if (eap->do_ecmd_cmd != NULL)
4823 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004824}
4825
4826/*
4827 * :blast to last buffer
4828 * :sblast split and to last buffer
4829 */
4830 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004831ex_blast(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004832{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004833 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01004834 return;
4835
Bram Moolenaar071d4272004-06-13 20:20:40 +00004836 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004837 if (eap->do_ecmd_cmd != NULL)
4838 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004839}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004840
Bram Moolenaar7a092242020-04-16 22:10:49 +02004841/*
4842 * Check if "c" ends an Ex command.
Bram Moolenaara494f562020-04-18 17:45:38 +02004843 * In Vim9 script does not check for white space before # or #{.
Bram Moolenaar7a092242020-04-16 22:10:49 +02004844 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004845 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004846ends_excmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004847{
Bram Moolenaarfbda69b2020-04-13 15:06:53 +02004848#ifdef FEAT_EVAL
Bram Moolenaar2c330432020-04-13 14:41:35 +02004849 if (c == '#')
Bram Moolenaar7a092242020-04-16 22:10:49 +02004850 return in_vim9script();
4851#endif
4852 return (c == NUL || c == '|' || c == '"' || c == '\n');
4853}
4854
4855/*
4856 * Like ends_excmd() but checks that a # in Vim9 script either has "cmd" equal
4857 * to "cmd_start" or has a white space character before it.
4858 */
4859 int
Bram Moolenaar21cfe502020-04-16 23:01:50 +02004860ends_excmd2(char_u *cmd_start UNUSED, char_u *cmd)
Bram Moolenaar7a092242020-04-16 22:10:49 +02004861{
4862 int c = *cmd;
4863
4864#ifdef FEAT_EVAL
Bram Moolenaara26b9702020-04-18 19:53:28 +02004865 if (c == '#' && cmd[1] != '{' && (cmd == cmd_start || VIM_ISWHITE(cmd[-1])))
Bram Moolenaar2c330432020-04-13 14:41:35 +02004866 return in_vim9script();
Bram Moolenaarfbda69b2020-04-13 15:06:53 +02004867#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004868 return (c == NUL || c == '|' || c == '"' || c == '\n');
4869}
4870
4871#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
4872 || defined(PROTO)
4873/*
4874 * Return the next command, after the first '|' or '\n'.
4875 * Return NULL if not found.
4876 */
4877 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004878find_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004879{
4880 while (*p != '|' && *p != '\n')
4881 {
4882 if (*p == NUL)
4883 return NULL;
4884 ++p;
4885 }
4886 return (p + 1);
4887}
4888#endif
4889
4890/*
Bram Moolenaar2256c992016-11-15 21:17:07 +01004891 * Check if *p is a separator between Ex commands, skipping over white space.
4892 * Return NULL if it isn't, the following character if it is.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004893 */
4894 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004895check_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004896{
Bram Moolenaar2256c992016-11-15 21:17:07 +01004897 char_u *s = skipwhite(p);
4898
4899 if (*s == '|' || *s == '\n')
4900 return (s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004901 else
4902 return NULL;
4903}
4904
4905/*
4906 * - if there are more files to edit
4907 * - and this is the last window
4908 * - and forceit not used
4909 * - and not repeated twice on a row
4910 * return FAIL and give error message if 'message' TRUE
4911 * return OK otherwise
4912 */
4913 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004914check_more(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004915 int message, // when FALSE check only, no messages
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004916 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004917{
4918 int n = ARGCOUNT - curwin->w_arg_idx - 1;
4919
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00004920 if (!forceit && only_one_window()
4921 && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004922 {
4923 if (message)
4924 {
4925#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
4926 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
4927 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02004928 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004929
Bram Moolenaarda6e8912018-08-21 15:12:14 +02004930 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
4931 NGETTEXT("%d more file to edit. Quit anyway?",
4932 "%d more files to edit. Quit anyway?", n), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004933 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
4934 return OK;
4935 return FAIL;
4936 }
4937#endif
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01004938 semsg(NGETTEXT("E173: %d more file to edit",
4939 "E173: %d more files to edit", n), n);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004940 quitmore = 2; // next try to quit is allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004941 }
4942 return FAIL;
4943 }
4944 return OK;
4945}
4946
Bram Moolenaar071d4272004-06-13 20:20:40 +00004947/*
4948 * Function given to ExpandGeneric() to obtain the list of command names.
4949 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004950 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004951get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004952{
4953 if (idx >= (int)CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954 return get_user_command_name(idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004955 return cmdnames[idx].cmd_name;
4956}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004957
Bram Moolenaar071d4272004-06-13 20:20:40 +00004958 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004959ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004960{
Bram Moolenaare6850792010-05-14 15:28:44 +02004961 if (*eap->arg == NUL)
4962 {
4963#ifdef FEAT_EVAL
4964 char_u *expr = vim_strsave((char_u *)"g:colors_name");
4965 char_u *p = NULL;
4966
4967 if (expr != NULL)
4968 {
4969 ++emsg_off;
Bram Moolenaarb171fb12020-06-24 20:34:03 +02004970 p = eval_to_string(expr, FALSE);
Bram Moolenaare6850792010-05-14 15:28:44 +02004971 --emsg_off;
4972 vim_free(expr);
4973 }
4974 if (p != NULL)
4975 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004976 msg((char *)p);
Bram Moolenaare6850792010-05-14 15:28:44 +02004977 vim_free(p);
4978 }
4979 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01004980 msg("default");
Bram Moolenaare6850792010-05-14 15:28:44 +02004981#else
Bram Moolenaar32526b32019-01-19 17:43:09 +01004982 msg(_("unknown"));
Bram Moolenaare6850792010-05-14 15:28:44 +02004983#endif
4984 }
4985 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004986 semsg(_("E185: Cannot find color scheme '%s'"), eap->arg);
Bram Moolenaarf58d81a2019-01-28 20:19:05 +01004987
4988#ifdef FEAT_VTP
4989 else if (has_vtp_working())
4990 {
4991 // background color change requires clear + redraw
4992 update_screen(CLEAR);
4993 redrawcmd();
4994 }
4995#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996}
4997
4998 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004999ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005000{
5001 if (*eap->arg == NUL && eap->cmd[2] == '!')
Bram Moolenaar32526b32019-01-19 17:43:09 +01005002 msg(_("Greetings, Vim user!"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005003 do_highlight(eap->arg, eap->forceit, FALSE);
5004}
5005
5006
5007/*
5008 * Call this function if we thought we were going to exit, but we won't
5009 * (because of an error). May need to restore the terminal mode.
5010 */
5011 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005012not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005013{
5014 exiting = FALSE;
5015 settmode(TMODE_RAW);
5016}
5017
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005018 static int
5019before_quit_autocmds(win_T *wp, int quit_all, int forceit)
5020{
5021 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, wp->w_buffer);
5022
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02005023 // Bail out when autocommands closed the window.
5024 // Refuse to quit when the buffer in the last window is being closed (can
5025 // only happen in autocommands).
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005026 if (!win_valid(wp)
5027 || curbuf_locked()
5028 || (wp->w_buffer->b_nwindows == 1 && wp->w_buffer->b_locked > 0))
5029 return TRUE;
5030
5031 if (quit_all || (check_more(FALSE, forceit) == OK && only_one_window()))
5032 {
5033 apply_autocmds(EVENT_EXITPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02005034 // Refuse to quit when locked or when the window was closed or the
5035 // buffer in the last window is being closed (can only happen in
5036 // autocommands).
5037 if (!win_valid(wp) || curbuf_locked()
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005038 || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
5039 return TRUE;
5040 }
5041
5042 return FALSE;
5043}
5044
Bram Moolenaar071d4272004-06-13 20:20:40 +00005045/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01005046 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005047 * ":{nr}quit": quit window {nr}
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02005048 * Also used when closing a terminal window that's the last one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005049 */
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02005050 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005051ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005053 win_T *wp;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005054
Bram Moolenaar071d4272004-06-13 20:20:40 +00005055#ifdef FEAT_CMDWIN
5056 if (cmdwin_type != 0)
5057 {
5058 cmdwin_result = Ctrl_C;
5059 return;
5060 }
5061#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005062 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005063 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005064 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005065 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005066 return;
5067 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005068 if (eap->addr_count > 0)
5069 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01005070 int wnr = eap->line2;
5071
5072 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
5073 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005074 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005075 }
5076 else
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005077 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01005078
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005079 // Refuse to quit when locked.
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02005080 if (curbuf_locked())
5081 return;
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005082
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005083 // Trigger QuitPre and maybe ExitPre
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005084 if (before_quit_autocmds(wp, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005085 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086
5087#ifdef FEAT_NETBEANS_INTG
5088 netbeansForcedQuit = eap->forceit;
5089#endif
5090
5091 /*
5092 * If there are more files or windows we won't exit.
5093 */
5094 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
5095 exiting = TRUE;
Bram Moolenaarff930ca2017-10-19 17:12:10 +02005096 if ((!buf_hide(wp->w_buffer)
5097 && check_changed(wp->w_buffer, (p_awa ? CCGD_AW : 0)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01005098 | (eap->forceit ? CCGD_FORCEIT : 0)
5099 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005100 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01005101 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005102 {
5103 not_exiting();
5104 }
5105 else
5106 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005107 // quit last window
5108 // Note: only_one_window() returns true, even so a help window is
5109 // still open. In that case only quit, if no address has been
5110 // specified. Example:
5111 // :h|wincmd w|1q - don't quit
5112 // :h|wincmd w|q - quit
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01005113 if (only_one_window() && (ONE_WINDOW || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005114 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02005115 not_exiting();
Bram Moolenaar4033c552017-09-16 20:54:51 +02005116#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005117 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005118#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005119 // close window; may free buffer
Bram Moolenaareb44a682017-08-03 22:44:55 +02005120 win_close(wp, !buf_hide(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005121 }
5122}
5123
5124/*
5125 * ":cquit".
5126 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005127 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005128ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005129{
Bram Moolenaar1860bde2020-01-06 21:47:21 +01005130 // this does not always pass on the exit code to the Manx compiler. why?
5131 getout(eap->addr_count > 0 ? (int)eap->line2 : EXIT_FAILURE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005132}
5133
5134/*
5135 * ":qall": try to quit all windows
5136 */
5137 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005138ex_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005139{
5140# ifdef FEAT_CMDWIN
5141 if (cmdwin_type != 0)
5142 {
5143 if (eap->forceit)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005144 cmdwin_result = K_XF1; // ex_window() takes care of this
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145 else
5146 cmdwin_result = K_XF2;
5147 return;
5148 }
5149# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005150
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005151 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005152 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005153 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005154 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005155 return;
5156 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005157
5158 if (before_quit_autocmds(curwin, TRUE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005159 return;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005160
Bram Moolenaar071d4272004-06-13 20:20:40 +00005161 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01005162 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005163 getout(0);
5164 not_exiting();
5165}
5166
Bram Moolenaar071d4272004-06-13 20:20:40 +00005167/*
5168 * ":close": close current window, unless it is the last one
5169 */
5170 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005171ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005172{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005173 win_T *win;
5174 int winnr = 0;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005175#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00005176 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02005177 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005178 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005179#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005180 if (!text_locked() && !curbuf_locked())
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005181 {
5182 if (eap->addr_count == 0)
5183 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar6d41c782018-06-06 09:11:12 +02005184 else
5185 {
Bram Moolenaar29323592016-07-24 22:04:11 +02005186 FOR_ALL_WINDOWS(win)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005187 {
5188 winnr++;
5189 if (winnr == eap->line2)
5190 break;
5191 }
5192 if (win == NULL)
5193 win = lastwin;
5194 ex_win_close(eap->forceit, win, NULL);
5195 }
5196 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005197}
5198
Bram Moolenaar4033c552017-09-16 20:54:51 +02005199#ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005200/*
5201 * ":pclose": Close any preview window.
5202 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005204ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005205{
5206 win_T *win;
5207
Bram Moolenaar79648732019-07-18 21:43:07 +02005208 // First close any normal window.
Bram Moolenaar29323592016-07-24 22:04:11 +02005209 FOR_ALL_WINDOWS(win)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005210 if (win->w_p_pvw)
5211 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005212 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar79648732019-07-18 21:43:07 +02005213 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005214 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005215# ifdef FEAT_PROP_POPUP
Bram Moolenaar79648732019-07-18 21:43:07 +02005216 // Also when 'previewpopup' is empty, it might have been cleared.
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02005217 popup_close_preview();
Bram Moolenaar79648732019-07-18 21:43:07 +02005218# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005219}
Bram Moolenaar4033c552017-09-16 20:54:51 +02005220#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005221
Bram Moolenaarf740b292006-02-16 22:11:02 +00005222/*
5223 * Close window "win" and take care of handling closing the last window for a
5224 * modified buffer.
5225 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005226 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005227ex_win_close(
5228 int forceit,
5229 win_T *win,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005230 tabpage_T *tp) // NULL or the tab page "win" is in
Bram Moolenaar071d4272004-06-13 20:20:40 +00005231{
5232 int need_hide;
5233 buf_T *buf = win->w_buffer;
5234
Bram Moolenaarcf844172020-06-26 19:44:06 +02005235 // Never close the autocommand window.
5236 if (win == aucmd_win)
5237 {
5238 emsg(_(e_autocmd_close));
5239 return;
5240 }
5241
Bram Moolenaar071d4272004-06-13 20:20:40 +00005242 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02005243 if (need_hide && !buf_hide(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005244 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02005245#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005246 if ((p_confirm || cmdmod.confirm) && p_write)
5247 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005248 bufref_T bufref;
5249
5250 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005251 dialog_changed(buf, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005252 if (bufref_valid(&bufref) && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005253 return;
5254 need_hide = FALSE;
5255 }
5256 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005257#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005258 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02005259 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005260 return;
5261 }
5262 }
5263
Bram Moolenaar4033c552017-09-16 20:54:51 +02005264#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005265 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005266#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005267
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005268 // free buffer when not hiding it or when it's a scratch buffer
Bram Moolenaarf740b292006-02-16 22:11:02 +00005269 if (tp == NULL)
Bram Moolenaareb44a682017-08-03 22:44:55 +02005270 win_close(win, !need_hide && !buf_hide(buf));
Bram Moolenaarf740b292006-02-16 22:11:02 +00005271 else
Bram Moolenaareb44a682017-08-03 22:44:55 +02005272 win_close_othertab(win, !need_hide && !buf_hide(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005273}
5274
Bram Moolenaar071d4272004-06-13 20:20:40 +00005275/*
Bram Moolenaardea25702017-01-29 15:18:10 +01005276 * Handle the argument for a tabpage related ex command.
5277 * Returns a tabpage number.
5278 * When an error is encountered then eap->errmsg is set.
5279 */
5280 static int
5281get_tabpage_arg(exarg_T *eap)
5282{
5283 int tab_number;
5284 int unaccept_arg0 = (eap->cmdidx == CMD_tabmove) ? 0 : 1;
5285
5286 if (eap->arg && *eap->arg != NUL)
5287 {
5288 char_u *p = eap->arg;
5289 char_u *p_save;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005290 int relative = 0; // argument +N/-N means: go to N places to the
5291 // right/left relative to the current position.
Bram Moolenaardea25702017-01-29 15:18:10 +01005292
5293 if (*p == '-')
5294 {
5295 relative = -1;
5296 p++;
5297 }
5298 else if (*p == '+')
5299 {
5300 relative = 1;
5301 p++;
5302 }
5303
5304 p_save = p;
5305 tab_number = getdigits(&p);
5306
5307 if (relative == 0)
5308 {
5309 if (STRCMP(p, "$") == 0)
5310 tab_number = LAST_TAB_NR;
5311 else if (p == p_save || *p_save == '-' || *p != NUL
5312 || tab_number > LAST_TAB_NR)
5313 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005314 // No numbers as argument.
Bram Moolenaardea25702017-01-29 15:18:10 +01005315 eap->errmsg = e_invarg;
5316 goto theend;
5317 }
5318 }
5319 else
5320 {
5321 if (*p_save == NUL)
5322 tab_number = 1;
5323 else if (p == p_save || *p_save == '-' || *p != NUL
5324 || tab_number == 0)
5325 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005326 // No numbers as argument.
Bram Moolenaardea25702017-01-29 15:18:10 +01005327 eap->errmsg = e_invarg;
5328 goto theend;
5329 }
5330 tab_number = tab_number * relative + tabpage_index(curtab);
5331 if (!unaccept_arg0 && relative == -1)
5332 --tab_number;
5333 }
5334 if (tab_number < unaccept_arg0 || tab_number > LAST_TAB_NR)
5335 eap->errmsg = e_invarg;
5336 }
5337 else if (eap->addr_count > 0)
5338 {
5339 if (unaccept_arg0 && eap->line2 == 0)
Bram Moolenaarc6251552017-01-29 21:42:20 +01005340 {
Bram Moolenaardea25702017-01-29 15:18:10 +01005341 eap->errmsg = e_invrange;
Bram Moolenaarc6251552017-01-29 21:42:20 +01005342 tab_number = 0;
5343 }
Bram Moolenaardea25702017-01-29 15:18:10 +01005344 else
5345 {
5346 tab_number = eap->line2;
Bram Moolenaar82a12462019-01-22 22:41:42 +01005347 if (!unaccept_arg0 && *skipwhite(*eap->cmdlinep) == '-')
Bram Moolenaardea25702017-01-29 15:18:10 +01005348 {
5349 --tab_number;
5350 if (tab_number < unaccept_arg0)
5351 eap->errmsg = e_invarg;
5352 }
5353 }
5354 }
5355 else
5356 {
5357 switch (eap->cmdidx)
5358 {
5359 case CMD_tabnext:
5360 tab_number = tabpage_index(curtab) + 1;
5361 if (tab_number > LAST_TAB_NR)
5362 tab_number = 1;
5363 break;
5364 case CMD_tabmove:
5365 tab_number = LAST_TAB_NR;
5366 break;
5367 default:
5368 tab_number = tabpage_index(curtab);
5369 }
5370 }
5371
5372theend:
5373 return tab_number;
5374}
5375
5376/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00005377 * ":tabclose": close current tab page, unless it is the last one.
5378 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005379 */
5380 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005381ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005382{
Bram Moolenaarf740b292006-02-16 22:11:02 +00005383 tabpage_T *tp;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005384 int tab_number;
Bram Moolenaarf740b292006-02-16 22:11:02 +00005385
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005386# ifdef FEAT_CMDWIN
5387 if (cmdwin_type != 0)
5388 cmdwin_result = K_IGNORE;
5389 else
5390# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005391 if (first_tabpage->tp_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005392 emsg(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00005393 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005394 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005395 tab_number = get_tabpage_arg(eap);
5396 if (eap->errmsg == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005397 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005398 tp = find_tabpage(tab_number);
Bram Moolenaarf740b292006-02-16 22:11:02 +00005399 if (tp == NULL)
5400 {
5401 beep_flush();
5402 return;
5403 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005404 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005405 {
5406 tabpage_close_other(tp, eap->forceit);
5407 return;
5408 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005409 else if (!text_locked() && !curbuf_locked())
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005410 tabpage_close(eap->forceit);
5411 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005412 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005413}
5414
5415/*
5416 * ":tabonly": close all tab pages except the current one
5417 */
5418 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005419ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005420{
5421 tabpage_T *tp;
5422 int done;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005423 int tab_number;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005424
5425# ifdef FEAT_CMDWIN
5426 if (cmdwin_type != 0)
5427 cmdwin_result = K_IGNORE;
5428 else
5429# endif
5430 if (first_tabpage->tp_next == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005431 msg(_("Already only one tab page"));
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005432 else
5433 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005434 tab_number = get_tabpage_arg(eap);
5435 if (eap->errmsg == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005436 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005437 goto_tabpage(tab_number);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005438 // Repeat this up to a 1000 times, because autocommands may
5439 // mess up the lists.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005440 for (done = 0; done < 1000; ++done)
5441 {
5442 FOR_ALL_TABPAGES(tp)
5443 if (tp->tp_topframe != topframe)
5444 {
5445 tabpage_close_other(tp, eap->forceit);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005446 // if we failed to close it quit
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005447 if (valid_tabpage(tp))
5448 done = 1000;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005449 // start over, "tp" is now invalid
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005450 break;
5451 }
5452 if (first_tabpage->tp_next == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005453 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005454 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005455 }
5456 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005457}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005458
5459/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00005460 * Close the current tab page.
5461 */
5462 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005463tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005464{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005465 // First close all the windows but the current one. If that worked then
5466 // close the last window in this tab, that will close it.
Bram Moolenaar459ca562016-11-10 18:16:33 +01005467 if (!ONE_WINDOW)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005468 close_others(TRUE, forceit);
Bram Moolenaar459ca562016-11-10 18:16:33 +01005469 if (ONE_WINDOW)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005470 ex_win_close(forceit, curwin, NULL);
5471# ifdef FEAT_GUI
5472 need_mouse_correct = TRUE;
5473# endif
5474}
5475
5476/*
5477 * Close tab page "tp", which is not the current tab page.
5478 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005479 * Also takes care of the tab pages line disappearing when closing the
5480 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00005481 */
5482 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005483tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005484{
5485 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005486 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005487 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00005488
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005489 // Limit to 1000 windows, autocommands may add a window while we close
5490 // one. OK, so I'm paranoid...
Bram Moolenaarf740b292006-02-16 22:11:02 +00005491 while (++done < 1000)
5492 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005493 wp = tp->tp_firstwin;
5494 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00005495
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005496 // Autocommands may delete the tab page under our fingers and we may
5497 // fail to close a window with a modified buffer.
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005498 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005499 break;
5500 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005501
Bram Moolenaar29323592016-07-24 22:04:11 +02005502 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar29323592016-07-24 22:04:11 +02005503
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005504 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005505 if (h != tabline_height())
5506 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00005507}
5508
5509/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005510 * ":only".
5511 */
5512 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005513ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005514{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005515 win_T *wp;
5516 int wnr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005517# ifdef FEAT_GUI
5518 need_mouse_correct = TRUE;
5519# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005520 if (eap->addr_count > 0)
5521 {
5522 wnr = eap->line2;
5523 for (wp = firstwin; --wnr > 0; )
5524 {
5525 if (wp->w_next == NULL)
5526 break;
5527 else
5528 wp = wp->w_next;
5529 }
5530 win_goto(wp);
5531 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005532 close_others(TRUE, eap->forceit);
5533}
5534
Bram Moolenaar071d4272004-06-13 20:20:40 +00005535 static void
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01005536ex_hide(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005537{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005538 // ":hide" or ":hide | cmd": hide current window
Bram Moolenaar2256c992016-11-15 21:17:07 +01005539 if (!eap->skip)
5540 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02005541#ifdef FEAT_GUI
Bram Moolenaar2256c992016-11-15 21:17:07 +01005542 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005543#endif
Bram Moolenaar2256c992016-11-15 21:17:07 +01005544 if (eap->addr_count == 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005545 win_close(curwin, FALSE); // don't free buffer
Bram Moolenaar2256c992016-11-15 21:17:07 +01005546 else
5547 {
5548 int winnr = 0;
5549 win_T *win;
Bram Moolenaarf240e182014-11-27 18:33:02 +01005550
Bram Moolenaar2256c992016-11-15 21:17:07 +01005551 FOR_ALL_WINDOWS(win)
5552 {
5553 winnr++;
5554 if (winnr == eap->line2)
5555 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005556 }
Bram Moolenaar2256c992016-11-15 21:17:07 +01005557 if (win == NULL)
5558 win = lastwin;
5559 win_close(win, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005560 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005561 }
5562}
5563
5564/*
5565 * ":stop" and ":suspend": Suspend Vim.
5566 */
5567 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005568ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005569{
5570 /*
5571 * Disallow suspending for "rvim".
5572 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005573 if (!check_restricted())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005574 {
5575 if (!eap->forceit)
5576 autowrite_all();
5577 windgoto((int)Rows - 1, 0);
5578 out_char('\n');
5579 out_flush();
5580 stoptermcap();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005581 out_flush(); // needed for SUN to restore xterm buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00005582#ifdef FEAT_TITLE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005583 mch_restore_title(SAVE_RESTORE_BOTH); // restore window titles
Bram Moolenaar071d4272004-06-13 20:20:40 +00005584#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005585 ui_suspend(); // call machine specific function
Bram Moolenaar071d4272004-06-13 20:20:40 +00005586#ifdef FEAT_TITLE
5587 maketitle();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005588 resettitle(); // force updating the title
Bram Moolenaar071d4272004-06-13 20:20:40 +00005589#endif
5590 starttermcap();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005591 scroll_start(); // scroll screen before redrawing
Bram Moolenaar071d4272004-06-13 20:20:40 +00005592 redraw_later_clear();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005593 shell_resized(); // may have resized window
Bram Moolenaar071d4272004-06-13 20:20:40 +00005594 }
5595}
5596
5597/*
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005598 * ":exit", ":xit" and ":wq": Write file and quite the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005599 */
5600 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005601ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005602{
5603#ifdef FEAT_CMDWIN
5604 if (cmdwin_type != 0)
5605 {
5606 cmdwin_result = Ctrl_C;
5607 return;
5608 }
5609#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005610 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005611 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005612 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005613 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005614 return;
5615 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005616
5617 if (before_quit_autocmds(curwin, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005618 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005619
5620 /*
5621 * if more files or windows we won't exit
5622 */
5623 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
5624 exiting = TRUE;
5625 if ( ((eap->cmdidx == CMD_wq
5626 || curbufIsChanged())
5627 && do_write(eap) == FAIL)
5628 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01005629 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005630 {
5631 not_exiting();
5632 }
5633 else
5634 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005635 if (only_one_window()) // quit last window, exit Vim
Bram Moolenaar071d4272004-06-13 20:20:40 +00005636 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02005637 not_exiting();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005638# ifdef FEAT_GUI
5639 need_mouse_correct = TRUE;
5640# endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005641 // Quit current window, may free the buffer.
Bram Moolenaareb44a682017-08-03 22:44:55 +02005642 win_close(curwin, !buf_hide(curwin->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005643 }
5644}
5645
5646/*
5647 * ":print", ":list", ":number".
5648 */
5649 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005650ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005651{
Bram Moolenaardf177f62005-02-22 08:39:57 +00005652 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005653 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +00005654 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005655 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00005656 for ( ;!got_int; ui_breakcheck())
5657 {
5658 print_line(eap->line1,
5659 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
5660 || (eap->flags & EXFLAG_NR)),
5661 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
5662 if (++eap->line1 > eap->line2)
5663 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005664 out_flush(); // show one line at a time
Bram Moolenaardf177f62005-02-22 08:39:57 +00005665 }
5666 setpcmark();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005667 // put cursor at last line
Bram Moolenaardf177f62005-02-22 08:39:57 +00005668 curwin->w_cursor.lnum = eap->line2;
5669 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005670 }
5671
Bram Moolenaar071d4272004-06-13 20:20:40 +00005672 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005673}
5674
5675#ifdef FEAT_BYTEOFF
5676 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005677ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005678{
5679 goto_byte(eap->line2);
5680}
5681#endif
5682
5683/*
5684 * ":shell".
5685 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005686 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005687ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005688{
5689 do_shell(NULL, 0);
5690}
5691
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005692#if defined(HAVE_DROP_FILE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005693
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005694static int drop_busy = FALSE;
5695static int drop_filec;
5696static char_u **drop_filev = NULL;
5697static int drop_split;
5698static void (*drop_callback)(void *);
5699static void *drop_cookie;
5700
5701 static void
5702handle_drop_internal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005703{
5704 exarg_T ea;
5705 int save_msg_scroll = msg_scroll;
5706
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005707 // Setting the argument list may cause screen updates and being called
5708 // recursively. Avoid that by setting drop_busy.
5709 drop_busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005710
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005711 // Check whether the current buffer is changed. If so, we will need
5712 // to split the current window or data could be lost.
5713 // We don't need to check if the 'hidden' option is set, as in this
5714 // case the buffer won't be lost.
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005715 if (!buf_hide(curbuf) && !drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005716 {
5717 ++emsg_off;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005718 drop_split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005719 --emsg_off;
5720 }
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005721 if (drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005722 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005723 if (win_split(0, 0) == FAIL)
5724 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02005725 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005726
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005727 // When splitting the window, create a new alist. Otherwise the
5728 // existing one is overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005729 alist_unlink(curwin->w_alist);
5730 alist_new();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005731 }
5732
5733 /*
5734 * Set up the new argument list.
5735 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005736 alist_set(ALIST(curwin), drop_filec, drop_filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005737
5738 /*
5739 * Move to the first file.
5740 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005741 // Fake up a minimal "next" command for do_argfile()
Bram Moolenaara80faa82020-04-12 19:37:17 +02005742 CLEAR_FIELD(ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005743 ea.cmd = (char_u *)"next";
5744 do_argfile(&ea, 0);
5745
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005746 // do_ecmd() may set need_start_insertmode, but since we never left Insert
5747 // mode that is not needed here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005748 need_start_insertmode = FALSE;
5749
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005750 // Restore msg_scroll, otherwise a following command may cause scrolling
5751 // unexpectedly. The screen will be redrawn by the caller, thus
5752 // msg_scroll being set by displaying a message is irrelevant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005753 msg_scroll = save_msg_scroll;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005754
5755 if (drop_callback != NULL)
5756 drop_callback(drop_cookie);
5757
5758 drop_filev = NULL;
5759 drop_busy = FALSE;
5760}
5761
5762/*
5763 * Handle a file drop. The code is here because a drop is *nearly* like an
5764 * :args command, but not quite (we have a list of exact filenames, so we
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01005765 * don't want to (a) parse a command line, or (b) expand wildcards). So the
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005766 * code is very similar to :args and hence needs access to a lot of the static
5767 * functions in this file.
5768 *
5769 * The "filev" list must have been allocated using alloc(), as should each item
5770 * in the list. This function takes over responsibility for freeing the "filev"
5771 * list.
5772 */
5773 void
5774handle_drop(
5775 int filec, // the number of files dropped
5776 char_u **filev, // the list of files dropped
5777 int split, // force splitting the window
5778 void (*callback)(void *), // to be called after setting the argument
5779 // list
5780 void *cookie) // argument for "callback" (allocated)
5781{
5782 // Cannot handle recursive drops, finish the pending one.
5783 if (drop_busy)
5784 {
5785 FreeWild(filec, filev);
5786 vim_free(cookie);
5787 return;
5788 }
5789
5790 // When calling handle_drop() more than once in a row we only use the last
5791 // one.
5792 if (drop_filev != NULL)
5793 {
5794 FreeWild(drop_filec, drop_filev);
5795 vim_free(drop_cookie);
5796 }
5797
5798 drop_filec = filec;
5799 drop_filev = filev;
5800 drop_split = split;
5801 drop_callback = callback;
5802 drop_cookie = cookie;
5803
5804 // Postpone this when:
5805 // - editing the command line
5806 // - not possible to change the current buffer
5807 // - updating the screen
5808 // As it may change buffers and window structures that are in use and cause
5809 // freed memory to be used.
5810 if (text_locked() || curbuf_locked() || updating_screen)
5811 return;
5812
5813 handle_drop_internal();
5814}
5815
5816/*
5817 * To be called when text is unlocked, curbuf is unlocked or updating_screen is
5818 * reset: Handle a postponed drop.
5819 */
5820 void
5821handle_any_postponed_drop(void)
5822{
5823 if (!drop_busy && drop_filev != NULL
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02005824 && !text_locked() && !curbuf_locked() && !updating_screen)
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005825 handle_drop_internal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005826}
5827#endif
5828
Bram Moolenaar071d4272004-06-13 20:20:40 +00005829/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005830 * ":preserve".
5831 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005832 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005833ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005834{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005835 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005836 ml_preserve(curbuf, TRUE);
5837}
5838
5839/*
5840 * ":recover".
5841 */
5842 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005843ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005844{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005845 // Set recoverymode right away to avoid the ATTENTION prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005846 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01005847 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
5848 | CCGD_MULTWIN
5849 | (eap->forceit ? CCGD_FORCEIT : 0)
5850 | CCGD_EXCMD)
5851
Bram Moolenaar071d4272004-06-13 20:20:40 +00005852 && (*eap->arg == NUL
5853 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
Bram Moolenaar99499b12019-05-23 21:35:48 +02005854 ml_recover(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005855 recoverymode = FALSE;
5856}
5857
5858/*
5859 * Command modifier used in a wrong way.
5860 */
5861 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005862ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005863{
5864 eap->errmsg = e_invcmd;
5865}
5866
Bram Moolenaar071d4272004-06-13 20:20:40 +00005867/*
5868 * :sview [+command] file split window with new file, read-only
5869 * :split [[+command] file] split window with current or new file
5870 * :vsplit [[+command] file] split window vertically with current or new file
5871 * :new [[+command] file] split window with no or new file
5872 * :vnew [[+command] file] split vertically window with no or new file
5873 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005874 *
5875 * :tabedit open new Tab page with empty window
5876 * :tabedit [+command] file open new Tab page and edit "file"
5877 * :tabnew [[+command] file] just like :tabedit
5878 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005879 */
5880 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005881ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005882{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005883 win_T *old_curwin = curwin;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005884#if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005885 char_u *fname = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005886#endif
5887#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005888 int browse_flag = cmdmod.browse;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005889#endif
Bram Moolenaar39902a02018-06-21 22:10:08 +02005890 int use_tab = eap->cmdidx == CMD_tabedit
5891 || eap->cmdidx == CMD_tabfind
5892 || eap->cmdidx == CMD_tabnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005893
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005894 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005895 return;
5896
Bram Moolenaar4033c552017-09-16 20:54:51 +02005897#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005898 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005899#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005900
Bram Moolenaar4033c552017-09-16 20:54:51 +02005901#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005902 // A ":split" in the quickfix window works like ":new". Don't want two
5903 // quickfix windows. But it's OK when doing ":tab split".
Bram Moolenaar05bb9532008-07-04 09:44:11 +00005904 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005905 {
5906 if (eap->cmdidx == CMD_split)
5907 eap->cmdidx = CMD_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005908 if (eap->cmdidx == CMD_vsplit)
5909 eap->cmdidx = CMD_vnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005910 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02005911#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005912
Bram Moolenaar4033c552017-09-16 20:54:51 +02005913#ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005914 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005915 {
5916 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
5917 FNAME_MESS, TRUE, curbuf->b_ffname);
5918 if (fname == NULL)
5919 goto theend;
5920 eap->arg = fname;
5921 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005922# ifdef FEAT_BROWSE
Bram Moolenaar4033c552017-09-16 20:54:51 +02005923 else
5924# endif
5925#endif
5926#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005927 if (cmdmod.browse
Bram Moolenaar071d4272004-06-13 20:20:40 +00005928 && eap->cmdidx != CMD_vnew
Bram Moolenaar071d4272004-06-13 20:20:40 +00005929 && eap->cmdidx != CMD_new)
5930 {
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005931 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005932# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005933 !gui.in_use &&
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005934# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005935 au_has_group((char_u *)"FileExplorer"))
5936 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005937 // No browsing supported but we do have the file explorer:
5938 // Edit the directory.
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005939 if (*eap->arg == NUL || !mch_isdir(eap->arg))
5940 eap->arg = (char_u *)".";
5941 }
5942 else
5943 {
Bram Moolenaar39902a02018-06-21 22:10:08 +02005944 fname = do_browse(0, (char_u *)(use_tab
5945 ? _("Edit File in new tab page")
5946 : _("Edit File in new window")),
Bram Moolenaar071d4272004-06-13 20:20:40 +00005947 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005948 if (fname == NULL)
5949 goto theend;
5950 eap->arg = fname;
5951 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005952 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005953 cmdmod.browse = FALSE; // Don't browse again in do_ecmd().
Bram Moolenaar4033c552017-09-16 20:54:51 +02005954#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005955
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005956 /*
5957 * Either open new tab page or split the window.
5958 */
Bram Moolenaar39902a02018-06-21 22:10:08 +02005959 if (use_tab)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005960 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005961 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
5962 : eap->addr_count == 0 ? 0
5963 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005964 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00005965 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005966
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005967 // set the alternate buffer for the window we came from
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005968 if (curwin != old_curwin
5969 && win_valid(old_curwin)
5970 && old_curwin->w_buffer != curbuf
5971 && !cmdmod.keepalt)
5972 old_curwin->w_alt_fnum = curbuf->b_fnum;
5973 }
5974 }
5975 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005976 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
5977 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005978 // Reset 'scrollbind' when editing another file, but keep it when
5979 // doing ":split" without arguments.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005980 if (*eap->arg != NUL
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01005981# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005982 || cmdmod.browse
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01005983# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005984 )
Bram Moolenaar3368ea22010-09-21 16:56:35 +02005985 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005986 else
5987 do_check_scrollbind(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005988 do_exedit(eap, old_curwin);
5989 }
5990
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005991# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005992 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005993# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005994
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005995# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005996theend:
5997 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005998# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005999}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006000
6001/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006002 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006003 */
6004 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006005tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006006{
6007 exarg_T ea;
6008
Bram Moolenaara80faa82020-04-12 19:37:17 +02006009 CLEAR_FIELD(ea);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006010 ea.cmdidx = CMD_tabnew;
6011 ea.cmd = (char_u *)"tabn";
6012 ea.arg = (char_u *)"";
6013 ex_splitview(&ea);
6014}
6015
6016/*
6017 * :tabnext command
6018 */
6019 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006020ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006021{
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006022 int tab_number;
6023
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02006024 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006025 return;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006026 switch (eap->cmdidx)
6027 {
6028 case CMD_tabfirst:
6029 case CMD_tabrewind:
6030 goto_tabpage(1);
6031 break;
6032 case CMD_tablast:
6033 goto_tabpage(9999);
6034 break;
6035 case CMD_tabprevious:
6036 case CMD_tabNext:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006037 if (eap->arg && *eap->arg != NUL)
6038 {
6039 char_u *p = eap->arg;
6040 char_u *p_save = p;
6041
6042 tab_number = getdigits(&p);
6043 if (p == p_save || *p_save == '-' || *p != NUL
6044 || tab_number == 0)
6045 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006046 // No numbers as argument.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006047 eap->errmsg = e_invarg;
6048 return;
6049 }
6050 }
6051 else
6052 {
6053 if (eap->addr_count == 0)
6054 tab_number = 1;
6055 else
6056 {
6057 tab_number = eap->line2;
6058 if (tab_number < 1)
6059 {
6060 eap->errmsg = e_invrange;
6061 return;
6062 }
6063 }
6064 }
6065 goto_tabpage(-tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006066 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006067 default: // CMD_tabnext
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006068 tab_number = get_tabpage_arg(eap);
6069 if (eap->errmsg == NULL)
6070 goto_tabpage(tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006071 break;
6072 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006073}
6074
6075/*
6076 * :tabmove command
6077 */
6078 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006079ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006080{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02006081 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02006082
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006083 tab_number = get_tabpage_arg(eap);
6084 if (eap->errmsg == NULL)
6085 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006086}
6087
6088/*
6089 * :tabs command: List tabs and their contents.
6090 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006091 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006092ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006093{
6094 tabpage_T *tp;
6095 win_T *wp;
6096 int tabcount = 1;
6097
6098 msg_start();
6099 msg_scroll = TRUE;
6100 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
6101 {
6102 msg_putchar('\n');
6103 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
Bram Moolenaar8820b482017-03-16 17:23:31 +01006104 msg_outtrans_attr(IObuff, HL_ATTR(HLF_T));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006105 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006106 ui_breakcheck();
6107
Bram Moolenaar030f0df2006-02-21 22:02:53 +00006108 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006109 wp = firstwin;
6110 else
6111 wp = tp->tp_firstwin;
6112 for ( ; wp != NULL && !got_int; wp = wp->w_next)
6113 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006114 msg_putchar('\n');
6115 msg_putchar(wp == curwin ? '>' : ' ');
6116 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006117 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
6118 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006119 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02006120 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006121 else
6122 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
6123 IObuff, IOSIZE, TRUE);
6124 msg_outtrans(IObuff);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006125 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006126 ui_breakcheck();
6127 }
6128 }
6129}
6130
Bram Moolenaar071d4272004-06-13 20:20:40 +00006131/*
6132 * ":mode": Set screen mode.
6133 * If no argument given, just get the screen size and redraw.
6134 */
6135 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006136ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006137{
6138 if (*eap->arg == NUL)
6139 shell_resized();
6140 else
Bram Moolenaar3c71aec2020-01-17 19:32:20 +01006141 emsg(_(e_screenmode));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006142}
6143
Bram Moolenaar071d4272004-06-13 20:20:40 +00006144/*
6145 * ":resize".
6146 * set, increment or decrement current window height
6147 */
6148 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006149ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006150{
6151 int n;
6152 win_T *wp = curwin;
6153
6154 if (eap->addr_count > 0)
6155 {
6156 n = eap->line2;
6157 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
6158 ;
6159 }
6160
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006161# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006162 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006163# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006164 n = atol((char *)eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006165 if (cmdmod.split & WSP_VERT)
6166 {
6167 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar02631462017-09-22 15:20:32 +02006168 n += curwin->w_width;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006169 else if (n == 0 && eap->arg[0] == NUL) // default is very wide
Bram Moolenaar071d4272004-06-13 20:20:40 +00006170 n = 9999;
6171 win_setwidth_win((int)n, wp);
6172 }
6173 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006174 {
6175 if (*eap->arg == '-' || *eap->arg == '+')
6176 n += curwin->w_height;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006177 else if (n == 0 && eap->arg[0] == NUL) // default is very high
Bram Moolenaar071d4272004-06-13 20:20:40 +00006178 n = 9999;
6179 win_setheight_win((int)n, wp);
6180 }
6181}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006182
6183/*
6184 * ":find [+command] <file>" command.
6185 */
6186 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006187ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006188{
6189#ifdef FEAT_SEARCHPATH
6190 char_u *fname;
6191 int count;
6192
6193 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
6194 TRUE, curbuf->b_ffname);
6195 if (eap->addr_count > 0)
6196 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006197 // Repeat finding the file "count" times. This matters when it
6198 // appears several times in the path.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006199 count = eap->line2;
6200 while (fname != NULL && --count > 0)
6201 {
6202 vim_free(fname);
6203 fname = find_file_in_path(NULL, 0, FNAME_MESS,
6204 FALSE, curbuf->b_ffname);
6205 }
6206 }
6207
6208 if (fname != NULL)
6209 {
6210 eap->arg = fname;
6211#endif
6212 do_exedit(eap, NULL);
6213#ifdef FEAT_SEARCHPATH
6214 vim_free(fname);
6215 }
6216#endif
6217}
6218
6219/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00006220 * ":open" simulation: for now just work like ":visual".
6221 */
6222 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006223ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006224{
6225 regmatch_T regmatch;
6226 char_u *p;
6227
6228 curwin->w_cursor.lnum = eap->line2;
6229 beginline(BL_SOL | BL_FIX);
6230 if (*eap->arg == '/')
6231 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006232 // ":open /pattern/": put cursor in column found with pattern
Bram Moolenaardf177f62005-02-22 08:39:57 +00006233 ++eap->arg;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006234 p = skip_regexp(eap->arg, '/', p_magic);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006235 *p = NUL;
6236 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
6237 if (regmatch.regprog != NULL)
6238 {
6239 regmatch.rm_ic = p_ic;
6240 p = ml_get_curline();
6241 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006242 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006243 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006244 emsg(_(e_nomatch));
Bram Moolenaar473de612013-06-08 18:19:48 +02006245 vim_regfree(regmatch.regprog);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006246 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006247 // Move to the NUL, ignore any other arguments.
Bram Moolenaardf177f62005-02-22 08:39:57 +00006248 eap->arg += STRLEN(eap->arg);
6249 }
6250 check_cursor();
6251
6252 eap->cmdidx = CMD_visual;
6253 do_exedit(eap, NULL);
6254}
6255
6256/*
6257 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006258 */
6259 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006260ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006261{
6262 do_exedit(eap, NULL);
6263}
6264
6265/*
Bram Moolenaar4b96df52020-01-26 22:00:26 +01006266 * ":edit <file>" command and alike.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006267 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006268 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006269do_exedit(
6270 exarg_T *eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006271 win_T *old_curwin) // curwin before doing a split or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006272{
6273 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006274 int need_hide;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006275 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006276
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01006277 if ((eap->cmdidx != CMD_pedit && ERROR_IF_POPUP_WINDOW)
6278 || ERROR_IF_TERM_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006279 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006280 /*
6281 * ":vi" command ends Ex mode.
6282 */
6283 if (exmode_active && (eap->cmdidx == CMD_visual
6284 || eap->cmdidx == CMD_view))
6285 {
6286 exmode_active = FALSE;
Bram Moolenaar158ea172020-06-18 17:28:39 +02006287 ex_pressedreturn = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006288 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006289 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006290 // Special case: ":global/pat/visual\NLvi-commands"
Bram Moolenaardf177f62005-02-22 08:39:57 +00006291 if (global_busy)
6292 {
6293 int rd = RedrawingDisabled;
6294 int nwr = no_wait_return;
6295 int ms = msg_scroll;
6296#ifdef FEAT_GUI
6297 int he = hold_gui_events;
6298#endif
6299
6300 if (eap->nextcmd != NULL)
6301 {
6302 stuffReadbuff(eap->nextcmd);
6303 eap->nextcmd = NULL;
6304 }
6305
6306 if (exmode_was != EXMODE_VIM)
6307 settmode(TMODE_RAW);
6308 RedrawingDisabled = 0;
6309 no_wait_return = 0;
6310 need_wait_return = FALSE;
6311 msg_scroll = 0;
6312#ifdef FEAT_GUI
6313 hold_gui_events = 0;
6314#endif
6315 must_redraw = CLEAR;
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01006316 pending_exmode_active = TRUE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006317
6318 main_loop(FALSE, TRUE);
6319
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01006320 pending_exmode_active = FALSE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006321 RedrawingDisabled = rd;
6322 no_wait_return = nwr;
6323 msg_scroll = ms;
6324#ifdef FEAT_GUI
6325 hold_gui_events = he;
6326#endif
6327 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006328 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006329 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006330 }
6331
6332 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006333 || eap->cmdidx == CMD_tabnew
6334 || eap->cmdidx == CMD_tabedit
Bram Moolenaar4033c552017-09-16 20:54:51 +02006335 || eap->cmdidx == CMD_vnew) && *eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006336 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006337 // ":new" or ":tabnew" without argument: edit an new empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006338 setpcmark();
6339 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006340 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
6341 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006342 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006343 else if ((eap->cmdidx != CMD_split && eap->cmdidx != CMD_vsplit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006344 || *eap->arg != NUL
6345#ifdef FEAT_BROWSE
6346 || cmdmod.browse
6347#endif
6348 )
6349 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006350 // Can't edit another file when "curbuf_lock" is set. Only ":edit"
6351 // can bring us here, others are stopped earlier.
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006352 if (*eap->arg != NUL && curbuf_locked())
6353 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006354
Bram Moolenaar071d4272004-06-13 20:20:40 +00006355 n = readonlymode;
6356 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
6357 readonlymode = TRUE;
6358 else if (eap->cmdidx == CMD_enew)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006359 readonlymode = FALSE; // 'readonly' doesn't make sense in an
6360 // empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006361 setpcmark();
6362 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
6363 NULL, eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006364 // ":edit" goes to first line if Vi compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00006365 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
6366 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
6367 ? ECMD_ONE : eap->do_ecmd_lnum,
Bram Moolenaareb44a682017-08-03 22:44:55 +02006368 (buf_hide(curbuf) ? ECMD_HIDE : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006369 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006370 // after a split we can use an existing buffer
Bram Moolenaar7b449342014-03-25 13:03:48 +01006371 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006372 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006373 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006374 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006375 // Editing the file failed. If the window was split, close it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006376 if (old_curwin != NULL)
6377 {
6378 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02006379 if (!need_hide || buf_hide(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006380 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006381#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006382 cleanup_T cs;
6383
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006384 // Reset the error/interrupt/exception state here so that
6385 // aborting() returns FALSE when closing a window.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006386 enter_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02006387#endif
6388#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006389 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006390#endif
Bram Moolenaareb44a682017-08-03 22:44:55 +02006391 win_close(curwin, !need_hide && !buf_hide(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006392
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006393#if defined(FEAT_EVAL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006394 // Restore the error/interrupt/exception state if not
6395 // discarded by a new aborting error, interrupt, or
6396 // uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006397 leave_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02006398#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006399 }
6400 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006401 }
6402 else if (readonlymode && curbuf->b_nwindows == 1)
6403 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006404 // When editing an already visited buffer, 'readonly' won't be set
6405 // but the previous value is kept. With ":view" and ":sview" we
6406 // want the file to be readonly, except when another window is
6407 // editing the same buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006408 curbuf->b_p_ro = TRUE;
6409 }
6410 readonlymode = n;
6411 }
6412 else
6413 {
6414 if (eap->do_ecmd_cmd != NULL)
6415 do_cmdline_cmd(eap->do_ecmd_cmd);
6416#ifdef FEAT_TITLE
6417 n = curwin->w_arg_idx_invalid;
6418#endif
6419 check_arg_idx(curwin);
6420#ifdef FEAT_TITLE
6421 if (n != curwin->w_arg_idx_invalid)
6422 maketitle();
6423#endif
6424 }
6425
Bram Moolenaar071d4272004-06-13 20:20:40 +00006426 /*
6427 * if ":split file" worked, set alternate file name in old window to new
6428 * file
6429 */
6430 if (old_curwin != NULL
6431 && *eap->arg != NUL
6432 && curwin != old_curwin
6433 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00006434 && old_curwin->w_buffer != curbuf
6435 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006436 old_curwin->w_alt_fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006437
6438 ex_no_reprint = TRUE;
6439}
6440
6441#ifndef FEAT_GUI
6442/*
6443 * ":gui" and ":gvim" when there is no GUI.
6444 */
6445 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006446ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006447{
6448 eap->errmsg = e_nogvim;
6449}
6450#endif
6451
Bram Moolenaar4f974752019-02-17 17:44:42 +01006452#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006453 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006454ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006455{
6456 gui_make_tearoff(eap->arg);
6457}
6458#endif
6459
Bram Moolenaar29a2c082018-03-05 21:06:23 +01006460#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
6461 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006462 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006463ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006464{
Bram Moolenaar29a2c082018-03-05 21:06:23 +01006465# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
6466 if (gui.in_use)
6467 gui_make_popup(eap->arg, eap->forceit);
6468# ifdef FEAT_TERM_POPUP_MENU
6469 else
6470# endif
6471# endif
6472# ifdef FEAT_TERM_POPUP_MENU
6473 pum_make_popup(eap->arg, eap->forceit);
6474# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006475}
6476#endif
6477
Bram Moolenaar071d4272004-06-13 20:20:40 +00006478 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006479ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006480{
6481 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006482 msg(_("No swap file"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006483 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01006484 msg((char *)curbuf->b_ml.ml_mfp->mf_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006485}
6486
6487/*
6488 * ":syncbind" forces all 'scrollbind' windows to have the same relative
6489 * offset.
6490 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
6491 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006492 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006493ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006494{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006495 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006496 win_T *save_curwin = curwin;
6497 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006498 long topline;
6499 long y;
6500 linenr_T old_linenr = curwin->w_cursor.lnum;
6501
6502 setpcmark();
6503
6504 /*
6505 * determine max topline
6506 */
6507 if (curwin->w_p_scb)
6508 {
6509 topline = curwin->w_topline;
Bram Moolenaar29323592016-07-24 22:04:11 +02006510 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006511 {
6512 if (wp->w_p_scb && wp->w_buffer)
6513 {
Bram Moolenaar375e3392019-01-31 18:26:10 +01006514 y = wp->w_buffer->b_ml.ml_line_count - get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006515 if (topline > y)
6516 topline = y;
6517 }
6518 }
6519 if (topline < 1)
6520 topline = 1;
6521 }
6522 else
6523 {
6524 topline = 1;
6525 }
6526
6527
6528 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006529 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006530 */
Bram Moolenaar29323592016-07-24 22:04:11 +02006531 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006532 {
6533 if (curwin->w_p_scb)
6534 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006535 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006536 y = topline - curwin->w_topline;
6537 if (y > 0)
6538 scrollup(y, TRUE);
6539 else
6540 scrolldown(-y, TRUE);
6541 curwin->w_scbind_pos = topline;
6542 redraw_later(VALID);
6543 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006544 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006545 }
6546 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006547 curwin = save_curwin;
6548 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006549 if (curwin->w_p_scb)
6550 {
6551 did_syncbind = TRUE;
6552 checkpcmark();
6553 if (old_linenr != curwin->w_cursor.lnum)
6554 {
6555 char_u ctrl_o[2];
6556
6557 ctrl_o[0] = Ctrl_O;
6558 ctrl_o[1] = 0;
6559 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
6560 }
6561 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006562}
6563
6564
6565 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006566ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006567{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006568 int i;
6569 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
6570 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006571
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006572 if (eap->usefilter) // :r!cmd
Bram Moolenaar071d4272004-06-13 20:20:40 +00006573 do_bang(1, eap, FALSE, FALSE, TRUE);
6574 else
6575 {
6576 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
6577 return;
6578
6579#ifdef FEAT_BROWSE
6580 if (cmdmod.browse)
6581 {
6582 char_u *browseFile;
6583
Bram Moolenaar7171abe2004-10-11 10:06:20 +00006584 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006585 NULL, NULL, NULL, curbuf);
6586 if (browseFile != NULL)
6587 {
6588 i = readfile(browseFile, NULL,
6589 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
6590 vim_free(browseFile);
6591 }
6592 else
6593 i = OK;
6594 }
6595 else
6596#endif
6597 if (*eap->arg == NUL)
6598 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006599 if (check_fname() == FAIL) // check for no file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00006600 return;
6601 i = readfile(curbuf->b_ffname, curbuf->b_fname,
6602 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
6603 }
6604 else
6605 {
6606 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
6607 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
6608 i = readfile(eap->arg, NULL,
6609 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
6610
6611 }
Bram Moolenaare13b9af2017-01-13 22:01:02 +01006612 if (i != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006613 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006614#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006615 if (!aborting())
6616#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006617 semsg(_(e_notopen), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006618 }
6619 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00006620 {
6621 if (empty && exmode_active)
6622 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006623 // Delete the empty line that remains. Historically ex does
6624 // this but vi doesn't.
Bram Moolenaardf177f62005-02-22 08:39:57 +00006625 if (eap->line2 == 0)
6626 lnum = curbuf->b_ml.ml_line_count;
6627 else
6628 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00006629 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006630 {
Bram Moolenaarca70c072020-05-30 20:30:46 +02006631 ml_delete(lnum);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00006632 if (curwin->w_cursor.lnum > 1
6633 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006634 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00006635 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006636 }
6637 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006638 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006639 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006640 }
6641}
6642
Bram Moolenaarea408852005-06-25 22:49:46 +00006643static char_u *prev_dir = NULL;
6644
6645#if defined(EXITFREE) || defined(PROTO)
6646 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006647free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00006648{
Bram Moolenaard23a8232018-02-10 18:45:26 +01006649 VIM_CLEAR(prev_dir);
6650 VIM_CLEAR(globaldir);
Bram Moolenaarea408852005-06-25 22:49:46 +00006651}
6652#endif
6653
Bram Moolenaarf4258302013-06-02 18:20:17 +02006654/*
Bram Moolenaar002bc792020-06-05 22:33:42 +02006655 * Get the previous directory for the given chdir scope.
6656 */
6657 static char_u *
6658get_prevdir(cdscope_T scope)
6659{
6660 if (scope == CDSCOPE_WINDOW)
6661 return curwin->w_prevdir;
6662 else if (scope == CDSCOPE_TABPAGE)
6663 return curtab->tp_prevdir;
6664 return prev_dir;
6665}
6666
6667/*
Bram Moolenaarf4258302013-06-02 18:20:17 +02006668 * Deal with the side effects of changing the current directory.
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006669 * When 'scope' is CDSCOPE_TABPAGE then this was after an ":tcd" command.
6670 * When 'scope' is CDSCOPE_WINDOW then this was after an ":lcd" command.
Bram Moolenaarf4258302013-06-02 18:20:17 +02006671 */
6672 void
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006673post_chdir(cdscope_T scope)
Bram Moolenaarf4258302013-06-02 18:20:17 +02006674{
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006675 if (scope != CDSCOPE_WINDOW)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006676 // Clear tab local directory for both :cd and :tcd
6677 VIM_CLEAR(curtab->tp_localdir);
Bram Moolenaard23a8232018-02-10 18:45:26 +01006678 VIM_CLEAR(curwin->w_localdir);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006679 if (scope != CDSCOPE_GLOBAL)
Bram Moolenaarf4258302013-06-02 18:20:17 +02006680 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02006681 char_u *pdir = get_prevdir(scope);
6682
6683 // If still in the global directory, need to remember current
6684 // directory as the global directory.
6685 if (globaldir == NULL && pdir != NULL)
6686 globaldir = vim_strsave(pdir);
6687
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006688 // Remember this local directory for the window.
Bram Moolenaarf4258302013-06-02 18:20:17 +02006689 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006690 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006691 if (scope == CDSCOPE_TABPAGE)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006692 curtab->tp_localdir = vim_strsave(NameBuff);
6693 else
6694 curwin->w_localdir = vim_strsave(NameBuff);
6695 }
Bram Moolenaarf4258302013-06-02 18:20:17 +02006696 }
6697 else
6698 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02006699 // We are now in the global directory, no need to remember its name.
Bram Moolenaard23a8232018-02-10 18:45:26 +01006700 VIM_CLEAR(globaldir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006701 }
6702
6703 shorten_fnames(TRUE);
6704}
6705
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006706/*
6707 * Change directory function used by :cd/:tcd/:lcd Ex commands and the
Bram Moolenaar95058722020-06-01 16:26:19 +02006708 * chdir() function.
6709 * scope == CDSCOPE_WINDOW: changes the window-local directory
6710 * scope == CDSCOPE_TABPAGE: changes the tab-local directory
6711 * Otherwise: changes the global directory
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006712 * Returns TRUE if the directory is successfully changed.
6713 */
6714 int
6715changedir_func(
6716 char_u *new_dir,
6717 int forceit,
6718 cdscope_T scope)
6719{
6720 char_u *tofree;
Bram Moolenaar002bc792020-06-05 22:33:42 +02006721 char_u *pdir = NULL;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006722 int dir_differs;
6723 int retval = FALSE;
6724
Bram Moolenaar7cc96922020-01-31 22:41:38 +01006725 if (new_dir == NULL || allbuf_locked())
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006726 return FALSE;
6727
6728 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged() && !forceit)
6729 {
6730 emsg(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
6731 return FALSE;
6732 }
6733
6734 // ":cd -": Change to previous directory
6735 if (STRCMP(new_dir, "-") == 0)
6736 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02006737 pdir = get_prevdir(scope);
6738 if (pdir == NULL)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006739 {
6740 emsg(_("E186: No previous directory"));
6741 return FALSE;
6742 }
Bram Moolenaar002bc792020-06-05 22:33:42 +02006743 new_dir = pdir;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006744 }
6745
Bram Moolenaar002bc792020-06-05 22:33:42 +02006746 // Free the previous directory
6747 tofree = get_prevdir(scope);
6748
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006749 // Save current directory for next ":cd -"
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006750 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar002bc792020-06-05 22:33:42 +02006751 pdir = vim_strsave(NameBuff);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006752 else
Bram Moolenaar002bc792020-06-05 22:33:42 +02006753 pdir = NULL;
6754 if (scope == CDSCOPE_WINDOW)
6755 curwin->w_prevdir = pdir;
6756 else if (scope == CDSCOPE_TABPAGE)
6757 curtab->tp_prevdir = pdir;
6758 else
6759 prev_dir = pdir;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006760
6761#if defined(UNIX) || defined(VMS)
6762 // for UNIX ":cd" means: go to home directory
6763 if (*new_dir == NUL)
6764 {
6765 // use NameBuff for home directory name
6766# ifdef VMS
6767 char_u *p;
6768
6769 p = mch_getenv((char_u *)"SYS$LOGIN");
6770 if (p == NULL || *p == NUL) // empty is the same as not set
6771 NameBuff[0] = NUL;
6772 else
6773 vim_strncpy(NameBuff, p, MAXPATHL - 1);
6774# else
6775 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
6776# endif
6777 new_dir = NameBuff;
6778 }
6779#endif
Bram Moolenaar002bc792020-06-05 22:33:42 +02006780 dir_differs = new_dir == NULL || pdir == NULL
6781 || pathcmp((char *)pdir, (char *)new_dir, -1) != 0;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006782 if (new_dir == NULL || (dir_differs && vim_chdir(new_dir)))
6783 emsg(_(e_failed));
6784 else
6785 {
6786 char_u *acmd_fname;
6787
6788 post_chdir(scope);
6789
6790 if (dir_differs)
6791 {
6792 if (scope == CDSCOPE_WINDOW)
6793 acmd_fname = (char_u *)"window";
6794 else if (scope == CDSCOPE_TABPAGE)
6795 acmd_fname = (char_u *)"tabpage";
6796 else
6797 acmd_fname = (char_u *)"global";
6798 apply_autocmds(EVENT_DIRCHANGED, acmd_fname, new_dir, FALSE,
6799 curbuf);
6800 }
6801 retval = TRUE;
6802 }
6803 vim_free(tofree);
6804
6805 return retval;
6806}
Bram Moolenaarea408852005-06-25 22:49:46 +00006807
Bram Moolenaar071d4272004-06-13 20:20:40 +00006808/*
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006809 * ":cd", ":tcd", ":lcd", ":chdir" ":tchdir" and ":lchdir".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006810 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00006811 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006812ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006813{
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01006814 char_u *new_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006815
6816 new_dir = eap->arg;
6817#if !defined(UNIX) && !defined(VMS)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006818 // for non-UNIX ":cd" means: print current directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006819 if (*new_dir == NUL)
6820 ex_pwd(NULL);
6821 else
6822#endif
6823 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006824 cdscope_T scope = CDSCOPE_GLOBAL;
6825
6826 if (eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir)
6827 scope = CDSCOPE_WINDOW;
6828 else if (eap->cmdidx == CMD_tcd || eap->cmdidx == CMD_tchdir)
6829 scope = CDSCOPE_TABPAGE;
6830
6831 if (changedir_func(new_dir, eap->forceit, scope))
Bram Moolenaardf177f62005-02-22 08:39:57 +00006832 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006833 // Echo the new current directory if the command was typed.
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00006834 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006835 ex_pwd(eap);
6836 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006837 }
6838}
6839
6840/*
6841 * ":pwd".
6842 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006843 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006844ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006845{
6846 if (mch_dirname(NameBuff, MAXPATHL) == OK)
6847 {
6848#ifdef BACKSLASH_IN_FILENAME
6849 slash_adjust(NameBuff);
6850#endif
Bram Moolenaar95058722020-06-01 16:26:19 +02006851 if (p_verbose > 0)
6852 {
6853 char *context = "global";
6854
6855 if (curwin->w_localdir != NULL)
6856 context = "window";
6857 else if (curtab->tp_localdir != NULL)
6858 context = "tabpage";
6859 smsg("[%s] %s", context, (char *)NameBuff);
6860 }
6861 else
6862 msg((char *)NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006863 }
6864 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006865 emsg(_("E187: Unknown"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006866}
6867
6868/*
6869 * ":=".
6870 */
6871 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006872ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006873{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006874 smsg("%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006875 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006876}
6877
6878 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006879ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006880{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006881 int n;
6882 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006883
6884 if (cursor_valid())
6885 {
6886 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
6887 if (n >= 0)
Bram Moolenaar53f81742017-09-22 14:35:51 +02006888 windgoto((int)n, curwin->w_wincol + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006889 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006890
6891 len = eap->line2;
6892 switch (*eap->arg)
6893 {
6894 case 'm': break;
6895 case NUL: len *= 1000L; break;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006896 default: semsg(_(e_invarg2), eap->arg); return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006897 }
6898 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006899}
6900
6901/*
6902 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
6903 */
6904 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006905do_sleep(long msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006906{
Bram Moolenaar52953192019-08-16 10:27:13 +02006907 long done = 0;
Bram Moolenaar975b5272016-03-15 23:10:59 +01006908 long wait_now;
Bram Moolenaar52953192019-08-16 10:27:13 +02006909# ifdef ELAPSED_FUNC
6910 elapsed_T start_tv;
6911
6912 // Remember at what time we started, so that we know how much longer we
6913 // should wait after waiting for a bit.
6914 ELAPSED_INIT(start_tv);
6915# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006916
6917 cursor_on();
Bram Moolenaarf45d7472018-09-30 18:22:26 +02006918 out_flush_cursor(FALSE, FALSE);
Bram Moolenaar52953192019-08-16 10:27:13 +02006919 while (!got_int && done < msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006920 {
Bram Moolenaar975b5272016-03-15 23:10:59 +01006921 wait_now = msec - done > 1000L ? 1000L : msec - done;
6922#ifdef FEAT_TIMERS
6923 {
6924 long due_time = check_due_timer();
6925
6926 if (due_time > 0 && due_time < wait_now)
6927 wait_now = due_time;
6928 }
6929#endif
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02006930#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar28e67e02019-08-15 23:05:49 +02006931 if (has_any_channel() && wait_now > 20L)
6932 wait_now = 20L;
6933#endif
6934#ifdef FEAT_SOUND
6935 if (has_any_sound_callback() && wait_now > 20L)
6936 wait_now = 20L;
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02006937#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +01006938 ui_delay(wait_now, TRUE);
Bram Moolenaar52953192019-08-16 10:27:13 +02006939
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02006940#ifdef FEAT_JOB_CHANNEL
6941 if (has_any_channel())
6942 ui_breakcheck_force(TRUE);
6943 else
6944#endif
6945 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006946#ifdef MESSAGE_QUEUE
Bram Moolenaar52953192019-08-16 10:27:13 +02006947 // Process the netbeans and clientserver messages that may have been
6948 // received in the call to ui_breakcheck() when the GUI is in use. This
6949 // may occur when running a test case.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006950 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02006951#endif
Bram Moolenaar52953192019-08-16 10:27:13 +02006952
6953# ifdef ELAPSED_FUNC
6954 // actual time passed
6955 done = ELAPSED_FUNC(start_tv);
6956# else
6957 // guestimate time passed (will actually be more)
6958 done += wait_now;
6959# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006960 }
Bram Moolenaar1ebff3d2018-07-07 16:18:13 +02006961
6962 // If CTRL-C was typed to interrupt the sleep, drop the CTRL-C from the
6963 // input buffer, otherwise a following call to input() fails.
6964 if (got_int)
6965 (void)vpeekc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006966}
6967
Bram Moolenaar071d4272004-06-13 20:20:40 +00006968/*
6969 * ":winsize" command (obsolete).
6970 */
6971 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006972ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006973{
6974 int w, h;
6975 char_u *arg = eap->arg;
6976 char_u *p;
6977
6978 w = getdigits(&arg);
6979 arg = skipwhite(arg);
6980 p = arg;
6981 h = getdigits(&arg);
6982 if (*p != NUL && *arg == NUL)
6983 set_shellsize(w, h, TRUE);
6984 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006985 emsg(_("E465: :winsize requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006986}
6987
Bram Moolenaar071d4272004-06-13 20:20:40 +00006988 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006989ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006990{
6991 int xchar = NUL;
6992 char_u *p;
6993
6994 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
6995 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006996 // CTRL-W g and CTRL-W CTRL-G have an extra command character
Bram Moolenaar071d4272004-06-13 20:20:40 +00006997 if (eap->arg[1] == NUL)
6998 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006999 emsg(_(e_invarg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007000 return;
7001 }
7002 xchar = eap->arg[1];
7003 p = eap->arg + 2;
7004 }
7005 else
7006 p = eap->arg + 1;
7007
7008 eap->nextcmd = check_nextcmd(p);
7009 p = skipwhite(p);
7010 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007011 emsg(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02007012 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007013 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007014 // Pass flags on for ":vertical wincmd ]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007015 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00007016 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007017 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
7018 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00007019 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007020 }
7021}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007022
Bram Moolenaar843ee412004-06-30 16:16:41 +00007023#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007024/*
7025 * ":winpos".
7026 */
7027 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007028ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007029{
7030 int x, y;
7031 char_u *arg = eap->arg;
7032 char_u *p;
7033
7034 if (*arg == NUL)
7035 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00007036# if defined(FEAT_GUI) || defined(MSWIN)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007037# ifdef VIMDLL
7038 if (gui.in_use ? gui_mch_get_winpos(&x, &y) != FAIL :
7039 mch_get_winpos(&x, &y) != FAIL)
7040# elif defined(FEAT_GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007041 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00007042# else
7043 if (mch_get_winpos(&x, &y) != FAIL)
7044# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007045 {
7046 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
Bram Moolenaar32526b32019-01-19 17:43:09 +01007047 msg((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007048 }
7049 else
7050# endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007051 emsg(_("E188: Obtaining window position not implemented for this platform"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007052 }
7053 else
7054 {
7055 x = getdigits(&arg);
7056 arg = skipwhite(arg);
7057 p = arg;
7058 y = getdigits(&arg);
7059 if (*p == NUL || *arg != NUL)
7060 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007061 emsg(_("E466: :winpos requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007062 return;
7063 }
7064# ifdef FEAT_GUI
7065 if (gui.in_use)
7066 gui_mch_set_winpos(x, y);
7067 else if (gui.starting)
7068 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007069 // Remember the coordinates for when the window is opened.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007070 gui_win_x = x;
7071 gui_win_y = y;
7072 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007073# if defined(HAVE_TGETENT) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007074 else
7075# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007076# endif
7077# if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar843ee412004-06-30 16:16:41 +00007078 mch_set_winpos(x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007079# endif
7080# ifdef HAVE_TGETENT
7081 if (*T_CWP)
7082 term_set_winpos(x, y);
7083# endif
7084 }
7085}
7086#endif
7087
7088/*
7089 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
7090 */
7091 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007092ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007093{
7094 oparg_T oa;
7095
7096 clear_oparg(&oa);
7097 oa.regname = eap->regname;
7098 oa.start.lnum = eap->line1;
7099 oa.end.lnum = eap->line2;
7100 oa.line_count = eap->line2 - eap->line1 + 1;
7101 oa.motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007102 virtual_op = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007103 if (eap->cmdidx != CMD_yank) // position cursor for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00007104 {
7105 setpcmark();
7106 curwin->w_cursor.lnum = eap->line1;
7107 beginline(BL_SOL | BL_FIX);
7108 }
7109
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007110 if (VIsual_active)
7111 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007112
Bram Moolenaar071d4272004-06-13 20:20:40 +00007113 switch (eap->cmdidx)
7114 {
7115 case CMD_delete:
7116 oa.op_type = OP_DELETE;
7117 op_delete(&oa);
7118 break;
7119
7120 case CMD_yank:
7121 oa.op_type = OP_YANK;
7122 (void)op_yank(&oa, FALSE, TRUE);
7123 break;
7124
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007125 default: // CMD_rshift or CMD_lshift
Bram Moolenaar6e707362013-06-14 19:15:58 +02007126 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007127#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02007128 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
7129#else
7130 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00007131#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02007132 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007133 oa.op_type = OP_RSHIFT;
7134 else
7135 oa.op_type = OP_LSHIFT;
7136 op_shift(&oa, FALSE, eap->amount);
7137 break;
7138 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007139 virtual_op = MAYBE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007140 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007141}
7142
7143/*
7144 * ":put".
7145 */
7146 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007147ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007148{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007149 // ":0put" works like ":1put!".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007150 if (eap->line2 == 0)
7151 {
7152 eap->line2 = 1;
7153 eap->forceit = TRUE;
7154 }
7155 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007156 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
7157 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007158}
7159
7160/*
7161 * Handle ":copy" and ":move".
7162 */
7163 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007164ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007165{
7166 long n;
7167
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02007168 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE, FALSE, 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007169 if (eap->arg == NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00007170 {
7171 eap->nextcmd = NULL;
7172 return;
7173 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00007174 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007175
7176 /*
7177 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
7178 */
7179 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
7180 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02007181 emsg(_(e_invrange));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007182 return;
7183 }
7184
7185 if (eap->cmdidx == CMD_move)
7186 {
7187 if (do_move(eap->line1, eap->line2, n) == FAIL)
7188 return;
7189 }
7190 else
7191 ex_copy(eap->line1, eap->line2, n);
7192 u_clearline();
7193 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007194 ex_may_print(eap);
7195}
7196
7197/*
7198 * Print the current line if flags were given to the Ex command.
7199 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02007200 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007201ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007202{
7203 if (eap->flags != 0)
7204 {
7205 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
7206 (eap->flags & EXFLAG_LIST));
7207 ex_no_reprint = TRUE;
7208 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007209}
7210
7211/*
7212 * ":smagic" and ":snomagic".
7213 */
7214 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007215ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007216{
7217 int magic_save = p_magic;
7218
7219 p_magic = (eap->cmdidx == CMD_smagic);
7220 do_sub(eap);
7221 p_magic = magic_save;
7222}
7223
7224/*
7225 * ":join".
7226 */
7227 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007228ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007229{
7230 curwin->w_cursor.lnum = eap->line1;
7231 if (eap->line1 == eap->line2)
7232 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007233 if (eap->addr_count >= 2) // :2,2join does nothing
Bram Moolenaar071d4272004-06-13 20:20:40 +00007234 return;
7235 if (eap->line2 == curbuf->b_ml.ml_line_count)
7236 {
7237 beep_flush();
7238 return;
7239 }
7240 ++eap->line2;
7241 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02007242 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007243 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007244 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007245}
7246
7247/*
7248 * ":[addr]@r" or ":[addr]*r": execute register
7249 */
7250 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007251ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007252{
7253 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00007254 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007255
7256 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4930a762016-09-11 14:39:53 +02007257 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007258
7259#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007260 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00007261#endif
7262
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007263 // get the register name. No name means to use the previous one
Bram Moolenaar071d4272004-06-13 20:20:40 +00007264 c = *eap->arg;
7265 if (c == NUL || (c == '*' && *eap->cmd == '*'))
7266 c = '@';
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007267 // Put the register in the typeahead buffer with the "silent" flag.
Bram Moolenaard333d1e2006-11-07 17:43:47 +00007268 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
7269 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007270 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007271 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00007272 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007273 else
7274 {
7275 int save_efr = exec_from_reg;
7276
7277 exec_from_reg = TRUE;
7278
7279 /*
7280 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00007281 * Continue until the stuff buffer is empty and all added characters
7282 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007283 */
Bram Moolenaar60462872009-11-03 11:40:19 +00007284 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007285 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
7286
7287 exec_from_reg = save_efr;
7288 }
7289}
7290
7291/*
7292 * ":!".
7293 */
7294 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007295ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007296{
7297 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
7298}
7299
7300/*
7301 * ":undo".
7302 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007303 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01007304ex_undo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007305{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007306 if (eap->addr_count == 1) // :undo 123
Bram Moolenaar730cde92010-06-27 05:18:54 +02007307 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00007308 else
7309 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007310}
7311
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007312#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02007313 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007314ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007315{
7316 char_u hash[UNDO_HASH_SIZE];
7317
7318 u_compute_hash(hash);
7319 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
7320}
7321
Bram Moolenaar6df6f472010-07-18 18:04:50 +02007322 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007323ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007324{
7325 char_u hash[UNDO_HASH_SIZE];
7326
7327 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02007328 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007329}
7330#endif
7331
Bram Moolenaar071d4272004-06-13 20:20:40 +00007332/*
7333 * ":redo".
7334 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007335 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007336ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007337{
7338 u_redo(1);
7339}
7340
7341/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007342 * ":earlier" and ":later".
7343 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007344 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007345ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007346{
7347 long count = 0;
7348 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02007349 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007350 char_u *p = eap->arg;
7351
7352 if (*p == NUL)
7353 count = 1;
7354 else if (isdigit(*p))
7355 {
7356 count = getdigits(&p);
7357 switch (*p)
7358 {
7359 case 's': ++p; sec = TRUE; break;
7360 case 'm': ++p; sec = TRUE; count *= 60; break;
7361 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02007362 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
7363 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007364 }
7365 }
7366
7367 if (*p != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007368 semsg(_(e_invarg2), eap->arg);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007369 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02007370 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
7371 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007372}
7373
7374/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007375 * ":redir": start/stop redirection.
7376 */
7377 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007378ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007379{
7380 char *mode;
7381 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00007382 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007383
Bram Moolenaarba768492016-07-08 15:32:54 +02007384#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02007385 if (redir_execute)
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007386 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007387 emsg(_("E930: Cannot use :redir inside execute()"));
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007388 return;
7389 }
Bram Moolenaarba768492016-07-08 15:32:54 +02007390#endif
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007391
Bram Moolenaar071d4272004-06-13 20:20:40 +00007392 if (STRICMP(eap->arg, "END") == 0)
7393 close_redir();
7394 else
7395 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007396 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007397 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007398 ++arg;
7399 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007400 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007401 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007402 mode = "a";
7403 }
7404 else
7405 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00007406 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007407
7408 close_redir();
7409
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007410 // Expand environment variables and "~/".
Bram Moolenaarca472992005-01-21 11:46:23 +00007411 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007412 if (fname == NULL)
7413 return;
7414#ifdef FEAT_BROWSE
7415 if (cmdmod.browse)
7416 {
7417 char_u *browseFile;
7418
Bram Moolenaar7171abe2004-10-11 10:06:20 +00007419 browseFile = do_browse(BROWSE_SAVE,
7420 (char_u *)_("Save Redirection"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +02007421 fname, NULL, NULL,
7422 (char_u *)_(BROWSE_FILTER_ALL_FILES), curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007423 if (browseFile == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007424 return; // operation cancelled
Bram Moolenaar071d4272004-06-13 20:20:40 +00007425 vim_free(fname);
7426 fname = browseFile;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007427 eap->forceit = TRUE; // since dialog already asked
Bram Moolenaar071d4272004-06-13 20:20:40 +00007428 }
7429#endif
7430
7431 redir_fd = open_exfile(fname, eap->forceit, mode);
7432 vim_free(fname);
7433 }
7434#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00007435 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007436 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007437 // redirect to a register a-z (resp. A-Z for appending)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007438 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00007439 ++arg;
7440 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007441# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00007442 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00007443 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00007444# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00007445 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007446 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007447 redir_reg = *arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007448 if (*arg == '>' && arg[1] == '>') // append
Bram Moolenaard9d30582005-05-18 22:10:28 +00007449 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00007450 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007451 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007452 // Can use both "@a" and "@a>".
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00007453 if (*arg == '>')
7454 arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007455 // Make register empty when not using @A-@Z and the
7456 // command is valid.
Bram Moolenaarc188b882007-10-19 14:20:54 +00007457 if (*arg == NUL && !isupper(redir_reg))
7458 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007459 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00007460 }
7461 if (*arg != NUL)
7462 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007463 redir_reg = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007464 semsg(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007465 }
7466 }
7467 else if (*arg == '=' && arg[1] == '>')
7468 {
7469 int append;
7470
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007471 // redirect to a variable
Bram Moolenaardf177f62005-02-22 08:39:57 +00007472 close_redir();
7473 arg += 2;
7474
7475 if (*arg == '>')
7476 {
7477 ++arg;
7478 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007479 }
7480 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00007481 append = FALSE;
7482
7483 if (var_redir_start(skipwhite(arg), append) == OK)
7484 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007485 }
7486#endif
7487
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007488 // TODO: redirect to a buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00007489
Bram Moolenaar071d4272004-06-13 20:20:40 +00007490 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007491 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007492 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00007493
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007494 // Make sure redirection is not off. Can happen for cmdline completion
7495 // that indirectly invokes a command to catch its output.
Bram Moolenaar29b2d262006-09-10 19:07:28 +00007496 if (redir_fd != NULL
7497#ifdef FEAT_EVAL
7498 || redir_reg || redir_vname
7499#endif
7500 )
7501 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007502}
7503
7504/*
7505 * ":redraw": force redraw
7506 */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01007507 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007508ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007509{
7510 int r = RedrawingDisabled;
7511 int p = p_lz;
7512
7513 RedrawingDisabled = 0;
7514 p_lz = FALSE;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01007515 validate_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007516 update_topline();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007517 update_screen(eap->forceit ? CLEAR : VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007518#ifdef FEAT_TITLE
7519 if (need_maketitle)
7520 maketitle();
7521#endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007522#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
7523# ifdef VIMDLL
7524 if (!gui.in_use)
7525# endif
7526 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01007527#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007528 RedrawingDisabled = r;
7529 p_lz = p;
7530
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007531 // Reset msg_didout, so that a message that's there is overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007532 msg_didout = FALSE;
7533 msg_col = 0;
7534
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007535 // No need to wait after an intentional redraw.
Bram Moolenaar56667a52013-07-17 11:54:28 +02007536 need_wait_return = FALSE;
7537
Bram Moolenaar071d4272004-06-13 20:20:40 +00007538 out_flush();
7539}
7540
7541/*
7542 * ":redrawstatus": force redraw of status line(s)
7543 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007544 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007545ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007546{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007547 int r = RedrawingDisabled;
7548 int p = p_lz;
7549
7550 RedrawingDisabled = 0;
7551 p_lz = FALSE;
7552 if (eap->forceit)
7553 status_redraw_all();
7554 else
7555 status_redraw_curbuf();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007556 update_screen(VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007557 RedrawingDisabled = r;
7558 p_lz = p;
7559 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007560}
7561
Bram Moolenaare12bab32019-01-08 22:02:56 +01007562/*
7563 * ":redrawtabline": force redraw of the tabline
7564 */
7565 static void
7566ex_redrawtabline(exarg_T *eap UNUSED)
7567{
7568 int r = RedrawingDisabled;
7569 int p = p_lz;
7570
7571 RedrawingDisabled = 0;
7572 p_lz = FALSE;
7573
7574 draw_tabline();
7575
7576 RedrawingDisabled = r;
7577 p_lz = p;
7578 out_flush();
7579}
7580
Bram Moolenaar071d4272004-06-13 20:20:40 +00007581 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007582close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007583{
7584 if (redir_fd != NULL)
7585 {
7586 fclose(redir_fd);
7587 redir_fd = NULL;
7588 }
7589#ifdef FEAT_EVAL
7590 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007591 if (redir_vname)
7592 {
7593 var_redir_stop();
7594 redir_vname = 0;
7595 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007596#endif
7597}
7598
Bram Moolenaarf96ae0b2019-07-28 15:21:55 +02007599#if (defined(FEAT_SESSION) || defined(FEAT_EVAL)) || defined(PROTO)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007600 int
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02007601vim_mkdir_emsg(char_u *name, int prot UNUSED)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007602{
7603 if (vim_mkdir(name, prot) != 0)
7604 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007605 semsg(_("E739: Cannot create directory: %s"), name);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007606 return FAIL;
7607 }
7608 return OK;
7609}
7610#endif
7611
Bram Moolenaar071d4272004-06-13 20:20:40 +00007612/*
7613 * Open a file for writing for an Ex command, with some checks.
7614 * Return file descriptor, or NULL on failure.
7615 */
7616 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007617open_exfile(
7618 char_u *fname,
7619 int forceit,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007620 char *mode) // "w" for create new file or "a" for append
Bram Moolenaar071d4272004-06-13 20:20:40 +00007621{
7622 FILE *fd;
7623
7624#ifdef UNIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007625 // with Unix it is possible to open a directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007626 if (mch_isdir(fname))
7627 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007628 semsg(_(e_isadir2), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007629 return NULL;
7630 }
7631#endif
7632 if (!forceit && *mode != 'a' && vim_fexists(fname))
7633 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007634 semsg(_("E189: \"%s\" exists (add ! to override)"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007635 return NULL;
7636 }
7637
7638 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007639 semsg(_("E190: Cannot open \"%s\" for writing"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007640
7641 return fd;
7642}
7643
7644/*
7645 * ":mark" and ":k".
7646 */
7647 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007648ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007649{
7650 pos_T pos;
7651
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007652 if (*eap->arg == NUL) // No argument?
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007653 emsg(_(e_argreq));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007654 else if (eap->arg[1] != NUL) // more than one character?
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007655 emsg(_(e_trailing));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007656 else
7657 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007658 pos = curwin->w_cursor; // save curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00007659 curwin->w_cursor.lnum = eap->line2;
7660 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007661 if (setmark(*eap->arg) == FAIL) // set mark
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007662 emsg(_("E191: Argument must be a letter or forward/backward quote"));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007663 curwin->w_cursor = pos; // restore curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00007664 }
7665}
7666
7667/*
7668 * Update w_topline, w_leftcol and the cursor position.
7669 */
7670 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007671update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007672{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007673 check_cursor(); // put cursor on valid line
Bram Moolenaar071d4272004-06-13 20:20:40 +00007674 update_topline();
7675 if (!curwin->w_p_wrap)
7676 validate_cursor();
7677 update_curswant();
7678}
7679
Bram Moolenaar071d4272004-06-13 20:20:40 +00007680/*
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007681 * Save the current State and go to Normal mode.
7682 * Return TRUE if the typeahead could be saved.
7683 */
7684 int
7685save_current_state(save_state_T *sst)
7686{
7687 sst->save_msg_scroll = msg_scroll;
7688 sst->save_restart_edit = restart_edit;
7689 sst->save_msg_didout = msg_didout;
7690 sst->save_State = State;
7691 sst->save_insertmode = p_im;
7692 sst->save_finish_op = finish_op;
7693 sst->save_opcount = opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01007694 sst->save_reg_executing = reg_executing;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007695
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007696 msg_scroll = FALSE; // no msg scrolling in Normal mode
7697 restart_edit = 0; // don't go to Insert mode
7698 p_im = FALSE; // don't use 'insertmode'
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007699
7700 /*
7701 * Save the current typeahead. This is required to allow using ":normal"
7702 * from an event handler and makes sure we don't hang when the argument
7703 * ends with half a command.
7704 */
7705 save_typeahead(&sst->tabuf);
7706 return sst->tabuf.typebuf_valid;
7707}
7708
7709 void
7710restore_current_state(save_state_T *sst)
7711{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007712 // Restore the previous typeahead.
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007713 restore_typeahead(&sst->tabuf);
7714
7715 msg_scroll = sst->save_msg_scroll;
7716 restart_edit = sst->save_restart_edit;
7717 p_im = sst->save_insertmode;
7718 finish_op = sst->save_finish_op;
7719 opcount = sst->save_opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01007720 reg_executing = sst->save_reg_executing;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007721 msg_didout |= sst->save_msg_didout; // don't reset msg_didout now
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007722
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007723 // Restore the state (needed when called from a function executed for
7724 // 'indentexpr'). Update the mouse and cursor, they may have changed.
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007725 State = sst->save_State;
7726#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007727 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007728#endif
7729}
7730
7731/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007732 * ":normal[!] {commands}": Execute normal mode commands.
7733 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01007734 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007735ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007736{
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007737 save_state_T save_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007738 char_u *arg = NULL;
7739 int l;
7740 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007741
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00007742 if (ex_normal_lock > 0)
7743 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007744 emsg(_(e_secure));
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00007745 return;
7746 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007747 if (ex_normal_busy >= p_mmd)
7748 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007749 emsg(_("E192: Recursive use of :normal too deep"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007750 return;
7751 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007752
Bram Moolenaar071d4272004-06-13 20:20:40 +00007753 /*
7754 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
7755 * this for the K_SPECIAL leading byte, otherwise special keys will not
7756 * work.
7757 */
7758 if (has_mbyte)
7759 {
7760 int len = 0;
7761
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007762 // Count the number of characters to be escaped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007763 for (p = eap->arg; *p != NUL; ++p)
7764 {
Bram Moolenaar13505972019-01-24 15:04:48 +01007765#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007766 if (*p == CSI) // leadbyte CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007767 len += 2;
Bram Moolenaar13505972019-01-24 15:04:48 +01007768#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007769 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007770 if (*++p == K_SPECIAL // trailbyte K_SPECIAL or CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01007771#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007772 || *p == CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01007773#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007774 )
7775 len += 2;
7776 }
7777 if (len > 0)
7778 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02007779 arg = alloc(STRLEN(eap->arg) + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007780 if (arg != NULL)
7781 {
7782 len = 0;
7783 for (p = eap->arg; *p != NUL; ++p)
7784 {
7785 arg[len++] = *p;
Bram Moolenaar13505972019-01-24 15:04:48 +01007786#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007787 if (*p == CSI)
7788 {
7789 arg[len++] = KS_EXTRA;
7790 arg[len++] = (int)KE_CSI;
7791 }
Bram Moolenaar13505972019-01-24 15:04:48 +01007792#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007793 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007794 {
7795 arg[len++] = *++p;
7796 if (*p == K_SPECIAL)
7797 {
7798 arg[len++] = KS_SPECIAL;
7799 arg[len++] = KE_FILLER;
7800 }
Bram Moolenaar13505972019-01-24 15:04:48 +01007801#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007802 else if (*p == CSI)
7803 {
7804 arg[len++] = KS_EXTRA;
7805 arg[len++] = (int)KE_CSI;
7806 }
Bram Moolenaar13505972019-01-24 15:04:48 +01007807#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007808 }
7809 arg[len] = NUL;
7810 }
7811 }
7812 }
7813 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007814
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007815 ++ex_normal_busy;
7816 if (save_current_state(&save_state))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007817 {
7818 /*
7819 * Repeat the :normal command for each line in the range. When no
7820 * range given, execute it just once, without positioning the cursor
7821 * first.
7822 */
7823 do
7824 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007825 if (eap->addr_count != 0)
7826 {
7827 curwin->w_cursor.lnum = eap->line1++;
7828 curwin->w_cursor.col = 0;
Bram Moolenaard5d37532017-03-27 23:02:07 +02007829 check_cursor_moved(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007830 }
7831
Bram Moolenaar13505972019-01-24 15:04:48 +01007832 exec_normal_cmd(arg != NULL
7833 ? arg
7834 : eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007835 }
7836 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
7837 }
7838
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007839 // Might not return to the main loop when in an event handler.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007840 update_topline_cursor();
7841
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007842 restore_current_state(&save_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007843 --ex_normal_busy;
Bram Moolenaareda73602014-11-05 09:53:23 +01007844 setmouse();
Bram Moolenaareda73602014-11-05 09:53:23 +01007845#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007846 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaareda73602014-11-05 09:53:23 +01007847#endif
7848
Bram Moolenaar071d4272004-06-13 20:20:40 +00007849 vim_free(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007850}
7851
7852/*
Bram Moolenaar64969662005-12-14 21:59:55 +00007853 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007854 */
7855 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007856ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007857{
Bram Moolenaarfd371682005-01-14 21:42:54 +00007858 if (eap->forceit)
7859 {
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007860 // cursor line can be zero on startup
Bram Moolenaar09ca9322017-09-26 17:40:45 +02007861 if (!curwin->w_cursor.lnum)
7862 curwin->w_cursor.lnum = 1;
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007863 set_cursor_for_append_to_line();
Bram Moolenaarfd371682005-01-14 21:42:54 +00007864 }
Bram Moolenaareb58a242020-04-19 18:13:19 +02007865#ifdef FEAT_TERMINAL
7866 // Ignore this when running in an active terminal.
7867 if (term_job_running(curbuf->b_term))
7868 return;
7869#endif
Bram Moolenaarfd371682005-01-14 21:42:54 +00007870
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007871 // Ignore the command when already in Insert mode. Inserting an
7872 // expression register that invokes a function can do this.
Bram Moolenaara40c5002005-01-09 21:16:21 +00007873 if (State & INSERT)
7874 return;
7875
Bram Moolenaar64969662005-12-14 21:59:55 +00007876 if (eap->cmdidx == CMD_startinsert)
7877 restart_edit = 'a';
7878 else if (eap->cmdidx == CMD_startreplace)
7879 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00007880 else
Bram Moolenaar64969662005-12-14 21:59:55 +00007881 restart_edit = 'V';
7882
7883 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007884 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00007885 if (eap->cmdidx == CMD_startinsert)
7886 restart_edit = 'i';
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007887 curwin->w_curswant = 0; // avoid MAXCOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007888 }
7889}
7890
7891/*
7892 * ":stopinsert"
7893 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007894 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007895ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007896{
7897 restart_edit = 0;
7898 stop_insert_mode = TRUE;
Bram Moolenaarfd773e92016-04-02 19:39:16 +02007899 clearmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007900}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007901
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007902/*
7903 * Execute normal mode command "cmd".
7904 * "remap" can be REMAP_NONE or REMAP_YES.
7905 */
7906 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007907exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007908{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007909 // Stuff the argument into the typeahead buffer.
Bram Moolenaar25281632016-01-21 23:32:32 +01007910 ins_typebuf(cmd, remap, 0, TRUE, silent);
Bram Moolenaar586c70c2018-10-02 16:23:58 +02007911 exec_normal(FALSE, FALSE, FALSE);
Bram Moolenaar25281632016-01-21 23:32:32 +01007912}
Bram Moolenaar25281632016-01-21 23:32:32 +01007913
Bram Moolenaar25281632016-01-21 23:32:32 +01007914/*
7915 * Execute normal_cmd() until there is no typeahead left.
Bram Moolenaar586c70c2018-10-02 16:23:58 +02007916 * When "use_vpeekc" is TRUE use vpeekc() to check for available chars.
Bram Moolenaar25281632016-01-21 23:32:32 +01007917 */
7918 void
Bram Moolenaar586c70c2018-10-02 16:23:58 +02007919exec_normal(int was_typed, int use_vpeekc, int may_use_terminal_loop UNUSED)
Bram Moolenaar25281632016-01-21 23:32:32 +01007920{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007921 oparg_T oa;
Bram Moolenaar905dd902019-04-07 14:21:47 +02007922 int c;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007923
Bram Moolenaar905dd902019-04-07 14:21:47 +02007924 // When calling vpeekc() from feedkeys() it will return Ctrl_C when there
7925 // is nothing to get, so also check for Ctrl_C.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007926 clear_oparg(&oa);
7927 finish_op = FALSE;
Bram Moolenaar586c70c2018-10-02 16:23:58 +02007928 while ((!stuff_empty()
7929 || ((was_typed || !typebuf_typed()) && typebuf.tb_len > 0)
Bram Moolenaar905dd902019-04-07 14:21:47 +02007930 || (use_vpeekc && (c = vpeekc()) != NUL && c != Ctrl_C))
Bram Moolenaar586c70c2018-10-02 16:23:58 +02007931 && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007932 {
7933 update_topline_cursor();
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02007934#ifdef FEAT_TERMINAL
Bram Moolenaar655a82a2018-05-08 22:01:07 +02007935 if (may_use_terminal_loop && term_use_loop()
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02007936 && oa.op_type == OP_NOP && oa.regname == NUL
7937 && !VIsual_active)
7938 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007939 // If terminal_loop() returns OK we got a key that is handled
7940 // in Normal model. With FAIL we first need to position the
7941 // cursor and the screen needs to be redrawn.
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02007942 if (terminal_loop(TRUE) == OK)
7943 normal_cmd(&oa, TRUE);
7944 }
7945 else
7946#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007947 // execute a Normal mode cmd
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02007948 normal_cmd(&oa, TRUE);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007949 }
7950}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007951
Bram Moolenaar071d4272004-06-13 20:20:40 +00007952#ifdef FEAT_FIND_ID
7953 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007954ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007955{
7956 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
7957 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
7958 (linenr_T)1, (linenr_T)MAXLNUM);
7959}
7960
Bram Moolenaar4033c552017-09-16 20:54:51 +02007961#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007962/*
7963 * ":psearch"
7964 */
7965 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007966ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007967{
7968 g_do_tagpreview = p_pvh;
7969 ex_findpat(eap);
7970 g_do_tagpreview = 0;
7971}
7972#endif
7973
7974 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007975ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007976{
7977 int whole = TRUE;
7978 long n;
7979 char_u *p;
7980 int action;
7981
7982 switch (cmdnames[eap->cmdidx].cmd_name[2])
7983 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007984 case 'e': // ":psearch", ":isearch" and ":dsearch"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007985 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
7986 action = ACTION_GOTO;
7987 else
7988 action = ACTION_SHOW;
7989 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007990 case 'i': // ":ilist" and ":dlist"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007991 action = ACTION_SHOW_ALL;
7992 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007993 case 'u': // ":ijump" and ":djump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007994 action = ACTION_GOTO;
7995 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007996 default: // ":isplit" and ":dsplit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007997 action = ACTION_SPLIT;
7998 break;
7999 }
8000
8001 n = 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008002 if (vim_isdigit(*eap->arg)) // get count
Bram Moolenaar071d4272004-06-13 20:20:40 +00008003 {
8004 n = getdigits(&eap->arg);
8005 eap->arg = skipwhite(eap->arg);
8006 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008007 if (*eap->arg == '/') // Match regexp, not just whole words
Bram Moolenaar071d4272004-06-13 20:20:40 +00008008 {
8009 whole = FALSE;
8010 ++eap->arg;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02008011 p = skip_regexp(eap->arg, '/', p_magic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008012 if (*p)
8013 {
8014 *p++ = NUL;
8015 p = skipwhite(p);
8016
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008017 // Check for trailing illegal characters
Bram Moolenaarfaac4102020-04-20 17:46:14 +02008018 if (!ends_excmd2(eap->arg, p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008019 eap->errmsg = e_trailing;
8020 else
8021 eap->nextcmd = check_nextcmd(p);
8022 }
8023 }
8024 if (!eap->skip)
8025 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
8026 whole, !eap->forceit,
8027 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
8028 n, action, eap->line1, eap->line2);
8029}
8030#endif
8031
Bram Moolenaar071d4272004-06-13 20:20:40 +00008032
Bram Moolenaar4033c552017-09-16 20:54:51 +02008033#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00008034/*
8035 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
8036 */
8037 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008038ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008039{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008040 g_do_tagpreview = p_pvh; // will be reset to 0 in ex_tag_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00008041 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
8042}
8043
8044/*
8045 * ":pedit"
8046 */
8047 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008048ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008049{
8050 win_T *curwin_save = curwin;
8051
Bram Moolenaar3a2505c2020-03-09 16:40:41 +01008052 if (ERROR_IF_ANY_POPUP_WINDOW)
8053 return;
8054
Bram Moolenaar026587b2019-08-17 15:08:00 +02008055 // Open the preview window or popup and make it the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008056 g_do_tagpreview = p_pvh;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02008057 prepare_tagpreview(TRUE, TRUE, FALSE);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008058
Bram Moolenaar026587b2019-08-17 15:08:00 +02008059 // Edit the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008060 do_exedit(eap, NULL);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008061
Bram Moolenaar071d4272004-06-13 20:20:40 +00008062 if (curwin != curwin_save && win_valid(curwin_save))
8063 {
Bram Moolenaar026587b2019-08-17 15:08:00 +02008064 // Return cursor to where we were
Bram Moolenaar071d4272004-06-13 20:20:40 +00008065 validate_cursor();
8066 redraw_later(VALID);
8067 win_enter(curwin_save, TRUE);
8068 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01008069# ifdef FEAT_PROP_POPUP
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008070 else if (WIN_IS_POPUP(curwin))
8071 {
8072 // can't keep focus in popup window
8073 win_enter(firstwin, TRUE);
8074 }
8075# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008076 g_do_tagpreview = 0;
8077}
Bram Moolenaar4033c552017-09-16 20:54:51 +02008078#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008079
8080/*
8081 * ":stag", ":stselect" and ":stjump".
8082 */
8083 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008084ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008085{
8086 postponed_split = -1;
8087 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008088 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008089 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
8090 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008091 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008092}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008093
8094/*
8095 * ":tag", ":tselect", ":tjump", ":tnext", etc.
8096 */
8097 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008098ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008099{
8100 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
8101}
8102
8103 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008104ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008105{
8106 int cmd;
8107
8108 switch (name[1])
8109 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008110 case 'j': cmd = DT_JUMP; // ":tjump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008111 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008112 case 's': cmd = DT_SELECT; // ":tselect"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008113 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008114 case 'p': cmd = DT_PREV; // ":tprevious"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008115 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008116 case 'N': cmd = DT_PREV; // ":tNext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008117 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008118 case 'n': cmd = DT_NEXT; // ":tnext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008119 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008120 case 'o': cmd = DT_POP; // ":pop"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008121 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008122 case 'f': // ":tfirst"
8123 case 'r': cmd = DT_FIRST; // ":trewind"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008124 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008125 case 'l': cmd = DT_LAST; // ":tlast"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008126 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008127 default: // ":tag"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008128#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +00008129 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008130 {
Bram Moolenaard4db7712016-11-12 19:16:46 +01008131 ex_cstag(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008132 return;
8133 }
8134#endif
8135 cmd = DT_TAG;
8136 break;
8137 }
8138
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00008139 if (name[0] == 'l')
8140 {
8141#ifndef FEAT_QUICKFIX
8142 ex_ni(eap);
8143 return;
8144#else
8145 cmd = DT_LTAG;
8146#endif
8147 }
8148
Bram Moolenaar071d4272004-06-13 20:20:40 +00008149 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
8150 eap->forceit, TRUE);
8151}
8152
8153/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008154 * Check "str" for starting with a special cmdline variable.
8155 * If found return one of the SPEC_ values and set "*usedlen" to the length of
8156 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
8157 */
8158 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008159find_cmdline_var(char_u *src, int *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008160{
8161 int len;
8162 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +00008163 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008164 "%",
8165#define SPEC_PERC 0
8166 "#",
Bram Moolenaar65f08472017-09-10 18:16:20 +02008167#define SPEC_HASH (SPEC_PERC + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008168 "<cword>", // cursor word
Bram Moolenaar65f08472017-09-10 18:16:20 +02008169#define SPEC_CWORD (SPEC_HASH + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008170 "<cWORD>", // cursor WORD
Bram Moolenaar65f08472017-09-10 18:16:20 +02008171#define SPEC_CCWORD (SPEC_CWORD + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008172 "<cexpr>", // expr under cursor
Bram Moolenaar65f08472017-09-10 18:16:20 +02008173#define SPEC_CEXPR (SPEC_CCWORD + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008174 "<cfile>", // cursor path name
Bram Moolenaar65f08472017-09-10 18:16:20 +02008175#define SPEC_CFILE (SPEC_CEXPR + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008176 "<sfile>", // ":so" file name
Bram Moolenaar65f08472017-09-10 18:16:20 +02008177#define SPEC_SFILE (SPEC_CFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008178 "<slnum>", // ":so" file line number
Bram Moolenaar65f08472017-09-10 18:16:20 +02008179#define SPEC_SLNUM (SPEC_SFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008180 "<afile>", // autocommand file name
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008181#define SPEC_AFILE (SPEC_SLNUM + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008182 "<abuf>", // autocommand buffer number
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008183#define SPEC_ABUF (SPEC_AFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008184 "<amatch>", // autocommand match name
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008185#define SPEC_AMATCH (SPEC_ABUF + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008186 "<sflnum>", // script file line number
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008187#define SPEC_SFLNUM (SPEC_AMATCH + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008188#ifdef FEAT_CLIENTSERVER
8189 "<client>"
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008190# define SPEC_CLIENT (SPEC_SFLNUM + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008191#endif
8192 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008193
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008194 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008195 {
8196 len = (int)STRLEN(spec_str[i]);
8197 if (STRNCMP(src, spec_str[i], len) == 0)
8198 {
8199 *usedlen = len;
8200 return i;
8201 }
8202 }
8203 return -1;
8204}
8205
8206/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008207 * Evaluate cmdline variables.
8208 *
8209 * change '%' to curbuf->b_ffname
8210 * '#' to curwin->w_altfile
8211 * '<cword>' to word under the cursor
8212 * '<cWORD>' to WORD under the cursor
Bram Moolenaar8071cb22019-07-12 17:58:01 +02008213 * '<cexpr>' to C-expression under the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00008214 * '<cfile>' to path name under the cursor
8215 * '<sfile>' to sourced file name
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008216 * '<slnum>' to sourced file line number
Bram Moolenaar071d4272004-06-13 20:20:40 +00008217 * '<afile>' to file name for autocommand
8218 * '<abuf>' to buffer number for autocommand
8219 * '<amatch>' to matching name for autocommand
8220 *
8221 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
8222 * "" for error without a message) and NULL is returned.
8223 * Returns an allocated string if a valid match was found.
8224 * Returns NULL if no match was found. "usedlen" then still contains the
8225 * number of characters to skip.
8226 */
8227 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008228eval_vars(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008229 char_u *src, // pointer into commandline
8230 char_u *srcstart, // beginning of valid memory for src
8231 int *usedlen, // characters after src that are used
8232 linenr_T *lnump, // line number for :e command, or NULL
8233 char **errormsg, // pointer to error message
8234 int *escaped) // return value has escaped white space (can
8235 // be NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008236{
8237 int i;
8238 char_u *s;
8239 char_u *result;
8240 char_u *resultbuf = NULL;
8241 int resultlen;
8242 buf_T *buf;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008243 int valid = VALID_HEAD + VALID_PATH; // assume valid result
Bram Moolenaar071d4272004-06-13 20:20:40 +00008244 int spec_idx;
Bram Moolenaarfd249462018-07-28 16:14:30 +02008245 int tilde_file = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008246 int skip_mod = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008247 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008248
8249 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +00008250 if (escaped != NULL)
8251 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008252
8253 /*
8254 * Check if there is something to do.
8255 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008256 spec_idx = find_cmdline_var(src, usedlen);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008257 if (spec_idx < 0) // no match
Bram Moolenaar071d4272004-06-13 20:20:40 +00008258 {
8259 *usedlen = 1;
8260 return NULL;
8261 }
8262
8263 /*
8264 * Skip when preceded with a backslash "\%" and "\#".
8265 * Note: In "\\%" the % is also not recognized!
8266 */
8267 if (src > srcstart && src[-1] == '\\')
8268 {
8269 *usedlen = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008270 STRMOVE(src - 1, src); // remove backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00008271 return NULL;
8272 }
8273
8274 /*
8275 * word or WORD under cursor
8276 */
Bram Moolenaar65f08472017-09-10 18:16:20 +02008277 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD
8278 || spec_idx == SPEC_CEXPR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008279 {
Bram Moolenaar65f08472017-09-10 18:16:20 +02008280 resultlen = find_ident_under_cursor(&result,
8281 spec_idx == SPEC_CWORD ? (FIND_IDENT | FIND_STRING)
8282 : spec_idx == SPEC_CEXPR ? (FIND_IDENT | FIND_STRING | FIND_EVAL)
8283 : FIND_STRING);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008284 if (resultlen == 0)
8285 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008286 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008287 return NULL;
8288 }
8289 }
8290
8291 /*
8292 * '#': Alternate file name
8293 * '%': Current file name
8294 * File name under the cursor
8295 * File name for autocommand
8296 * and following modifiers
8297 */
8298 else
8299 {
8300 switch (spec_idx)
8301 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008302 case SPEC_PERC: // '%': current file
Bram Moolenaar071d4272004-06-13 20:20:40 +00008303 if (curbuf->b_fname == NULL)
8304 {
8305 result = (char_u *)"";
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008306 valid = 0; // Must have ":p:h" to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00008307 }
8308 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008309 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008310 result = curbuf->b_fname;
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008311 tilde_file = STRCMP(result, "~") == 0;
8312 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008313 break;
8314
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008315 case SPEC_HASH: // '#' or "#99": alternate file
8316 if (src[1] == '#') // "##": the argument list
Bram Moolenaar071d4272004-06-13 20:20:40 +00008317 {
8318 result = arg_all();
8319 resultbuf = result;
8320 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +00008321 if (escaped != NULL)
8322 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008323 skip_mod = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008324 break;
8325 }
8326 s = src + 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008327 if (*s == '<') // "#<99" uses v:oldfiles
Bram Moolenaard812df62008-11-09 12:46:09 +00008328 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008329 i = (int)getdigits(&s);
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008330 if (s == src + 2 && src[1] == '-')
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008331 // just a minus sign, don't skip over it
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008332 s--;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008333 *usedlen = (int)(s - src); // length of what we expand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008334
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008335 if (src[1] == '<' && i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008336 {
Bram Moolenaard812df62008-11-09 12:46:09 +00008337 if (*usedlen < 2)
8338 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008339 // Should we give an error message for #<text?
Bram Moolenaard812df62008-11-09 12:46:09 +00008340 *usedlen = 1;
8341 return NULL;
8342 }
8343#ifdef FEAT_EVAL
8344 result = list_find_str(get_vim_var_list(VV_OLDFILES),
8345 (long)i);
8346 if (result == NULL)
8347 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008348 *errormsg = "";
Bram Moolenaard812df62008-11-09 12:46:09 +00008349 return NULL;
8350 }
8351#else
Bram Moolenaar8e481e82019-01-15 20:07:48 +01008352 *errormsg = _("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008353 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00008354#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008355 }
8356 else
Bram Moolenaard812df62008-11-09 12:46:09 +00008357 {
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008358 if (i == 0 && src[1] == '<' && *usedlen > 1)
8359 *usedlen = 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00008360 buf = buflist_findnr(i);
8361 if (buf == NULL)
8362 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008363 *errormsg = _("E194: No alternate file name to substitute for '#'");
Bram Moolenaard812df62008-11-09 12:46:09 +00008364 return NULL;
8365 }
8366 if (lnump != NULL)
8367 *lnump = ECMD_LAST;
8368 if (buf->b_fname == NULL)
8369 {
8370 result = (char_u *)"";
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008371 valid = 0; // Must have ":p:h" to be valid
Bram Moolenaard812df62008-11-09 12:46:09 +00008372 }
8373 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008374 {
Bram Moolenaard812df62008-11-09 12:46:09 +00008375 result = buf->b_fname;
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008376 tilde_file = STRCMP(result, "~") == 0;
8377 }
Bram Moolenaard812df62008-11-09 12:46:09 +00008378 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008379 break;
8380
8381#ifdef FEAT_SEARCHPATH
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008382 case SPEC_CFILE: // file name under cursor
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008383 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008384 if (result == NULL)
8385 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008386 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008387 return NULL;
8388 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008389 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00008390 break;
8391#endif
8392
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008393 case SPEC_AFILE: // file name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008394 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +00008395 if (result != NULL && !autocmd_fname_full)
8396 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008397 // Still need to turn the fname into a full path. It is
8398 // postponed to avoid a delay when <afile> is not used.
Bram Moolenaarf6dad432008-09-18 19:29:58 +00008399 autocmd_fname_full = TRUE;
8400 result = FullName_save(autocmd_fname, FALSE);
8401 vim_free(autocmd_fname);
8402 autocmd_fname = result;
8403 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008404 if (result == NULL)
8405 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008406 *errormsg = _("E495: no autocommand file name to substitute for \"<afile>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008407 return NULL;
8408 }
Bram Moolenaara0174af2008-01-02 20:08:25 +00008409 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008410 break;
8411
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008412 case SPEC_ABUF: // buffer number for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008413 if (autocmd_bufnr <= 0)
8414 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008415 *errormsg = _("E496: no autocommand buffer number to substitute for \"<abuf>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008416 return NULL;
8417 }
8418 sprintf((char *)strbuf, "%d", autocmd_bufnr);
8419 result = strbuf;
8420 break;
8421
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008422 case SPEC_AMATCH: // match name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008423 result = autocmd_match;
8424 if (result == NULL)
8425 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008426 *errormsg = _("E497: no autocommand match name to substitute for \"<amatch>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008427 return NULL;
8428 }
8429 break;
8430
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008431 case SPEC_SFILE: // file name for ":so" command
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008432 result = estack_sfile();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008433 if (result == NULL)
8434 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008435 *errormsg = _("E498: no :source file name to substitute for \"<sfile>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008436 return NULL;
8437 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008438 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00008439 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008440
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008441 case SPEC_SLNUM: // line in file for ":so" command
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008442 if (SOURCING_NAME == NULL || SOURCING_LNUM == 0)
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008443 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008444 *errormsg = _("E842: no line number to use for \"<slnum>\"");
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008445 return NULL;
8446 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008447 sprintf((char *)strbuf, "%ld", SOURCING_LNUM);
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008448 result = strbuf;
8449 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008450
8451#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008452 case SPEC_SFLNUM: // line in script file
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008453 if (current_sctx.sc_lnum + SOURCING_LNUM == 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008454 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008455 *errormsg = _("E961: no line number to use for \"<sflnum>\"");
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008456 return NULL;
8457 }
8458 sprintf((char *)strbuf, "%ld",
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008459 (long)(current_sctx.sc_lnum + SOURCING_LNUM));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008460 result = strbuf;
8461 break;
8462#endif
8463
8464#ifdef FEAT_CLIENTSERVER
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008465 case SPEC_CLIENT: // Source of last submitted input
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008466 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
8467 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008468 result = strbuf;
8469 break;
8470#endif
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008471
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02008472 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008473 result = (char_u *)""; // avoid gcc warning
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02008474 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008475 }
8476
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008477 resultlen = (int)STRLEN(result); // length of new string
8478 if (src[*usedlen] == '<') // remove the file name extension
Bram Moolenaar071d4272004-06-13 20:20:40 +00008479 {
8480 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008481 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008482 resultlen = (int)(s - result);
8483 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008484 else if (!skip_mod)
8485 {
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008486 valid |= modify_fname(src, tilde_file, usedlen, &result, &resultbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008487 &resultlen);
8488 if (result == NULL)
8489 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008490 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008491 return NULL;
8492 }
8493 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008494 }
8495
8496 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
8497 {
8498 if (valid != VALID_HEAD + VALID_PATH)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008499 // xgettext:no-c-format
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008500 *errormsg = _("E499: Empty file name for '%' or '#', only works with \":p:h\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008501 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008502 *errormsg = _("E500: Evaluates to an empty string");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008503 result = NULL;
8504 }
8505 else
8506 result = vim_strnsave(result, resultlen);
8507 vim_free(resultbuf);
8508 return result;
8509}
8510
8511/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008512 * Expand the <sfile> string in "arg".
8513 *
8514 * Returns an allocated string, or NULL for any error.
8515 */
8516 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008517expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008518{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008519 char *errormsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008520 int len;
8521 char_u *result;
8522 char_u *newres;
8523 char_u *repl;
8524 int srclen;
8525 char_u *p;
8526
8527 result = vim_strsave(arg);
8528 if (result == NULL)
8529 return NULL;
8530
8531 for (p = result; *p; )
8532 {
8533 if (STRNCMP(p, "<sfile>", 7) != 0)
8534 ++p;
8535 else
8536 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008537 // replace "<sfile>" with the sourced file name, and do ":" stuff
Bram Moolenaar63b92542007-03-27 14:57:09 +00008538 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008539 if (errormsg != NULL)
8540 {
8541 if (*errormsg)
8542 emsg(errormsg);
8543 vim_free(result);
8544 return NULL;
8545 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008546 if (repl == NULL) // no match (cannot happen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008547 {
8548 p += srclen;
8549 continue;
8550 }
8551 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
8552 newres = alloc(len);
8553 if (newres == NULL)
8554 {
8555 vim_free(repl);
8556 vim_free(result);
8557 return NULL;
8558 }
8559 mch_memmove(newres, result, (size_t)(p - result));
8560 STRCPY(newres + (p - result), repl);
8561 len = (int)STRLEN(newres);
8562 STRCAT(newres, p + srclen);
8563 vim_free(repl);
8564 vim_free(result);
8565 result = newres;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008566 p = newres + len; // continue after the match
Bram Moolenaar071d4272004-06-13 20:20:40 +00008567 }
8568 }
8569
8570 return result;
8571}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008572
Bram Moolenaar071d4272004-06-13 20:20:40 +00008573#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00008574/*
Bram Moolenaard9462e32011-04-11 21:35:11 +02008575 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +00008576 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +00008577 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008578 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008579dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008580{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008581 if (fname == NULL)
8582 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +02008583 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008584}
8585#endif
8586
8587/*
8588 * ":behave {mswin,xterm}"
8589 */
8590 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008591ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008592{
8593 if (STRCMP(eap->arg, "mswin") == 0)
8594 {
8595 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
8596 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
8597 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
8598 set_option_value((char_u *)"keymodel", 0L,
8599 (char_u *)"startsel,stopsel", 0);
8600 }
8601 else if (STRCMP(eap->arg, "xterm") == 0)
8602 {
8603 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
8604 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
8605 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
8606 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
8607 }
8608 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008609 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008610}
8611
Bram Moolenaar071d4272004-06-13 20:20:40 +00008612static int filetype_detect = FALSE;
8613static int filetype_plugin = FALSE;
8614static int filetype_indent = FALSE;
8615
8616/*
8617 * ":filetype [plugin] [indent] {on,off,detect}"
8618 * on: Load the filetype.vim file to install autocommands for file types.
8619 * off: Load the ftoff.vim file to remove all autocommands for file types.
8620 * plugin on: load filetype.vim and ftplugin.vim
8621 * plugin off: load ftplugof.vim
8622 * indent on: load filetype.vim and indent.vim
8623 * indent off: load indoff.vim
8624 */
8625 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008626ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008627{
8628 char_u *arg = eap->arg;
8629 int plugin = FALSE;
8630 int indent = FALSE;
8631
8632 if (*eap->arg == NUL)
8633 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008634 // Print current status.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008635 smsg("filetype detection:%s plugin:%s indent:%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00008636 filetype_detect ? "ON" : "OFF",
8637 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
8638 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
8639 return;
8640 }
8641
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008642 // Accept "plugin" and "indent" in any order.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008643 for (;;)
8644 {
8645 if (STRNCMP(arg, "plugin", 6) == 0)
8646 {
8647 plugin = TRUE;
8648 arg = skipwhite(arg + 6);
8649 continue;
8650 }
8651 if (STRNCMP(arg, "indent", 6) == 0)
8652 {
8653 indent = TRUE;
8654 arg = skipwhite(arg + 6);
8655 continue;
8656 }
8657 break;
8658 }
8659 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
8660 {
8661 if (*arg == 'o' || !filetype_detect)
8662 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008663 source_runtime((char_u *)FILETYPE_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008664 filetype_detect = TRUE;
8665 if (plugin)
8666 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008667 source_runtime((char_u *)FTPLUGIN_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008668 filetype_plugin = TRUE;
8669 }
8670 if (indent)
8671 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008672 source_runtime((char_u *)INDENT_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008673 filetype_indent = TRUE;
8674 }
8675 }
8676 if (*arg == 'd')
8677 {
Bram Moolenaar1610d052016-06-09 22:53:01 +02008678 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +00008679 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008680 }
8681 }
8682 else if (STRCMP(arg, "off") == 0)
8683 {
8684 if (plugin || indent)
8685 {
8686 if (plugin)
8687 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008688 source_runtime((char_u *)FTPLUGOF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008689 filetype_plugin = FALSE;
8690 }
8691 if (indent)
8692 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008693 source_runtime((char_u *)INDOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008694 filetype_indent = FALSE;
8695 }
8696 }
8697 else
8698 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008699 source_runtime((char_u *)FTOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008700 filetype_detect = FALSE;
8701 }
8702 }
8703 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008704 semsg(_(e_invarg2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008705}
8706
8707/*
Bram Moolenaar3e545692017-06-04 19:00:32 +02008708 * ":setfiletype [FALLBACK] {name}"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008709 */
8710 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008711ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008712{
8713 if (!did_filetype)
Bram Moolenaar3e545692017-06-04 19:00:32 +02008714 {
8715 char_u *arg = eap->arg;
8716
8717 if (STRNCMP(arg, "FALLBACK ", 9) == 0)
8718 arg += 9;
8719
8720 set_option_value((char_u *)"filetype", 0L, arg, OPT_LOCAL);
8721 if (arg != eap->arg)
8722 did_filetype = FALSE;
8723 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008724}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008725
Bram Moolenaar071d4272004-06-13 20:20:40 +00008726 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008727ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008728{
8729#ifdef FEAT_DIGRAPHS
8730 if (*eap->arg != NUL)
8731 putdigraph(eap->arg);
8732 else
Bram Moolenaareae8ae12018-12-14 18:53:02 +01008733 listdigraphs(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008734#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008735 emsg(_("E196: No digraphs in this version"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008736#endif
8737}
8738
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02008739#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
8740 void
8741set_no_hlsearch(int flag)
8742{
8743 no_hlsearch = flag;
8744# ifdef FEAT_EVAL
8745 set_vim_var_nr(VV_HLSEARCH, !no_hlsearch && p_hls);
8746# endif
8747}
8748
Bram Moolenaar071d4272004-06-13 20:20:40 +00008749/*
8750 * ":nohlsearch"
8751 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008752 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008753ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008754{
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02008755 set_no_hlsearch(TRUE);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00008756 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008757}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008758#endif
8759
8760#ifdef FEAT_CRYPT
8761/*
8762 * ":X": Get crypt key
8763 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008764 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008765ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008766{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +01008767 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02008768 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008769}
8770#endif
8771
8772#ifdef FEAT_FOLDING
8773 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008774ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008775{
8776 if (foldManualAllowed(TRUE))
8777 foldCreate(eap->line1, eap->line2);
8778}
8779
8780 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008781ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008782{
8783 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
8784 eap->forceit, FALSE);
8785}
8786
8787 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008788ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008789{
8790 linenr_T lnum;
8791
Bram Moolenaar4facea32019-10-12 20:17:40 +02008792# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02008793 start_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +02008794# endif
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02008795
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008796 // First set the marks for all lines closed/open.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008797 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
8798 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
8799 ml_setmarked(lnum);
8800
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008801 // Execute the command on the marked lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008802 global_exe(eap->arg);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008803 ml_clearmarked(); // clear rest of the marks
Bram Moolenaar4facea32019-10-12 20:17:40 +02008804# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02008805 end_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +02008806# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008807}
8808#endif
Bram Moolenaarf5291f32017-09-14 22:55:37 +02008809
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008810#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar39665952018-08-15 20:59:48 +02008811/*
8812 * Returns TRUE if the supplied Ex cmdidx is for a location list command
8813 * instead of a quickfix command.
8814 */
8815 int
8816is_loclist_cmd(int cmdidx)
8817{
Bram Moolenaar74c8be22018-08-23 22:51:40 +02008818 if (cmdidx < 0 || cmdidx >= CMD_SIZE)
Bram Moolenaar39665952018-08-15 20:59:48 +02008819 return FALSE;
8820 return cmdnames[cmdidx].cmd_name[0] == 'l';
8821}
8822#endif
8823
Bram Moolenaar4facea32019-10-12 20:17:40 +02008824#if defined(FEAT_TIMERS) || defined(PROTO)
Bram Moolenaarf5291f32017-09-14 22:55:37 +02008825 int
8826get_pressedreturn(void)
8827{
8828 return ex_pressedreturn;
8829}
8830
8831 void
8832set_pressedreturn(int val)
8833{
8834 ex_pressedreturn = val;
8835}
8836#endif