blob: 96eac7fbe489b06df97117c4d59caca43ed87468 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * ex_docmd.c: functions for executing an Ex command line.
12 */
13
14#include "vim.h"
15
Bram Moolenaar071d4272004-06-13 20:20:40 +000016static int quitmore = 0;
17static int ex_pressedreturn = FALSE;
18#ifndef FEAT_PRINTER
19# define ex_hardcopy ex_ni
20#endif
21
Bram Moolenaar071d4272004-06-13 20:20:40 +000022#ifdef FEAT_EVAL
Bram Moolenaarddef1292019-12-16 17:10:33 +010023static char_u *do_one_cmd(char_u **, int, cstack_T *, char_u *(*fgetline)(int, void *, int, int), void *cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024#else
Bram Moolenaare96a2492019-06-25 04:12:16 +020025static char_u *do_one_cmd(char_u **, int, char_u *(*fgetline)(int, void *, int, int), void *cookie);
Bram Moolenaar217e1b82019-12-01 21:41:28 +010026static int if_level = 0; // depth in :if
Bram Moolenaar071d4272004-06-13 20:20:40 +000027#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010028static void append_command(char_u *cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000029
Bram Moolenaar071d4272004-06-13 20:20:40 +000030#ifndef FEAT_MENU
31# define ex_emenu ex_ni
32# define ex_menu ex_ni
33# define ex_menutranslate ex_ni
34#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010035static void ex_autocmd(exarg_T *eap);
36static void ex_doautocmd(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010037static void ex_bunload(exarg_T *eap);
38static void ex_buffer(exarg_T *eap);
39static void ex_bmodified(exarg_T *eap);
40static void ex_bnext(exarg_T *eap);
41static void ex_bprevious(exarg_T *eap);
42static void ex_brewind(exarg_T *eap);
43static void ex_blast(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010044static char_u *getargcmd(char_u **);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010045static int getargopt(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000046#ifndef FEAT_QUICKFIX
47# define ex_make ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +000048# define ex_cbuffer ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000049# define ex_cc ex_ni
50# define ex_cnext ex_ni
Bram Moolenaar3ff33112019-05-03 21:56:35 +020051# define ex_cbelow ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000052# define ex_cfile ex_ni
53# define qf_list ex_ni
54# define qf_age ex_ni
Bram Moolenaarf6acffb2016-07-16 16:54:24 +020055# define qf_history ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000056# define ex_helpgrep ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +000057# define ex_vimgrep ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000058#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +020059#if !defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +000060# define ex_cclose ex_ni
61# define ex_copen ex_ni
62# define ex_cwindow ex_ni
Bram Moolenaardcb17002016-07-07 18:58:59 +020063# define ex_cbottom ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000064#endif
Bram Moolenaar1e015462005-09-25 22:16:38 +000065#if !defined(FEAT_QUICKFIX) || !defined(FEAT_EVAL)
66# define ex_cexpr ex_ni
67#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000068
Bram Moolenaarb7316892019-05-01 18:08:42 +020069static linenr_T get_address(exarg_T *, char_u **, cmd_addr_T addr_type, int skip, int silent, int to_other_file, int address_count);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010070static void get_flags(exarg_T *eap);
Bram Moolenaar85363ab2010-07-18 13:58:26 +020071#if !defined(FEAT_PERL) \
72 || !defined(FEAT_PYTHON) || !defined(FEAT_PYTHON3) \
73 || !defined(FEAT_TCL) \
74 || !defined(FEAT_RUBY) \
75 || !defined(FEAT_LUA) \
76 || !defined(FEAT_MZSCHEME)
Bram Moolenaar7bb75552007-07-16 18:39:49 +000077# define HAVE_EX_SCRIPT_NI
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010078static void ex_script_ni(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000079#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010080static char *invalid_range(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010081static void correct_range(exarg_T *eap);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000082#ifdef FEAT_QUICKFIX
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010083static char_u *replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000084#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010085static char_u *repl_cmdline(exarg_T *eap, char_u *src, int srclen, char_u *repl, char_u **cmdlinep);
86static void ex_highlight(exarg_T *eap);
87static void ex_colorscheme(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010088static void ex_cquit(exarg_T *eap);
89static void ex_quit_all(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010090static void ex_close(exarg_T *eap);
91static void ex_win_close(int forceit, win_T *win, tabpage_T *tp);
92static void ex_only(exarg_T *eap);
93static void ex_resize(exarg_T *eap);
94static void ex_stag(exarg_T *eap);
95static void ex_tabclose(exarg_T *eap);
96static void ex_tabonly(exarg_T *eap);
97static void ex_tabnext(exarg_T *eap);
98static void ex_tabmove(exarg_T *eap);
99static void ex_tabs(exarg_T *eap);
Bram Moolenaar4033c552017-09-16 20:54:51 +0200100#if defined(FEAT_QUICKFIX)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100101static void ex_pclose(exarg_T *eap);
102static void ex_ptag(exarg_T *eap);
103static void ex_pedit(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000104#else
105# define ex_pclose ex_ni
106# define ex_ptag ex_ni
107# define ex_pedit ex_ni
108#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100109static void ex_hide(exarg_T *eap);
110static void ex_stop(exarg_T *eap);
111static void ex_exit(exarg_T *eap);
112static void ex_print(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000113#ifdef FEAT_BYTEOFF
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100114static void ex_goto(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000115#else
116# define ex_goto ex_ni
117#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100118static void ex_shell(exarg_T *eap);
119static void ex_preserve(exarg_T *eap);
120static void ex_recover(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100121static void ex_mode(exarg_T *eap);
122static void ex_wrongmodifier(exarg_T *eap);
123static void ex_find(exarg_T *eap);
124static void ex_open(exarg_T *eap);
125static void ex_edit(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000126#ifndef FEAT_GUI
127# define ex_gui ex_nogui
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100128static void ex_nogui(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000129#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +0100130#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100131static void ex_tearoff(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000132#else
133# define ex_tearoff ex_ni
134#endif
Bram Moolenaar29a2c082018-03-05 21:06:23 +0100135#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
136 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100137static void ex_popup(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000138#else
139# define ex_popup ex_ni
140#endif
141#ifndef FEAT_GUI_MSWIN
142# define ex_simalt ex_ni
143#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000144#if !defined(FEAT_GUI_MSWIN) && !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000145# define gui_mch_find_dialog ex_ni
146# define gui_mch_replace_dialog ex_ni
147#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000148#if !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000149# define ex_helpfind ex_ni
150#endif
151#ifndef FEAT_CSCOPE
Bram Moolenaard4db7712016-11-12 19:16:46 +0100152# define ex_cscope ex_ni
153# define ex_scscope ex_ni
154# define ex_cstag ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000155#endif
156#ifndef FEAT_SYN_HL
157# define ex_syntax ex_ni
Bram Moolenaar860cae12010-06-05 23:22:07 +0200158# define ex_ownsyntax ex_ni
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000159#endif
Bram Moolenaarf7512552013-06-06 14:55:19 +0200160#if !defined(FEAT_SYN_HL) || !defined(FEAT_PROFILE)
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +0200161# define ex_syntime ex_ni
162#endif
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000163#ifndef FEAT_SPELL
Bram Moolenaarb765d632005-06-07 21:00:02 +0000164# define ex_spell ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000165# define ex_mkspell ex_ni
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000166# define ex_spelldump ex_ni
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000167# define ex_spellinfo ex_ni
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000168# define ex_spellrepall ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000169#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200170#ifndef FEAT_PERSISTENT_UNDO
171# define ex_rundo ex_ni
172# define ex_wundo ex_ni
173#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200174#ifndef FEAT_LUA
175# define ex_lua ex_script_ni
176# define ex_luado ex_ni
177# define ex_luafile ex_ni
178#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000179#ifndef FEAT_MZSCHEME
180# define ex_mzscheme ex_script_ni
181# define ex_mzfile ex_ni
182#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000183#ifndef FEAT_PERL
184# define ex_perl ex_script_ni
185# define ex_perldo ex_ni
186#endif
187#ifndef FEAT_PYTHON
188# define ex_python ex_script_ni
Bram Moolenaard620aa92013-05-17 16:40:06 +0200189# define ex_pydo ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000190# define ex_pyfile ex_ni
191#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200192#ifndef FEAT_PYTHON3
Bram Moolenaar368373e2010-07-19 20:46:22 +0200193# define ex_py3 ex_script_ni
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200194# define ex_py3do ex_ni
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200195# define ex_py3file ex_ni
196#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100197#if !defined(FEAT_PYTHON) && !defined(FEAT_PYTHON3)
198# define ex_pyx ex_script_ni
199# define ex_pyxdo ex_ni
200# define ex_pyxfile ex_ni
201#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000202#ifndef FEAT_TCL
203# define ex_tcl ex_script_ni
204# define ex_tcldo ex_ni
205# define ex_tclfile ex_ni
206#endif
207#ifndef FEAT_RUBY
208# define ex_ruby ex_script_ni
209# define ex_rubydo ex_ni
210# define ex_rubyfile ex_ni
211#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000212#ifndef FEAT_KEYMAP
213# define ex_loadkeymap ex_ni
214#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100215static void ex_swapname(exarg_T *eap);
216static void ex_syncbind(exarg_T *eap);
217static void ex_read(exarg_T *eap);
218static void ex_pwd(exarg_T *eap);
219static void ex_equal(exarg_T *eap);
220static void ex_sleep(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100221static void ex_winsize(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100222static void ex_wincmd(exarg_T *eap);
Bram Moolenaar843ee412004-06-30 16:16:41 +0000223#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100224static void ex_winpos(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000225#else
226# define ex_winpos ex_ni
227#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100228static void ex_operators(exarg_T *eap);
229static void ex_put(exarg_T *eap);
230static void ex_copymove(exarg_T *eap);
231static void ex_submagic(exarg_T *eap);
232static void ex_join(exarg_T *eap);
233static void ex_at(exarg_T *eap);
234static void ex_bang(exarg_T *eap);
235static void ex_undo(exarg_T *eap);
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200236#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100237static void ex_wundo(exarg_T *eap);
238static void ex_rundo(exarg_T *eap);
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200239#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100240static void ex_redo(exarg_T *eap);
241static void ex_later(exarg_T *eap);
242static void ex_redir(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100243static void ex_redrawstatus(exarg_T *eap);
Bram Moolenaare12bab32019-01-08 22:02:56 +0100244static void ex_redrawtabline(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100245static void close_redir(void);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100246static void ex_mark(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100247static void ex_startinsert(exarg_T *eap);
248static void ex_stopinsert(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000249#ifdef FEAT_FIND_ID
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100250static void ex_checkpath(exarg_T *eap);
251static void ex_findpat(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000252#else
253# define ex_findpat ex_ni
254# define ex_checkpath ex_ni
255#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +0200256#if defined(FEAT_FIND_ID) && defined(FEAT_QUICKFIX)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100257static void ex_psearch(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000258#else
259# define ex_psearch ex_ni
260#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100261static void ex_tag(exarg_T *eap);
262static void ex_tag_cmd(exarg_T *eap, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000263#ifndef FEAT_EVAL
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200264# define ex_break ex_ni
265# define ex_breakadd ex_ni
266# define ex_breakdel ex_ni
267# define ex_breaklist ex_ni
268# define ex_call ex_ni
269# define ex_catch ex_ni
270# define ex_compiler ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200271# define ex_continue ex_ni
272# define ex_debug ex_ni
273# define ex_debuggreedy ex_ni
Bram Moolenaar822ba242020-05-24 23:00:18 +0200274# define ex_def ex_ni
275# define ex_defcompile ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200276# define ex_delfunction ex_ni
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100277# define ex_disassemble ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000278# define ex_echo ex_ni
279# define ex_echohl ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000280# define ex_else ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200281# define ex_endfunction ex_ni
282# define ex_endif ex_ni
283# define ex_endtry ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000284# define ex_endwhile ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200285# define ex_eval ex_ni
286# define ex_execute ex_ni
287# define ex_finally ex_ni
288# define ex_finish ex_ni
289# define ex_function ex_ni
290# define ex_if ex_ni
291# define ex_let ex_ni
292# define ex_lockvar ex_ni
293# define ex_oldfiles ex_ni
294# define ex_options ex_ni
295# define ex_packadd ex_ni
296# define ex_packloadall ex_ni
297# define ex_return ex_ni
298# define ex_scriptnames ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000299# define ex_throw ex_ni
300# define ex_try ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000301# define ex_unlet ex_ni
Bram Moolenaar65c1b012005-01-31 19:02:28 +0000302# define ex_unlockvar ex_ni
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100303# define ex_vim9script ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200304# define ex_while ex_ni
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100305# define ex_import ex_ni
306# define ex_export ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000307#endif
Bram Moolenaar84538072019-07-28 14:15:42 +0200308#ifndef FEAT_SESSION
Bram Moolenaar071d4272004-06-13 20:20:40 +0000309# define ex_loadview ex_ni
310#endif
Bram Moolenaardefa0672019-07-21 19:25:37 +0200311#ifndef FEAT_VIMINFO
Bram Moolenaar071d4272004-06-13 20:20:40 +0000312# define ex_viminfo ex_ni
313#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100314static void ex_behave(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100315static void ex_filetype(exarg_T *eap);
316static void ex_setfiletype(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000317#ifndef FEAT_DIFF
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000318# define ex_diffoff ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000319# define ex_diffpatch ex_ni
320# define ex_diffgetput ex_ni
321# define ex_diffsplit ex_ni
322# define ex_diffthis ex_ni
323# define ex_diffupdate ex_ni
324#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100325static void ex_digraphs(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000326#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100327static void ex_nohlsearch(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000328#else
329# define ex_nohlsearch ex_ni
330# define ex_match ex_ni
331#endif
332#ifdef FEAT_CRYPT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100333static void ex_X(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000334#else
335# define ex_X ex_ni
336#endif
337#ifdef FEAT_FOLDING
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100338static void ex_fold(exarg_T *eap);
339static void ex_foldopen(exarg_T *eap);
340static void ex_folddo(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000341#else
342# define ex_fold ex_ni
343# define ex_foldopen ex_ni
344# define ex_folddo ex_ni
345#endif
Bram Moolenaar13505972019-01-24 15:04:48 +0100346#if !(defined(HAVE_LOCALE_H) || defined(X_LOCALE))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000347# define ex_language ex_ni
348#endif
349#ifndef FEAT_SIGNS
350# define ex_sign ex_ni
351#endif
Bram Moolenaar009b2592004-10-24 19:18:58 +0000352#ifndef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200353# define ex_nbclose ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000354# define ex_nbkey ex_ni
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200355# define ex_nbstart ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000356#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000357
Bram Moolenaar071d4272004-06-13 20:20:40 +0000358#ifndef FEAT_JUMPLIST
359# define ex_jumps ex_ni
Bram Moolenaar2d358992016-06-12 21:20:54 +0200360# define ex_clearjumps ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000361# define ex_changes ex_ni
362#endif
363
Bram Moolenaar05159a02005-02-26 23:04:13 +0000364#ifndef FEAT_PROFILE
365# define ex_profile ex_ni
366#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200367#ifndef FEAT_TERMINAL
368# define ex_terminal ex_ni
369#endif
Bram Moolenaard4aa83a2019-05-09 18:59:31 +0200370#if !defined(FEAT_X11) || !defined(FEAT_XCLIPBOARD)
371# define ex_xrestore ex_ni
372#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100373#if !defined(FEAT_PROP_POPUP)
Bram Moolenaar682725c2019-05-25 20:10:37 +0200374# define ex_popupclear ex_ni
375#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +0000376
Bram Moolenaar071d4272004-06-13 20:20:40 +0000377/*
378 * Declare cmdnames[].
379 */
380#define DO_DECLARE_EXCMD
381#include "ex_cmds.h"
Bram Moolenaar6de5e122017-04-20 21:55:44 +0200382#include "ex_cmdidxs.h"
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +0100383
Bram Moolenaar071d4272004-06-13 20:20:40 +0000384static char_u dollar_command[2] = {'$', 0};
385
386
387#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100388// Struct for storing a line inside a while/for loop
Bram Moolenaar071d4272004-06-13 20:20:40 +0000389typedef struct
390{
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100391 char_u *line; // command line
392 linenr_T lnum; // sourcing_lnum of the line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000393} wcmd_T;
394
395/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000396 * Structure used to store info for line position in a while or for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000397 * This is required, because do_one_cmd() may invoke ex_function(), which
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000398 * reads more lines that may come from the while/for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000399 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000400struct loop_cookie
Bram Moolenaar071d4272004-06-13 20:20:40 +0000401{
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100402 garray_T *lines_gap; // growarray with line info
403 int current_line; // last read line from growarray
404 int repeating; // TRUE when looping a second time
405 // When "repeating" is FALSE use "getline" and "cookie" to get lines
Bram Moolenaare96a2492019-06-25 04:12:16 +0200406 char_u *(*getline)(int, void *, int, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000407 void *cookie;
408};
409
Bram Moolenaare96a2492019-06-25 04:12:16 +0200410static char_u *get_loop_line(int c, void *cookie, int indent, int do_concat);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100411static int store_loop_line(garray_T *gap, char_u *line);
412static void free_cmdlines(garray_T *gap);
Bram Moolenaared203462004-06-16 11:19:22 +0000413
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100414// Struct to save a few things while debugging. Used in do_cmdline() only.
Bram Moolenaared203462004-06-16 11:19:22 +0000415struct dbg_stuff
416{
417 int trylevel;
418 int force_abort;
419 except_T *caught_stack;
420 char_u *vv_exception;
421 char_u *vv_throwpoint;
422 int did_emsg;
423 int got_int;
424 int did_throw;
425 int need_rethrow;
426 int check_cstack;
427 except_T *current_exception;
428};
429
Bram Moolenaared203462004-06-16 11:19:22 +0000430 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100431save_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000432{
433 dsp->trylevel = trylevel; trylevel = 0;
434 dsp->force_abort = force_abort; force_abort = FALSE;
435 dsp->caught_stack = caught_stack; caught_stack = NULL;
436 dsp->vv_exception = v_exception(NULL);
437 dsp->vv_throwpoint = v_throwpoint(NULL);
438
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100439 // Necessary for debugging an inactive ":catch", ":finally", ":endtry"
Bram Moolenaared203462004-06-16 11:19:22 +0000440 dsp->did_emsg = did_emsg; did_emsg = FALSE;
441 dsp->got_int = got_int; got_int = FALSE;
442 dsp->did_throw = did_throw; did_throw = FALSE;
443 dsp->need_rethrow = need_rethrow; need_rethrow = FALSE;
444 dsp->check_cstack = check_cstack; check_cstack = FALSE;
445 dsp->current_exception = current_exception; current_exception = NULL;
446}
447
448 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100449restore_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000450{
451 suppress_errthrow = FALSE;
452 trylevel = dsp->trylevel;
453 force_abort = dsp->force_abort;
454 caught_stack = dsp->caught_stack;
455 (void)v_exception(dsp->vv_exception);
456 (void)v_throwpoint(dsp->vv_throwpoint);
457 did_emsg = dsp->did_emsg;
458 got_int = dsp->got_int;
459 did_throw = dsp->did_throw;
460 need_rethrow = dsp->need_rethrow;
461 check_cstack = dsp->check_cstack;
462 current_exception = dsp->current_exception;
463}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000464#endif
465
Bram Moolenaar071d4272004-06-13 20:20:40 +0000466/*
467 * do_exmode(): Repeatedly get commands for the "Ex" mode, until the ":vi"
468 * command is given.
469 */
470 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100471do_exmode(
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100472 int improved) // TRUE for "improved Ex" mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000473{
474 int save_msg_scroll;
475 int prev_msg_row;
476 linenr_T prev_line;
Bram Moolenaar79518e22017-02-17 16:31:35 +0100477 varnumber_T changedtick;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000478
479 if (improved)
480 exmode_active = EXMODE_VIM;
481 else
482 exmode_active = EXMODE_NORMAL;
483 State = NORMAL;
484
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100485 // When using ":global /pat/ visual" and then "Q" we return to continue
486 // the :global command.
Bram Moolenaardf177f62005-02-22 08:39:57 +0000487 if (global_busy)
488 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000489
490 save_msg_scroll = msg_scroll;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100491 ++RedrawingDisabled; // don't redisplay the window
492 ++no_wait_return; // don't wait for return
Bram Moolenaar071d4272004-06-13 20:20:40 +0000493#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100494 // Ignore scrollbar and mouse events in Ex mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000495 ++hold_gui_events;
496#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000497
Bram Moolenaar32526b32019-01-19 17:43:09 +0100498 msg(_("Entering Ex mode. Type \"visual\" to go to Normal mode."));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000499 while (exmode_active)
500 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100501 // Check for a ":normal" command and no more characters left.
Bram Moolenaar7c626922005-02-07 22:01:03 +0000502 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
503 {
504 exmode_active = FALSE;
505 break;
506 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000507 msg_scroll = TRUE;
508 need_wait_return = FALSE;
509 ex_pressedreturn = FALSE;
510 ex_no_reprint = FALSE;
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100511 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000512 prev_msg_row = msg_row;
513 prev_line = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000514 if (improved)
515 {
516 cmdline_row = msg_row;
517 do_cmdline(NULL, getexline, NULL, 0);
518 }
519 else
520 do_cmdline(NULL, getexmodeline, NULL, DOCMD_NOWAIT);
521 lines_left = Rows - 1;
522
Bram Moolenaardf177f62005-02-22 08:39:57 +0000523 if ((prev_line != curwin->w_cursor.lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100524 || changedtick != CHANGEDTICK(curbuf)) && !ex_no_reprint)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000525 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000526 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100527 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000528 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000529 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000530 if (ex_pressedreturn)
531 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100532 // go up one line, to overwrite the ":<CR>" line, so the
533 // output doesn't contain empty lines.
Bram Moolenaardf177f62005-02-22 08:39:57 +0000534 msg_row = prev_msg_row;
535 if (prev_msg_row == Rows - 1)
536 msg_row--;
537 }
538 msg_col = 0;
539 print_line_no_prefix(curwin->w_cursor.lnum, FALSE, FALSE);
540 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000541 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000542 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100543 else if (ex_pressedreturn && !ex_no_reprint) // must be at EOF
Bram Moolenaardf177f62005-02-22 08:39:57 +0000544 {
545 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100546 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000547 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100548 emsg(_("E501: At end-of-file"));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000549 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000550 }
551
552#ifdef FEAT_GUI
553 --hold_gui_events;
554#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000555 --RedrawingDisabled;
556 --no_wait_return;
557 update_screen(CLEAR);
558 need_wait_return = FALSE;
559 msg_scroll = save_msg_scroll;
560}
561
562/*
Bram Moolenaar4facea32019-10-12 20:17:40 +0200563 * Print the executed command for when 'verbose' is set.
564 * When "lnum" is 0 only print the command.
565 */
566 static void
567msg_verbose_cmd(linenr_T lnum, char_u *cmd)
568{
569 ++no_wait_return;
570 verbose_enter_scroll();
571
572 if (lnum == 0)
573 smsg(_("Executing: %s"), cmd);
574 else
575 smsg(_("line %ld: %s"), (long)lnum, cmd);
576 if (msg_silent == 0)
577 msg_puts("\n"); // don't overwrite this
578
579 verbose_leave_scroll();
580 --no_wait_return;
581}
582
583/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000584 * Execute a simple command line. Used for translated commands like "*".
585 */
586 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100587do_cmdline_cmd(char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000588{
589 return do_cmdline(cmd, NULL, NULL,
590 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
591}
592
593/*
594 * do_cmdline(): execute one Ex command line
595 *
596 * 1. Execute "cmdline" when it is not NULL.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100597 * If "cmdline" is NULL, or more lines are needed, fgetline() is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000598 * 2. Split up in parts separated with '|'.
599 *
600 * This function can be called recursively!
601 *
602 * flags:
603 * DOCMD_VERBOSE - The command will be included in the error message.
604 * DOCMD_NOWAIT - Don't call wait_return() and friends.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100605 * DOCMD_REPEAT - Repeat execution until fgetline() returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000606 * DOCMD_KEYTYPED - Don't reset KeyTyped.
607 * DOCMD_EXCRESET - Reset the exception environment (used for debugging).
608 * DOCMD_KEEPLINE - Store first typed line (for repeating with ".").
609 *
610 * return FAIL if cmdline could not be executed, OK otherwise
611 */
612 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100613do_cmdline(
614 char_u *cmdline,
Bram Moolenaare96a2492019-06-25 04:12:16 +0200615 char_u *(*fgetline)(int, void *, int, int),
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100616 void *cookie, // argument for fgetline()
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100617 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000618{
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100619 char_u *next_cmdline; // next cmd to execute
620 char_u *cmdline_copy = NULL; // copy of cmd line
621 int used_getline = FALSE; // used "fgetline" to obtain command
622 static int recursive = 0; // recursive depth
Bram Moolenaar071d4272004-06-13 20:20:40 +0000623 int msg_didout_before_start = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100624 int count = 0; // line number count
625 int did_inc = FALSE; // incremented RedrawingDisabled
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626 int retval = OK;
627#ifdef FEAT_EVAL
Bram Moolenaarddef1292019-12-16 17:10:33 +0100628 cstack_T cstack; // conditional stack
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100629 garray_T lines_ga; // keep lines for ":while"/":for"
630 int current_line = 0; // active line in lines_ga
Bram Moolenaard5053d02020-06-28 15:51:16 +0200631 int current_line_before = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100632 char_u *fname = NULL; // function or script name
633 linenr_T *breakpoint = NULL; // ptr to breakpoint field in cookie
634 int *dbg_tick = NULL; // ptr to dbg_tick field in cookie
635 struct dbg_stuff debug_saved; // saved things for debug mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000636 int initial_trylevel;
Bram Moolenaar25e0f582020-05-25 22:36:50 +0200637 msglist_T **saved_msg_list = NULL;
638 msglist_T *private_msg_list;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000639
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100640 // "fgetline" and "cookie" passed to do_one_cmd()
Bram Moolenaare96a2492019-06-25 04:12:16 +0200641 char_u *(*cmd_getline)(int, void *, int, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642 void *cmd_cookie;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000643 struct loop_cookie cmd_loop_cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000644 void *real_cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000645 int getline_is_func;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000646#else
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100647# define cmd_getline fgetline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000648# define cmd_cookie cookie
649#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100650 static int call_depth = 0; // recursiveness
Bram Moolenaar2196bce2020-04-12 20:01:11 +0200651#ifdef FEAT_EVAL
Bram Moolenaare31ee862020-01-07 20:59:34 +0100652 ESTACK_CHECK_DECLARATION
Bram Moolenaar071d4272004-06-13 20:20:40 +0000653
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100654 // For every pair of do_cmdline()/do_one_cmd() calls, use an extra memory
655 // location for storing error messages to be converted to an exception.
656 // This ensures that the do_errthrow() call in do_one_cmd() does not
657 // combine the messages stored by an earlier invocation of do_one_cmd()
658 // with the command name of the later one. This would happen when
659 // BufWritePost autocommands are executed after a write error.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000660 saved_msg_list = msg_list;
661 msg_list = &private_msg_list;
662 private_msg_list = NULL;
663#endif
664
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100665 // It's possible to create an endless loop with ":execute", catch that
666 // here. The value of 200 allows nested function calls, ":source", etc.
667 // Allow 200 or 'maxfuncdepth', whatever is larger.
Bram Moolenaarb094ff42017-01-02 16:16:39 +0100668 if (call_depth >= 200
669#ifdef FEAT_EVAL
670 && call_depth >= p_mfd
671#endif
672 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000673 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100674 emsg(_("E169: Command too recursive"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100676 // When converting to an exception, we do not include the command name
677 // since this is not an error of the specific command.
Bram Moolenaarddef1292019-12-16 17:10:33 +0100678 do_errthrow((cstack_T *)NULL, (char_u *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679 msg_list = saved_msg_list;
680#endif
681 return FAIL;
682 }
683 ++call_depth;
684
685#ifdef FEAT_EVAL
686 cstack.cs_idx = -1;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000687 cstack.cs_looplevel = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688 cstack.cs_trylevel = 0;
689 cstack.cs_emsg_silent_list = NULL;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000690 cstack.cs_lflags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691 ga_init2(&lines_ga, (int)sizeof(wcmd_T), 10);
692
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100693 real_cookie = getline_cookie(fgetline, cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100695 // Inside a function use a higher nesting level.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100696 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000697 if (getline_is_func && ex_nesting_level == func_level(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698 ++ex_nesting_level;
699
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100700 // Get the function or script name and the address where the next breakpoint
701 // line and the debug tick for a function or script are stored.
Bram Moolenaar05159a02005-02-26 23:04:13 +0000702 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000703 {
704 fname = func_name(real_cookie);
705 breakpoint = func_breakpoint(real_cookie);
706 dbg_tick = func_dbg_tick(real_cookie);
707 }
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100708 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100710 fname = SOURCING_NAME;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000711 breakpoint = source_breakpoint(real_cookie);
712 dbg_tick = source_dbg_tick(real_cookie);
713 }
714
715 /*
716 * Initialize "force_abort" and "suppress_errthrow" at the top level.
717 */
718 if (!recursive)
719 {
720 force_abort = FALSE;
721 suppress_errthrow = FALSE;
722 }
723
724 /*
725 * If requested, store and reset the global values controlling the
Bram Moolenaar89d40322006-08-29 15:30:07 +0000726 * exception handling (used when debugging). Otherwise clear it to avoid
727 * a bogus compiler warning when the optimizer uses inline functions...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000728 */
Bram Moolenaarb4872942006-05-13 10:32:52 +0000729 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +0000730 save_dbg_stuff(&debug_saved);
Bram Moolenaar89d40322006-08-29 15:30:07 +0000731 else
Bram Moolenaara80faa82020-04-12 19:37:17 +0200732 CLEAR_FIELD(debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000733
734 initial_trylevel = trylevel;
735
736 /*
737 * "did_throw" will be set to TRUE when an exception is being thrown.
738 */
739 did_throw = FALSE;
740#endif
741 /*
742 * "did_emsg" will be set to TRUE when emsg() is used, in which case we
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000743 * cancel the whole command line, and any if/endif or loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000744 * If force_abort is set, we cancel everything.
745 */
746 did_emsg = FALSE;
747
748 /*
749 * KeyTyped is only set when calling vgetc(). Reset it here when not
750 * calling vgetc() (sourced command lines).
751 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100752 if (!(flags & DOCMD_KEYTYPED)
753 && !getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754 KeyTyped = FALSE;
755
756 /*
757 * Continue executing command lines:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000758 * - when inside an ":if", ":while" or ":for"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000759 * - for multiple commands on one line, separated with '|'
760 * - when repeating until there are no more lines (for ":source")
761 */
762 next_cmdline = cmdline;
763 do
764 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000765#ifdef FEAT_EVAL
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100766 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000767#endif
768
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100769 // stop skipping cmds for an error msg after all endif/while/for
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770 if (next_cmdline == NULL
771#ifdef FEAT_EVAL
772 && !force_abort
773 && cstack.cs_idx < 0
Bram Moolenaar05159a02005-02-26 23:04:13 +0000774 && !(getline_is_func && func_has_abort(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775#endif
776 )
777 did_emsg = FALSE;
778
779 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000780 * 1. If repeating a line in a loop, get a line from lines_ga.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100781 * 2. If no line given: Get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782 * 3. If a line is given: Make a copy, so we can mess with it.
783 */
784
785#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100786 // 1. If repeating, get a previous line from lines_ga.
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000787 if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100789 // Each '|' separated command is stored separately in lines_ga, to
790 // be able to jump to it. Don't use next_cmdline now.
Bram Moolenaard23a8232018-02-10 18:45:26 +0100791 VIM_CLEAR(cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100793 // Check if a function has returned or, unless it has an unclosed
794 // try conditional, aborted.
Bram Moolenaar05159a02005-02-26 23:04:13 +0000795 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000797# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000798 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000799 func_line_end(real_cookie);
800# endif
801 if (func_has_ended(real_cookie))
802 {
803 retval = FAIL;
804 break;
805 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000807#ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000808 else if (do_profiling == PROF_YES
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100809 && getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000810 script_line_end();
811#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000812
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100813 // Check if a sourced file hit a ":finish" command.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100814 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815 {
816 retval = FAIL;
817 break;
818 }
819
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100820 // If breakpoints have been added/deleted need to check for it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000821 if (breakpoint != NULL && dbg_tick != NULL
822 && *dbg_tick != debug_tick)
823 {
824 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100825 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100826 fname, SOURCING_LNUM);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827 *dbg_tick = debug_tick;
828 }
829
830 next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100831 SOURCING_LNUM = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100833 // Did we encounter a breakpoint?
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834 if (breakpoint != NULL && *breakpoint != 0
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100835 && *breakpoint <= SOURCING_LNUM)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000836 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100837 dbg_breakpoint(fname, SOURCING_LNUM);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100838 // Find next breakpoint.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100840 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100841 fname, SOURCING_LNUM);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842 *dbg_tick = debug_tick;
843 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000844# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000845 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000846 {
847 if (getline_is_func)
848 func_line_start(real_cookie);
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100849 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000850 script_line_start();
851 }
852# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000853 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854#endif
855
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100856 // 2. If no line given, get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000857 if (next_cmdline == NULL)
858 {
859 /*
860 * Need to set msg_didout for the first line after an ":if",
861 * otherwise the ":if" will be overwritten.
862 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100863 if (count == 1 && getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000864 msg_didout = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100865 if (fgetline == NULL || (next_cmdline = fgetline(':', cookie,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000866#ifdef FEAT_EVAL
867 cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2
868#else
869 0
870#endif
Bram Moolenaare96a2492019-06-25 04:12:16 +0200871 , TRUE)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000872 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100873 // Don't call wait_return for aborted command line. The NULL
874 // returned for the end of a sourced file or executed function
875 // doesn't do this.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876 if (KeyTyped && !(flags & DOCMD_REPEAT))
877 need_wait_return = FALSE;
878 retval = FAIL;
879 break;
880 }
881 used_getline = TRUE;
882
883 /*
884 * Keep the first typed line. Clear it when more lines are typed.
885 */
886 if (flags & DOCMD_KEEPLINE)
887 {
888 vim_free(repeat_cmdline);
889 if (count == 0)
890 repeat_cmdline = vim_strsave(next_cmdline);
891 else
892 repeat_cmdline = NULL;
893 }
894 }
895
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100896 // 3. Make a copy of the command so we can mess with it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000897 else if (cmdline_copy == NULL)
898 {
899 next_cmdline = vim_strsave(next_cmdline);
900 if (next_cmdline == NULL)
901 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100902 emsg(_(e_outofmem));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000903 retval = FAIL;
904 break;
905 }
906 }
907 cmdline_copy = next_cmdline;
908
909#ifdef FEAT_EVAL
910 /*
Bram Moolenaard5053d02020-06-28 15:51:16 +0200911 * Inside a while/for loop, and when the command looks like a ":while"
912 * or ":for", the line is stored, because we may need it later when
913 * looping.
914 *
915 * When there is a '|' and another command, it is stored separately,
916 * because we need to be able to jump back to it from an
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000917 * :endwhile/:endfor.
Bram Moolenaard5053d02020-06-28 15:51:16 +0200918 *
919 * Pass a different "fgetline" function to do_one_cmd() below,
920 * that it stores lines in or reads them from "lines_ga". Makes it
921 * possible to define a function inside a while/for loop and handles
922 * line continuation.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000923 */
Bram Moolenaard5053d02020-06-28 15:51:16 +0200924 if ((cstack.cs_looplevel > 0 || has_loop_cmd(next_cmdline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000925 {
Bram Moolenaard5053d02020-06-28 15:51:16 +0200926 cmd_getline = get_loop_line;
927 cmd_cookie = (void *)&cmd_loop_cookie;
928 cmd_loop_cookie.lines_gap = &lines_ga;
929 cmd_loop_cookie.current_line = current_line;
930 cmd_loop_cookie.getline = fgetline;
931 cmd_loop_cookie.cookie = cookie;
932 cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len);
933
934 // Save the current line when encountering it the first time.
935 if (current_line == lines_ga.ga_len
936 && store_loop_line(&lines_ga, next_cmdline) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937 {
938 retval = FAIL;
939 break;
940 }
Bram Moolenaard5053d02020-06-28 15:51:16 +0200941 current_line_before = current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000942 }
Bram Moolenaard5053d02020-06-28 15:51:16 +0200943 else
944 {
945 cmd_getline = fgetline;
946 cmd_cookie = cookie;
947 }
948
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949 did_endif = FALSE;
950#endif
951
952 if (count++ == 0)
953 {
954 /*
955 * All output from the commands is put below each other, without
956 * waiting for a return. Don't do this when executing commands
957 * from a script or when being called recursive (e.g. for ":e
958 * +command file").
959 */
960 if (!(flags & DOCMD_NOWAIT) && !recursive)
961 {
962 msg_didout_before_start = msg_didout;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100963 msg_didany = FALSE; // no output yet
Bram Moolenaar071d4272004-06-13 20:20:40 +0000964 msg_start();
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100965 msg_scroll = TRUE; // put messages below each other
966 ++no_wait_return; // don't wait for return until finished
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967 ++RedrawingDisabled;
968 did_inc = TRUE;
969 }
970 }
971
Bram Moolenaar823654b2020-05-29 23:03:09 +0200972 if ((p_verbose >= 15 && SOURCING_NAME != NULL) || p_verbose >= 16)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100973 msg_verbose_cmd(SOURCING_LNUM, cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000974
975 /*
976 * 2. Execute one '|' separated command.
977 * do_one_cmd() will return NULL if there is no trailing '|'.
978 * "cmdline_copy" can change, e.g. for '%' and '#' expansion.
979 */
980 ++recursive;
981 next_cmdline = do_one_cmd(&cmdline_copy, flags & DOCMD_VERBOSE,
982#ifdef FEAT_EVAL
983 &cstack,
984#endif
985 cmd_getline, cmd_cookie);
986 --recursive;
987
988#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000989 if (cmd_cookie == (void *)&cmd_loop_cookie)
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100990 // Use "current_line" from "cmd_loop_cookie", it may have been
991 // incremented when defining a function.
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000992 current_line = cmd_loop_cookie.current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993#endif
994
995 if (next_cmdline == NULL)
996 {
Bram Moolenaard23a8232018-02-10 18:45:26 +0100997 VIM_CLEAR(cmdline_copy);
Bram Moolenaard7663c22019-08-06 21:59:57 +0200998
Bram Moolenaar071d4272004-06-13 20:20:40 +0000999 /*
1000 * If the command was typed, remember it for the ':' register.
1001 * Do this AFTER executing the command to make :@: work.
1002 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001003 if (getline_equal(fgetline, cookie, getexline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001004 && new_last_cmdline != NULL)
1005 {
1006 vim_free(last_cmdline);
1007 last_cmdline = new_last_cmdline;
1008 new_last_cmdline = NULL;
1009 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010 }
1011 else
1012 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001013 // need to copy the command after the '|' to cmdline_copy, for the
1014 // next do_one_cmd()
Bram Moolenaara7241f52008-06-24 20:39:31 +00001015 STRMOVE(cmdline_copy, next_cmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016 next_cmdline = cmdline_copy;
1017 }
1018
1019
1020#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001021 // reset did_emsg for a function that is not aborted by an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022 if (did_emsg && !force_abort
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001023 && getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001024 && !func_has_abort(real_cookie))
1025 did_emsg = FALSE;
1026
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001027 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028 {
1029 ++current_line;
1030
1031 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001032 * An ":endwhile", ":endfor" and ":continue" is handled here.
1033 * If we were executing commands, jump back to the ":while" or
1034 * ":for".
1035 * If we were not executing commands, decrement cs_looplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001037 if (cstack.cs_lflags & (CSL_HAD_CONT | CSL_HAD_ENDLOOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001039 cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001041 // Jump back to the matching ":while" or ":for". Be careful
1042 // not to use a cs_line[] from an entry that isn't a ":while"
1043 // or ":for": It would make "current_line" invalid and can
1044 // cause a crash.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045 if (!did_emsg && !got_int && !did_throw
1046 && cstack.cs_idx >= 0
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001047 && (cstack.cs_flags[cstack.cs_idx]
1048 & (CSF_WHILE | CSF_FOR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049 && cstack.cs_line[cstack.cs_idx] >= 0
1050 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE))
1051 {
1052 current_line = cstack.cs_line[cstack.cs_idx];
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001053 // remember we jumped there
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001054 cstack.cs_lflags |= CSL_HAD_LOOP;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001055 line_breakcheck(); // check if CTRL-C typed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001057 // Check for the next breakpoint at or after the ":while"
1058 // or ":for".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059 if (breakpoint != NULL)
1060 {
1061 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001062 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063 fname,
1064 ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1);
1065 *dbg_tick = debug_tick;
1066 }
1067 }
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001068 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001070 // can only get here with ":endwhile" or ":endfor"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071 if (cstack.cs_idx >= 0)
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001072 rewind_conditionals(&cstack, cstack.cs_idx - 1,
1073 CSF_WHILE | CSF_FOR, &cstack.cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074 }
1075 }
1076
1077 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001078 * For a ":while" or ":for" we need to remember the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001080 else if (cstack.cs_lflags & CSL_HAD_LOOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001082 cstack.cs_lflags &= ~CSL_HAD_LOOP;
Bram Moolenaard5053d02020-06-28 15:51:16 +02001083 cstack.cs_line[cstack.cs_idx] = current_line_before;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084 }
1085 }
1086
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001087 // Check for the next breakpoint after a watchexpression
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01001088 if (breakpoint != NULL && has_watchexpr())
1089 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001090 *breakpoint = dbg_find_breakpoint(FALSE, fname, SOURCING_LNUM);
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01001091 *dbg_tick = debug_tick;
1092 }
1093
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094 /*
1095 * When not inside any ":while" loop, clear remembered lines.
1096 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001097 if (cstack.cs_looplevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098 {
1099 if (lines_ga.ga_len > 0)
1100 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001101 SOURCING_LNUM =
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102 ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum;
1103 free_cmdlines(&lines_ga);
1104 }
1105 current_line = 0;
1106 }
1107
1108 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001109 * A ":finally" makes did_emsg, got_int, and did_throw pending for
1110 * being restored at the ":endtry". Reset them here and set the
1111 * ACTIVE and FINALLY flags, so that the finally clause gets executed.
1112 * This includes the case where a missing ":endif", ":endwhile" or
1113 * ":endfor" was detected by the ":finally" itself.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001115 if (cstack.cs_lflags & CSL_HAD_FINA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001116 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001117 cstack.cs_lflags &= ~CSL_HAD_FINA;
1118 report_make_pending(cstack.cs_pending[cstack.cs_idx]
1119 & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001120 did_throw ? (void *)current_exception : NULL);
1121 did_emsg = got_int = did_throw = FALSE;
1122 cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY;
1123 }
1124
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001125 // Update global "trylevel" for recursive calls to do_cmdline() from
1126 // within this loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001127 trylevel = initial_trylevel + cstack.cs_trylevel;
1128
1129 /*
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001130 * If the outermost try conditional (across function calls and sourced
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131 * files) is aborted because of an error, an interrupt, or an uncaught
1132 * exception, cancel everything. If it is left normally, reset
1133 * force_abort to get the non-EH compatible abortion behavior for
1134 * the rest of the script.
1135 */
1136 if (trylevel == 0 && !did_emsg && !got_int && !did_throw)
1137 force_abort = FALSE;
1138
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001139 // Convert an interrupt to an exception if appropriate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001140 (void)do_intthrow(&cstack);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001141#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142
1143 }
1144 /*
1145 * Continue executing command lines when:
1146 * - no CTRL-C typed, no aborting error, no exception thrown or try
1147 * conditionals need to be checked for executing finally clauses or
1148 * catching an interrupt exception
1149 * - didn't get an error message or lines are not typed
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001150 * - there is a command after '|', inside a :if, :while, :for or :try, or
Bram Moolenaar071d4272004-06-13 20:20:40 +00001151 * looping for ":source" command or function call.
1152 */
1153 while (!((got_int
1154#ifdef FEAT_EVAL
1155 || (did_emsg && force_abort) || did_throw
1156#endif
1157 )
1158#ifdef FEAT_EVAL
1159 && cstack.cs_trylevel == 0
1160#endif
1161 )
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001162 && !(did_emsg
1163#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001164 // Keep going when inside try/catch, so that the error can be
1165 // deal with, except when it is a syntax error, it may cause
1166 // the :endtry to be missed.
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001167 && (cstack.cs_trylevel == 0 || did_emsg_syntax)
1168#endif
1169 && used_getline
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001170 && (getline_equal(fgetline, cookie, getexmodeline)
1171 || getline_equal(fgetline, cookie, getexline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172 && (next_cmdline != NULL
1173#ifdef FEAT_EVAL
1174 || cstack.cs_idx >= 0
1175#endif
1176 || (flags & DOCMD_REPEAT)));
1177
1178 vim_free(cmdline_copy);
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001179 did_emsg_syntax = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180#ifdef FEAT_EVAL
1181 free_cmdlines(&lines_ga);
1182 ga_clear(&lines_ga);
1183
1184 if (cstack.cs_idx >= 0)
1185 {
1186 /*
1187 * If a sourced file or executed function ran to its end, report the
1188 * unclosed conditional.
1189 */
1190 if (!got_int && !did_throw
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001191 && ((getline_equal(fgetline, cookie, getsourceline)
1192 && !source_finished(fgetline, cookie))
1193 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194 && !func_has_ended(real_cookie))))
1195 {
1196 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001197 emsg(_(e_endtry));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001199 emsg(_(e_endwhile));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001200 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001201 emsg(_(e_endfor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001203 emsg(_(e_endif));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204 }
1205
1206 /*
1207 * Reset "trylevel" in case of a ":finish" or ":return" or a missing
1208 * ":endtry" in a sourced file or executed function. If the try
1209 * conditional is in its finally clause, ignore anything pending.
1210 * If it is in a catch clause, finish the caught exception.
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001211 * Also cleanup any "cs_forinfo" structures.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212 */
1213 do
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001214 {
1215 int idx = cleanup_conditionals(&cstack, 0, TRUE);
1216
Bram Moolenaar89e5d682005-01-17 22:06:23 +00001217 if (idx >= 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001218 --idx; // remove try block not in its finally clause
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001219 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
1220 &cstack.cs_looplevel);
1221 }
1222 while (cstack.cs_idx >= 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223 trylevel = initial_trylevel;
1224 }
1225
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001226 // If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
1227 // lack was reported above and the error message is to be converted to an
1228 // exception, do this now after rewinding the cstack.
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001229 do_errthrow(&cstack, getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001230 ? (char_u *)"endfunction" : (char_u *)NULL);
1231
1232 if (trylevel == 0)
1233 {
1234 /*
1235 * When an exception is being thrown out of the outermost try
1236 * conditional, discard the uncaught exception, disable the conversion
1237 * of interrupts or errors to exceptions, and ensure that no more
1238 * commands are executed.
1239 */
1240 if (did_throw)
1241 {
1242 void *p = NULL;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02001243 msglist_T *messages = NULL, *next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244
1245 /*
1246 * If the uncaught exception is a user exception, report it as an
1247 * error. If it is an error exception, display the saved error
1248 * message now. For an interrupt exception, do nothing; the
1249 * interrupt message is given elsewhere.
1250 */
1251 switch (current_exception->type)
1252 {
1253 case ET_USER:
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001254 vim_snprintf((char *)IObuff, IOSIZE,
1255 _("E605: Exception not caught: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256 current_exception->value);
1257 p = vim_strsave(IObuff);
1258 break;
1259 case ET_ERROR:
1260 messages = current_exception->messages;
1261 current_exception->messages = NULL;
1262 break;
1263 case ET_INTERRUPT:
1264 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265 }
1266
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001267 estack_push(ETYPE_EXCEPT, current_exception->throw_name,
1268 current_exception->throw_lnum);
Bram Moolenaare31ee862020-01-07 20:59:34 +01001269 ESTACK_CHECK_SETUP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270 current_exception->throw_name = NULL;
1271
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001272 discard_current_exception(); // uses IObuff if 'verbose'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273 suppress_errthrow = TRUE;
1274 force_abort = TRUE;
1275
1276 if (messages != NULL)
1277 {
1278 do
1279 {
1280 next = messages->next;
1281 emsg(messages->msg);
1282 vim_free(messages->msg);
Bram Moolenaar5fbf3bc2020-06-01 21:13:11 +02001283 vim_free(messages->sfile);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284 vim_free(messages);
1285 messages = next;
1286 }
1287 while (messages != NULL);
1288 }
1289 else if (p != NULL)
1290 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01001291 emsg(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292 vim_free(p);
1293 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001294 vim_free(SOURCING_NAME);
Bram Moolenaare31ee862020-01-07 20:59:34 +01001295 ESTACK_CHECK_NOW
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001296 estack_pop();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297 }
1298
1299 /*
1300 * On an interrupt or an aborting error not converted to an exception,
1301 * disable the conversion of errors to exceptions. (Interrupts are not
Bram Moolenaar2196bce2020-04-12 20:01:11 +02001302 * converted anymore, here.) This enables also the interrupt message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303 * when force_abort is set and did_emsg unset in case of an interrupt
1304 * from a finally clause after an error.
1305 */
1306 else if (got_int || (did_emsg && force_abort))
1307 suppress_errthrow = TRUE;
1308 }
1309
1310 /*
1311 * The current cstack will be freed when do_cmdline() returns. An uncaught
1312 * exception will have to be rethrown in the previous cstack. If a function
1313 * has just returned or a script file was just finished and the previous
1314 * cstack belongs to the same function or, respectively, script file, it
1315 * will have to be checked for finally clauses to be executed due to the
1316 * ":return" or ":finish". This is done in do_one_cmd().
1317 */
1318 if (did_throw)
1319 need_rethrow = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001320 if ((getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321 && ex_nesting_level > source_level(real_cookie))
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001322 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323 && ex_nesting_level > func_level(real_cookie) + 1))
1324 {
1325 if (!did_throw)
1326 check_cstack = TRUE;
1327 }
1328 else
1329 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001330 // When leaving a function, reduce nesting level.
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001331 if (getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332 --ex_nesting_level;
1333 /*
1334 * Go to debug mode when returning from a function in which we are
1335 * single-stepping.
1336 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001337 if ((getline_equal(fgetline, cookie, getsourceline)
1338 || getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339 && ex_nesting_level + 1 <= debug_break_level)
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001340 do_debug(getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341 ? (char_u *)_("End of sourced file")
1342 : (char_u *)_("End of function"));
1343 }
1344
1345 /*
1346 * Restore the exception environment (done after returning from the
1347 * debugger).
1348 */
1349 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001350 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351
1352 msg_list = saved_msg_list;
Bram Moolenaar292b90d2020-03-18 15:23:16 +01001353
1354 // Cleanup if "cs_emsg_silent_list" remains.
1355 if (cstack.cs_emsg_silent_list != NULL)
1356 {
1357 eslist_T *elem, *temp;
1358
1359 for (elem = cstack.cs_emsg_silent_list; elem != NULL; elem = temp)
1360 {
1361 temp = elem->next;
1362 vim_free(elem);
1363 }
1364 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001365#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366
1367 /*
1368 * If there was too much output to fit on the command line, ask the user to
1369 * hit return before redrawing the screen. With the ":global" command we do
1370 * this only once after the command is finished.
1371 */
1372 if (did_inc)
1373 {
1374 --RedrawingDisabled;
1375 --no_wait_return;
1376 msg_scroll = FALSE;
1377
1378 /*
1379 * When just finished an ":if"-":else" which was typed, no need to
1380 * wait for hit-return. Also for an error situation.
1381 */
1382 if (retval == FAIL
1383#ifdef FEAT_EVAL
1384 || (did_endif && KeyTyped && !did_emsg)
1385#endif
1386 )
1387 {
1388 need_wait_return = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001389 msg_didany = FALSE; // don't wait when restarting edit
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390 }
1391 else if (need_wait_return)
1392 {
1393 /*
1394 * The msg_start() above clears msg_didout. The wait_return we do
1395 * here should not overwrite the command that may be shown before
1396 * doing that.
1397 */
1398 msg_didout |= msg_didout_before_start;
1399 wait_return(FALSE);
1400 }
1401 }
1402
Bram Moolenaar2a942252012-11-28 23:03:07 +01001403#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001404 did_endif = FALSE; // in case do_cmdline used recursively
Bram Moolenaar2a942252012-11-28 23:03:07 +01001405#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406 /*
1407 * Reset if_level, in case a sourced script file contains more ":if" than
1408 * ":endif" (could be ":if x | foo | endif").
1409 */
1410 if_level = 0;
Bram Moolenaar2e18a122012-11-28 19:10:54 +01001411#endif
Bram Moolenaard4ad0d42012-11-28 17:34:48 +01001412
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413 --call_depth;
1414 return retval;
1415}
1416
1417#ifdef FEAT_EVAL
1418/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001419 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420 */
1421 static char_u *
Bram Moolenaare96a2492019-06-25 04:12:16 +02001422get_loop_line(int c, void *cookie, int indent, int do_concat)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001424 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425 wcmd_T *wp;
1426 char_u *line;
1427
1428 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1429 {
1430 if (cp->repeating)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001431 return NULL; // trying to read past ":endwhile"/":endfor"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001433 // First time inside the ":while"/":for": get line normally.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001434 if (cp->getline == NULL)
Bram Moolenaare96a2492019-06-25 04:12:16 +02001435 line = getcmdline(c, 0L, indent, do_concat);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001436 else
Bram Moolenaare96a2492019-06-25 04:12:16 +02001437 line = cp->getline(c, cp->cookie, indent, do_concat);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001438 if (line != NULL && store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001439 ++cp->current_line;
1440
1441 return line;
1442 }
1443
1444 KeyTyped = FALSE;
1445 ++cp->current_line;
1446 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001447 SOURCING_LNUM = wp->lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001448 return vim_strsave(wp->line);
1449}
1450
1451/*
1452 * Store a line in "gap" so that a ":while" loop can execute it again.
1453 */
1454 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001455store_loop_line(garray_T *gap, char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001456{
1457 if (ga_grow(gap, 1) == FAIL)
1458 return FAIL;
1459 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001460 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = SOURCING_LNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001461 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001462 return OK;
1463}
1464
1465/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001466 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001467 */
1468 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001469free_cmdlines(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470{
1471 while (gap->ga_len > 0)
1472 {
1473 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1474 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001475 }
1476}
1477#endif
1478
1479/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001480 * If "fgetline" is get_loop_line(), return TRUE if the getline it uses equals
1481 * "func". * Otherwise return TRUE when "fgetline" equals "func".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001484getline_equal(
Bram Moolenaare96a2492019-06-25 04:12:16 +02001485 char_u *(*fgetline)(int, void *, int, int),
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001486 void *cookie UNUSED, // argument for fgetline()
Bram Moolenaare96a2492019-06-25 04:12:16 +02001487 char_u *(*func)(int, void *, int, int))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488{
1489#ifdef FEAT_EVAL
Bram Moolenaare96a2492019-06-25 04:12:16 +02001490 char_u *(*gp)(int, void *, int, int);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001491 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001492
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001493 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1494 // function that's originally used to obtain the lines. This may be
1495 // nested several levels.
Bram Moolenaar89d40322006-08-29 15:30:07 +00001496 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001497 cp = (struct loop_cookie *)cookie;
1498 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001499 {
1500 gp = cp->getline;
1501 cp = cp->cookie;
1502 }
1503 return gp == func;
1504#else
Bram Moolenaar89d40322006-08-29 15:30:07 +00001505 return fgetline == func;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001506#endif
1507}
1508
Bram Moolenaar071d4272004-06-13 20:20:40 +00001509/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001510 * If "fgetline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511 * getline function. Otherwise return "cookie".
1512 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513 void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001514getline_cookie(
Bram Moolenaare96a2492019-06-25 04:12:16 +02001515 char_u *(*fgetline)(int, void *, int, int) UNUSED,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001516 void *cookie) // argument for fgetline()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517{
Bram Moolenaar13505972019-01-24 15:04:48 +01001518#ifdef FEAT_EVAL
Bram Moolenaare96a2492019-06-25 04:12:16 +02001519 char_u *(*gp)(int, void *, int, int);
Bram Moolenaard5053d02020-06-28 15:51:16 +02001520 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001521
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001522 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1523 // cookie that's originally used to obtain the lines. This may be nested
1524 // several levels.
Bram Moolenaar89d40322006-08-29 15:30:07 +00001525 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001526 cp = (struct loop_cookie *)cookie;
1527 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001528 {
1529 gp = cp->getline;
1530 cp = cp->cookie;
1531 }
1532 return cp;
Bram Moolenaar13505972019-01-24 15:04:48 +01001533#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001534 return cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001535#endif
Bram Moolenaar13505972019-01-24 15:04:48 +01001536}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537
Bram Moolenaard5053d02020-06-28 15:51:16 +02001538#if defined(FEAT_EVAL) || defined(PROT)
1539/*
1540 * Get the next line source line without advancing.
1541 */
1542 char_u *
1543getline_peek(
1544 char_u *(*fgetline)(int, void *, int, int) UNUSED,
1545 void *cookie) // argument for fgetline()
1546{
1547 char_u *(*gp)(int, void *, int, int);
1548 struct loop_cookie *cp;
1549 wcmd_T *wp;
1550
1551 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1552 // cookie that's originally used to obtain the lines. This may be nested
1553 // several levels.
1554 gp = fgetline;
1555 cp = (struct loop_cookie *)cookie;
1556 while (gp == get_loop_line)
1557 {
1558 if (cp->current_line + 1 < cp->lines_gap->ga_len)
1559 {
1560 // executing lines a second time, use the stored copy
1561 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line + 1;
1562 return wp->line;
1563 }
1564 gp = cp->getline;
1565 cp = cp->cookie;
1566 }
1567 if (gp == getsourceline)
1568 return source_nextline(cp);
1569 return NULL;
1570}
1571#endif
1572
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001573
1574/*
1575 * Helper function to apply an offset for buffer commands, i.e. ":bdelete",
1576 * ":bwipeout", etc.
1577 * Returns the buffer number.
1578 */
1579 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001580compute_buffer_local_count(int addr_type, int lnum, int offset)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001581{
1582 buf_T *buf;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001583 buf_T *nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001584 int count = offset;
1585
1586 buf = firstbuf;
1587 while (buf->b_next != NULL && buf->b_fnum < lnum)
1588 buf = buf->b_next;
1589 while (count != 0)
1590 {
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001591 count += (offset < 0) ? 1 : -1;
1592 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1593 if (nextbuf == NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001594 break;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001595 buf = nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001596 if (addr_type == ADDR_LOADED_BUFFERS)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001597 // skip over unloaded buffers
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001598 while (buf->b_ml.ml_mfp == NULL)
1599 {
1600 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1601 if (nextbuf == NULL)
1602 break;
1603 buf = nextbuf;
1604 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001605 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001606 // we might have gone too far, last buffer is not loadedd
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001607 if (addr_type == ADDR_LOADED_BUFFERS)
1608 while (buf->b_ml.ml_mfp == NULL)
1609 {
1610 nextbuf = (offset >= 0) ? buf->b_prev : buf->b_next;
1611 if (nextbuf == NULL)
1612 break;
1613 buf = nextbuf;
1614 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001615 return buf->b_fnum;
1616}
1617
Bram Moolenaarb7316892019-05-01 18:08:42 +02001618/*
1619 * Return the window number of "win".
1620 * When "win" is NULL return the number of windows.
1621 */
Bram Moolenaarf240e182014-11-27 18:33:02 +01001622 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001623current_win_nr(win_T *win)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001624{
1625 win_T *wp;
1626 int nr = 0;
1627
Bram Moolenaar29323592016-07-24 22:04:11 +02001628 FOR_ALL_WINDOWS(wp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001629 {
1630 ++nr;
1631 if (wp == win)
1632 break;
1633 }
1634 return nr;
1635}
1636
1637 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001638current_tab_nr(tabpage_T *tab)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001639{
1640 tabpage_T *tp;
1641 int nr = 0;
1642
Bram Moolenaar29323592016-07-24 22:04:11 +02001643 FOR_ALL_TABPAGES(tp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001644 {
1645 ++nr;
1646 if (tp == tab)
1647 break;
1648 }
1649 return nr;
1650}
1651
Bram Moolenaar6e36b1c2020-07-17 20:47:51 +02001652 static int
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001653comment_start(char_u *p, int starts_with_colon UNUSED)
1654{
1655#ifdef FEAT_EVAL
1656 if (in_vim9script())
1657 return p[0] == '#' && p[1] != '{' && !starts_with_colon;
1658#endif
1659 return *p == '"';
1660}
1661
Bram Moolenaarf240e182014-11-27 18:33:02 +01001662# define CURRENT_WIN_NR current_win_nr(curwin)
1663# define LAST_WIN_NR current_win_nr(NULL)
1664# define CURRENT_TAB_NR current_tab_nr(curtab)
1665# define LAST_TAB_NR current_tab_nr(NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001666
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667/*
1668 * Execute one Ex command.
1669 *
1670 * If 'sourcing' is TRUE, the command will be included in the error message.
1671 *
1672 * 1. skip comment lines and leading space
1673 * 2. handle command modifiers
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001674 * 3. find the command
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001675 * 4. parse range
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001676 * 5. Parse the command.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001677 * 6. parse arguments
1678 * 7. switch on command name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001679 *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001680 * Note: "fgetline" can be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681 *
1682 * This function may be called recursively!
1683 */
1684#if (_MSC_VER == 1200)
1685/*
Bram Moolenaared203462004-06-16 11:19:22 +00001686 * Avoid optimisation bug in VC++ version 6.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687 */
Bram Moolenaar281bdce2005-01-25 21:53:18 +00001688 #pragma optimize( "g", off )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689#endif
1690 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001691do_one_cmd(
Bram Moolenaarddef1292019-12-16 17:10:33 +01001692 char_u **cmdlinep,
1693 int sourcing,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001694#ifdef FEAT_EVAL
Bram Moolenaarddef1292019-12-16 17:10:33 +01001695 cstack_T *cstack,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001696#endif
Bram Moolenaarddef1292019-12-16 17:10:33 +01001697 char_u *(*fgetline)(int, void *, int, int),
1698 void *cookie) // argument for fgetline()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001699{
Bram Moolenaarddef1292019-12-16 17:10:33 +01001700 char_u *p;
1701 linenr_T lnum;
1702 long n;
1703 char *errormsg = NULL; // error message
1704 char_u *after_modifier = NULL;
1705 exarg_T ea; // Ex command arguments
1706 int save_msg_scroll = msg_scroll;
1707 cmdmod_T save_cmdmod;
1708 int save_reg_executing = reg_executing;
1709 int ni; // set when Not Implemented
1710 char_u *cmd;
Bram Moolenaar6e36b1c2020-07-17 20:47:51 +02001711 int starts_with_colon = FALSE;
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001712#ifdef FEAT_EVAL
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02001713 int may_have_range;
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001714 int vim9script = in_vim9script();
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001715#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716
Bram Moolenaara80faa82020-04-12 19:37:17 +02001717 CLEAR_FIELD(ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718 ea.line1 = 1;
1719 ea.line2 = 1;
1720#ifdef FEAT_EVAL
1721 ++ex_nesting_level;
1722#endif
1723
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001724 // When the last file has not been edited :q has to be typed twice.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725 if (quitmore
1726#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001727 // avoid that a function call in 'statusline' does this
Bram Moolenaar89d40322006-08-29 15:30:07 +00001728 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001729#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001730 // avoid that an autocommand, e.g. QuitPre, does this
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001731 && !getline_equal(fgetline, cookie, getnextac))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732 --quitmore;
1733
1734 /*
1735 * Reset browse, confirm, etc.. They are restored when returning, for
1736 * recursive calls.
1737 */
1738 save_cmdmod = cmdmod;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001740 // "#!anything" is handled like a comment.
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001741 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1742 goto doend;
1743
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02001744/*
1745 * 1. Skip comment lines and leading white space and colons.
1746 * 2. Handle command modifiers.
1747 */
1748 // The "ea" structure holds the arguments that can be used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749 ea.cmd = *cmdlinep;
Bram Moolenaareffed932018-08-14 13:38:17 +02001750 ea.cmdlinep = cmdlinep;
1751 ea.getline = fgetline;
1752 ea.cookie = cookie;
1753#ifdef FEAT_EVAL
1754 ea.cstack = cstack;
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001755 starts_with_colon = *skipwhite(ea.cmd) == ':';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001756#endif
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02001757 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaareffed932018-08-14 13:38:17 +02001758 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001760 after_modifier = ea.cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001761
1762#ifdef FEAT_EVAL
1763 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
1764 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
1765#else
1766 ea.skip = (if_level > 0);
1767#endif
1768
Bram Moolenaar071d4272004-06-13 20:20:40 +00001769/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001770 * 3. Skip over the range to find the command. Let "p" point to after it.
1771 *
1772 * We need the command to know what kind of range it uses.
1773 */
1774 cmd = ea.cmd;
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001775#ifdef FEAT_EVAL
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02001776 // In Vim9 script a colon is required before the range.
1777 may_have_range = !vim9script || starts_with_colon;
1778 if (may_have_range)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001779#endif
1780 ea.cmd = skip_range(ea.cmd, NULL);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001781 if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
1782 ea.cmd = skipwhite(ea.cmd + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001783
1784#ifdef FEAT_EVAL
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001785 if (vim9script && !starts_with_colon)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02001786 {
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02001787 if (ea.cmd == cmd + 1 && *cmd == '$')
1788 // should be "$VAR = val"
1789 --ea.cmd;
1790 else if (ea.cmd > cmd)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02001791 {
1792 emsg(_(e_colon_required));
1793 goto doend;
1794 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001795 p = find_ex_command(&ea, NULL, lookup_scriptvar, NULL);
Bram Moolenaardf069ee2020-06-22 23:02:51 +02001796 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001797 else
1798#endif
1799 p = find_ex_command(&ea, NULL, NULL, NULL);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001800
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001801#ifdef FEAT_EVAL
1802# ifdef FEAT_PROFILE
1803 // Count this line for profiling if skip is TRUE.
1804 if (do_profiling == PROF_YES
1805 && (!ea.skip || cstack->cs_idx == 0 || (cstack->cs_idx > 0
1806 && (cstack->cs_flags[cstack->cs_idx - 1] & CSF_ACTIVE))))
1807 {
1808 int skip = did_emsg || got_int || did_throw;
1809
1810 if (ea.cmdidx == CMD_catch)
1811 skip = !skip && !(cstack->cs_idx >= 0
1812 && (cstack->cs_flags[cstack->cs_idx] & CSF_THROWN)
1813 && !(cstack->cs_flags[cstack->cs_idx] & CSF_CAUGHT));
1814 else if (ea.cmdidx == CMD_else || ea.cmdidx == CMD_elseif)
1815 skip = skip || !(cstack->cs_idx >= 0
1816 && !(cstack->cs_flags[cstack->cs_idx]
1817 & (CSF_ACTIVE | CSF_TRUE)));
1818 else if (ea.cmdidx == CMD_finally)
1819 skip = FALSE;
1820 else if (ea.cmdidx != CMD_endif
1821 && ea.cmdidx != CMD_endfor
1822 && ea.cmdidx != CMD_endtry
1823 && ea.cmdidx != CMD_endwhile)
1824 skip = ea.skip;
1825
1826 if (!skip)
1827 {
1828 if (getline_equal(fgetline, cookie, get_func_line))
1829 func_line_exec(getline_cookie(fgetline, cookie));
1830 else if (getline_equal(fgetline, cookie, getsourceline))
1831 script_line_exec();
1832 }
1833 }
1834# endif
1835
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001836 // May go to debug mode. If this happens and the ">quit" debug command is
1837 // used, throw an interrupt exception and skip the next command.
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001838 dbg_check_breakpoint(&ea);
1839 if (!ea.skip && got_int)
1840 {
1841 ea.skip = TRUE;
1842 (void)do_intthrow(cstack);
1843 }
1844#endif
1845
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001846/*
1847 * 4. parse a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848 *
1849 * where 'addr' is:
1850 *
1851 * % (entire file)
1852 * $ [+-NUM]
1853 * 'x [+-NUM] (where x denotes a currently defined mark)
1854 * . [+-NUM]
1855 * [+-NUM]..
1856 * NUM
1857 *
1858 * The ea.cmd pointer is updated to point to the first character following the
1859 * range spec. If an initial address is found, but no second, the upper bound
1860 * is equal to the lower.
1861 */
1862
Bram Moolenaar25190db2019-05-04 15:05:28 +02001863 // ea.addr_type for user commands is set by find_ucmd
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001864 if (!IS_USER_CMDIDX(ea.cmdidx))
1865 {
1866 if (ea.cmdidx != CMD_SIZE)
1867 ea.addr_type = cmdnames[(int)ea.cmdidx].cmd_addr_type;
1868 else
1869 ea.addr_type = ADDR_LINES;
1870
Bram Moolenaar25190db2019-05-04 15:05:28 +02001871 // :wincmd range depends on the argument.
Bram Moolenaar164f3262015-01-14 21:22:01 +01001872 if (ea.cmdidx == CMD_wincmd && p != NULL)
1873 get_wincmd_addr_type(skipwhite(p), &ea);
Bram Moolenaar25190db2019-05-04 15:05:28 +02001874#ifdef FEAT_QUICKFIX
1875 // :.cc in quickfix window uses line number
1876 if ((ea.cmdidx == CMD_cc || ea.cmdidx == CMD_ll) && bt_quickfix(curbuf))
1877 ea.addr_type = ADDR_OTHER;
1878#endif
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001879 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001880
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001881 ea.cmd = cmd;
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001882#ifdef FEAT_EVAL
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02001883 if (may_have_range)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001884#endif
1885 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
1886 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887
Bram Moolenaar071d4272004-06-13 20:20:40 +00001888/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001889 * 5. Parse the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001890 */
1891
1892 /*
1893 * Skip ':' and any white space
1894 */
1895 ea.cmd = skipwhite(ea.cmd);
1896 while (*ea.cmd == ':')
1897 ea.cmd = skipwhite(ea.cmd + 1);
1898
1899 /*
1900 * If we got a line, but no command, then go to the line.
1901 * If we find a '|' or '\n' we set ea.nextcmd.
1902 */
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001903 if (*ea.cmd == NUL || comment_start(ea.cmd, starts_with_colon)
1904 || (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905 {
1906 /*
1907 * strange vi behaviour:
1908 * ":3" jumps to line 3
1909 * ":3|..." prints line 3
1910 * ":|" prints current line
1911 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001912 if (ea.skip) // skip this if inside :if
Bram Moolenaar071d4272004-06-13 20:20:40 +00001913 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001914 if (*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001915 {
1916 ea.cmdidx = CMD_print;
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001917 ea.argt = EX_RANGE+EX_COUNT+EX_TRLBAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001918 if ((errormsg = invalid_range(&ea)) == NULL)
1919 {
1920 correct_range(&ea);
1921 ex_print(&ea);
1922 }
1923 }
1924 else if (ea.addr_count != 0)
1925 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001926 if (ea.line2 > curbuf->b_ml.ml_line_count)
1927 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001928 // With '-' in 'cpoptions' a line number past the file is an
1929 // error, otherwise put it at the end of the file.
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001930 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
1931 ea.line2 = -1;
1932 else
1933 ea.line2 = curbuf->b_ml.ml_line_count;
1934 }
1935
1936 if (ea.line2 < 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001937 errormsg = _(e_invrange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938 else
1939 {
1940 if (ea.line2 == 0)
1941 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001942 else
1943 curwin->w_cursor.lnum = ea.line2;
1944 beginline(BL_SOL | BL_FIX);
1945 }
1946 }
1947 goto doend;
1948 }
1949
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001950 // If this looks like an undefined user command and there are CmdUndefined
1951 // autocommands defined, trigger the matching autocommands.
Bram Moolenaard5005162014-08-22 23:05:54 +02001952 if (p != NULL && ea.cmdidx == CMD_SIZE && !ea.skip
1953 && ASCII_ISUPPER(*ea.cmd)
1954 && has_cmdundefined())
1955 {
Bram Moolenaard5005162014-08-22 23:05:54 +02001956 int ret;
1957
Bram Moolenaar5a31b462014-08-23 14:16:20 +02001958 p = ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02001959 while (ASCII_ISALNUM(*p))
1960 ++p;
Bram Moolenaar158ea172020-06-18 17:28:39 +02001961 p = vim_strnsave(ea.cmd, p - ea.cmd);
Bram Moolenaard5005162014-08-22 23:05:54 +02001962 ret = apply_autocmds(EVENT_CMDUNDEFINED, p, p, TRUE, NULL);
1963 vim_free(p);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001964 // If the autocommands did something and didn't cause an error, try
1965 // finding the command again.
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001966 p = (ret
1967#ifdef FEAT_EVAL
1968 && !aborting()
Bram Moolenaard5005162014-08-22 23:05:54 +02001969#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001970 ) ? find_ex_command(&ea, NULL, NULL, NULL) : ea.cmd;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001971 }
Bram Moolenaard5005162014-08-22 23:05:54 +02001972
Bram Moolenaar071d4272004-06-13 20:20:40 +00001973 if (p == NULL)
1974 {
1975 if (!ea.skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001976 errormsg = _("E464: Ambiguous use of user-defined command");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001977 goto doend;
1978 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001979 // Check for wrong commands.
Bram Moolenaar6f9a4762017-06-22 20:39:17 +02001980 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78
1981 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982 {
1983 errormsg = uc_fun_cmd();
1984 goto doend;
1985 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001986
Bram Moolenaar071d4272004-06-13 20:20:40 +00001987 if (ea.cmdidx == CMD_SIZE)
1988 {
1989 if (!ea.skip)
1990 {
1991 STRCPY(IObuff, _("E492: Not an editor command"));
1992 if (!sourcing)
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001993 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001994 // If the modifier was parsed OK the error must be in the
1995 // following command
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001996 if (after_modifier != NULL)
1997 append_command(after_modifier);
1998 else
1999 append_command(*cmdlinep);
2000 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002001 errormsg = (char *)IObuff;
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02002002 did_emsg_syntax = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002003 }
2004 goto doend;
2005 }
2006
Bram Moolenaar958636c2014-10-21 20:01:58 +02002007 ni = (!IS_USER_CMDIDX(ea.cmdidx)
2008 && (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00002009#ifdef HAVE_EX_SCRIPT_NI
2010 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
2011#endif
2012 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002013
2014#ifndef FEAT_EVAL
2015 /*
2016 * When the expression evaluation is disabled, recognize the ":if" and
2017 * ":endif" commands and ignore everything in between it.
2018 */
2019 if (ea.cmdidx == CMD_if)
2020 ++if_level;
2021 if (if_level)
2022 {
2023 if (ea.cmdidx == CMD_endif)
2024 --if_level;
2025 goto doend;
2026 }
2027
2028#endif
2029
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002030 // forced commands
Bram Moolenaar196b3b02008-06-20 16:51:41 +00002031 if (*p == '!' && ea.cmdidx != CMD_substitute
2032 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002033 {
2034 ++p;
2035 ea.forceit = TRUE;
2036 }
2037 else
2038 ea.forceit = FALSE;
2039
2040/*
Bram Moolenaarb7316892019-05-01 18:08:42 +02002041 * 6. Parse arguments. Then check for errors.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002042 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02002043 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002044 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002045
2046 if (!ea.skip)
2047 {
2048#ifdef HAVE_SANDBOX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002049 if (sandbox != 0 && !(ea.argt & EX_SBOXOK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050 {
Bram Moolenaar8c62a082019-02-08 14:34:10 +01002051 // Command not allowed in sandbox.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002052 errormsg = _(e_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002053 goto doend;
2054 }
2055#endif
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002056 if (restricted != 0 && (ea.argt & EX_RESTRICT))
Bram Moolenaar8c62a082019-02-08 14:34:10 +01002057 {
2058 errormsg = _("E981: Command not allowed in rvim");
2059 goto doend;
2060 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002061 if (!curbuf->b_p_ma && (ea.argt & EX_MODIFY))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002062 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002063 // Command not allowed in non-'modifiable' buffer
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002064 errormsg = _(e_modifiable);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002065 goto doend;
2066 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002067
Bram Moolenaar37394ff2020-07-25 19:38:18 +02002068 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069 {
Bram Moolenaar37394ff2020-07-25 19:38:18 +02002070#ifdef FEAT_CMDWIN
2071 if (cmdwin_type != 0 && !(ea.argt & EX_CMDWIN))
2072 {
2073 // Command not allowed in the command line window
2074 errormsg = _(e_cmdwin);
2075 goto doend;
2076 }
2077#endif
2078 if (text_locked() && !(ea.argt & EX_LOCK_OK))
2079 {
2080 // Command not allowed when text is locked
2081 errormsg = _(get_text_locked_msg());
2082 goto doend;
2083 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084 }
Bram Moolenaar379fb762018-08-30 15:58:28 +02002085
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002086 // Disallow editing another buffer when "curbuf_lock" is set.
2087 // Do allow ":checktime" (it is postponed).
2088 // Do allow ":edit" (check for an argument later).
2089 // Do allow ":file" with no arguments (check for an argument later).
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02002090 if (!(ea.argt & (EX_CMDWIN | EX_LOCK_OK))
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002091 && ea.cmdidx != CMD_checktime
Bram Moolenaar379fb762018-08-30 15:58:28 +02002092 && ea.cmdidx != CMD_edit
2093 && ea.cmdidx != CMD_file
Bram Moolenaar958636c2014-10-21 20:01:58 +02002094 && !IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002095 && curbuf_locked())
2096 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002097
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002098 if (!ni && !(ea.argt & EX_RANGE) && ea.addr_count > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002099 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002100 // no range allowed
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002101 errormsg = _(e_norange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002102 goto doend;
2103 }
2104 }
2105
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002106 if (!ni && !(ea.argt & EX_BANG) && ea.forceit) // no <!> allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002107 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002108 errormsg = _(e_nobang);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002109 goto doend;
2110 }
2111
2112 /*
2113 * Don't complain about the range if it is not used
2114 * (could happen if line_count is accidentally set to 0).
2115 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002116 if (!ea.skip && !ni && (ea.argt & EX_RANGE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002117 {
2118 /*
2119 * If the range is backwards, ask for confirmation and, if given, swap
2120 * ea.line1 & ea.line2 so it's forwards again.
2121 * When global command is busy, don't ask, will fail below.
2122 */
2123 if (!global_busy && ea.line1 > ea.line2)
2124 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002125 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002126 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002127 if (sourcing || exmode_active)
2128 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002129 errormsg = _("E493: Backwards range given");
Bram Moolenaara5792f52005-11-23 21:25:05 +00002130 goto doend;
2131 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132 if (ask_yesno((char_u *)
2133 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002134 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135 }
2136 lnum = ea.line1;
2137 ea.line1 = ea.line2;
2138 ea.line2 = lnum;
2139 }
2140 if ((errormsg = invalid_range(&ea)) != NULL)
2141 goto doend;
2142 }
2143
Bram Moolenaarb7316892019-05-01 18:08:42 +02002144 if ((ea.addr_type == ADDR_OTHER) && ea.addr_count == 0)
2145 // default is 1, not cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002146 ea.line2 = 1;
2147
2148 correct_range(&ea);
2149
2150#ifdef FEAT_FOLDING
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002151 if (((ea.argt & EX_WHOLEFOLD) || ea.addr_count >= 2) && !global_busy
Bram Moolenaara3306952016-01-02 21:41:06 +01002152 && ea.addr_type == ADDR_LINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002153 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002154 // Put the first line at the start of a closed fold, put the last line
2155 // at the end of a closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002156 (void)hasFolding(ea.line1, &ea.line1, NULL);
2157 (void)hasFolding(ea.line2, NULL, &ea.line2);
2158 }
2159#endif
2160
2161#ifdef FEAT_QUICKFIX
2162 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002163 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164 * option here, so things like % get expanded.
2165 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002166 p = replace_makeprg(&ea, p, cmdlinep);
2167 if (p == NULL)
2168 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002169#endif
2170
2171 /*
2172 * Skip to start of argument.
2173 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2174 */
2175 if (ea.cmdidx == CMD_bang)
2176 ea.arg = p;
2177 else
2178 ea.arg = skipwhite(p);
2179
Bram Moolenaar379fb762018-08-30 15:58:28 +02002180 // ":file" cannot be run with an argument when "curbuf_lock" is set
2181 if (ea.cmdidx == CMD_file && *ea.arg != NUL && curbuf_locked())
2182 goto doend;
2183
Bram Moolenaar071d4272004-06-13 20:20:40 +00002184 /*
2185 * Check for "++opt=val" argument.
2186 * Must be first, allow ":w ++enc=utf8 !cmd"
2187 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002188 if (ea.argt & EX_ARGOPT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002189 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2190 if (getargopt(&ea) == FAIL && !ni)
2191 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002192 errormsg = _(e_invarg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002193 goto doend;
2194 }
2195
2196 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2197 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002198 if (*ea.arg == '>') // append
Bram Moolenaar071d4272004-06-13 20:20:40 +00002199 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002200 if (*++ea.arg != '>') // typed wrong
Bram Moolenaar071d4272004-06-13 20:20:40 +00002201 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002202 errormsg = _("E494: Use w or w>>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002203 goto doend;
2204 }
2205 ea.arg = skipwhite(ea.arg + 1);
2206 ea.append = TRUE;
2207 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002208 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) // :w !filter
Bram Moolenaar071d4272004-06-13 20:20:40 +00002209 {
2210 ++ea.arg;
2211 ea.usefilter = TRUE;
2212 }
2213 }
2214
2215 if (ea.cmdidx == CMD_read)
2216 {
2217 if (ea.forceit)
2218 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002219 ea.usefilter = TRUE; // :r! filter if ea.forceit
Bram Moolenaar071d4272004-06-13 20:20:40 +00002220 ea.forceit = FALSE;
2221 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002222 else if (*ea.arg == '!') // :r !filter
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223 {
2224 ++ea.arg;
2225 ea.usefilter = TRUE;
2226 }
2227 }
2228
2229 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2230 {
2231 ea.amount = 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002232 while (*ea.arg == *ea.cmd) // count number of '>' or '<'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002233 {
2234 ++ea.arg;
2235 ++ea.amount;
2236 }
2237 ea.arg = skipwhite(ea.arg);
2238 }
2239
2240 /*
2241 * Check for "+command" argument, before checking for next command.
2242 * Don't do this for ":read !cmd" and ":write !cmd".
2243 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002244 if ((ea.argt & EX_CMDARG) && !ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002245 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2246
2247 /*
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002248 * Check for '|' to separate commands and '"' or '#' to start comments.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249 * Don't do this for ":read !cmd" and ":write !cmd".
2250 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002251 if ((ea.argt & EX_TRLBAR) && !ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002252 separate_nextcmd(&ea);
2253
2254 /*
2255 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002256 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2257 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002258 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002259 else if (ea.cmdidx == CMD_bang
Bram Moolenaar67883b42017-07-27 22:57:00 +02002260 || ea.cmdidx == CMD_terminal
Bram Moolenaardf177f62005-02-22 08:39:57 +00002261 || ea.cmdidx == CMD_global
2262 || ea.cmdidx == CMD_vglobal
2263 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002264 {
2265 for (p = ea.arg; *p; ++p)
2266 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002267 // Remove one backslash before a newline, so that it's possible to
2268 // pass a newline to the shell and also a newline that is preceded
2269 // with a backslash. This makes it impossible to end a shell
2270 // command in a backslash, but that doesn't appear useful.
2271 // Halving the number of backslashes is incompatible with previous
2272 // versions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002273 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002274 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002275 else if (*p == '\n')
2276 {
2277 ea.nextcmd = p + 1;
2278 *p = NUL;
2279 break;
2280 }
2281 }
2282 }
2283
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002284 if ((ea.argt & EX_DFLALL) && ea.addr_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285 {
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002286 buf_T *buf;
2287
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288 ea.line1 = 1;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002289 switch (ea.addr_type)
2290 {
2291 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02002292 case ADDR_OTHER:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002293 ea.line2 = curbuf->b_ml.ml_line_count;
2294 break;
2295 case ADDR_LOADED_BUFFERS:
2296 buf = firstbuf;
2297 while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL)
2298 buf = buf->b_next;
2299 ea.line1 = buf->b_fnum;
2300 buf = lastbuf;
2301 while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL)
2302 buf = buf->b_prev;
2303 ea.line2 = buf->b_fnum;
2304 break;
2305 case ADDR_BUFFERS:
2306 ea.line1 = firstbuf->b_fnum;
2307 ea.line2 = lastbuf->b_fnum;
2308 break;
2309 case ADDR_WINDOWS:
2310 ea.line2 = LAST_WIN_NR;
2311 break;
2312 case ADDR_TABS:
2313 ea.line2 = LAST_TAB_NR;
2314 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01002315 case ADDR_TABS_RELATIVE:
2316 ea.line2 = 1;
2317 break;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002318 case ADDR_ARGUMENTS:
2319 if (ARGCOUNT == 0)
2320 ea.line1 = ea.line2 = 0;
2321 else
2322 ea.line2 = ARGCOUNT;
2323 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02002324 case ADDR_QUICKFIX_VALID:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002325#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02002326 ea.line2 = qf_get_valid_size(&ea);
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002327 if (ea.line2 == 0)
2328 ea.line2 = 1;
Bram Moolenaare906c502015-09-09 21:10:39 +02002329#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002330 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02002331 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02002332 case ADDR_UNSIGNED:
2333 case ADDR_QUICKFIX:
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002334 iemsg(_("INTERNAL: Cannot use EX_DFLALL with ADDR_NONE, ADDR_UNSIGNED or ADDR_QUICKFIX"));
Bram Moolenaarb7316892019-05-01 18:08:42 +02002335 break;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002336 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002337 }
2338
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002339 // accept numbered register only when no count allowed (:put)
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002340 if ( (ea.argt & EX_REGSTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341 && *ea.arg != NUL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002342 // Do not allow register = for user commands
Bram Moolenaar958636c2014-10-21 20:01:58 +02002343 && (!IS_USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002344 && !((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002346#ifndef FEAT_CLIPBOARD
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002347 // check these explicitly for a more specific error message
Bram Moolenaar85de2062011-05-05 14:26:41 +02002348 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002349 {
Bram Moolenaar99b12722019-01-14 20:16:40 +01002350 errormsg = _(e_invalidreg);
Bram Moolenaar85de2062011-05-05 14:26:41 +02002351 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352 }
2353#endif
Bram Moolenaar958636c2014-10-21 20:01:58 +02002354 if (valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2355 && !IS_USER_CMDIDX(ea.cmdidx))))
Bram Moolenaar85de2062011-05-05 14:26:41 +02002356 {
2357 ea.regname = *ea.arg++;
2358#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002359 // for '=' register: accept the rest of the line as an expression
Bram Moolenaar85de2062011-05-05 14:26:41 +02002360 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2361 {
2362 set_expr_line(vim_strsave(ea.arg));
2363 ea.arg += STRLEN(ea.arg);
2364 }
2365#endif
2366 ea.arg = skipwhite(ea.arg);
2367 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368 }
2369
2370 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002371 * Check for a count. When accepting a EX_BUFNAME, don't use "123foo" as a
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372 * count, it's a buffer name.
2373 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002374 if ((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)
2375 && (!(ea.argt & EX_BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01002376 || VIM_ISWHITE(*p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377 {
2378 n = getdigits(&ea.arg);
2379 ea.arg = skipwhite(ea.arg);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002380 if (n <= 0 && !ni && (ea.argt & EX_ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002381 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002382 errormsg = _(e_zerocount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002383 goto doend;
2384 }
Bram Moolenaarb7316892019-05-01 18:08:42 +02002385 if (ea.addr_type != ADDR_LINES) // e.g. :buffer 2, :sleep 3
Bram Moolenaar071d4272004-06-13 20:20:40 +00002386 {
2387 ea.line2 = n;
2388 if (ea.addr_count == 0)
2389 ea.addr_count = 1;
2390 }
2391 else
2392 {
2393 ea.line1 = ea.line2;
2394 ea.line2 += n - 1;
2395 ++ea.addr_count;
2396 /*
2397 * Be vi compatible: no error message for out of range.
2398 */
Bram Moolenaarb7316892019-05-01 18:08:42 +02002399 if (ea.line2 > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400 ea.line2 = curbuf->b_ml.ml_line_count;
2401 }
2402 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002403
2404 /*
2405 * Check for flags: 'l', 'p' and '#'.
2406 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002407 if (ea.argt & EX_FLAGS)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002408 get_flags(&ea);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002409 if (!ni && !(ea.argt & EX_EXTRA) && *ea.arg != NUL
2410 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & EX_TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002412 // no arguments allowed but there is something
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02002413 errormsg = ex_errmsg(e_trailing_arg, ea.arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414 goto doend;
2415 }
2416
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002417 if (!ni && (ea.argt & EX_NEEDARG) && *ea.arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002418 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002419 errormsg = _(e_argreq);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420 goto doend;
2421 }
2422
2423#ifdef FEAT_EVAL
2424 /*
2425 * Skip the command when it's not going to be executed.
2426 * The commands like :if, :endif, etc. always need to be executed.
2427 * Also make an exception for commands that handle a trailing command
2428 * themselves.
2429 */
2430 if (ea.skip)
2431 {
2432 switch (ea.cmdidx)
2433 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002434 // commands that need evaluation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435 case CMD_while:
2436 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002437 case CMD_for:
2438 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002439 case CMD_if:
2440 case CMD_elseif:
2441 case CMD_else:
2442 case CMD_endif:
2443 case CMD_try:
2444 case CMD_catch:
2445 case CMD_finally:
2446 case CMD_endtry:
2447 case CMD_function:
Bram Moolenaarab55c682020-03-01 19:41:43 +01002448 case CMD_def:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002449 break;
2450
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002451 // Commands that handle '|' themselves. Check: A command should
2452 // either have the EX_TRLBAR flag, appear in this list or appear in
2453 // the list at ":help :bar".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454 case CMD_aboveleft:
2455 case CMD_and:
2456 case CMD_belowright:
2457 case CMD_botright:
2458 case CMD_browse:
2459 case CMD_call:
2460 case CMD_confirm:
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01002461 case CMD_const:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002462 case CMD_delfunction:
2463 case CMD_djump:
2464 case CMD_dlist:
2465 case CMD_dsearch:
2466 case CMD_dsplit:
2467 case CMD_echo:
2468 case CMD_echoerr:
2469 case CMD_echomsg:
2470 case CMD_echon:
Bram Moolenaara76b3152020-02-16 16:20:21 +01002471 case CMD_eval:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472 case CMD_execute:
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002473 case CMD_filter:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474 case CMD_help:
2475 case CMD_hide:
2476 case CMD_ijump:
2477 case CMD_ilist:
2478 case CMD_isearch:
2479 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002480 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481 case CMD_keepjumps:
2482 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01002483 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484 case CMD_leftabove:
2485 case CMD_let:
2486 case CMD_lockmarks:
Bram Moolenaarcc4423a2019-11-26 17:05:00 +01002487 case CMD_lockvar:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002488 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002489 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002490 case CMD_mzscheme:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01002491 case CMD_noautocmd:
2492 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493 case CMD_perl:
2494 case CMD_psearch:
2495 case CMD_python:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002496 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002497 case CMD_python3:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498 case CMD_return:
2499 case CMD_rightbelow:
2500 case CMD_ruby:
2501 case CMD_silent:
2502 case CMD_smagic:
2503 case CMD_snomagic:
2504 case CMD_substitute:
2505 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002506 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002507 case CMD_tcl:
2508 case CMD_throw:
2509 case CMD_tilde:
2510 case CMD_topleft:
2511 case CMD_unlet:
Bram Moolenaarcc4423a2019-11-26 17:05:00 +01002512 case CMD_unlockvar:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002513 case CMD_verbose:
2514 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002515 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002516 break;
2517
2518 default: goto doend;
2519 }
2520 }
2521#endif
2522
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002523 if (ea.argt & EX_XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524 {
2525 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2526 goto doend;
2527 }
2528
Bram Moolenaar071d4272004-06-13 20:20:40 +00002529 /*
2530 * Accept buffer name. Cannot be used at the same time with a buffer
2531 * number. Don't do this for a user command.
2532 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002533 if ((ea.argt & EX_BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
Bram Moolenaar958636c2014-10-21 20:01:58 +02002534 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002535 {
2536 /*
2537 * :bdelete, :bwipeout and :bunload take several arguments, separated
2538 * by spaces: find next space (skipping over escaped characters).
2539 * The others take one argument: ignore trailing spaces.
2540 */
2541 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2542 || ea.cmdidx == CMD_bunload)
2543 p = skiptowhite_esc(ea.arg);
2544 else
2545 {
2546 p = ea.arg + STRLEN(ea.arg);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002547 while (p > ea.arg && VIM_ISWHITE(p[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002548 --p;
2549 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002550 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & EX_BUFUNL) != 0,
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002551 FALSE, FALSE);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002552 if (ea.line2 < 0) // failed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553 goto doend;
2554 ea.addr_count = 1;
2555 ea.arg = skipwhite(p);
2556 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002557
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002558 // The :try command saves the emsg_silent flag, reset it here when
2559 // ":silent! try" was used, it should only apply to :try itself.
Bram Moolenaareffed932018-08-14 13:38:17 +02002560 if (ea.cmdidx == CMD_try && ea.did_esilent > 0)
Bram Moolenaar2be57332018-02-13 18:05:18 +01002561 {
Bram Moolenaareffed932018-08-14 13:38:17 +02002562 emsg_silent -= ea.did_esilent;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002563 if (emsg_silent < 0)
2564 emsg_silent = 0;
Bram Moolenaareffed932018-08-14 13:38:17 +02002565 ea.did_esilent = 0;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002566 }
2567
Bram Moolenaar071d4272004-06-13 20:20:40 +00002568/*
Bram Moolenaar2be57332018-02-13 18:05:18 +01002569 * 7. Execute the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002570 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002571
Bram Moolenaar958636c2014-10-21 20:01:58 +02002572 if (IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002573 {
2574 /*
2575 * Execute a user-defined command.
2576 */
2577 do_ucmd(&ea);
2578 }
2579 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580 {
2581 /*
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002582 * Call the function to execute the builtin command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002583 */
2584 ea.errmsg = NULL;
2585 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2586 if (ea.errmsg != NULL)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02002587 errormsg = ea.errmsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002588 }
2589
2590#ifdef FEAT_EVAL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002591 // Set flag that any command was executed, used by ex_vim9script().
Bram Moolenaar16531552020-02-08 16:00:46 +01002592 if (getline_equal(ea.getline, ea.cookie, getsourceline)
2593 && current_sctx.sc_sid > 0)
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002594 SCRIPT_ITEM(current_sctx.sc_sid)->sn_had_command = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002595
Bram Moolenaar071d4272004-06-13 20:20:40 +00002596 /*
2597 * If the command just executed called do_cmdline(), any throw or ":return"
2598 * or ":finish" encountered there must also check the cstack of the still
2599 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2600 * exception, or reanimate a returned function or finished script file and
2601 * return or finish it again.
2602 */
2603 if (need_rethrow)
2604 do_throw(cstack);
2605 else if (check_cstack)
2606 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002607 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002608 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002609 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002610 && current_func_returned())
2611 do_return(&ea, TRUE, FALSE, NULL);
2612 }
2613 need_rethrow = check_cstack = FALSE;
2614#endif
2615
2616doend:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002617 if (curwin->w_cursor.lnum == 0) // can happen with zero line number
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002618 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002619 curwin->w_cursor.lnum = 1;
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002620 curwin->w_cursor.col = 0;
2621 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002622
2623 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2624 {
2625 if (sourcing)
2626 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002627 if (errormsg != (char *)IObuff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002628 {
2629 STRCPY(IObuff, errormsg);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002630 errormsg = (char *)IObuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002632 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002633 }
2634 emsg(errormsg);
2635 }
2636#ifdef FEAT_EVAL
2637 do_errthrow(cstack,
Bram Moolenaar958636c2014-10-21 20:01:58 +02002638 (ea.cmdidx != CMD_SIZE && !IS_USER_CMDIDX(ea.cmdidx))
2639 ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640#endif
2641
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002642 undo_cmdmod(&ea, save_msg_scroll);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643 cmdmod = save_cmdmod;
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01002644 reg_executing = save_reg_executing;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002646#ifdef HAVE_SANDBOX
Bram Moolenaareffed932018-08-14 13:38:17 +02002647 if (ea.did_sandbox)
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002648 --sandbox;
2649#endif
2650
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002651 if (ea.nextcmd && *ea.nextcmd == NUL) // not really a next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652 ea.nextcmd = NULL;
2653
2654#ifdef FEAT_EVAL
2655 --ex_nesting_level;
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002656 vim_free(ea.cmdline_tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002657#endif
2658
2659 return ea.nextcmd;
2660}
2661#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00002662 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663#endif
2664
Bram Moolenaar8930caa2020-07-23 16:37:03 +02002665static char ex_error_buf[MSG_BUF_LEN];
2666
2667/*
2668 * Return an error message with argument included.
2669 * Uses a static buffer, only the last error will be kept.
2670 * "msg" will be translated, caller should use N_().
2671 */
2672 char *
2673ex_errmsg(char *msg, char_u *arg)
2674{
2675 vim_snprintf(ex_error_buf, MSG_BUF_LEN, _(msg), arg);
2676 return ex_error_buf;
2677}
2678
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679/*
Bram Moolenaareffed932018-08-14 13:38:17 +02002680 * Parse and skip over command modifiers:
2681 * - update eap->cmd
2682 * - store flags in "cmdmod".
2683 * - Set ex_pressedreturn for an empty command line.
2684 * - set msg_silent for ":silent"
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002685 * - set 'eventignore' to "all" for ":noautocmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002686 * - set p_verbose for ":verbose"
2687 * - Increment "sandbox" for ":sandbox"
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002688 * When "skip_only" is TRUE the global variables are not changed, except for
2689 * "cmdmod".
Bram Moolenaareffed932018-08-14 13:38:17 +02002690 * Return FAIL when the command is not to be executed.
2691 * May set "errormsg" to an error message.
2692 */
2693 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002694parse_command_modifiers(exarg_T *eap, char **errormsg, int skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002695{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002696 char_u *p;
2697 int starts_with_colon = FALSE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002698
Bram Moolenaara80faa82020-04-12 19:37:17 +02002699 CLEAR_FIELD(cmdmod);
Bram Moolenaareffed932018-08-14 13:38:17 +02002700 eap->verbose_save = -1;
2701 eap->save_msg_silent = -1;
2702
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002703 // Repeat until no more command modifiers are found.
Bram Moolenaareffed932018-08-14 13:38:17 +02002704 for (;;)
2705 {
Bram Moolenaareffed932018-08-14 13:38:17 +02002706 while (*eap->cmd == ' ' || *eap->cmd == '\t' || *eap->cmd == ':')
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002707 {
2708 if (*eap->cmd == ':')
2709 starts_with_colon = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002710 ++eap->cmd;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002711 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002712
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002713 // in ex mode, an empty line works like :+
Bram Moolenaareffed932018-08-14 13:38:17 +02002714 if (*eap->cmd == NUL && exmode_active
2715 && (getline_equal(eap->getline, eap->cookie, getexmodeline)
2716 || getline_equal(eap->getline, eap->cookie, getexline))
2717 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2718 {
2719 eap->cmd = (char_u *)"+";
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002720 if (!skip_only)
2721 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002722 }
2723
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002724 // ignore comment and empty lines
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002725 if (comment_start(eap->cmd, starts_with_colon))
Bram Moolenaareffed932018-08-14 13:38:17 +02002726 return FAIL;
2727 if (*eap->cmd == NUL)
2728 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002729 if (!skip_only)
2730 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002731 return FAIL;
2732 }
2733
Bram Moolenaareffed932018-08-14 13:38:17 +02002734 p = skip_range(eap->cmd, NULL);
2735 switch (*p)
2736 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002737 // When adding an entry, also modify cmd_exists().
Bram Moolenaareffed932018-08-14 13:38:17 +02002738 case 'a': if (!checkforcmd(&eap->cmd, "aboveleft", 3))
2739 break;
2740 cmdmod.split |= WSP_ABOVE;
2741 continue;
2742
2743 case 'b': if (checkforcmd(&eap->cmd, "belowright", 3))
2744 {
2745 cmdmod.split |= WSP_BELOW;
2746 continue;
2747 }
2748 if (checkforcmd(&eap->cmd, "browse", 3))
2749 {
2750#ifdef FEAT_BROWSE_CMD
2751 cmdmod.browse = TRUE;
2752#endif
2753 continue;
2754 }
2755 if (!checkforcmd(&eap->cmd, "botright", 2))
2756 break;
2757 cmdmod.split |= WSP_BOT;
2758 continue;
2759
2760 case 'c': if (!checkforcmd(&eap->cmd, "confirm", 4))
2761 break;
2762#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2763 cmdmod.confirm = TRUE;
2764#endif
2765 continue;
2766
2767 case 'k': if (checkforcmd(&eap->cmd, "keepmarks", 3))
2768 {
2769 cmdmod.keepmarks = TRUE;
2770 continue;
2771 }
2772 if (checkforcmd(&eap->cmd, "keepalt", 5))
2773 {
2774 cmdmod.keepalt = TRUE;
2775 continue;
2776 }
2777 if (checkforcmd(&eap->cmd, "keeppatterns", 5))
2778 {
2779 cmdmod.keeppatterns = TRUE;
2780 continue;
2781 }
2782 if (!checkforcmd(&eap->cmd, "keepjumps", 5))
2783 break;
2784 cmdmod.keepjumps = TRUE;
2785 continue;
2786
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002787 case 'f': // only accept ":filter {pat} cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002788 {
2789 char_u *reg_pat;
2790
2791 if (!checkforcmd(&p, "filter", 4)
2792 || *p == NUL || ends_excmd(*p))
2793 break;
2794 if (*p == '!')
2795 {
2796 cmdmod.filter_force = TRUE;
2797 p = skipwhite(p + 1);
2798 if (*p == NUL || ends_excmd(*p))
2799 break;
2800 }
Bram Moolenaar1e624c92020-07-11 14:08:04 +02002801#ifdef FEAT_EVAL
Bram Moolenaarb074e8b2020-07-11 13:40:45 +02002802 // Avoid that "filter(arg)" is recognized.
2803 if (in_vim9script() && !VIM_ISWHITE(*p))
2804 break;
Bram Moolenaar1e624c92020-07-11 14:08:04 +02002805#endif
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002806 if (skip_only)
2807 p = skip_vimgrep_pat(p, NULL, NULL);
2808 else
2809 // NOTE: This puts a NUL after the pattern.
2810 p = skip_vimgrep_pat(p, &reg_pat, NULL);
Bram Moolenaareffed932018-08-14 13:38:17 +02002811 if (p == NULL || *p == NUL)
2812 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002813 if (!skip_only)
2814 {
2815 cmdmod.filter_regmatch.regprog =
Bram Moolenaareffed932018-08-14 13:38:17 +02002816 vim_regcomp(reg_pat, RE_MAGIC);
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002817 if (cmdmod.filter_regmatch.regprog == NULL)
2818 break;
2819 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002820 eap->cmd = p;
2821 continue;
2822 }
2823
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002824 // ":hide" and ":hide | cmd" are not modifiers
Bram Moolenaareffed932018-08-14 13:38:17 +02002825 case 'h': if (p != eap->cmd || !checkforcmd(&p, "hide", 3)
2826 || *p == NUL || ends_excmd(*p))
2827 break;
2828 eap->cmd = p;
2829 cmdmod.hide = TRUE;
2830 continue;
2831
2832 case 'l': if (checkforcmd(&eap->cmd, "lockmarks", 3))
2833 {
2834 cmdmod.lockmarks = TRUE;
2835 continue;
2836 }
2837
2838 if (!checkforcmd(&eap->cmd, "leftabove", 5))
2839 break;
2840 cmdmod.split |= WSP_ABOVE;
2841 continue;
2842
2843 case 'n': if (checkforcmd(&eap->cmd, "noautocmd", 3))
2844 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002845 if (cmdmod.save_ei == NULL && !skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002846 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002847 // Set 'eventignore' to "all". Restore the
2848 // existing option value later.
Bram Moolenaareffed932018-08-14 13:38:17 +02002849 cmdmod.save_ei = vim_strsave(p_ei);
2850 set_string_option_direct((char_u *)"ei", -1,
2851 (char_u *)"all", OPT_FREE, SID_NONE);
2852 }
2853 continue;
2854 }
2855 if (!checkforcmd(&eap->cmd, "noswapfile", 3))
2856 break;
2857 cmdmod.noswapfile = TRUE;
2858 continue;
2859
2860 case 'r': if (!checkforcmd(&eap->cmd, "rightbelow", 6))
2861 break;
2862 cmdmod.split |= WSP_BELOW;
2863 continue;
2864
2865 case 's': if (checkforcmd(&eap->cmd, "sandbox", 3))
2866 {
2867#ifdef HAVE_SANDBOX
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002868 if (!skip_only)
2869 {
2870 if (!eap->did_sandbox)
2871 ++sandbox;
2872 eap->did_sandbox = TRUE;
2873 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002874#endif
2875 continue;
2876 }
2877 if (!checkforcmd(&eap->cmd, "silent", 3))
2878 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002879 if (!skip_only)
2880 {
2881 if (eap->save_msg_silent == -1)
2882 eap->save_msg_silent = msg_silent;
2883 ++msg_silent;
2884 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002885 if (*eap->cmd == '!' && !VIM_ISWHITE(eap->cmd[-1]))
2886 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002887 // ":silent!", but not "silent !cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002888 eap->cmd = skipwhite(eap->cmd + 1);
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002889 if (!skip_only)
2890 {
2891 ++emsg_silent;
2892 ++eap->did_esilent;
2893 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002894 }
2895 continue;
2896
2897 case 't': if (checkforcmd(&p, "tab", 3))
2898 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002899 if (!skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002900 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002901 long tabnr = get_address(eap, &eap->cmd,
2902 ADDR_TABS, eap->skip,
2903 skip_only, FALSE, 1);
2904 if (tabnr == MAXLNUM)
2905 cmdmod.tab = tabpage_index(curtab) + 1;
2906 else
Bram Moolenaareffed932018-08-14 13:38:17 +02002907 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002908 if (tabnr < 0 || tabnr > LAST_TAB_NR)
2909 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002910 *errormsg = _(e_invrange);
Bram Moolenaar548e5982018-12-26 21:45:00 +01002911 return FAIL;
2912 }
2913 cmdmod.tab = tabnr + 1;
Bram Moolenaareffed932018-08-14 13:38:17 +02002914 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002915 }
2916 eap->cmd = p;
2917 continue;
2918 }
2919 if (!checkforcmd(&eap->cmd, "topleft", 2))
2920 break;
2921 cmdmod.split |= WSP_TOP;
2922 continue;
2923
2924 case 'u': if (!checkforcmd(&eap->cmd, "unsilent", 3))
2925 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002926 if (!skip_only)
2927 {
2928 if (eap->save_msg_silent == -1)
2929 eap->save_msg_silent = msg_silent;
2930 msg_silent = 0;
2931 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002932 continue;
2933
2934 case 'v': if (checkforcmd(&eap->cmd, "vertical", 4))
2935 {
2936 cmdmod.split |= WSP_VERT;
2937 continue;
2938 }
2939 if (!checkforcmd(&p, "verbose", 4))
2940 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002941 if (!skip_only)
2942 {
2943 if (eap->verbose_save < 0)
2944 eap->verbose_save = p_verbose;
2945 if (vim_isdigit(*eap->cmd))
2946 p_verbose = atoi((char *)eap->cmd);
2947 else
2948 p_verbose = 1;
2949 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002950 eap->cmd = p;
2951 continue;
2952 }
2953 break;
2954 }
2955
2956 return OK;
2957}
2958
2959/*
Bram Moolenaarb074e8b2020-07-11 13:40:45 +02002960 * Undo and free contents of "cmdmod".
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002961 */
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002962 void
2963undo_cmdmod(exarg_T *eap, int save_msg_scroll)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002964{
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002965 if (eap->verbose_save >= 0)
2966 p_verbose = eap->verbose_save;
2967
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002968 if (cmdmod.save_ei != NULL)
2969 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002970 // Restore 'eventignore' to the value before ":noautocmd".
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002971 set_string_option_direct((char_u *)"ei", -1, cmdmod.save_ei,
2972 OPT_FREE, SID_NONE);
2973 free_string_option(cmdmod.save_ei);
2974 }
2975
2976 if (cmdmod.filter_regmatch.regprog != NULL)
2977 vim_regfree(cmdmod.filter_regmatch.regprog);
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002978
2979 if (eap->save_msg_silent != -1)
2980 {
2981 // messages could be enabled for a serious error, need to check if the
2982 // counters don't become negative
2983 if (!did_emsg || msg_silent > eap->save_msg_silent)
2984 msg_silent = eap->save_msg_silent;
2985 emsg_silent -= eap->did_esilent;
2986 if (emsg_silent < 0)
2987 emsg_silent = 0;
2988 // Restore msg_scroll, it's set by file I/O commands, even when no
2989 // message is actually displayed.
2990 msg_scroll = save_msg_scroll;
2991
2992 // "silent reg" or "silent echo x" inside "redir" leaves msg_col
2993 // somewhere in the line. Put it back in the first column.
2994 if (redirecting())
2995 msg_col = 0;
2996 }
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002997}
2998
2999/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003000 * Parse the address range, if any, in "eap".
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003001 * May set the last search pattern, unless "silent" is TRUE.
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003002 * Return FAIL and set "errormsg" or return OK.
3003 */
3004 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003005parse_cmd_address(exarg_T *eap, char **errormsg, int silent)
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003006{
3007 int address_count = 1;
3008 linenr_T lnum;
3009
3010 // Repeat for all ',' or ';' separated addresses.
3011 for (;;)
3012 {
3013 eap->line1 = eap->line2;
3014 switch (eap->addr_type)
3015 {
3016 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003017 case ADDR_OTHER:
Bram Moolenaarb5383b12020-05-18 19:46:48 +02003018 // Default is the cursor line number. Avoid using an invalid
3019 // line number though.
3020 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
3021 eap->line2 = curbuf->b_ml.ml_line_count;
3022 else
3023 eap->line2 = curwin->w_cursor.lnum;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003024 break;
3025 case ADDR_WINDOWS:
3026 eap->line2 = CURRENT_WIN_NR;
3027 break;
3028 case ADDR_ARGUMENTS:
3029 eap->line2 = curwin->w_arg_idx + 1;
3030 if (eap->line2 > ARGCOUNT)
3031 eap->line2 = ARGCOUNT;
3032 break;
3033 case ADDR_LOADED_BUFFERS:
3034 case ADDR_BUFFERS:
3035 eap->line2 = curbuf->b_fnum;
3036 break;
3037 case ADDR_TABS:
3038 eap->line2 = CURRENT_TAB_NR;
3039 break;
3040 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003041 case ADDR_UNSIGNED:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003042 eap->line2 = 1;
3043 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003044 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003045#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02003046 eap->line2 = qf_get_cur_idx(eap);
3047#endif
3048 break;
3049 case ADDR_QUICKFIX_VALID:
3050#ifdef FEAT_QUICKFIX
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003051 eap->line2 = qf_get_cur_valid_idx(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003052#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003053 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003054 case ADDR_NONE:
3055 // Will give an error later if a range is found.
3056 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003057 }
3058 eap->cmd = skipwhite(eap->cmd);
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003059 lnum = get_address(eap, &eap->cmd, eap->addr_type, eap->skip, silent,
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003060 eap->addr_count == 0, address_count++);
3061 if (eap->cmd == NULL) // error detected
3062 return FAIL;
3063 if (lnum == MAXLNUM)
3064 {
3065 if (*eap->cmd == '%') // '%' - all lines
3066 {
3067 ++eap->cmd;
3068 switch (eap->addr_type)
3069 {
3070 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003071 case ADDR_OTHER:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003072 eap->line1 = 1;
3073 eap->line2 = curbuf->b_ml.ml_line_count;
3074 break;
3075 case ADDR_LOADED_BUFFERS:
3076 {
3077 buf_T *buf = firstbuf;
3078
3079 while (buf->b_next != NULL
3080 && buf->b_ml.ml_mfp == NULL)
3081 buf = buf->b_next;
3082 eap->line1 = buf->b_fnum;
3083 buf = lastbuf;
3084 while (buf->b_prev != NULL
3085 && buf->b_ml.ml_mfp == NULL)
3086 buf = buf->b_prev;
3087 eap->line2 = buf->b_fnum;
3088 break;
3089 }
3090 case ADDR_BUFFERS:
3091 eap->line1 = firstbuf->b_fnum;
3092 eap->line2 = lastbuf->b_fnum;
3093 break;
3094 case ADDR_WINDOWS:
3095 case ADDR_TABS:
3096 if (IS_USER_CMDIDX(eap->cmdidx))
3097 {
3098 eap->line1 = 1;
3099 eap->line2 = eap->addr_type == ADDR_WINDOWS
3100 ? LAST_WIN_NR : LAST_TAB_NR;
3101 }
3102 else
3103 {
3104 // there is no Vim command which uses '%' and
3105 // ADDR_WINDOWS or ADDR_TABS
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003106 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003107 return FAIL;
3108 }
3109 break;
3110 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003111 case ADDR_UNSIGNED:
3112 case ADDR_QUICKFIX:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003113 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003114 return FAIL;
3115 case ADDR_ARGUMENTS:
3116 if (ARGCOUNT == 0)
3117 eap->line1 = eap->line2 = 0;
3118 else
3119 {
3120 eap->line1 = 1;
3121 eap->line2 = ARGCOUNT;
3122 }
3123 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003124 case ADDR_QUICKFIX_VALID:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003125#ifdef FEAT_QUICKFIX
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003126 eap->line1 = 1;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003127 eap->line2 = qf_get_valid_size(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003128 if (eap->line2 == 0)
3129 eap->line2 = 1;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003130#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003131 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003132 case ADDR_NONE:
3133 // Will give an error later if a range is found.
3134 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003135 }
3136 ++eap->addr_count;
3137 }
3138 else if (*eap->cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
3139 {
3140 pos_T *fp;
3141
3142 // '*' - visual area
3143 if (eap->addr_type != ADDR_LINES)
3144 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003145 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003146 return FAIL;
3147 }
3148
3149 ++eap->cmd;
3150 if (!eap->skip)
3151 {
3152 fp = getmark('<', FALSE);
3153 if (check_mark(fp) == FAIL)
3154 return FAIL;
3155 eap->line1 = fp->lnum;
3156 fp = getmark('>', FALSE);
3157 if (check_mark(fp) == FAIL)
3158 return FAIL;
3159 eap->line2 = fp->lnum;
3160 ++eap->addr_count;
3161 }
3162 }
3163 }
3164 else
3165 eap->line2 = lnum;
3166 eap->addr_count++;
3167
3168 if (*eap->cmd == ';')
3169 {
3170 if (!eap->skip)
3171 {
3172 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar0e717042020-04-27 19:29:01 +02003173 // Don't leave the cursor on an illegal line or column, but do
3174 // accept zero as address, so 0;/PATTERN/ works correctly.
3175 if (eap->line2 > 0)
3176 check_cursor();
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003177 }
3178 }
3179 else if (*eap->cmd != ',')
3180 break;
3181 ++eap->cmd;
3182 }
3183
3184 // One address given: set start and end lines.
3185 if (eap->addr_count == 1)
3186 {
3187 eap->line1 = eap->line2;
3188 // ... but only implicit: really no address given
3189 if (lnum == MAXLNUM)
3190 eap->addr_count = 0;
3191 }
3192 return OK;
3193}
3194
3195/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003196 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003197 * If there is a match advance "pp" to the argument and return TRUE.
3198 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003199 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003200checkforcmd(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003201 char_u **pp, // start of command
3202 char *cmd, // name of command
3203 int len) // required length
Bram Moolenaar071d4272004-06-13 20:20:40 +00003204{
3205 int i;
3206
3207 for (i = 0; cmd[i] != NUL; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003208 if (((char_u *)cmd)[i] != (*pp)[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003209 break;
3210 if (i >= len && !isalpha((*pp)[i]))
3211 {
3212 *pp = skipwhite(*pp + i);
3213 return TRUE;
3214 }
3215 return FALSE;
3216}
3217
3218/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003219 * Append "cmd" to the error message in IObuff.
3220 * Takes care of limiting the length and handling 0xa0, which would be
3221 * invisible otherwise.
3222 */
3223 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003224append_command(char_u *cmd)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003225{
3226 char_u *s = cmd;
3227 char_u *d;
3228
3229 STRCAT(IObuff, ": ");
3230 d = IObuff + STRLEN(IObuff);
3231 while (*s != NUL && d - IObuff < IOSIZE - 7)
3232 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003233 if (enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) : *s == 0xa0)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003234 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003235 s += enc_utf8 ? 2 : 1;
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003236 STRCPY(d, "<a0>");
3237 d += 4;
3238 }
3239 else
3240 MB_COPY_CHAR(s, d);
3241 }
3242 *d = NUL;
3243}
3244
3245/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003247 * Start of the name can be found at eap->cmd.
Bram Moolenaar25190db2019-05-04 15:05:28 +02003248 * Sets eap->cmdidx and returns a pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003249 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003250 *
3251 * If "lookup" is not NULL recognize expression without "eval" or "call" and
3252 * assignment without "let". Sets eap->cmdidx to the command while returning
3253 * "eap->cmd".
3254 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255 * Returns NULL for an ambiguous user command.
3256 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003257 char_u *
3258find_ex_command(
3259 exarg_T *eap,
Bram Moolenaara494f562020-04-18 17:45:38 +02003260 int *full UNUSED,
Bram Moolenaarb84a3812020-05-01 15:44:29 +02003261 void *(*lookup)(char_u *, size_t, cctx_T *) UNUSED,
Bram Moolenaara494f562020-04-18 17:45:38 +02003262 cctx_T *cctx UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003263{
3264 int len;
3265 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003266 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003267
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003268#ifdef FEAT_EVAL
3269 /*
3270 * Recognize a Vim9 script function/method call and assignment:
3271 * "lvar = value", "lvar(arg)", "[1, 2 3]->Func()"
3272 */
Bram Moolenaar0c6ceaf2020-02-22 18:36:32 +01003273 p = eap->cmd;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003274 if (lookup != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003275 {
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003276 // Skip over first char for "&opt = val", "$ENV = val" and "@r = val".
Bram Moolenaar83144542020-08-02 20:40:43 +02003277 char_u *pskip = (*eap->cmd == '&' || *eap->cmd == '$')
3278 ? eap->cmd + 1 : eap->cmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003279
Bram Moolenaar83144542020-08-02 20:40:43 +02003280 if (vim_strchr((char_u *)"{('[\"@", *p) != NULL
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003281 || ((p = to_name_const_end(pskip)) > eap->cmd && *p != NUL))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003282 {
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003283 int oplen;
3284 int heredoc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003285
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003286 if (
3287 // "(..." is an expression.
3288 // "funcname(" is always a function call.
3289 *p == '('
3290 || (p == eap->cmd
3291 ? (
3292 // "{..." is an dict expression.
3293 *eap->cmd == '{'
3294 // "'string'->func()" is an expression.
3295 || *eap->cmd == '\''
Bram Moolenaar10409562020-07-29 20:00:38 +02003296 // '"string"->func()' is an expression.
3297 || *eap->cmd == '"'
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003298 // "g:varname" is an expression.
3299 || eap->cmd[1] == ':'
3300 )
3301 : (
3302 // "varname[]" is an expression.
3303 *p == '['
3304 // "varname->func()" is an expression.
3305 || (*p == '-' && p[1] == '>')
3306 // "varname.expr" is an expression.
3307 || (*p == '.' && ASCII_ISALPHA(p[1]))
3308 )))
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003309 {
3310 eap->cmdidx = CMD_eval;
3311 return eap->cmd;
3312 }
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003313
3314 // "[...]->Method()" is a list expression, but "[a, b] = Func()" is
3315 // an assignment.
3316 // If there is no line break inside the "[...]" then "p" is
3317 // advanced to after the "]" by to_name_const_end(): check if a "="
3318 // follows.
3319 // If "[...]" has a line break "p" still points at the "[" and it
3320 // can't be an assignment.
3321 if (*eap->cmd == '[')
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003322 {
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003323 p = to_name_const_end(eap->cmd);
3324 if (p == eap->cmd || *skipwhite(p) != '=')
3325 {
3326 eap->cmdidx = CMD_eval;
3327 return eap->cmd;
3328 }
3329 if (p > eap->cmd && *skipwhite(p) == '=')
3330 {
3331 eap->cmdidx = CMD_let;
3332 return eap->cmd;
3333 }
3334 }
3335
3336 // Recognize an assignment if we recognize the variable name:
3337 // "g:var = expr"
3338 // "var = expr" where "var" is a local var name.
Bram Moolenaar83144542020-08-02 20:40:43 +02003339 if (*eap->cmd == '@')
3340 p = eap->cmd + 2;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003341 oplen = assignment_len(skipwhite(p), &heredoc);
3342 if (oplen > 0)
3343 {
3344 if (((p - eap->cmd) > 2 && eap->cmd[1] == ':')
3345 || *eap->cmd == '&'
3346 || *eap->cmd == '$'
3347 || *eap->cmd == '@'
3348 || lookup(eap->cmd, p - eap->cmd, cctx) != NULL)
3349 {
3350 eap->cmdidx = CMD_let;
3351 return eap->cmd;
3352 }
3353 }
3354
3355 // Recognize using a type for a w:, b:, t: or g: variable:
3356 // "w:varname: number = 123".
3357 if (eap->cmd[1] == ':' && *p == ':')
3358 {
3359 eap->cmdidx = CMD_eval;
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003360 return eap->cmd;
3361 }
Bram Moolenaard2ef6b32020-07-02 21:11:34 +02003362 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003363 }
3364#endif
3365
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366 /*
3367 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00003368 * Exceptions:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003369 * - the 'k' command can directly be followed by any character.
3370 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003371 * but :sre[wind] is another command, as are :scr[iptnames],
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00003373 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374 */
3375 p = eap->cmd;
3376 if (*p == 'k')
3377 {
3378 eap->cmdidx = CMD_k;
3379 ++p;
3380 }
3381 else if (p[0] == 's'
Bram Moolenaar204b93f2015-08-04 22:02:51 +02003382 && ((p[1] == 'c' && (p[2] == NUL || (p[2] != 's' && p[2] != 'r'
3383 && (p[3] == NUL || (p[3] != 'i' && p[4] != 'p')))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384 || p[1] == 'g'
3385 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
3386 || p[1] == 'I'
3387 || (p[1] == 'r' && p[2] != 'e')))
3388 {
3389 eap->cmdidx = CMD_substitute;
3390 ++p;
3391 }
3392 else
3393 {
3394 while (ASCII_ISALPHA(*p))
3395 ++p;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003396 // for python 3.x support ":py3", ":python3", ":py3file", etc.
Bram Moolenaar55d5c032010-07-17 23:52:29 +02003397 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003398 {
Bram Moolenaarb6590522010-07-21 16:00:43 +02003399 while (ASCII_ISALNUM(*p))
3400 ++p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003401 }
3402 else if (*p == '9' && STRNCMP("vim9", eap->cmd, 4) == 0)
3403 {
3404 // include "9" for "vim9script"
3405 ++p;
3406 while (ASCII_ISALPHA(*p))
3407 ++p;
3408 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02003409
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003410 // check for non-alpha command
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3412 ++p;
3413 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003414 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
3415 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003416 // Check for ":dl", ":dell", etc. to ":deletel": that's
3417 // :delete with the 'l' flag. Same for 'p'.
Bram Moolenaardf177f62005-02-22 08:39:57 +00003418 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003419 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00003420 break;
3421 if (i == len - 1)
3422 {
3423 --len;
3424 if (p[-1] == 'l')
3425 eap->flags |= EXFLAG_LIST;
3426 else
3427 eap->flags |= EXFLAG_PRINT;
3428 }
3429 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003431 if (ASCII_ISLOWER(eap->cmd[0]))
3432 {
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003433 int c1 = eap->cmd[0];
Bram Moolenaar94f82cb2019-07-24 22:30:27 +02003434 int c2 = len == 1 ? NUL : eap->cmd[1];
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003435
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003436 if (command_count != (int)CMD_SIZE)
3437 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003438 iemsg(_("E943: Command table needs to be updated, run 'make cmdidxs'"));
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003439 getout(1);
3440 }
3441
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003442 // Use a precomputed index for fast look-up in cmdnames[]
3443 // taking into account the first 2 letters of eap->cmd.
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003444 eap->cmdidx = cmdidxs1[CharOrdLow(c1)];
3445 if (ASCII_ISLOWER(c2))
3446 eap->cmdidx += cmdidxs2[CharOrdLow(c1)][CharOrdLow(c2)];
3447 }
Bram Moolenaara494f562020-04-18 17:45:38 +02003448 else if (ASCII_ISUPPER(eap->cmd[0]))
3449 eap->cmdidx = CMD_Next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450 else
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003451 eap->cmdidx = CMD_bang;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452
3453 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
3454 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
3455 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
3456 (size_t)len) == 0)
3457 {
3458#ifdef FEAT_EVAL
3459 if (full != NULL
3460 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
3461 *full = TRUE;
3462#endif
3463 break;
3464 }
3465
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003466 // Look for a user defined command as a last resort. Let ":Print" be
3467 // overruled by a user defined command.
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01003468 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
3469 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003470 {
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003471 // User defined commands may contain digits.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472 while (ASCII_ISALNUM(*p))
3473 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003474 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003476 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003477 eap->cmdidx = CMD_SIZE;
3478 }
3479
3480 return p;
3481}
3482
3483#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003484static struct cmdmod
3485{
3486 char *name;
3487 int minlen;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003488 int has_count; // :123verbose :3tab
Bram Moolenaared53fb92007-11-24 20:50:24 +00003489} cmdmods[] = {
3490 {"aboveleft", 3, FALSE},
3491 {"belowright", 3, FALSE},
3492 {"botright", 2, FALSE},
3493 {"browse", 3, FALSE},
3494 {"confirm", 4, FALSE},
Bram Moolenaar7b668e82016-08-23 23:51:21 +02003495 {"filter", 4, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003496 {"hide", 3, FALSE},
3497 {"keepalt", 5, FALSE},
3498 {"keepjumps", 5, FALSE},
3499 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01003500 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003501 {"leftabove", 5, FALSE},
3502 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003503 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003504 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003505 {"rightbelow", 6, FALSE},
3506 {"sandbox", 3, FALSE},
3507 {"silent", 3, FALSE},
3508 {"tab", 3, TRUE},
3509 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003510 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003511 {"verbose", 4, TRUE},
3512 {"vertical", 4, FALSE},
3513};
3514
3515/*
3516 * Return length of a command modifier (including optional count).
3517 * Return zero when it's not a modifier.
3518 */
3519 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003520modifier_len(char_u *cmd)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003521{
3522 int i, j;
3523 char_u *p = cmd;
3524
3525 if (VIM_ISDIGIT(*cmd))
3526 p = skipwhite(skipdigits(cmd));
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003527 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003528 {
3529 for (j = 0; p[j] != NUL; ++j)
3530 if (p[j] != cmdmods[i].name[j])
3531 break;
Bram Moolenaar2d473ab2013-06-12 17:12:24 +02003532 if (!ASCII_ISALPHA(p[j]) && j >= cmdmods[i].minlen
Bram Moolenaared53fb92007-11-24 20:50:24 +00003533 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003534 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003535 }
3536 return 0;
3537}
3538
Bram Moolenaar071d4272004-06-13 20:20:40 +00003539/*
3540 * Return > 0 if an Ex command "name" exists.
3541 * Return 2 if there is an exact match.
3542 * Return 3 if there is an ambiguous match.
3543 */
3544 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003545cmd_exists(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546{
3547 exarg_T ea;
3548 int full = FALSE;
3549 int i;
3550 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003551 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003552
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003553 // Check command modifiers.
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003554 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555 {
3556 for (j = 0; name[j] != NUL; ++j)
3557 if (name[j] != cmdmods[i].name[j])
3558 break;
3559 if (name[j] == NUL && j >= cmdmods[i].minlen)
3560 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3561 }
3562
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003563 // Check built-in commands and user defined commands.
3564 // For ":2match" and ":3match" we need to skip the number.
Bram Moolenaara9587612006-05-04 21:47:50 +00003565 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003567 p = find_ex_command(&ea, &full, NULL, NULL);
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003568 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003570 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3571 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003572 if (*skipwhite(p) != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003573 return 0; // trailing garbage
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3575}
3576#endif
3577
Bram Moolenaard0190392019-08-23 21:17:35 +02003578 cmdidx_T
3579excmd_get_cmdidx(char_u *cmd, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003580{
Bram Moolenaard0190392019-08-23 21:17:35 +02003581 cmdidx_T idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582
Bram Moolenaard0190392019-08-23 21:17:35 +02003583 for (idx = (cmdidx_T)0; (int)idx < (int)CMD_SIZE;
3584 idx = (cmdidx_T)((int)idx + 1))
3585 if (STRNCMP(cmdnames[(int)idx].cmd_name, cmd, (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586 break;
3587
Bram Moolenaard0190392019-08-23 21:17:35 +02003588 return idx;
3589}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003590
Bram Moolenaard0190392019-08-23 21:17:35 +02003591 long
3592excmd_get_argt(cmdidx_T idx)
3593{
3594 return (long)cmdnames[(int)idx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003595}
3596
3597/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003598 * Skip a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599 *
3600 * Backslashed delimiters after / or ? will be skipped, and commands will
3601 * not be expanded between /'s and ?'s or after "'".
3602 *
Bram Moolenaardf069ee2020-06-22 23:02:51 +02003603 * Also skip white space and ":" characters after the range.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003604 * Returns the "cmd" pointer advanced to beyond the range.
3605 */
3606 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003607skip_range(
3608 char_u *cmd,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003609 int *ctx) // pointer to xp_context or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003611 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01003613 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;\\", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614 {
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01003615 if (*cmd == '\\')
3616 {
3617 if (cmd[1] == '?' || cmd[1] == '/' || cmd[1] == '&')
3618 ++cmd;
3619 else
3620 break;
3621 }
3622 else if (*cmd == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623 {
3624 if (*++cmd == NUL && ctx != NULL)
3625 *ctx = EXPAND_NOTHING;
3626 }
3627 else if (*cmd == '/' || *cmd == '?')
3628 {
3629 delim = *cmd++;
3630 while (*cmd != NUL && *cmd != delim)
3631 if (*cmd++ == '\\' && *cmd != NUL)
3632 ++cmd;
3633 if (*cmd == NUL && ctx != NULL)
3634 *ctx = EXPAND_NOTHING;
3635 }
3636 if (*cmd != NUL)
3637 ++cmd;
3638 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00003639
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003640 // Skip ":" and white space.
Bram Moolenaardf177f62005-02-22 08:39:57 +00003641 while (*cmd == ':')
3642 cmd = skipwhite(cmd + 1);
3643
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644 return cmd;
3645}
3646
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003647 static void
3648addr_error(cmd_addr_T addr_type)
3649{
3650 if (addr_type == ADDR_NONE)
3651 emsg(_(e_norange));
3652 else
3653 emsg(_(e_invrange));
3654}
3655
Bram Moolenaar071d4272004-06-13 20:20:40 +00003656/*
Bram Moolenaar198cb662018-09-06 21:44:17 +02003657 * Get a single EX address.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658 *
3659 * Set ptr to the next character after the part that was interpreted.
3660 * Set ptr to NULL when an error is encountered.
Bram Moolenaar198cb662018-09-06 21:44:17 +02003661 * This may set the last used search pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662 *
3663 * Return MAXLNUM when no Ex address was found.
3664 */
3665 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003666get_address(
3667 exarg_T *eap UNUSED,
3668 char_u **ptr,
Bram Moolenaar1b03a192019-12-08 17:08:29 +01003669 cmd_addr_T addr_type,
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003670 int skip, // only skip the address, don't use it
3671 int silent, // no errors or side effects
3672 int to_other_file, // flag: may jump to other file
3673 int address_count UNUSED) // 1 for first address, >1 after comma
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674{
3675 int c;
3676 int i;
3677 long n;
3678 char_u *cmd;
3679 pos_T pos;
3680 pos_T *fp;
3681 linenr_T lnum;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003682 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003683
3684 cmd = skipwhite(*ptr);
3685 lnum = MAXLNUM;
3686 do
3687 {
3688 switch (*cmd)
3689 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003690 case '.': // '.' - Cursor position
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003691 ++cmd;
3692 switch (addr_type)
3693 {
3694 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003695 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003696 lnum = curwin->w_cursor.lnum;
3697 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003698 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003699 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003700 break;
3701 case ADDR_ARGUMENTS:
3702 lnum = curwin->w_arg_idx + 1;
3703 break;
3704 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003705 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003706 lnum = curbuf->b_fnum;
3707 break;
3708 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003709 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003710 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003711 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003712 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003713 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003714 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003715 cmd = NULL;
3716 goto error;
3717 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003718 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003719#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02003720 lnum = qf_get_cur_idx(eap);
3721#endif
3722 break;
3723 case ADDR_QUICKFIX_VALID:
3724#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003725 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02003726#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003727 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003728 }
3729 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003731 case '$': // '$' - last line
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003732 ++cmd;
3733 switch (addr_type)
3734 {
3735 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003736 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737 lnum = curbuf->b_ml.ml_line_count;
3738 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003739 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003740 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003741 break;
3742 case ADDR_ARGUMENTS:
3743 lnum = ARGCOUNT;
3744 break;
3745 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003746 buf = lastbuf;
3747 while (buf->b_ml.ml_mfp == NULL)
3748 {
3749 if (buf->b_prev == NULL)
3750 break;
3751 buf = buf->b_prev;
3752 }
3753 lnum = buf->b_fnum;
3754 break;
3755 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003756 lnum = lastbuf->b_fnum;
3757 break;
3758 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003759 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003760 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003761 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003762 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003763 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003764 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003765 cmd = NULL;
3766 goto error;
3767 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003768 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003769#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003770 lnum = qf_get_size(eap);
3771 if (lnum == 0)
3772 lnum = 1;
Bram Moolenaare906c502015-09-09 21:10:39 +02003773#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003774 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003775 case ADDR_QUICKFIX_VALID:
3776#ifdef FEAT_QUICKFIX
3777 lnum = qf_get_valid_size(eap);
3778 if (lnum == 0)
3779 lnum = 1;
3780#endif
3781 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003782 }
3783 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003784
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003785 case '\'': // ''' - mark
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003786 if (*++cmd == NUL)
3787 {
3788 cmd = NULL;
3789 goto error;
3790 }
3791 if (addr_type != ADDR_LINES)
3792 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003793 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003794 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003795 goto error;
3796 }
3797 if (skip)
3798 ++cmd;
3799 else
3800 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003801 // Only accept a mark in another file when it is
3802 // used by itself: ":'M".
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003803 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
3804 ++cmd;
3805 if (fp == (pos_T *)-1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003806 // Jumped to another file.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003807 lnum = curwin->w_cursor.lnum;
3808 else
3809 {
3810 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811 {
3812 cmd = NULL;
3813 goto error;
3814 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003815 lnum = fp->lnum;
3816 }
3817 }
3818 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003819
3820 case '/':
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003821 case '?': // '/' or '?' - search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003822 c = *cmd++;
3823 if (addr_type != ADDR_LINES)
3824 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003825 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003826 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003827 goto error;
3828 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003829 if (skip) // skip "/pat/"
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003830 {
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02003831 cmd = skip_regexp(cmd, c, (int)p_magic);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003832 if (*cmd == c)
3833 ++cmd;
3834 }
3835 else
3836 {
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003837 int flags;
3838
3839 pos = curwin->w_cursor; // save curwin->w_cursor
3840
3841 // When '/' or '?' follows another address, start from
3842 // there.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003843 if (lnum != MAXLNUM)
3844 curwin->w_cursor.lnum = lnum;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003845
3846 // Start a forward search at the end of the line (unless
3847 // before the first line).
3848 // Start a backward search at the start of the line.
3849 // This makes sure we never match in the current
3850 // line, and can match anywhere in the
3851 // next/previous line.
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01003852 if (c == '/' && curwin->w_cursor.lnum > 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003853 curwin->w_cursor.col = MAXCOL;
3854 else
3855 curwin->w_cursor.col = 0;
3856 searchcmdlen = 0;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003857 flags = silent ? 0 : SEARCH_HIS | SEARCH_MSG;
Bram Moolenaarc036e872020-02-21 21:30:52 +01003858 if (!do_search(NULL, c, c, cmd, 1L, flags, NULL))
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003859 {
3860 curwin->w_cursor = pos;
3861 cmd = NULL;
3862 goto error;
3863 }
3864 lnum = curwin->w_cursor.lnum;
3865 curwin->w_cursor = pos;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003866 // adjust command string pointer
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003867 cmd += searchcmdlen;
3868 }
3869 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003871 case '\\': // "\?", "\/" or "\&", repeat search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003872 ++cmd;
3873 if (addr_type != ADDR_LINES)
3874 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003875 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003876 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003877 goto error;
3878 }
3879 if (*cmd == '&')
3880 i = RE_SUBST;
3881 else if (*cmd == '?' || *cmd == '/')
3882 i = RE_SEARCH;
3883 else
3884 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003885 emsg(_(e_backslash));
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003886 cmd = NULL;
3887 goto error;
3888 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003890 if (!skip)
3891 {
3892 /*
3893 * When search follows another address, start from
3894 * there.
3895 */
3896 if (lnum != MAXLNUM)
3897 pos.lnum = lnum;
3898 else
3899 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003901 /*
3902 * Start the search just like for the above
3903 * do_search().
3904 */
3905 if (*cmd != '?')
3906 pos.col = MAXCOL;
3907 else
3908 pos.col = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02003909 pos.coladd = 0;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01003910 if (searchit(curwin, curbuf, &pos, NULL,
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003911 *cmd == '?' ? BACKWARD : FORWARD,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02003912 (char_u *)"", 1L, SEARCH_MSG, i, NULL) != FAIL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003913 lnum = pos.lnum;
3914 else
3915 {
3916 cmd = NULL;
3917 goto error;
3918 }
3919 }
3920 ++cmd;
3921 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003922
3923 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003924 if (VIM_ISDIGIT(*cmd)) // absolute line number
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003925 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926 }
3927
3928 for (;;)
3929 {
3930 cmd = skipwhite(cmd);
3931 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
3932 break;
3933
3934 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003935 {
3936 switch (addr_type)
3937 {
3938 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003939 case ADDR_OTHER:
3940 // "+1" is same as ".+1"
Bram Moolenaarf240e182014-11-27 18:33:02 +01003941 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003942 break;
3943 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003944 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003945 break;
3946 case ADDR_ARGUMENTS:
3947 lnum = curwin->w_arg_idx + 1;
3948 break;
3949 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003950 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003951 lnum = curbuf->b_fnum;
3952 break;
3953 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003954 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003955 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003956 case ADDR_TABS_RELATIVE:
3957 lnum = 1;
3958 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003959 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003960#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02003961 lnum = qf_get_cur_idx(eap);
3962#endif
3963 break;
3964 case ADDR_QUICKFIX_VALID:
3965#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003966 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02003967#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003968 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003969 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003970 case ADDR_UNSIGNED:
3971 lnum = 0;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003972 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003973 }
3974 }
3975
Bram Moolenaar071d4272004-06-13 20:20:40 +00003976 if (VIM_ISDIGIT(*cmd))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003977 i = '+'; // "number" is same as "+number"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978 else
3979 i = *cmd++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003980 if (!VIM_ISDIGIT(*cmd)) // '+' is '+1', but '+0' is not '+1'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003981 n = 1;
3982 else
3983 n = getdigits(&cmd);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003984
3985 if (addr_type == ADDR_TABS_RELATIVE)
3986 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003987 emsg(_(e_invrange));
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003988 cmd = NULL;
3989 goto error;
3990 }
3991 else if (addr_type == ADDR_LOADED_BUFFERS
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003992 || addr_type == ADDR_BUFFERS)
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003993 lnum = compute_buffer_local_count(
3994 addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995 else
Bram Moolenaarded27822017-01-02 14:27:34 +01003996 {
3997#ifdef FEAT_FOLDING
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003998 // Relative line addressing, need to adjust for folded lines
3999 // now, but only do it after the first address.
Bram Moolenaarded27822017-01-02 14:27:34 +01004000 if (addr_type == ADDR_LINES && (i == '-' || i == '+')
4001 && address_count >= 2)
4002 (void)hasFolding(lnum, NULL, &lnum);
4003#endif
4004 if (i == '-')
4005 lnum -= n;
4006 else
4007 lnum += n;
4008 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009 }
4010 } while (*cmd == '/' || *cmd == '?');
4011
4012error:
4013 *ptr = cmd;
4014 return lnum;
4015}
4016
4017/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004018 * Get flags from an Ex command argument.
4019 */
4020 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004021get_flags(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004022{
4023 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4024 {
4025 if (*eap->arg == 'l')
4026 eap->flags |= EXFLAG_LIST;
4027 else if (*eap->arg == 'p')
4028 eap->flags |= EXFLAG_PRINT;
4029 else
4030 eap->flags |= EXFLAG_NR;
4031 eap->arg = skipwhite(eap->arg + 1);
4032 }
4033}
4034
4035/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004036 * Function called for command which is Not Implemented. NI!
4037 */
4038 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004039ex_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004040{
4041 if (!eap->skip)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02004042 eap->errmsg =
4043 _("E319: Sorry, the command is not available in this version");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044}
4045
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004046#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004047/*
4048 * Function called for script command which is Not Implemented. NI!
4049 * Skips over ":perl <<EOF" constructs.
4050 */
4051 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004052ex_script_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053{
4054 if (!eap->skip)
4055 ex_ni(eap);
4056 else
4057 vim_free(script_get(eap, eap->arg));
4058}
4059#endif
4060
4061/*
4062 * Check range in Ex command for validity.
4063 * Return NULL when valid, error message when invalid.
4064 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004065 static char *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004066invalid_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004067{
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004068 buf_T *buf;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004069
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070 if ( eap->line1 < 0
4071 || eap->line2 < 0
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004072 || eap->line1 > eap->line2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004073 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004074
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004075 if (eap->argt & EX_RANGE)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004076 {
Bram Moolenaarb7316892019-05-01 18:08:42 +02004077 switch (eap->addr_type)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004078 {
4079 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004080 if (eap->line2 > curbuf->b_ml.ml_line_count
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004081#ifdef FEAT_DIFF
4082 + (eap->cmdidx == CMD_diffget)
4083#endif
4084 )
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004085 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004086 break;
4087 case ADDR_ARGUMENTS:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004088 // add 1 if ARGCOUNT is 0
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004089 if (eap->line2 > ARGCOUNT + (!ARGCOUNT))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004090 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004091 break;
4092 case ADDR_BUFFERS:
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02004093 // Only a boundary check, not whether the buffers actually
4094 // exist.
4095 if (eap->line1 < 1 || eap->line2 > get_highest_fnum())
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004096 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004097 break;
4098 case ADDR_LOADED_BUFFERS:
4099 buf = firstbuf;
4100 while (buf->b_ml.ml_mfp == NULL)
4101 {
4102 if (buf->b_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004103 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004104 buf = buf->b_next;
4105 }
4106 if (eap->line1 < buf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004107 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004108 buf = lastbuf;
4109 while (buf->b_ml.ml_mfp == NULL)
4110 {
4111 if (buf->b_prev == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004112 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004113 buf = buf->b_prev;
4114 }
4115 if (eap->line2 > buf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004116 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004117 break;
4118 case ADDR_WINDOWS:
Bram Moolenaar8be63882015-01-14 11:25:05 +01004119 if (eap->line2 > LAST_WIN_NR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004120 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004121 break;
4122 case ADDR_TABS:
4123 if (eap->line2 > LAST_TAB_NR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004124 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004125 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004126 case ADDR_TABS_RELATIVE:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004127 case ADDR_OTHER:
4128 // Any range is OK.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004129 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004130 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004131#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004132 // No error for value that is too big, will use the last entry.
4133 if (eap->line2 <= 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004134 return _(e_invrange);
Bram Moolenaare906c502015-09-09 21:10:39 +02004135#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004136 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004137 case ADDR_QUICKFIX_VALID:
4138#ifdef FEAT_QUICKFIX
4139 if ((eap->line2 != 1 && eap->line2 > qf_get_valid_size(eap))
4140 || eap->line2 < 0)
4141 return _(e_invrange);
4142#endif
4143 break;
4144 case ADDR_UNSIGNED:
4145 if (eap->line2 < 0)
4146 return _(e_invrange);
4147 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004148 case ADDR_NONE:
4149 // Will give an error elsewhere.
4150 break;
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004151 }
4152 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004153 return NULL;
4154}
4155
4156/*
4157 * Correct the range for zero line number, if required.
4158 */
4159 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004160correct_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004161{
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004162 if (!(eap->argt & EX_ZEROR)) // zero in range not allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004163 {
4164 if (eap->line1 == 0)
4165 eap->line1 = 1;
4166 if (eap->line2 == 0)
4167 eap->line2 = 1;
4168 }
4169}
4170
Bram Moolenaar748bf032005-02-02 23:04:36 +00004171#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004172/*
4173 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4174 * pattern. Otherwise return eap->arg.
4175 */
4176 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004177skip_grep_pat(exarg_T *eap)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004178{
4179 char_u *p = eap->arg;
4180
Bram Moolenaara37420f2006-02-04 22:37:47 +00004181 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4182 || eap->cmdidx == CMD_vimgrepadd
4183 || eap->cmdidx == CMD_lvimgrepadd
4184 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004185 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004186 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004187 if (p == NULL)
4188 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004189 }
4190 return p;
4191}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004192
4193/*
4194 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4195 * in the command line, so that things like % get expanded.
4196 */
4197 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004198replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004199{
4200 char_u *new_cmdline;
4201 char_u *program;
4202 char_u *pos;
4203 char_u *ptr;
4204 int len;
4205 int i;
4206
4207 /*
4208 * Don't do it when ":vimgrep" is used for ":grep".
4209 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004210 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4211 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4212 || eap->cmdidx == CMD_grepadd
4213 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004214 && !grep_internal(eap->cmdidx))
4215 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004216 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4217 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004218 {
4219 if (*curbuf->b_p_gp == NUL)
4220 program = p_gp;
4221 else
4222 program = curbuf->b_p_gp;
4223 }
4224 else
4225 {
4226 if (*curbuf->b_p_mp == NUL)
4227 program = p_mp;
4228 else
4229 program = curbuf->b_p_mp;
4230 }
4231
4232 p = skipwhite(p);
4233
4234 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4235 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004236 // replace $* by given arguments
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004237 i = 1;
4238 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4239 ++i;
4240 len = (int)STRLEN(p);
Bram Moolenaar51e14382019-05-25 20:21:28 +02004241 new_cmdline = alloc(STRLEN(program) + i * (len - 2) + 1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004242 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004243 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004244 ptr = new_cmdline;
4245 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4246 {
4247 i = (int)(pos - program);
4248 STRNCPY(ptr, program, i);
4249 STRCPY(ptr += i, p);
4250 ptr += len;
4251 program = pos + 2;
4252 }
4253 STRCPY(ptr, program);
4254 }
4255 else
4256 {
Bram Moolenaar51e14382019-05-25 20:21:28 +02004257 new_cmdline = alloc(STRLEN(program) + STRLEN(p) + 2);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004258 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004259 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004260 STRCPY(new_cmdline, program);
4261 STRCAT(new_cmdline, " ");
4262 STRCAT(new_cmdline, p);
4263 }
4264 msg_make(p);
4265
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004266 // 'eap->cmd' is not set here, because it is not used at CMD_make
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004267 vim_free(*cmdlinep);
4268 *cmdlinep = new_cmdline;
4269 p = new_cmdline;
4270 }
4271 return p;
4272}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004273#endif
4274
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275/*
4276 * Expand file name in Ex command argument.
Bram Moolenaar0abb4272019-06-15 16:06:00 +02004277 * When an error is detected, "errormsgp" is set to a non-NULL pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004278 * Return FAIL for failure, OK otherwise.
4279 */
4280 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004281expand_filename(
4282 exarg_T *eap,
4283 char_u **cmdlinep,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004284 char **errormsgp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004285{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004286 int has_wildcards; // need to expand wildcards
Bram Moolenaar071d4272004-06-13 20:20:40 +00004287 char_u *repl;
4288 int srclen;
4289 char_u *p;
4290 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004291 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004292
Bram Moolenaar748bf032005-02-02 23:04:36 +00004293#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004294 // Skip a regexp pattern for ":vimgrep[add] pat file..."
Bram Moolenaar748bf032005-02-02 23:04:36 +00004295 p = skip_grep_pat(eap);
4296#else
4297 p = eap->arg;
4298#endif
4299
Bram Moolenaar071d4272004-06-13 20:20:40 +00004300 /*
4301 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4302 * the file name contains a wildcard it should not cause expanding.
4303 * (it will be expanded anyway if there is a wildcard before replacing).
4304 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004305 has_wildcards = mch_has_wildcard(p);
4306 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004308#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004309 // Skip over `=expr`, wildcards in it are not expanded.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004310 if (p[0] == '`' && p[1] == '=')
4311 {
4312 p += 2;
4313 (void)skip_expr(&p);
4314 if (*p == '`')
4315 ++p;
4316 continue;
4317 }
4318#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319 /*
4320 * Quick check if this cannot be the start of a special string.
4321 * Also removes backslash before '%', '#' and '<'.
4322 */
4323 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4324 {
4325 ++p;
4326 continue;
4327 }
4328
4329 /*
4330 * Try to find a match at this position.
4331 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00004332 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
4333 errormsgp, &escaped);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004334 if (*errormsgp != NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00004335 return FAIL;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004336 if (repl == NULL) // no match found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004337 {
4338 p += srclen;
4339 continue;
4340 }
4341
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004342 // Wildcards won't be expanded below, the replacement is taken
4343 // literally. But do expand "~/file", "~user/file" and "$HOME/file".
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004344 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4345 {
4346 char_u *l = repl;
4347
4348 repl = expand_env_save(repl);
4349 vim_free(l);
4350 }
4351
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004352 // Need to escape white space et al. with a backslash.
4353 // Don't do this for:
4354 // - replacement that already has been escaped: "##"
4355 // - shell commands (may have to use quotes instead).
4356 // - non-unix systems when there is a single argument (spaces don't
4357 // separate arguments then).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004358 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00004359 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00004360 && eap->cmdidx != CMD_bang
Bram Moolenaar071d4272004-06-13 20:20:40 +00004361 && eap->cmdidx != CMD_grep
4362 && eap->cmdidx != CMD_grepadd
Bram Moolenaarbf15b8d2017-06-04 20:43:48 +02004363 && eap->cmdidx != CMD_hardcopy
Bram Moolenaar67883b42017-07-27 22:57:00 +02004364 && eap->cmdidx != CMD_lgrep
4365 && eap->cmdidx != CMD_lgrepadd
4366 && eap->cmdidx != CMD_lmake
4367 && eap->cmdidx != CMD_make
4368 && eap->cmdidx != CMD_terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00004369#ifndef UNIX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004370 && !(eap->argt & EX_NOSPC)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004371#endif
4372 )
4373 {
4374 char_u *l;
4375#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004376 // Don't escape a backslash here, because rem_backslash() doesn't
4377 // remove it later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004378 static char_u *nobslash = (char_u *)" \t\"|";
4379# define ESCAPE_CHARS nobslash
4380#else
4381# define ESCAPE_CHARS escape_chars
4382#endif
4383
4384 for (l = repl; *l; ++l)
4385 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
4386 {
4387 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
4388 if (l != NULL)
4389 {
4390 vim_free(repl);
4391 repl = l;
4392 }
4393 break;
4394 }
4395 }
4396
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004397 // For a shell command a '!' must be escaped.
Bram Moolenaar67883b42017-07-27 22:57:00 +02004398 if ((eap->usefilter || eap->cmdidx == CMD_bang
4399 || eap->cmdidx == CMD_terminal)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004400 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004401 {
4402 char_u *l;
4403
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004404 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405 if (l != NULL)
4406 {
4407 vim_free(repl);
4408 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004409 }
4410 }
4411
4412 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
4413 vim_free(repl);
4414 if (p == NULL)
4415 return FAIL;
4416 }
4417
4418 /*
4419 * One file argument: Expand wildcards.
4420 * Don't do this with ":r !command" or ":w !command".
4421 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004422 if ((eap->argt & EX_NOSPC) && !eap->usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004423 {
4424 /*
4425 * May do this twice:
4426 * 1. Replace environment variables.
4427 * 2. Replace any other wildcards, remove backslashes.
4428 */
4429 for (n = 1; n <= 2; ++n)
4430 {
4431 if (n == 2)
4432 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004433 /*
4434 * Halve the number of backslashes (this is Vi compatible).
4435 * For Unix and OS/2, when wildcards are expanded, this is
4436 * done by ExpandOne() below.
4437 */
Bram Moolenaare7fedb62015-12-31 19:07:19 +01004438#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004439 if (!has_wildcards)
4440#endif
4441 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004442 }
4443
4444 if (has_wildcards)
4445 {
4446 if (n == 1)
4447 {
4448 /*
4449 * First loop: May expand environment variables. This
4450 * can be done much faster with expand_env() than with
4451 * something else (e.g., calling a shell).
4452 * After expanding environment variables, check again
4453 * if there are still wildcards present.
4454 */
4455 if (vim_strchr(eap->arg, '$') != NULL
4456 || vim_strchr(eap->arg, '~') != NULL)
4457 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00004458 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004459 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004460 has_wildcards = mch_has_wildcard(NameBuff);
4461 p = NameBuff;
4462 }
4463 else
4464 p = NULL;
4465 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004466 else // n == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004467 {
4468 expand_T xpc;
Bram Moolenaarb40c2572019-10-19 21:01:05 +02004469 int options = WILD_LIST_NOTFOUND
4470 | WILD_NOERROR | WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004471
4472 ExpandInit(&xpc);
4473 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004474 if (p_wic)
4475 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01004477 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004478 if (p == NULL)
4479 return FAIL;
4480 }
4481 if (p != NULL)
4482 {
4483 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
4484 p, cmdlinep);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004485 if (n == 2) // p came from ExpandOne()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004486 vim_free(p);
4487 }
4488 }
4489 }
4490 }
4491 return OK;
4492}
4493
4494/*
4495 * Replace part of the command line, keeping eap->cmd, eap->arg and
4496 * eap->nextcmd correct.
4497 * "src" points to the part that is to be replaced, of length "srclen".
4498 * "repl" is the replacement string.
4499 * Returns a pointer to the character after the replaced string.
4500 * Returns NULL for failure.
4501 */
4502 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004503repl_cmdline(
4504 exarg_T *eap,
4505 char_u *src,
4506 int srclen,
4507 char_u *repl,
4508 char_u **cmdlinep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004509{
4510 int len;
4511 int i;
4512 char_u *new_cmdline;
4513
4514 /*
4515 * The new command line is build in new_cmdline[].
4516 * First allocate it.
4517 * Careful: a "+cmd" argument may have been NUL terminated.
4518 */
4519 len = (int)STRLEN(repl);
4520 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004521 if (eap->nextcmd != NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004522 i += (int)STRLEN(eap->nextcmd);// add space for next command
Bram Moolenaar964b3742019-05-24 18:54:09 +02004523 if ((new_cmdline = alloc(i)) == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004524 return NULL; // out of memory!
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525
4526 /*
4527 * Copy the stuff before the expanded part.
4528 * Copy the expanded stuff.
4529 * Copy what came after the expanded part.
4530 * Copy the next commands, if there are any.
4531 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004532 i = (int)(src - *cmdlinep); // length of part before match
Bram Moolenaar071d4272004-06-13 20:20:40 +00004533 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00004534
Bram Moolenaar071d4272004-06-13 20:20:40 +00004535 mch_memmove(new_cmdline + i, repl, (size_t)len);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004536 i += len; // remember the end of the string
Bram Moolenaar071d4272004-06-13 20:20:40 +00004537 STRCPY(new_cmdline + i, src + srclen);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004538 src = new_cmdline + i; // remember where to continue
Bram Moolenaar071d4272004-06-13 20:20:40 +00004539
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004540 if (eap->nextcmd != NULL) // append next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00004541 {
4542 i = (int)STRLEN(new_cmdline) + 1;
4543 STRCPY(new_cmdline + i, eap->nextcmd);
4544 eap->nextcmd = new_cmdline + i;
4545 }
4546 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
4547 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
4548 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
4549 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
4550 vim_free(*cmdlinep);
4551 *cmdlinep = new_cmdline;
4552
4553 return src;
4554}
4555
4556/*
4557 * Check for '|' to separate commands and '"' to start comments.
4558 */
4559 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004560separate_nextcmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004561{
4562 char_u *p;
4563
Bram Moolenaar86b68352004-12-27 21:59:20 +00004564#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004565 p = skip_grep_pat(eap);
4566#else
4567 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004568#endif
4569
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004570 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004571 {
4572 if (*p == Ctrl_V)
4573 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004574 if (eap->argt & (EX_CTRLV | EX_XFILE))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004575 ++p; // skip CTRL-V and next char
Bram Moolenaar071d4272004-06-13 20:20:40 +00004576 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004577 // remove CTRL-V and skip next char
Bram Moolenaara7241f52008-06-24 20:39:31 +00004578 STRMOVE(p, p + 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004579 if (*p == NUL) // stop at NUL after CTRL-V
Bram Moolenaar071d4272004-06-13 20:20:40 +00004580 break;
4581 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004582
4583#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004584 // Skip over `=expr` when wildcards are expanded.
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004585 else if (p[0] == '`' && p[1] == '=' && (eap->argt & EX_XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004586 {
4587 p += 2;
4588 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004589 }
4590#endif
4591
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004592 // Check for '"': start of comment or '|': next command
4593 // :@" and :*" do not start a comment!
4594 // :redir @" doesn't either.
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004595 else if ((*p == '"'
Bram Moolenaara26b9702020-04-18 19:53:28 +02004596#ifdef FEAT_EVAL
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004597 && !in_vim9script()
4598#endif
4599 && !(eap->argt & EX_NOTRLCOM)
4600 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
4601 || p != eap->arg)
4602 && (eap->cmdidx != CMD_redir
4603 || p != eap->arg + 1 || p[-1] != '@'))
4604#ifdef FEAT_EVAL
4605 || (*p == '#'
4606 && in_vim9script()
4607 && p[1] != '{'
4608 && p > eap->cmd && VIM_ISWHITE(p[-1]))
Bram Moolenaara26b9702020-04-18 19:53:28 +02004609#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004610 || *p == '|' || *p == '\n')
4611 {
4612 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004613 * We remove the '\' before the '|', unless EX_CTRLV is used
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614 * AND 'b' is present in 'cpoptions'.
4615 */
4616 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004617 || !(eap->argt & EX_CTRLV)) && *(p - 1) == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004618 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004619 STRMOVE(p - 1, p); // remove the '\'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004620 --p;
4621 }
4622 else
4623 {
4624 eap->nextcmd = check_nextcmd(p);
4625 *p = NUL;
4626 break;
4627 }
4628 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004629 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004630
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004631 if (!(eap->argt & EX_NOTRLCOM)) // remove trailing spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00004632 del_trailing_spaces(eap->arg);
4633}
4634
4635/*
4636 * get + command from ex argument
4637 */
4638 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004639getargcmd(char_u **argp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004640{
4641 char_u *arg = *argp;
4642 char_u *command = NULL;
4643
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004644 if (*arg == '+') // +[command]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004645 {
4646 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02004647 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004648 command = dollar_command;
4649 else
4650 {
4651 command = arg;
4652 arg = skip_cmd_arg(command, TRUE);
4653 if (*arg != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004654 *arg++ = NUL; // terminate command with NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004655 }
4656
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004657 arg = skipwhite(arg); // skip over spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00004658 *argp = arg;
4659 }
4660 return command;
4661}
4662
4663/*
4664 * Find end of "+command" argument. Skip over "\ " and "\\".
4665 */
Bram Moolenaard0190392019-08-23 21:17:35 +02004666 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004667skip_cmd_arg(
4668 char_u *p,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004669 int rembs) // TRUE to halve the number of backslashes
Bram Moolenaar071d4272004-06-13 20:20:40 +00004670{
4671 while (*p && !vim_isspace(*p))
4672 {
4673 if (*p == '\\' && p[1] != NUL)
4674 {
4675 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00004676 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004677 else
4678 ++p;
4679 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004680 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004681 }
4682 return p;
4683}
4684
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004685 int
4686get_bad_opt(char_u *p, exarg_T *eap)
4687{
4688 if (STRICMP(p, "keep") == 0)
4689 eap->bad_char = BAD_KEEP;
4690 else if (STRICMP(p, "drop") == 0)
4691 eap->bad_char = BAD_DROP;
4692 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
4693 eap->bad_char = *p;
Bram Moolenaar75808492018-06-12 12:39:41 +02004694 else
4695 return FAIL;
4696 return OK;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004697}
4698
Bram Moolenaar071d4272004-06-13 20:20:40 +00004699/*
4700 * Get "++opt=arg" argument.
4701 * Return FAIL or OK.
4702 */
4703 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004704getargopt(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004705{
4706 char_u *arg = eap->arg + 2;
4707 int *pp = NULL;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004708 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004709 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004710
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004711 // ":edit ++[no]bin[ary] file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004712 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
4713 {
4714 if (*arg == 'n')
4715 {
4716 arg += 2;
4717 eap->force_bin = FORCE_NOBIN;
4718 }
4719 else
4720 eap->force_bin = FORCE_BIN;
4721 if (!checkforcmd(&arg, "binary", 3))
4722 return FAIL;
4723 eap->arg = skipwhite(arg);
4724 return OK;
4725 }
4726
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004727 // ":read ++edit file"
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004728 if (STRNCMP(arg, "edit", 4) == 0)
4729 {
4730 eap->read_edit = TRUE;
4731 eap->arg = skipwhite(arg + 4);
4732 return OK;
4733 }
4734
Bram Moolenaar071d4272004-06-13 20:20:40 +00004735 if (STRNCMP(arg, "ff", 2) == 0)
4736 {
4737 arg += 2;
4738 pp = &eap->force_ff;
4739 }
4740 else if (STRNCMP(arg, "fileformat", 10) == 0)
4741 {
4742 arg += 10;
4743 pp = &eap->force_ff;
4744 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004745 else if (STRNCMP(arg, "enc", 3) == 0)
4746 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01004747 if (STRNCMP(arg, "encoding", 8) == 0)
4748 arg += 8;
4749 else
4750 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004751 pp = &eap->force_enc;
4752 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004753 else if (STRNCMP(arg, "bad", 3) == 0)
4754 {
4755 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004756 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004757 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004758
4759 if (pp == NULL || *arg != '=')
4760 return FAIL;
4761
4762 ++arg;
4763 *pp = (int)(arg - eap->cmd);
4764 arg = skip_cmd_arg(arg, FALSE);
4765 eap->arg = skipwhite(arg);
4766 *arg = NUL;
4767
Bram Moolenaar071d4272004-06-13 20:20:40 +00004768 if (pp == &eap->force_ff)
4769 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
4771 return FAIL;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004772 eap->force_ff = eap->cmd[eap->force_ff];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004773 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004774 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004775 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004776 // Make 'fileencoding' lower case.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004777 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
4778 *p = TOLOWER_ASC(*p);
4779 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004780 else
4781 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004782 // Check ++bad= argument. Must be a single-byte character, "keep" or
4783 // "drop".
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004784 if (get_bad_opt(eap->cmd + bad_char_idx, eap) == FAIL)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004785 return FAIL;
4786 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004787
4788 return OK;
4789}
4790
Bram Moolenaar071d4272004-06-13 20:20:40 +00004791 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004792ex_autocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004793{
4794 /*
Bram Moolenaar26d4b892018-07-04 22:26:28 +02004795 * Disallow autocommands from .exrc and .vimrc in current
Bram Moolenaar071d4272004-06-13 20:20:40 +00004796 * directory for security reasons.
4797 */
4798 if (secure)
4799 {
4800 secure = 2;
Bram Moolenaar8930caa2020-07-23 16:37:03 +02004801 eap->errmsg = _(e_curdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004802 }
4803 else if (eap->cmdidx == CMD_autocmd)
4804 do_autocmd(eap->arg, eap->forceit);
4805 else
4806 do_augroup(eap->arg, eap->forceit);
4807}
4808
4809/*
4810 * ":doautocmd": Apply the automatic commands to the current buffer.
4811 */
4812 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004813ex_doautocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004814{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01004815 char_u *arg = eap->arg;
4816 int call_do_modelines = check_nomodeline(&arg);
Bram Moolenaar1610d052016-06-09 22:53:01 +02004817 int did_aucmd;
Bram Moolenaar60542ac2012-02-12 20:14:01 +01004818
Bram Moolenaar1610d052016-06-09 22:53:01 +02004819 (void)do_doautocmd(arg, TRUE, &did_aucmd);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004820 // Only when there is no <nomodeline>.
Bram Moolenaar1610d052016-06-09 22:53:01 +02004821 if (call_do_modelines && did_aucmd)
Bram Moolenaar60542ac2012-02-12 20:14:01 +01004822 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004823}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004824
Bram Moolenaar071d4272004-06-13 20:20:40 +00004825/*
4826 * :[N]bunload[!] [N] [bufname] unload buffer
4827 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
4828 * :[N]bwipeout[!] [N] [bufname] delete buffer really
4829 */
4830 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004831ex_bunload(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 Moolenaar815b76b2019-06-01 14:15:52 +02004834 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004835 eap->errmsg = do_bufdel(
4836 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
4837 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
4838 : DOBUF_UNLOAD, eap->arg,
4839 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
4840}
4841
4842/*
4843 * :[N]buffer [N] to buffer N
4844 * :[N]sbuffer [N] to buffer N
4845 */
4846 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004847ex_buffer(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004848{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004849 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02004850 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004851 if (*eap->arg)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02004852 eap->errmsg = ex_errmsg(e_trailing_arg, eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004853 else
4854 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004855 if (eap->addr_count == 0) // default is current buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00004856 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
4857 else
4858 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004859 if (eap->do_ecmd_cmd != NULL)
4860 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861 }
4862}
4863
4864/*
4865 * :[N]bmodified [N] to next mod. buffer
4866 * :[N]sbmodified [N] to next mod. buffer
4867 */
4868 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004869ex_bmodified(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004870{
4871 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004872 if (eap->do_ecmd_cmd != NULL)
4873 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004874}
4875
4876/*
4877 * :[N]bnext [N] to next buffer
4878 * :[N]sbnext [N] split and to next buffer
4879 */
4880 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004881ex_bnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004882{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004883 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01004884 return;
4885
Bram Moolenaar071d4272004-06-13 20:20:40 +00004886 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004887 if (eap->do_ecmd_cmd != NULL)
4888 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004889}
4890
4891/*
4892 * :[N]bNext [N] to previous buffer
4893 * :[N]bprevious [N] to previous buffer
4894 * :[N]sbNext [N] split and to previous buffer
4895 * :[N]sbprevious [N] split and to previous buffer
4896 */
4897 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004898ex_bprevious(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004899{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004900 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01004901 return;
4902
Bram Moolenaar071d4272004-06-13 20:20:40 +00004903 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004904 if (eap->do_ecmd_cmd != NULL)
4905 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004906}
4907
4908/*
4909 * :brewind to first buffer
4910 * :bfirst to first buffer
4911 * :sbrewind split and to first buffer
4912 * :sbfirst split and to first buffer
4913 */
4914 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004915ex_brewind(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004917 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01004918 return;
4919
Bram Moolenaar071d4272004-06-13 20:20:40 +00004920 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004921 if (eap->do_ecmd_cmd != NULL)
4922 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004923}
4924
4925/*
4926 * :blast to last buffer
4927 * :sblast split and to last buffer
4928 */
4929 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004930ex_blast(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004931{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004932 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01004933 return;
4934
Bram Moolenaar071d4272004-06-13 20:20:40 +00004935 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004936 if (eap->do_ecmd_cmd != NULL)
4937 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004939
Bram Moolenaar7a092242020-04-16 22:10:49 +02004940/*
4941 * Check if "c" ends an Ex command.
Bram Moolenaara494f562020-04-18 17:45:38 +02004942 * In Vim9 script does not check for white space before # or #{.
Bram Moolenaar7a092242020-04-16 22:10:49 +02004943 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004945ends_excmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004946{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004947 int comment_char = '"';
4948
Bram Moolenaarfbda69b2020-04-13 15:06:53 +02004949#ifdef FEAT_EVAL
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004950 if (in_vim9script())
4951 comment_char = '#';
Bram Moolenaar7a092242020-04-16 22:10:49 +02004952#endif
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004953 return (c == NUL || c == '|' || c == comment_char || c == '\n');
Bram Moolenaar7a092242020-04-16 22:10:49 +02004954}
4955
4956/*
4957 * Like ends_excmd() but checks that a # in Vim9 script either has "cmd" equal
4958 * to "cmd_start" or has a white space character before it.
4959 */
4960 int
Bram Moolenaar21cfe502020-04-16 23:01:50 +02004961ends_excmd2(char_u *cmd_start UNUSED, char_u *cmd)
Bram Moolenaar7a092242020-04-16 22:10:49 +02004962{
4963 int c = *cmd;
4964
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004965 if (c == NUL || c == '|' || c == '\n')
4966 return TRUE;
Bram Moolenaar7a092242020-04-16 22:10:49 +02004967#ifdef FEAT_EVAL
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004968 if (in_vim9script())
4969 return c == '#' && cmd[1] != '{'
4970 && (cmd == cmd_start || VIM_ISWHITE(cmd[-1]));
Bram Moolenaarfbda69b2020-04-13 15:06:53 +02004971#endif
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004972 return c == '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004973}
4974
4975#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
4976 || defined(PROTO)
4977/*
4978 * Return the next command, after the first '|' or '\n'.
4979 * Return NULL if not found.
4980 */
4981 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004982find_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004983{
4984 while (*p != '|' && *p != '\n')
4985 {
4986 if (*p == NUL)
4987 return NULL;
4988 ++p;
4989 }
4990 return (p + 1);
4991}
4992#endif
4993
4994/*
Bram Moolenaar2256c992016-11-15 21:17:07 +01004995 * Check if *p is a separator between Ex commands, skipping over white space.
4996 * Return NULL if it isn't, the following character if it is.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004997 */
4998 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004999check_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005000{
Bram Moolenaar2256c992016-11-15 21:17:07 +01005001 char_u *s = skipwhite(p);
5002
5003 if (*s == '|' || *s == '\n')
5004 return (s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005005 else
5006 return NULL;
5007}
5008
5009/*
5010 * - if there are more files to edit
5011 * - and this is the last window
5012 * - and forceit not used
5013 * - and not repeated twice on a row
5014 * return FAIL and give error message if 'message' TRUE
5015 * return OK otherwise
5016 */
5017 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005018check_more(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005019 int message, // when FALSE check only, no messages
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005020 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021{
5022 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5023
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005024 if (!forceit && only_one_window()
5025 && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005026 {
5027 if (message)
5028 {
5029#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
5030 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
5031 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005032 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005033
Bram Moolenaarda6e8912018-08-21 15:12:14 +02005034 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
5035 NGETTEXT("%d more file to edit. Quit anyway?",
5036 "%d more files to edit. Quit anyway?", n), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005037 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5038 return OK;
5039 return FAIL;
5040 }
5041#endif
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01005042 semsg(NGETTEXT("E173: %d more file to edit",
5043 "E173: %d more files to edit", n), n);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005044 quitmore = 2; // next try to quit is allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00005045 }
5046 return FAIL;
5047 }
5048 return OK;
5049}
5050
Bram Moolenaar071d4272004-06-13 20:20:40 +00005051/*
5052 * Function given to ExpandGeneric() to obtain the list of command names.
5053 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005055get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056{
5057 if (idx >= (int)CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005058 return get_user_command_name(idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059 return cmdnames[idx].cmd_name;
5060}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005061
Bram Moolenaar071d4272004-06-13 20:20:40 +00005062 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005063ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005064{
Bram Moolenaare6850792010-05-14 15:28:44 +02005065 if (*eap->arg == NUL)
5066 {
5067#ifdef FEAT_EVAL
5068 char_u *expr = vim_strsave((char_u *)"g:colors_name");
5069 char_u *p = NULL;
5070
5071 if (expr != NULL)
5072 {
5073 ++emsg_off;
Bram Moolenaarb171fb12020-06-24 20:34:03 +02005074 p = eval_to_string(expr, FALSE);
Bram Moolenaare6850792010-05-14 15:28:44 +02005075 --emsg_off;
5076 vim_free(expr);
5077 }
5078 if (p != NULL)
5079 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005080 msg((char *)p);
Bram Moolenaare6850792010-05-14 15:28:44 +02005081 vim_free(p);
5082 }
5083 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005084 msg("default");
Bram Moolenaare6850792010-05-14 15:28:44 +02005085#else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005086 msg(_("unknown"));
Bram Moolenaare6850792010-05-14 15:28:44 +02005087#endif
5088 }
5089 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005090 semsg(_("E185: Cannot find color scheme '%s'"), eap->arg);
Bram Moolenaarf58d81a2019-01-28 20:19:05 +01005091
5092#ifdef FEAT_VTP
5093 else if (has_vtp_working())
5094 {
5095 // background color change requires clear + redraw
5096 update_screen(CLEAR);
5097 redrawcmd();
5098 }
5099#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005100}
5101
5102 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005103ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005104{
5105 if (*eap->arg == NUL && eap->cmd[2] == '!')
Bram Moolenaar32526b32019-01-19 17:43:09 +01005106 msg(_("Greetings, Vim user!"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005107 do_highlight(eap->arg, eap->forceit, FALSE);
5108}
5109
5110
5111/*
5112 * Call this function if we thought we were going to exit, but we won't
5113 * (because of an error). May need to restore the terminal mode.
5114 */
5115 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005116not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005117{
5118 exiting = FALSE;
5119 settmode(TMODE_RAW);
5120}
5121
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005122 static int
5123before_quit_autocmds(win_T *wp, int quit_all, int forceit)
5124{
5125 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, wp->w_buffer);
5126
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02005127 // Bail out when autocommands closed the window.
5128 // Refuse to quit when the buffer in the last window is being closed (can
5129 // only happen in autocommands).
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005130 if (!win_valid(wp)
5131 || curbuf_locked()
5132 || (wp->w_buffer->b_nwindows == 1 && wp->w_buffer->b_locked > 0))
5133 return TRUE;
5134
5135 if (quit_all || (check_more(FALSE, forceit) == OK && only_one_window()))
5136 {
5137 apply_autocmds(EVENT_EXITPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02005138 // Refuse to quit when locked or when the window was closed or the
5139 // buffer in the last window is being closed (can only happen in
5140 // autocommands).
5141 if (!win_valid(wp) || curbuf_locked()
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005142 || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
5143 return TRUE;
5144 }
5145
5146 return FALSE;
5147}
5148
Bram Moolenaar071d4272004-06-13 20:20:40 +00005149/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01005150 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005151 * ":{nr}quit": quit window {nr}
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02005152 * Also used when closing a terminal window that's the last one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153 */
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02005154 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005155ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005156{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005157 win_T *wp;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005158
Bram Moolenaar071d4272004-06-13 20:20:40 +00005159#ifdef FEAT_CMDWIN
5160 if (cmdwin_type != 0)
5161 {
5162 cmdwin_result = Ctrl_C;
5163 return;
5164 }
5165#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005166 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005167 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005168 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005169 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005170 return;
5171 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005172 if (eap->addr_count > 0)
5173 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01005174 int wnr = eap->line2;
5175
5176 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
5177 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005178 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005179 }
5180 else
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005181 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01005182
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005183 // Refuse to quit when locked.
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02005184 if (curbuf_locked())
5185 return;
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005186
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005187 // Trigger QuitPre and maybe ExitPre
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005188 if (before_quit_autocmds(wp, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005189 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005190
5191#ifdef FEAT_NETBEANS_INTG
5192 netbeansForcedQuit = eap->forceit;
5193#endif
5194
5195 /*
5196 * If there are more files or windows we won't exit.
5197 */
5198 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
5199 exiting = TRUE;
Bram Moolenaarff930ca2017-10-19 17:12:10 +02005200 if ((!buf_hide(wp->w_buffer)
5201 && check_changed(wp->w_buffer, (p_awa ? CCGD_AW : 0)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01005202 | (eap->forceit ? CCGD_FORCEIT : 0)
5203 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005204 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01005205 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005206 {
5207 not_exiting();
5208 }
5209 else
5210 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005211 // quit last window
5212 // Note: only_one_window() returns true, even so a help window is
5213 // still open. In that case only quit, if no address has been
5214 // specified. Example:
5215 // :h|wincmd w|1q - don't quit
5216 // :h|wincmd w|q - quit
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01005217 if (only_one_window() && (ONE_WINDOW || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005218 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02005219 not_exiting();
Bram Moolenaar4033c552017-09-16 20:54:51 +02005220#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005221 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005222#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005223 // close window; may free buffer
Bram Moolenaareb44a682017-08-03 22:44:55 +02005224 win_close(wp, !buf_hide(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005225 }
5226}
5227
5228/*
5229 * ":cquit".
5230 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005231 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005232ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005233{
Bram Moolenaar1860bde2020-01-06 21:47:21 +01005234 // this does not always pass on the exit code to the Manx compiler. why?
5235 getout(eap->addr_count > 0 ? (int)eap->line2 : EXIT_FAILURE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005236}
5237
5238/*
5239 * ":qall": try to quit all windows
5240 */
5241 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005242ex_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243{
5244# ifdef FEAT_CMDWIN
5245 if (cmdwin_type != 0)
5246 {
5247 if (eap->forceit)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005248 cmdwin_result = K_XF1; // ex_window() takes care of this
Bram Moolenaar071d4272004-06-13 20:20:40 +00005249 else
5250 cmdwin_result = K_XF2;
5251 return;
5252 }
5253# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005254
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005255 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005256 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005257 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005258 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005259 return;
5260 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005261
5262 if (before_quit_autocmds(curwin, TRUE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005263 return;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005264
Bram Moolenaar071d4272004-06-13 20:20:40 +00005265 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01005266 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005267 getout(0);
5268 not_exiting();
5269}
5270
Bram Moolenaar071d4272004-06-13 20:20:40 +00005271/*
5272 * ":close": close current window, unless it is the last one
5273 */
5274 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005275ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005276{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005277 win_T *win;
5278 int winnr = 0;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005279#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00005280 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02005281 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005282 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005283#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005284 if (!text_locked() && !curbuf_locked())
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005285 {
5286 if (eap->addr_count == 0)
5287 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar6d41c782018-06-06 09:11:12 +02005288 else
5289 {
Bram Moolenaar29323592016-07-24 22:04:11 +02005290 FOR_ALL_WINDOWS(win)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005291 {
5292 winnr++;
5293 if (winnr == eap->line2)
5294 break;
5295 }
5296 if (win == NULL)
5297 win = lastwin;
5298 ex_win_close(eap->forceit, win, NULL);
5299 }
5300 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005301}
5302
Bram Moolenaar4033c552017-09-16 20:54:51 +02005303#ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005304/*
5305 * ":pclose": Close any preview window.
5306 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005307 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005308ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005309{
5310 win_T *win;
5311
Bram Moolenaar79648732019-07-18 21:43:07 +02005312 // First close any normal window.
Bram Moolenaar29323592016-07-24 22:04:11 +02005313 FOR_ALL_WINDOWS(win)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005314 if (win->w_p_pvw)
5315 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005316 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar79648732019-07-18 21:43:07 +02005317 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005318 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005319# ifdef FEAT_PROP_POPUP
Bram Moolenaar79648732019-07-18 21:43:07 +02005320 // Also when 'previewpopup' is empty, it might have been cleared.
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02005321 popup_close_preview();
Bram Moolenaar79648732019-07-18 21:43:07 +02005322# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005323}
Bram Moolenaar4033c552017-09-16 20:54:51 +02005324#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005325
Bram Moolenaarf740b292006-02-16 22:11:02 +00005326/*
5327 * Close window "win" and take care of handling closing the last window for a
5328 * modified buffer.
5329 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005330 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005331ex_win_close(
5332 int forceit,
5333 win_T *win,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005334 tabpage_T *tp) // NULL or the tab page "win" is in
Bram Moolenaar071d4272004-06-13 20:20:40 +00005335{
5336 int need_hide;
5337 buf_T *buf = win->w_buffer;
5338
Bram Moolenaarcf844172020-06-26 19:44:06 +02005339 // Never close the autocommand window.
5340 if (win == aucmd_win)
5341 {
5342 emsg(_(e_autocmd_close));
5343 return;
5344 }
5345
Bram Moolenaar071d4272004-06-13 20:20:40 +00005346 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02005347 if (need_hide && !buf_hide(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005348 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02005349#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005350 if ((p_confirm || cmdmod.confirm) && p_write)
5351 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005352 bufref_T bufref;
5353
5354 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005355 dialog_changed(buf, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005356 if (bufref_valid(&bufref) && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005357 return;
5358 need_hide = FALSE;
5359 }
5360 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005361#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005362 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02005363 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005364 return;
5365 }
5366 }
5367
Bram Moolenaar4033c552017-09-16 20:54:51 +02005368#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005369 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005370#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005371
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005372 // free buffer when not hiding it or when it's a scratch buffer
Bram Moolenaarf740b292006-02-16 22:11:02 +00005373 if (tp == NULL)
Bram Moolenaareb44a682017-08-03 22:44:55 +02005374 win_close(win, !need_hide && !buf_hide(buf));
Bram Moolenaarf740b292006-02-16 22:11:02 +00005375 else
Bram Moolenaareb44a682017-08-03 22:44:55 +02005376 win_close_othertab(win, !need_hide && !buf_hide(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005377}
5378
Bram Moolenaar071d4272004-06-13 20:20:40 +00005379/*
Bram Moolenaardea25702017-01-29 15:18:10 +01005380 * Handle the argument for a tabpage related ex command.
5381 * Returns a tabpage number.
5382 * When an error is encountered then eap->errmsg is set.
5383 */
5384 static int
5385get_tabpage_arg(exarg_T *eap)
5386{
5387 int tab_number;
5388 int unaccept_arg0 = (eap->cmdidx == CMD_tabmove) ? 0 : 1;
5389
5390 if (eap->arg && *eap->arg != NUL)
5391 {
5392 char_u *p = eap->arg;
5393 char_u *p_save;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005394 int relative = 0; // argument +N/-N means: go to N places to the
5395 // right/left relative to the current position.
Bram Moolenaardea25702017-01-29 15:18:10 +01005396
5397 if (*p == '-')
5398 {
5399 relative = -1;
5400 p++;
5401 }
5402 else if (*p == '+')
5403 {
5404 relative = 1;
5405 p++;
5406 }
5407
5408 p_save = p;
5409 tab_number = getdigits(&p);
5410
5411 if (relative == 0)
5412 {
5413 if (STRCMP(p, "$") == 0)
5414 tab_number = LAST_TAB_NR;
5415 else if (p == p_save || *p_save == '-' || *p != NUL
5416 || tab_number > LAST_TAB_NR)
5417 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005418 // No numbers as argument.
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005419 eap->errmsg = ex_errmsg(e_invarg2, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01005420 goto theend;
5421 }
5422 }
5423 else
5424 {
5425 if (*p_save == NUL)
5426 tab_number = 1;
5427 else if (p == p_save || *p_save == '-' || *p != NUL
5428 || tab_number == 0)
5429 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005430 // No numbers as argument.
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005431 eap->errmsg = ex_errmsg(e_invarg2, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01005432 goto theend;
5433 }
5434 tab_number = tab_number * relative + tabpage_index(curtab);
5435 if (!unaccept_arg0 && relative == -1)
5436 --tab_number;
5437 }
5438 if (tab_number < unaccept_arg0 || tab_number > LAST_TAB_NR)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005439 eap->errmsg = ex_errmsg(e_invarg2, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01005440 }
5441 else if (eap->addr_count > 0)
5442 {
5443 if (unaccept_arg0 && eap->line2 == 0)
Bram Moolenaarc6251552017-01-29 21:42:20 +01005444 {
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005445 eap->errmsg = _(e_invrange);
Bram Moolenaarc6251552017-01-29 21:42:20 +01005446 tab_number = 0;
5447 }
Bram Moolenaardea25702017-01-29 15:18:10 +01005448 else
5449 {
5450 tab_number = eap->line2;
Bram Moolenaar82a12462019-01-22 22:41:42 +01005451 if (!unaccept_arg0 && *skipwhite(*eap->cmdlinep) == '-')
Bram Moolenaardea25702017-01-29 15:18:10 +01005452 {
5453 --tab_number;
5454 if (tab_number < unaccept_arg0)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005455 eap->errmsg = _(e_invrange);
Bram Moolenaardea25702017-01-29 15:18:10 +01005456 }
5457 }
5458 }
5459 else
5460 {
5461 switch (eap->cmdidx)
5462 {
5463 case CMD_tabnext:
5464 tab_number = tabpage_index(curtab) + 1;
5465 if (tab_number > LAST_TAB_NR)
5466 tab_number = 1;
5467 break;
5468 case CMD_tabmove:
5469 tab_number = LAST_TAB_NR;
5470 break;
5471 default:
5472 tab_number = tabpage_index(curtab);
5473 }
5474 }
5475
5476theend:
5477 return tab_number;
5478}
5479
5480/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00005481 * ":tabclose": close current tab page, unless it is the last one.
5482 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005483 */
5484 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005485ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005486{
Bram Moolenaarf740b292006-02-16 22:11:02 +00005487 tabpage_T *tp;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005488 int tab_number;
Bram Moolenaarf740b292006-02-16 22:11:02 +00005489
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005490# ifdef FEAT_CMDWIN
5491 if (cmdwin_type != 0)
5492 cmdwin_result = K_IGNORE;
5493 else
5494# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005495 if (first_tabpage->tp_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005496 emsg(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00005497 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005498 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005499 tab_number = get_tabpage_arg(eap);
5500 if (eap->errmsg == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005501 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005502 tp = find_tabpage(tab_number);
Bram Moolenaarf740b292006-02-16 22:11:02 +00005503 if (tp == NULL)
5504 {
5505 beep_flush();
5506 return;
5507 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005508 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005509 {
5510 tabpage_close_other(tp, eap->forceit);
5511 return;
5512 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005513 else if (!text_locked() && !curbuf_locked())
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005514 tabpage_close(eap->forceit);
5515 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005516 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005517}
5518
5519/*
5520 * ":tabonly": close all tab pages except the current one
5521 */
5522 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005523ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005524{
5525 tabpage_T *tp;
5526 int done;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005527 int tab_number;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005528
5529# ifdef FEAT_CMDWIN
5530 if (cmdwin_type != 0)
5531 cmdwin_result = K_IGNORE;
5532 else
5533# endif
5534 if (first_tabpage->tp_next == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005535 msg(_("Already only one tab page"));
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005536 else
5537 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005538 tab_number = get_tabpage_arg(eap);
5539 if (eap->errmsg == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005540 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005541 goto_tabpage(tab_number);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005542 // Repeat this up to a 1000 times, because autocommands may
5543 // mess up the lists.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005544 for (done = 0; done < 1000; ++done)
5545 {
5546 FOR_ALL_TABPAGES(tp)
5547 if (tp->tp_topframe != topframe)
5548 {
5549 tabpage_close_other(tp, eap->forceit);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005550 // if we failed to close it quit
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005551 if (valid_tabpage(tp))
5552 done = 1000;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005553 // start over, "tp" is now invalid
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005554 break;
5555 }
5556 if (first_tabpage->tp_next == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005557 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005558 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005559 }
5560 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005561}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005562
5563/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00005564 * Close the current tab page.
5565 */
5566 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005567tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005568{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005569 // First close all the windows but the current one. If that worked then
5570 // close the last window in this tab, that will close it.
Bram Moolenaar459ca562016-11-10 18:16:33 +01005571 if (!ONE_WINDOW)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005572 close_others(TRUE, forceit);
Bram Moolenaar459ca562016-11-10 18:16:33 +01005573 if (ONE_WINDOW)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005574 ex_win_close(forceit, curwin, NULL);
5575# ifdef FEAT_GUI
5576 need_mouse_correct = TRUE;
5577# endif
5578}
5579
5580/*
5581 * Close tab page "tp", which is not the current tab page.
5582 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005583 * Also takes care of the tab pages line disappearing when closing the
5584 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00005585 */
5586 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005587tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005588{
5589 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005590 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005591 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00005592
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005593 // Limit to 1000 windows, autocommands may add a window while we close
5594 // one. OK, so I'm paranoid...
Bram Moolenaarf740b292006-02-16 22:11:02 +00005595 while (++done < 1000)
5596 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005597 wp = tp->tp_firstwin;
5598 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00005599
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005600 // Autocommands may delete the tab page under our fingers and we may
5601 // fail to close a window with a modified buffer.
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005602 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005603 break;
5604 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005605
Bram Moolenaar29323592016-07-24 22:04:11 +02005606 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar29323592016-07-24 22:04:11 +02005607
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005608 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005609 if (h != tabline_height())
5610 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00005611}
5612
5613/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005614 * ":only".
5615 */
5616 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005617ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005618{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005619 win_T *wp;
5620 int wnr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005621# ifdef FEAT_GUI
5622 need_mouse_correct = TRUE;
5623# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005624 if (eap->addr_count > 0)
5625 {
5626 wnr = eap->line2;
5627 for (wp = firstwin; --wnr > 0; )
5628 {
5629 if (wp->w_next == NULL)
5630 break;
5631 else
5632 wp = wp->w_next;
5633 }
5634 win_goto(wp);
5635 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005636 close_others(TRUE, eap->forceit);
5637}
5638
Bram Moolenaar071d4272004-06-13 20:20:40 +00005639 static void
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01005640ex_hide(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005641{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005642 // ":hide" or ":hide | cmd": hide current window
Bram Moolenaar2256c992016-11-15 21:17:07 +01005643 if (!eap->skip)
5644 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02005645#ifdef FEAT_GUI
Bram Moolenaar2256c992016-11-15 21:17:07 +01005646 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005647#endif
Bram Moolenaar2256c992016-11-15 21:17:07 +01005648 if (eap->addr_count == 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005649 win_close(curwin, FALSE); // don't free buffer
Bram Moolenaar2256c992016-11-15 21:17:07 +01005650 else
5651 {
5652 int winnr = 0;
5653 win_T *win;
Bram Moolenaarf240e182014-11-27 18:33:02 +01005654
Bram Moolenaar2256c992016-11-15 21:17:07 +01005655 FOR_ALL_WINDOWS(win)
5656 {
5657 winnr++;
5658 if (winnr == eap->line2)
5659 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005660 }
Bram Moolenaar2256c992016-11-15 21:17:07 +01005661 if (win == NULL)
5662 win = lastwin;
5663 win_close(win, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005664 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005665 }
5666}
5667
5668/*
5669 * ":stop" and ":suspend": Suspend Vim.
5670 */
5671 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005672ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005673{
5674 /*
5675 * Disallow suspending for "rvim".
5676 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005677 if (!check_restricted())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005678 {
5679 if (!eap->forceit)
5680 autowrite_all();
5681 windgoto((int)Rows - 1, 0);
5682 out_char('\n');
5683 out_flush();
5684 stoptermcap();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005685 out_flush(); // needed for SUN to restore xterm buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00005686#ifdef FEAT_TITLE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005687 mch_restore_title(SAVE_RESTORE_BOTH); // restore window titles
Bram Moolenaar071d4272004-06-13 20:20:40 +00005688#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005689 ui_suspend(); // call machine specific function
Bram Moolenaar071d4272004-06-13 20:20:40 +00005690#ifdef FEAT_TITLE
5691 maketitle();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005692 resettitle(); // force updating the title
Bram Moolenaar071d4272004-06-13 20:20:40 +00005693#endif
5694 starttermcap();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005695 scroll_start(); // scroll screen before redrawing
Bram Moolenaar071d4272004-06-13 20:20:40 +00005696 redraw_later_clear();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005697 shell_resized(); // may have resized window
Bram Moolenaar071d4272004-06-13 20:20:40 +00005698 }
5699}
5700
5701/*
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005702 * ":exit", ":xit" and ":wq": Write file and quite the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005703 */
5704 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005705ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005706{
Bram Moolenaar461f2122020-07-28 20:25:47 +02005707#ifdef FEAT_EVAL
Bram Moolenaarae616492020-07-28 20:07:27 +02005708 if (not_in_vim9(eap) == FAIL)
5709 return;
Bram Moolenaar461f2122020-07-28 20:25:47 +02005710#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005711#ifdef FEAT_CMDWIN
5712 if (cmdwin_type != 0)
5713 {
5714 cmdwin_result = Ctrl_C;
5715 return;
5716 }
5717#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005718 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005719 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005720 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005721 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005722 return;
5723 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005724
5725 if (before_quit_autocmds(curwin, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005726 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005727
5728 /*
5729 * if more files or windows we won't exit
5730 */
5731 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
5732 exiting = TRUE;
5733 if ( ((eap->cmdidx == CMD_wq
5734 || curbufIsChanged())
5735 && do_write(eap) == FAIL)
5736 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01005737 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005738 {
5739 not_exiting();
5740 }
5741 else
5742 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005743 if (only_one_window()) // quit last window, exit Vim
Bram Moolenaar071d4272004-06-13 20:20:40 +00005744 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02005745 not_exiting();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005746# ifdef FEAT_GUI
5747 need_mouse_correct = TRUE;
5748# endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005749 // Quit current window, may free the buffer.
Bram Moolenaareb44a682017-08-03 22:44:55 +02005750 win_close(curwin, !buf_hide(curwin->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005751 }
5752}
5753
5754/*
5755 * ":print", ":list", ":number".
5756 */
5757 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005758ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005759{
Bram Moolenaardf177f62005-02-22 08:39:57 +00005760 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005761 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +00005762 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005763 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00005764 for ( ;!got_int; ui_breakcheck())
5765 {
5766 print_line(eap->line1,
5767 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
5768 || (eap->flags & EXFLAG_NR)),
5769 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
5770 if (++eap->line1 > eap->line2)
5771 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005772 out_flush(); // show one line at a time
Bram Moolenaardf177f62005-02-22 08:39:57 +00005773 }
5774 setpcmark();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005775 // put cursor at last line
Bram Moolenaardf177f62005-02-22 08:39:57 +00005776 curwin->w_cursor.lnum = eap->line2;
5777 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005778 }
5779
Bram Moolenaar071d4272004-06-13 20:20:40 +00005780 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005781}
5782
5783#ifdef FEAT_BYTEOFF
5784 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005785ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005786{
5787 goto_byte(eap->line2);
5788}
5789#endif
5790
5791/*
5792 * ":shell".
5793 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005794 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005795ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005796{
5797 do_shell(NULL, 0);
5798}
5799
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005800#if defined(HAVE_DROP_FILE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005801
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005802static int drop_busy = FALSE;
5803static int drop_filec;
5804static char_u **drop_filev = NULL;
5805static int drop_split;
5806static void (*drop_callback)(void *);
5807static void *drop_cookie;
5808
5809 static void
5810handle_drop_internal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005811{
5812 exarg_T ea;
5813 int save_msg_scroll = msg_scroll;
5814
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005815 // Setting the argument list may cause screen updates and being called
5816 // recursively. Avoid that by setting drop_busy.
5817 drop_busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005818
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005819 // Check whether the current buffer is changed. If so, we will need
5820 // to split the current window or data could be lost.
5821 // We don't need to check if the 'hidden' option is set, as in this
5822 // case the buffer won't be lost.
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005823 if (!buf_hide(curbuf) && !drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005824 {
5825 ++emsg_off;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005826 drop_split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005827 --emsg_off;
5828 }
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005829 if (drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005830 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005831 if (win_split(0, 0) == FAIL)
5832 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02005833 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005834
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005835 // When splitting the window, create a new alist. Otherwise the
5836 // existing one is overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005837 alist_unlink(curwin->w_alist);
5838 alist_new();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005839 }
5840
5841 /*
5842 * Set up the new argument list.
5843 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005844 alist_set(ALIST(curwin), drop_filec, drop_filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005845
5846 /*
5847 * Move to the first file.
5848 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005849 // Fake up a minimal "next" command for do_argfile()
Bram Moolenaara80faa82020-04-12 19:37:17 +02005850 CLEAR_FIELD(ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005851 ea.cmd = (char_u *)"next";
5852 do_argfile(&ea, 0);
5853
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005854 // do_ecmd() may set need_start_insertmode, but since we never left Insert
5855 // mode that is not needed here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005856 need_start_insertmode = FALSE;
5857
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005858 // Restore msg_scroll, otherwise a following command may cause scrolling
5859 // unexpectedly. The screen will be redrawn by the caller, thus
5860 // msg_scroll being set by displaying a message is irrelevant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005861 msg_scroll = save_msg_scroll;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005862
5863 if (drop_callback != NULL)
5864 drop_callback(drop_cookie);
5865
5866 drop_filev = NULL;
5867 drop_busy = FALSE;
5868}
5869
5870/*
5871 * Handle a file drop. The code is here because a drop is *nearly* like an
5872 * :args command, but not quite (we have a list of exact filenames, so we
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01005873 * don't want to (a) parse a command line, or (b) expand wildcards). So the
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005874 * code is very similar to :args and hence needs access to a lot of the static
5875 * functions in this file.
5876 *
5877 * The "filev" list must have been allocated using alloc(), as should each item
5878 * in the list. This function takes over responsibility for freeing the "filev"
5879 * list.
5880 */
5881 void
5882handle_drop(
5883 int filec, // the number of files dropped
5884 char_u **filev, // the list of files dropped
5885 int split, // force splitting the window
5886 void (*callback)(void *), // to be called after setting the argument
5887 // list
5888 void *cookie) // argument for "callback" (allocated)
5889{
5890 // Cannot handle recursive drops, finish the pending one.
5891 if (drop_busy)
5892 {
5893 FreeWild(filec, filev);
5894 vim_free(cookie);
5895 return;
5896 }
5897
5898 // When calling handle_drop() more than once in a row we only use the last
5899 // one.
5900 if (drop_filev != NULL)
5901 {
5902 FreeWild(drop_filec, drop_filev);
5903 vim_free(drop_cookie);
5904 }
5905
5906 drop_filec = filec;
5907 drop_filev = filev;
5908 drop_split = split;
5909 drop_callback = callback;
5910 drop_cookie = cookie;
5911
5912 // Postpone this when:
5913 // - editing the command line
5914 // - not possible to change the current buffer
5915 // - updating the screen
5916 // As it may change buffers and window structures that are in use and cause
5917 // freed memory to be used.
5918 if (text_locked() || curbuf_locked() || updating_screen)
5919 return;
5920
5921 handle_drop_internal();
5922}
5923
5924/*
5925 * To be called when text is unlocked, curbuf is unlocked or updating_screen is
5926 * reset: Handle a postponed drop.
5927 */
5928 void
5929handle_any_postponed_drop(void)
5930{
5931 if (!drop_busy && drop_filev != NULL
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02005932 && !text_locked() && !curbuf_locked() && !updating_screen)
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005933 handle_drop_internal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005934}
5935#endif
5936
Bram Moolenaar071d4272004-06-13 20:20:40 +00005937/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005938 * ":preserve".
5939 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005940 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005941ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005942{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005943 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005944 ml_preserve(curbuf, TRUE);
5945}
5946
5947/*
5948 * ":recover".
5949 */
5950 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005951ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005952{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005953 // Set recoverymode right away to avoid the ATTENTION prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005954 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01005955 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
5956 | CCGD_MULTWIN
5957 | (eap->forceit ? CCGD_FORCEIT : 0)
5958 | CCGD_EXCMD)
5959
Bram Moolenaar071d4272004-06-13 20:20:40 +00005960 && (*eap->arg == NUL
5961 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
Bram Moolenaar99499b12019-05-23 21:35:48 +02005962 ml_recover(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005963 recoverymode = FALSE;
5964}
5965
5966/*
5967 * Command modifier used in a wrong way.
5968 */
5969 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005970ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005971{
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005972 eap->errmsg = _(e_invcmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005973}
5974
Bram Moolenaar071d4272004-06-13 20:20:40 +00005975/*
5976 * :sview [+command] file split window with new file, read-only
5977 * :split [[+command] file] split window with current or new file
5978 * :vsplit [[+command] file] split window vertically with current or new file
5979 * :new [[+command] file] split window with no or new file
5980 * :vnew [[+command] file] split vertically window with no or new file
5981 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005982 *
5983 * :tabedit open new Tab page with empty window
5984 * :tabedit [+command] file open new Tab page and edit "file"
5985 * :tabnew [[+command] file] just like :tabedit
5986 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005987 */
5988 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005989ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005990{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005991 win_T *old_curwin = curwin;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005992#if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005993 char_u *fname = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005994#endif
5995#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005996 int browse_flag = cmdmod.browse;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005997#endif
Bram Moolenaar39902a02018-06-21 22:10:08 +02005998 int use_tab = eap->cmdidx == CMD_tabedit
5999 || eap->cmdidx == CMD_tabfind
6000 || eap->cmdidx == CMD_tabnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006001
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01006002 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006003 return;
6004
Bram Moolenaar4033c552017-09-16 20:54:51 +02006005#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006006 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006007#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006008
Bram Moolenaar4033c552017-09-16 20:54:51 +02006009#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006010 // A ":split" in the quickfix window works like ":new". Don't want two
6011 // quickfix windows. But it's OK when doing ":tab split".
Bram Moolenaar05bb9532008-07-04 09:44:11 +00006012 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006013 {
6014 if (eap->cmdidx == CMD_split)
6015 eap->cmdidx = CMD_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006016 if (eap->cmdidx == CMD_vsplit)
6017 eap->cmdidx = CMD_vnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006018 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006019#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006020
Bram Moolenaar4033c552017-09-16 20:54:51 +02006021#ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006022 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006023 {
6024 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
6025 FNAME_MESS, TRUE, curbuf->b_ffname);
6026 if (fname == NULL)
6027 goto theend;
6028 eap->arg = fname;
6029 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006030# ifdef FEAT_BROWSE
Bram Moolenaar4033c552017-09-16 20:54:51 +02006031 else
6032# endif
6033#endif
6034#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006035 if (cmdmod.browse
Bram Moolenaar071d4272004-06-13 20:20:40 +00006036 && eap->cmdidx != CMD_vnew
Bram Moolenaar071d4272004-06-13 20:20:40 +00006037 && eap->cmdidx != CMD_new)
6038 {
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006039 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006040# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006041 !gui.in_use &&
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006042# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006043 au_has_group((char_u *)"FileExplorer"))
6044 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006045 // No browsing supported but we do have the file explorer:
6046 // Edit the directory.
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006047 if (*eap->arg == NUL || !mch_isdir(eap->arg))
6048 eap->arg = (char_u *)".";
6049 }
6050 else
6051 {
Bram Moolenaar39902a02018-06-21 22:10:08 +02006052 fname = do_browse(0, (char_u *)(use_tab
6053 ? _("Edit File in new tab page")
6054 : _("Edit File in new window")),
Bram Moolenaar071d4272004-06-13 20:20:40 +00006055 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006056 if (fname == NULL)
6057 goto theend;
6058 eap->arg = fname;
6059 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006060 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006061 cmdmod.browse = FALSE; // Don't browse again in do_ecmd().
Bram Moolenaar4033c552017-09-16 20:54:51 +02006062#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006063
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006064 /*
6065 * Either open new tab page or split the window.
6066 */
Bram Moolenaar39902a02018-06-21 22:10:08 +02006067 if (use_tab)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006068 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006069 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
6070 : eap->addr_count == 0 ? 0
6071 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006072 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00006073 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006074
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006075 // set the alternate buffer for the window we came from
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006076 if (curwin != old_curwin
6077 && win_valid(old_curwin)
6078 && old_curwin->w_buffer != curbuf
6079 && !cmdmod.keepalt)
6080 old_curwin->w_alt_fnum = curbuf->b_fnum;
6081 }
6082 }
6083 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006084 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
6085 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006086 // Reset 'scrollbind' when editing another file, but keep it when
6087 // doing ":split" without arguments.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006088 if (*eap->arg != NUL
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01006089# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006090 || cmdmod.browse
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01006091# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006092 )
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006093 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006094 else
6095 do_check_scrollbind(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006096 do_exedit(eap, old_curwin);
6097 }
6098
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006099# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006100 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006101# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006102
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006103# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006104theend:
6105 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006106# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006107}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006108
6109/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006110 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006111 */
6112 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006113tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006114{
6115 exarg_T ea;
6116
Bram Moolenaara80faa82020-04-12 19:37:17 +02006117 CLEAR_FIELD(ea);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006118 ea.cmdidx = CMD_tabnew;
6119 ea.cmd = (char_u *)"tabn";
6120 ea.arg = (char_u *)"";
6121 ex_splitview(&ea);
6122}
6123
6124/*
6125 * :tabnext command
6126 */
6127 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006128ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006129{
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006130 int tab_number;
6131
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02006132 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006133 return;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006134 switch (eap->cmdidx)
6135 {
6136 case CMD_tabfirst:
6137 case CMD_tabrewind:
6138 goto_tabpage(1);
6139 break;
6140 case CMD_tablast:
6141 goto_tabpage(9999);
6142 break;
6143 case CMD_tabprevious:
6144 case CMD_tabNext:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006145 if (eap->arg && *eap->arg != NUL)
6146 {
6147 char_u *p = eap->arg;
6148 char_u *p_save = p;
6149
6150 tab_number = getdigits(&p);
6151 if (p == p_save || *p_save == '-' || *p != NUL
6152 || tab_number == 0)
6153 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006154 // No numbers as argument.
Bram Moolenaar8930caa2020-07-23 16:37:03 +02006155 eap->errmsg = ex_errmsg(e_invarg2, eap->arg);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006156 return;
6157 }
6158 }
6159 else
6160 {
6161 if (eap->addr_count == 0)
6162 tab_number = 1;
6163 else
6164 {
6165 tab_number = eap->line2;
6166 if (tab_number < 1)
6167 {
Bram Moolenaar8930caa2020-07-23 16:37:03 +02006168 eap->errmsg = _(e_invrange);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006169 return;
6170 }
6171 }
6172 }
6173 goto_tabpage(-tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006174 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006175 default: // CMD_tabnext
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006176 tab_number = get_tabpage_arg(eap);
6177 if (eap->errmsg == NULL)
6178 goto_tabpage(tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006179 break;
6180 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006181}
6182
6183/*
6184 * :tabmove command
6185 */
6186 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006187ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006188{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02006189 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02006190
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006191 tab_number = get_tabpage_arg(eap);
6192 if (eap->errmsg == NULL)
6193 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006194}
6195
6196/*
6197 * :tabs command: List tabs and their contents.
6198 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006199 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006200ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006201{
6202 tabpage_T *tp;
6203 win_T *wp;
6204 int tabcount = 1;
6205
6206 msg_start();
6207 msg_scroll = TRUE;
6208 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
6209 {
6210 msg_putchar('\n');
6211 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
Bram Moolenaar8820b482017-03-16 17:23:31 +01006212 msg_outtrans_attr(IObuff, HL_ATTR(HLF_T));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006213 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006214 ui_breakcheck();
6215
Bram Moolenaar030f0df2006-02-21 22:02:53 +00006216 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006217 wp = firstwin;
6218 else
6219 wp = tp->tp_firstwin;
6220 for ( ; wp != NULL && !got_int; wp = wp->w_next)
6221 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006222 msg_putchar('\n');
6223 msg_putchar(wp == curwin ? '>' : ' ');
6224 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006225 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
6226 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006227 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02006228 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006229 else
6230 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
6231 IObuff, IOSIZE, TRUE);
6232 msg_outtrans(IObuff);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006233 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006234 ui_breakcheck();
6235 }
6236 }
6237}
6238
Bram Moolenaar071d4272004-06-13 20:20:40 +00006239/*
6240 * ":mode": Set screen mode.
6241 * If no argument given, just get the screen size and redraw.
6242 */
6243 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006244ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006245{
6246 if (*eap->arg == NUL)
6247 shell_resized();
6248 else
Bram Moolenaar3c71aec2020-01-17 19:32:20 +01006249 emsg(_(e_screenmode));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006250}
6251
Bram Moolenaar071d4272004-06-13 20:20:40 +00006252/*
6253 * ":resize".
6254 * set, increment or decrement current window height
6255 */
6256 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006257ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006258{
6259 int n;
6260 win_T *wp = curwin;
6261
6262 if (eap->addr_count > 0)
6263 {
6264 n = eap->line2;
6265 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
6266 ;
6267 }
6268
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006269# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006270 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006271# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006272 n = atol((char *)eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006273 if (cmdmod.split & WSP_VERT)
6274 {
6275 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar02631462017-09-22 15:20:32 +02006276 n += curwin->w_width;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006277 else if (n == 0 && eap->arg[0] == NUL) // default is very wide
Bram Moolenaar071d4272004-06-13 20:20:40 +00006278 n = 9999;
6279 win_setwidth_win((int)n, wp);
6280 }
6281 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006282 {
6283 if (*eap->arg == '-' || *eap->arg == '+')
6284 n += curwin->w_height;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006285 else if (n == 0 && eap->arg[0] == NUL) // default is very high
Bram Moolenaar071d4272004-06-13 20:20:40 +00006286 n = 9999;
6287 win_setheight_win((int)n, wp);
6288 }
6289}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006290
6291/*
6292 * ":find [+command] <file>" command.
6293 */
6294 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006295ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006296{
6297#ifdef FEAT_SEARCHPATH
6298 char_u *fname;
6299 int count;
6300
6301 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
6302 TRUE, curbuf->b_ffname);
6303 if (eap->addr_count > 0)
6304 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006305 // Repeat finding the file "count" times. This matters when it
6306 // appears several times in the path.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006307 count = eap->line2;
6308 while (fname != NULL && --count > 0)
6309 {
6310 vim_free(fname);
6311 fname = find_file_in_path(NULL, 0, FNAME_MESS,
6312 FALSE, curbuf->b_ffname);
6313 }
6314 }
6315
6316 if (fname != NULL)
6317 {
6318 eap->arg = fname;
6319#endif
6320 do_exedit(eap, NULL);
6321#ifdef FEAT_SEARCHPATH
6322 vim_free(fname);
6323 }
6324#endif
6325}
6326
6327/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00006328 * ":open" simulation: for now just work like ":visual".
6329 */
6330 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006331ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006332{
6333 regmatch_T regmatch;
6334 char_u *p;
6335
6336 curwin->w_cursor.lnum = eap->line2;
6337 beginline(BL_SOL | BL_FIX);
6338 if (*eap->arg == '/')
6339 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006340 // ":open /pattern/": put cursor in column found with pattern
Bram Moolenaardf177f62005-02-22 08:39:57 +00006341 ++eap->arg;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006342 p = skip_regexp(eap->arg, '/', p_magic);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006343 *p = NUL;
6344 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
6345 if (regmatch.regprog != NULL)
6346 {
6347 regmatch.rm_ic = p_ic;
6348 p = ml_get_curline();
6349 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006350 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006351 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006352 emsg(_(e_nomatch));
Bram Moolenaar473de612013-06-08 18:19:48 +02006353 vim_regfree(regmatch.regprog);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006354 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006355 // Move to the NUL, ignore any other arguments.
Bram Moolenaardf177f62005-02-22 08:39:57 +00006356 eap->arg += STRLEN(eap->arg);
6357 }
6358 check_cursor();
6359
6360 eap->cmdidx = CMD_visual;
6361 do_exedit(eap, NULL);
6362}
6363
6364/*
6365 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006366 */
6367 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006368ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006369{
6370 do_exedit(eap, NULL);
6371}
6372
6373/*
Bram Moolenaar4b96df52020-01-26 22:00:26 +01006374 * ":edit <file>" command and alike.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006375 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006376 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006377do_exedit(
6378 exarg_T *eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006379 win_T *old_curwin) // curwin before doing a split or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006380{
6381 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006382 int need_hide;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006383 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006384
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01006385 if ((eap->cmdidx != CMD_pedit && ERROR_IF_POPUP_WINDOW)
6386 || ERROR_IF_TERM_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006387 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006388 /*
6389 * ":vi" command ends Ex mode.
6390 */
6391 if (exmode_active && (eap->cmdidx == CMD_visual
6392 || eap->cmdidx == CMD_view))
6393 {
6394 exmode_active = FALSE;
Bram Moolenaar158ea172020-06-18 17:28:39 +02006395 ex_pressedreturn = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006396 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006397 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006398 // Special case: ":global/pat/visual\NLvi-commands"
Bram Moolenaardf177f62005-02-22 08:39:57 +00006399 if (global_busy)
6400 {
6401 int rd = RedrawingDisabled;
6402 int nwr = no_wait_return;
6403 int ms = msg_scroll;
6404#ifdef FEAT_GUI
6405 int he = hold_gui_events;
6406#endif
6407
6408 if (eap->nextcmd != NULL)
6409 {
6410 stuffReadbuff(eap->nextcmd);
6411 eap->nextcmd = NULL;
6412 }
6413
6414 if (exmode_was != EXMODE_VIM)
6415 settmode(TMODE_RAW);
6416 RedrawingDisabled = 0;
6417 no_wait_return = 0;
6418 need_wait_return = FALSE;
6419 msg_scroll = 0;
6420#ifdef FEAT_GUI
6421 hold_gui_events = 0;
6422#endif
6423 must_redraw = CLEAR;
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01006424 pending_exmode_active = TRUE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006425
6426 main_loop(FALSE, TRUE);
6427
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01006428 pending_exmode_active = FALSE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006429 RedrawingDisabled = rd;
6430 no_wait_return = nwr;
6431 msg_scroll = ms;
6432#ifdef FEAT_GUI
6433 hold_gui_events = he;
6434#endif
6435 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006436 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006437 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006438 }
6439
6440 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006441 || eap->cmdidx == CMD_tabnew
6442 || eap->cmdidx == CMD_tabedit
Bram Moolenaar4033c552017-09-16 20:54:51 +02006443 || eap->cmdidx == CMD_vnew) && *eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006444 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006445 // ":new" or ":tabnew" without argument: edit an new empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006446 setpcmark();
6447 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006448 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
6449 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006450 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006451 else if ((eap->cmdidx != CMD_split && eap->cmdidx != CMD_vsplit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006452 || *eap->arg != NUL
6453#ifdef FEAT_BROWSE
6454 || cmdmod.browse
6455#endif
6456 )
6457 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006458 // Can't edit another file when "curbuf_lock" is set. Only ":edit"
6459 // can bring us here, others are stopped earlier.
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006460 if (*eap->arg != NUL && curbuf_locked())
6461 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006462
Bram Moolenaar071d4272004-06-13 20:20:40 +00006463 n = readonlymode;
6464 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
6465 readonlymode = TRUE;
6466 else if (eap->cmdidx == CMD_enew)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006467 readonlymode = FALSE; // 'readonly' doesn't make sense in an
6468 // empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006469 setpcmark();
6470 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
6471 NULL, eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006472 // ":edit" goes to first line if Vi compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00006473 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
6474 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
6475 ? ECMD_ONE : eap->do_ecmd_lnum,
Bram Moolenaareb44a682017-08-03 22:44:55 +02006476 (buf_hide(curbuf) ? ECMD_HIDE : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006477 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006478 // after a split we can use an existing buffer
Bram Moolenaar7b449342014-03-25 13:03:48 +01006479 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006480 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006481 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006482 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006483 // Editing the file failed. If the window was split, close it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006484 if (old_curwin != NULL)
6485 {
6486 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02006487 if (!need_hide || buf_hide(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006488 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006489#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006490 cleanup_T cs;
6491
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006492 // Reset the error/interrupt/exception state here so that
6493 // aborting() returns FALSE when closing a window.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006494 enter_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02006495#endif
6496#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006497 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006498#endif
Bram Moolenaareb44a682017-08-03 22:44:55 +02006499 win_close(curwin, !need_hide && !buf_hide(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006500
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006501#if defined(FEAT_EVAL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006502 // Restore the error/interrupt/exception state if not
6503 // discarded by a new aborting error, interrupt, or
6504 // uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006505 leave_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02006506#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006507 }
6508 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006509 }
6510 else if (readonlymode && curbuf->b_nwindows == 1)
6511 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006512 // When editing an already visited buffer, 'readonly' won't be set
6513 // but the previous value is kept. With ":view" and ":sview" we
6514 // want the file to be readonly, except when another window is
6515 // editing the same buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006516 curbuf->b_p_ro = TRUE;
6517 }
6518 readonlymode = n;
6519 }
6520 else
6521 {
6522 if (eap->do_ecmd_cmd != NULL)
6523 do_cmdline_cmd(eap->do_ecmd_cmd);
6524#ifdef FEAT_TITLE
6525 n = curwin->w_arg_idx_invalid;
6526#endif
6527 check_arg_idx(curwin);
6528#ifdef FEAT_TITLE
6529 if (n != curwin->w_arg_idx_invalid)
6530 maketitle();
6531#endif
6532 }
6533
Bram Moolenaar071d4272004-06-13 20:20:40 +00006534 /*
6535 * if ":split file" worked, set alternate file name in old window to new
6536 * file
6537 */
6538 if (old_curwin != NULL
6539 && *eap->arg != NUL
6540 && curwin != old_curwin
6541 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00006542 && old_curwin->w_buffer != curbuf
6543 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006544 old_curwin->w_alt_fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006545
6546 ex_no_reprint = TRUE;
6547}
6548
6549#ifndef FEAT_GUI
6550/*
6551 * ":gui" and ":gvim" when there is no GUI.
6552 */
6553 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006554ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006555{
Bram Moolenaar8930caa2020-07-23 16:37:03 +02006556 eap->errmsg = _(e_nogvim);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006557}
6558#endif
6559
Bram Moolenaar4f974752019-02-17 17:44:42 +01006560#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006561 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006562ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006563{
6564 gui_make_tearoff(eap->arg);
6565}
6566#endif
6567
Bram Moolenaar29a2c082018-03-05 21:06:23 +01006568#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
6569 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006570 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006571ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006572{
Bram Moolenaar29a2c082018-03-05 21:06:23 +01006573# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
6574 if (gui.in_use)
6575 gui_make_popup(eap->arg, eap->forceit);
6576# ifdef FEAT_TERM_POPUP_MENU
6577 else
6578# endif
6579# endif
6580# ifdef FEAT_TERM_POPUP_MENU
6581 pum_make_popup(eap->arg, eap->forceit);
6582# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006583}
6584#endif
6585
Bram Moolenaar071d4272004-06-13 20:20:40 +00006586 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006587ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006588{
6589 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006590 msg(_("No swap file"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006591 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01006592 msg((char *)curbuf->b_ml.ml_mfp->mf_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006593}
6594
6595/*
6596 * ":syncbind" forces all 'scrollbind' windows to have the same relative
6597 * offset.
6598 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
6599 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006600 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006601ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006602{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006603 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006604 win_T *save_curwin = curwin;
6605 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006606 long topline;
6607 long y;
6608 linenr_T old_linenr = curwin->w_cursor.lnum;
6609
6610 setpcmark();
6611
6612 /*
6613 * determine max topline
6614 */
6615 if (curwin->w_p_scb)
6616 {
6617 topline = curwin->w_topline;
Bram Moolenaar29323592016-07-24 22:04:11 +02006618 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006619 {
6620 if (wp->w_p_scb && wp->w_buffer)
6621 {
Bram Moolenaar375e3392019-01-31 18:26:10 +01006622 y = wp->w_buffer->b_ml.ml_line_count - get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006623 if (topline > y)
6624 topline = y;
6625 }
6626 }
6627 if (topline < 1)
6628 topline = 1;
6629 }
6630 else
6631 {
6632 topline = 1;
6633 }
6634
6635
6636 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006637 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006638 */
Bram Moolenaar29323592016-07-24 22:04:11 +02006639 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006640 {
6641 if (curwin->w_p_scb)
6642 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006643 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006644 y = topline - curwin->w_topline;
6645 if (y > 0)
6646 scrollup(y, TRUE);
6647 else
6648 scrolldown(-y, TRUE);
6649 curwin->w_scbind_pos = topline;
6650 redraw_later(VALID);
6651 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006652 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006653 }
6654 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006655 curwin = save_curwin;
6656 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006657 if (curwin->w_p_scb)
6658 {
6659 did_syncbind = TRUE;
6660 checkpcmark();
6661 if (old_linenr != curwin->w_cursor.lnum)
6662 {
6663 char_u ctrl_o[2];
6664
6665 ctrl_o[0] = Ctrl_O;
6666 ctrl_o[1] = 0;
6667 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
6668 }
6669 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006670}
6671
6672
6673 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006674ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006675{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006676 int i;
6677 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
6678 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006679
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006680 if (eap->usefilter) // :r!cmd
Bram Moolenaar071d4272004-06-13 20:20:40 +00006681 do_bang(1, eap, FALSE, FALSE, TRUE);
6682 else
6683 {
6684 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
6685 return;
6686
6687#ifdef FEAT_BROWSE
6688 if (cmdmod.browse)
6689 {
6690 char_u *browseFile;
6691
Bram Moolenaar7171abe2004-10-11 10:06:20 +00006692 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006693 NULL, NULL, NULL, curbuf);
6694 if (browseFile != NULL)
6695 {
6696 i = readfile(browseFile, NULL,
6697 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
6698 vim_free(browseFile);
6699 }
6700 else
6701 i = OK;
6702 }
6703 else
6704#endif
6705 if (*eap->arg == NUL)
6706 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006707 if (check_fname() == FAIL) // check for no file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00006708 return;
6709 i = readfile(curbuf->b_ffname, curbuf->b_fname,
6710 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
6711 }
6712 else
6713 {
6714 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
6715 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
6716 i = readfile(eap->arg, NULL,
6717 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
6718
6719 }
Bram Moolenaare13b9af2017-01-13 22:01:02 +01006720 if (i != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006721 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006722#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006723 if (!aborting())
6724#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006725 semsg(_(e_notopen), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006726 }
6727 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00006728 {
6729 if (empty && exmode_active)
6730 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006731 // Delete the empty line that remains. Historically ex does
6732 // this but vi doesn't.
Bram Moolenaardf177f62005-02-22 08:39:57 +00006733 if (eap->line2 == 0)
6734 lnum = curbuf->b_ml.ml_line_count;
6735 else
6736 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00006737 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006738 {
Bram Moolenaarca70c072020-05-30 20:30:46 +02006739 ml_delete(lnum);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00006740 if (curwin->w_cursor.lnum > 1
6741 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006742 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00006743 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006744 }
6745 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006746 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006747 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006748 }
6749}
6750
Bram Moolenaarea408852005-06-25 22:49:46 +00006751static char_u *prev_dir = NULL;
6752
6753#if defined(EXITFREE) || defined(PROTO)
6754 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006755free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00006756{
Bram Moolenaard23a8232018-02-10 18:45:26 +01006757 VIM_CLEAR(prev_dir);
6758 VIM_CLEAR(globaldir);
Bram Moolenaarea408852005-06-25 22:49:46 +00006759}
6760#endif
6761
Bram Moolenaarf4258302013-06-02 18:20:17 +02006762/*
Bram Moolenaar002bc792020-06-05 22:33:42 +02006763 * Get the previous directory for the given chdir scope.
6764 */
6765 static char_u *
6766get_prevdir(cdscope_T scope)
6767{
6768 if (scope == CDSCOPE_WINDOW)
6769 return curwin->w_prevdir;
6770 else if (scope == CDSCOPE_TABPAGE)
6771 return curtab->tp_prevdir;
6772 return prev_dir;
6773}
6774
6775/*
Bram Moolenaarf4258302013-06-02 18:20:17 +02006776 * Deal with the side effects of changing the current directory.
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006777 * When 'scope' is CDSCOPE_TABPAGE then this was after an ":tcd" command.
6778 * When 'scope' is CDSCOPE_WINDOW then this was after an ":lcd" command.
Bram Moolenaarf4258302013-06-02 18:20:17 +02006779 */
6780 void
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006781post_chdir(cdscope_T scope)
Bram Moolenaarf4258302013-06-02 18:20:17 +02006782{
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006783 if (scope != CDSCOPE_WINDOW)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006784 // Clear tab local directory for both :cd and :tcd
6785 VIM_CLEAR(curtab->tp_localdir);
Bram Moolenaard23a8232018-02-10 18:45:26 +01006786 VIM_CLEAR(curwin->w_localdir);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006787 if (scope != CDSCOPE_GLOBAL)
Bram Moolenaarf4258302013-06-02 18:20:17 +02006788 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02006789 char_u *pdir = get_prevdir(scope);
6790
6791 // If still in the global directory, need to remember current
6792 // directory as the global directory.
6793 if (globaldir == NULL && pdir != NULL)
6794 globaldir = vim_strsave(pdir);
6795
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006796 // Remember this local directory for the window.
Bram Moolenaarf4258302013-06-02 18:20:17 +02006797 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006798 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006799 if (scope == CDSCOPE_TABPAGE)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006800 curtab->tp_localdir = vim_strsave(NameBuff);
6801 else
6802 curwin->w_localdir = vim_strsave(NameBuff);
6803 }
Bram Moolenaarf4258302013-06-02 18:20:17 +02006804 }
6805 else
6806 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02006807 // We are now in the global directory, no need to remember its name.
Bram Moolenaard23a8232018-02-10 18:45:26 +01006808 VIM_CLEAR(globaldir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006809 }
6810
6811 shorten_fnames(TRUE);
6812}
6813
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006814/*
6815 * Change directory function used by :cd/:tcd/:lcd Ex commands and the
Bram Moolenaar95058722020-06-01 16:26:19 +02006816 * chdir() function.
6817 * scope == CDSCOPE_WINDOW: changes the window-local directory
6818 * scope == CDSCOPE_TABPAGE: changes the tab-local directory
6819 * Otherwise: changes the global directory
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006820 * Returns TRUE if the directory is successfully changed.
6821 */
6822 int
6823changedir_func(
6824 char_u *new_dir,
6825 int forceit,
6826 cdscope_T scope)
6827{
6828 char_u *tofree;
Bram Moolenaar002bc792020-06-05 22:33:42 +02006829 char_u *pdir = NULL;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006830 int dir_differs;
6831 int retval = FALSE;
6832
Bram Moolenaar7cc96922020-01-31 22:41:38 +01006833 if (new_dir == NULL || allbuf_locked())
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006834 return FALSE;
6835
6836 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged() && !forceit)
6837 {
6838 emsg(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
6839 return FALSE;
6840 }
6841
6842 // ":cd -": Change to previous directory
6843 if (STRCMP(new_dir, "-") == 0)
6844 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02006845 pdir = get_prevdir(scope);
6846 if (pdir == NULL)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006847 {
6848 emsg(_("E186: No previous directory"));
6849 return FALSE;
6850 }
Bram Moolenaar002bc792020-06-05 22:33:42 +02006851 new_dir = pdir;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006852 }
6853
Bram Moolenaar002bc792020-06-05 22:33:42 +02006854 // Free the previous directory
6855 tofree = get_prevdir(scope);
6856
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006857 // Save current directory for next ":cd -"
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006858 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar002bc792020-06-05 22:33:42 +02006859 pdir = vim_strsave(NameBuff);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006860 else
Bram Moolenaar002bc792020-06-05 22:33:42 +02006861 pdir = NULL;
6862 if (scope == CDSCOPE_WINDOW)
6863 curwin->w_prevdir = pdir;
6864 else if (scope == CDSCOPE_TABPAGE)
6865 curtab->tp_prevdir = pdir;
6866 else
6867 prev_dir = pdir;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006868
6869#if defined(UNIX) || defined(VMS)
6870 // for UNIX ":cd" means: go to home directory
6871 if (*new_dir == NUL)
6872 {
6873 // use NameBuff for home directory name
6874# ifdef VMS
6875 char_u *p;
6876
6877 p = mch_getenv((char_u *)"SYS$LOGIN");
6878 if (p == NULL || *p == NUL) // empty is the same as not set
6879 NameBuff[0] = NUL;
6880 else
6881 vim_strncpy(NameBuff, p, MAXPATHL - 1);
6882# else
6883 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
6884# endif
6885 new_dir = NameBuff;
6886 }
6887#endif
Bram Moolenaar002bc792020-06-05 22:33:42 +02006888 dir_differs = new_dir == NULL || pdir == NULL
6889 || pathcmp((char *)pdir, (char *)new_dir, -1) != 0;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006890 if (new_dir == NULL || (dir_differs && vim_chdir(new_dir)))
6891 emsg(_(e_failed));
6892 else
6893 {
6894 char_u *acmd_fname;
6895
6896 post_chdir(scope);
6897
6898 if (dir_differs)
6899 {
6900 if (scope == CDSCOPE_WINDOW)
6901 acmd_fname = (char_u *)"window";
6902 else if (scope == CDSCOPE_TABPAGE)
6903 acmd_fname = (char_u *)"tabpage";
6904 else
6905 acmd_fname = (char_u *)"global";
6906 apply_autocmds(EVENT_DIRCHANGED, acmd_fname, new_dir, FALSE,
6907 curbuf);
6908 }
6909 retval = TRUE;
6910 }
6911 vim_free(tofree);
6912
6913 return retval;
6914}
Bram Moolenaarea408852005-06-25 22:49:46 +00006915
Bram Moolenaar071d4272004-06-13 20:20:40 +00006916/*
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006917 * ":cd", ":tcd", ":lcd", ":chdir" ":tchdir" and ":lchdir".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006918 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00006919 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006920ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006921{
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01006922 char_u *new_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006923
6924 new_dir = eap->arg;
6925#if !defined(UNIX) && !defined(VMS)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006926 // for non-UNIX ":cd" means: print current directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006927 if (*new_dir == NUL)
6928 ex_pwd(NULL);
6929 else
6930#endif
6931 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006932 cdscope_T scope = CDSCOPE_GLOBAL;
6933
6934 if (eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir)
6935 scope = CDSCOPE_WINDOW;
6936 else if (eap->cmdidx == CMD_tcd || eap->cmdidx == CMD_tchdir)
6937 scope = CDSCOPE_TABPAGE;
6938
6939 if (changedir_func(new_dir, eap->forceit, scope))
Bram Moolenaardf177f62005-02-22 08:39:57 +00006940 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006941 // Echo the new current directory if the command was typed.
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00006942 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006943 ex_pwd(eap);
6944 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006945 }
6946}
6947
6948/*
6949 * ":pwd".
6950 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006951 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006952ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006953{
6954 if (mch_dirname(NameBuff, MAXPATHL) == OK)
6955 {
6956#ifdef BACKSLASH_IN_FILENAME
6957 slash_adjust(NameBuff);
6958#endif
Bram Moolenaar95058722020-06-01 16:26:19 +02006959 if (p_verbose > 0)
6960 {
6961 char *context = "global";
6962
6963 if (curwin->w_localdir != NULL)
6964 context = "window";
6965 else if (curtab->tp_localdir != NULL)
6966 context = "tabpage";
6967 smsg("[%s] %s", context, (char *)NameBuff);
6968 }
6969 else
6970 msg((char *)NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006971 }
6972 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006973 emsg(_("E187: Unknown"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006974}
6975
6976/*
6977 * ":=".
6978 */
6979 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006980ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006981{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006982 smsg("%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006983 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006984}
6985
6986 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006987ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006988{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006989 int n;
6990 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006991
6992 if (cursor_valid())
6993 {
6994 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
6995 if (n >= 0)
Bram Moolenaar53f81742017-09-22 14:35:51 +02006996 windgoto((int)n, curwin->w_wincol + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006997 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006998
6999 len = eap->line2;
7000 switch (*eap->arg)
7001 {
7002 case 'm': break;
7003 case NUL: len *= 1000L; break;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007004 default: semsg(_(e_invarg2), eap->arg); return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007005 }
7006 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007007}
7008
7009/*
7010 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
7011 */
7012 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007013do_sleep(long msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007014{
Bram Moolenaar52953192019-08-16 10:27:13 +02007015 long done = 0;
Bram Moolenaar975b5272016-03-15 23:10:59 +01007016 long wait_now;
Bram Moolenaar52953192019-08-16 10:27:13 +02007017# ifdef ELAPSED_FUNC
7018 elapsed_T start_tv;
7019
7020 // Remember at what time we started, so that we know how much longer we
7021 // should wait after waiting for a bit.
7022 ELAPSED_INIT(start_tv);
7023# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007024
7025 cursor_on();
Bram Moolenaarf45d7472018-09-30 18:22:26 +02007026 out_flush_cursor(FALSE, FALSE);
Bram Moolenaar52953192019-08-16 10:27:13 +02007027 while (!got_int && done < msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007028 {
Bram Moolenaar975b5272016-03-15 23:10:59 +01007029 wait_now = msec - done > 1000L ? 1000L : msec - done;
7030#ifdef FEAT_TIMERS
7031 {
7032 long due_time = check_due_timer();
7033
7034 if (due_time > 0 && due_time < wait_now)
7035 wait_now = due_time;
7036 }
7037#endif
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007038#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar28e67e02019-08-15 23:05:49 +02007039 if (has_any_channel() && wait_now > 20L)
7040 wait_now = 20L;
7041#endif
7042#ifdef FEAT_SOUND
7043 if (has_any_sound_callback() && wait_now > 20L)
7044 wait_now = 20L;
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007045#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +01007046 ui_delay(wait_now, TRUE);
Bram Moolenaar52953192019-08-16 10:27:13 +02007047
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007048#ifdef FEAT_JOB_CHANNEL
7049 if (has_any_channel())
7050 ui_breakcheck_force(TRUE);
7051 else
7052#endif
7053 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007054#ifdef MESSAGE_QUEUE
Bram Moolenaar52953192019-08-16 10:27:13 +02007055 // Process the netbeans and clientserver messages that may have been
7056 // received in the call to ui_breakcheck() when the GUI is in use. This
7057 // may occur when running a test case.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007058 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02007059#endif
Bram Moolenaar52953192019-08-16 10:27:13 +02007060
7061# ifdef ELAPSED_FUNC
7062 // actual time passed
7063 done = ELAPSED_FUNC(start_tv);
7064# else
7065 // guestimate time passed (will actually be more)
7066 done += wait_now;
7067# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007068 }
Bram Moolenaar1ebff3d2018-07-07 16:18:13 +02007069
7070 // If CTRL-C was typed to interrupt the sleep, drop the CTRL-C from the
7071 // input buffer, otherwise a following call to input() fails.
7072 if (got_int)
7073 (void)vpeekc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007074}
7075
Bram Moolenaar071d4272004-06-13 20:20:40 +00007076/*
7077 * ":winsize" command (obsolete).
7078 */
7079 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007080ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007081{
7082 int w, h;
7083 char_u *arg = eap->arg;
7084 char_u *p;
7085
7086 w = getdigits(&arg);
7087 arg = skipwhite(arg);
7088 p = arg;
7089 h = getdigits(&arg);
7090 if (*p != NUL && *arg == NUL)
7091 set_shellsize(w, h, TRUE);
7092 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007093 emsg(_("E465: :winsize requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007094}
7095
Bram Moolenaar071d4272004-06-13 20:20:40 +00007096 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007097ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007098{
7099 int xchar = NUL;
7100 char_u *p;
7101
7102 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
7103 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007104 // CTRL-W g and CTRL-W CTRL-G have an extra command character
Bram Moolenaar071d4272004-06-13 20:20:40 +00007105 if (eap->arg[1] == NUL)
7106 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007107 emsg(_(e_invarg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007108 return;
7109 }
7110 xchar = eap->arg[1];
7111 p = eap->arg + 2;
7112 }
7113 else
7114 p = eap->arg + 1;
7115
7116 eap->nextcmd = check_nextcmd(p);
7117 p = skipwhite(p);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02007118 if (*p != NUL && *p != (
7119#ifdef FEAT_EVAL
7120 in_vim9script() ? '#' :
7121#endif
7122 '"')
7123 && eap->nextcmd == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007124 emsg(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02007125 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007126 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007127 // Pass flags on for ":vertical wincmd ]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007128 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00007129 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007130 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
7131 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00007132 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007133 }
7134}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007135
Bram Moolenaar843ee412004-06-30 16:16:41 +00007136#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007137/*
7138 * ":winpos".
7139 */
7140 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007141ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007142{
7143 int x, y;
7144 char_u *arg = eap->arg;
7145 char_u *p;
7146
7147 if (*arg == NUL)
7148 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00007149# if defined(FEAT_GUI) || defined(MSWIN)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007150# ifdef VIMDLL
7151 if (gui.in_use ? gui_mch_get_winpos(&x, &y) != FAIL :
7152 mch_get_winpos(&x, &y) != FAIL)
7153# elif defined(FEAT_GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007154 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00007155# else
7156 if (mch_get_winpos(&x, &y) != FAIL)
7157# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007158 {
7159 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
Bram Moolenaar32526b32019-01-19 17:43:09 +01007160 msg((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007161 }
7162 else
7163# endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007164 emsg(_("E188: Obtaining window position not implemented for this platform"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007165 }
7166 else
7167 {
7168 x = getdigits(&arg);
7169 arg = skipwhite(arg);
7170 p = arg;
7171 y = getdigits(&arg);
7172 if (*p == NUL || *arg != NUL)
7173 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007174 emsg(_("E466: :winpos requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007175 return;
7176 }
7177# ifdef FEAT_GUI
7178 if (gui.in_use)
7179 gui_mch_set_winpos(x, y);
7180 else if (gui.starting)
7181 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007182 // Remember the coordinates for when the window is opened.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007183 gui_win_x = x;
7184 gui_win_y = y;
7185 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007186# if defined(HAVE_TGETENT) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007187 else
7188# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007189# endif
7190# if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar843ee412004-06-30 16:16:41 +00007191 mch_set_winpos(x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007192# endif
7193# ifdef HAVE_TGETENT
7194 if (*T_CWP)
7195 term_set_winpos(x, y);
7196# endif
7197 }
7198}
7199#endif
7200
7201/*
7202 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
7203 */
7204 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007205ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007206{
7207 oparg_T oa;
7208
7209 clear_oparg(&oa);
7210 oa.regname = eap->regname;
7211 oa.start.lnum = eap->line1;
7212 oa.end.lnum = eap->line2;
7213 oa.line_count = eap->line2 - eap->line1 + 1;
7214 oa.motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007215 virtual_op = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007216 if (eap->cmdidx != CMD_yank) // position cursor for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00007217 {
7218 setpcmark();
7219 curwin->w_cursor.lnum = eap->line1;
7220 beginline(BL_SOL | BL_FIX);
7221 }
7222
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007223 if (VIsual_active)
7224 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007225
Bram Moolenaar071d4272004-06-13 20:20:40 +00007226 switch (eap->cmdidx)
7227 {
7228 case CMD_delete:
7229 oa.op_type = OP_DELETE;
7230 op_delete(&oa);
7231 break;
7232
7233 case CMD_yank:
7234 oa.op_type = OP_YANK;
7235 (void)op_yank(&oa, FALSE, TRUE);
7236 break;
7237
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007238 default: // CMD_rshift or CMD_lshift
Bram Moolenaar6e707362013-06-14 19:15:58 +02007239 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007240#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02007241 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
7242#else
7243 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00007244#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02007245 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007246 oa.op_type = OP_RSHIFT;
7247 else
7248 oa.op_type = OP_LSHIFT;
7249 op_shift(&oa, FALSE, eap->amount);
7250 break;
7251 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007252 virtual_op = MAYBE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007253 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007254}
7255
7256/*
7257 * ":put".
7258 */
7259 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007260ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007261{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007262 // ":0put" works like ":1put!".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007263 if (eap->line2 == 0)
7264 {
7265 eap->line2 = 1;
7266 eap->forceit = TRUE;
7267 }
7268 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007269 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
7270 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007271}
7272
7273/*
7274 * Handle ":copy" and ":move".
7275 */
7276 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007277ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007278{
7279 long n;
7280
Bram Moolenaar491799b2020-08-01 19:23:43 +02007281#ifdef FEAT_EVAL
Bram Moolenaarf5a48012020-08-01 17:00:03 +02007282 if (not_in_vim9(eap) == FAIL)
7283 return;
Bram Moolenaar491799b2020-08-01 19:23:43 +02007284#endif
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02007285 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE, FALSE, 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007286 if (eap->arg == NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00007287 {
7288 eap->nextcmd = NULL;
7289 return;
7290 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00007291 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007292
7293 /*
7294 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
7295 */
7296 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
7297 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02007298 emsg(_(e_invrange));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007299 return;
7300 }
7301
7302 if (eap->cmdidx == CMD_move)
7303 {
7304 if (do_move(eap->line1, eap->line2, n) == FAIL)
7305 return;
7306 }
7307 else
7308 ex_copy(eap->line1, eap->line2, n);
7309 u_clearline();
7310 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007311 ex_may_print(eap);
7312}
7313
7314/*
7315 * Print the current line if flags were given to the Ex command.
7316 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02007317 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007318ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007319{
7320 if (eap->flags != 0)
7321 {
7322 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
7323 (eap->flags & EXFLAG_LIST));
7324 ex_no_reprint = TRUE;
7325 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007326}
7327
7328/*
7329 * ":smagic" and ":snomagic".
7330 */
7331 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007332ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007333{
7334 int magic_save = p_magic;
7335
7336 p_magic = (eap->cmdidx == CMD_smagic);
7337 do_sub(eap);
7338 p_magic = magic_save;
7339}
7340
7341/*
7342 * ":join".
7343 */
7344 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007345ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007346{
7347 curwin->w_cursor.lnum = eap->line1;
7348 if (eap->line1 == eap->line2)
7349 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007350 if (eap->addr_count >= 2) // :2,2join does nothing
Bram Moolenaar071d4272004-06-13 20:20:40 +00007351 return;
7352 if (eap->line2 == curbuf->b_ml.ml_line_count)
7353 {
7354 beep_flush();
7355 return;
7356 }
7357 ++eap->line2;
7358 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02007359 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007360 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007361 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007362}
7363
7364/*
7365 * ":[addr]@r" or ":[addr]*r": execute register
7366 */
7367 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007368ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007369{
7370 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00007371 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007372
7373 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4930a762016-09-11 14:39:53 +02007374 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007375
7376#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007377 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00007378#endif
7379
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007380 // get the register name. No name means to use the previous one
Bram Moolenaar071d4272004-06-13 20:20:40 +00007381 c = *eap->arg;
7382 if (c == NUL || (c == '*' && *eap->cmd == '*'))
7383 c = '@';
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007384 // Put the register in the typeahead buffer with the "silent" flag.
Bram Moolenaard333d1e2006-11-07 17:43:47 +00007385 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
7386 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007387 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007388 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00007389 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007390 else
7391 {
7392 int save_efr = exec_from_reg;
7393
7394 exec_from_reg = TRUE;
7395
7396 /*
7397 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00007398 * Continue until the stuff buffer is empty and all added characters
7399 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007400 */
Bram Moolenaar60462872009-11-03 11:40:19 +00007401 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007402 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
7403
7404 exec_from_reg = save_efr;
7405 }
7406}
7407
7408/*
7409 * ":!".
7410 */
7411 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007412ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007413{
7414 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
7415}
7416
7417/*
7418 * ":undo".
7419 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007420 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01007421ex_undo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007422{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007423 if (eap->addr_count == 1) // :undo 123
Bram Moolenaar730cde92010-06-27 05:18:54 +02007424 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00007425 else
7426 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007427}
7428
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007429#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02007430 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007431ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007432{
7433 char_u hash[UNDO_HASH_SIZE];
7434
7435 u_compute_hash(hash);
7436 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
7437}
7438
Bram Moolenaar6df6f472010-07-18 18:04:50 +02007439 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007440ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007441{
7442 char_u hash[UNDO_HASH_SIZE];
7443
7444 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02007445 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007446}
7447#endif
7448
Bram Moolenaar071d4272004-06-13 20:20:40 +00007449/*
7450 * ":redo".
7451 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007452 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007453ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007454{
7455 u_redo(1);
7456}
7457
7458/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007459 * ":earlier" and ":later".
7460 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007461 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007462ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007463{
7464 long count = 0;
7465 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02007466 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007467 char_u *p = eap->arg;
7468
7469 if (*p == NUL)
7470 count = 1;
7471 else if (isdigit(*p))
7472 {
7473 count = getdigits(&p);
7474 switch (*p)
7475 {
7476 case 's': ++p; sec = TRUE; break;
7477 case 'm': ++p; sec = TRUE; count *= 60; break;
7478 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02007479 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
7480 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007481 }
7482 }
7483
7484 if (*p != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007485 semsg(_(e_invarg2), eap->arg);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007486 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02007487 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
7488 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007489}
7490
7491/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007492 * ":redir": start/stop redirection.
7493 */
7494 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007495ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007496{
7497 char *mode;
7498 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00007499 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007500
Bram Moolenaarba768492016-07-08 15:32:54 +02007501#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02007502 if (redir_execute)
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007503 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007504 emsg(_("E930: Cannot use :redir inside execute()"));
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007505 return;
7506 }
Bram Moolenaarba768492016-07-08 15:32:54 +02007507#endif
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007508
Bram Moolenaar071d4272004-06-13 20:20:40 +00007509 if (STRICMP(eap->arg, "END") == 0)
7510 close_redir();
7511 else
7512 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007513 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007514 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007515 ++arg;
7516 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007517 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007518 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007519 mode = "a";
7520 }
7521 else
7522 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00007523 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007524
7525 close_redir();
7526
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007527 // Expand environment variables and "~/".
Bram Moolenaarca472992005-01-21 11:46:23 +00007528 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007529 if (fname == NULL)
7530 return;
7531#ifdef FEAT_BROWSE
7532 if (cmdmod.browse)
7533 {
7534 char_u *browseFile;
7535
Bram Moolenaar7171abe2004-10-11 10:06:20 +00007536 browseFile = do_browse(BROWSE_SAVE,
7537 (char_u *)_("Save Redirection"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +02007538 fname, NULL, NULL,
7539 (char_u *)_(BROWSE_FILTER_ALL_FILES), curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007540 if (browseFile == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007541 return; // operation cancelled
Bram Moolenaar071d4272004-06-13 20:20:40 +00007542 vim_free(fname);
7543 fname = browseFile;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007544 eap->forceit = TRUE; // since dialog already asked
Bram Moolenaar071d4272004-06-13 20:20:40 +00007545 }
7546#endif
7547
7548 redir_fd = open_exfile(fname, eap->forceit, mode);
7549 vim_free(fname);
7550 }
7551#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00007552 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007553 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007554 // redirect to a register a-z (resp. A-Z for appending)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007555 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00007556 ++arg;
7557 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007558# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00007559 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00007560 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00007561# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00007562 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007563 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007564 redir_reg = *arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007565 if (*arg == '>' && arg[1] == '>') // append
Bram Moolenaard9d30582005-05-18 22:10:28 +00007566 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00007567 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007568 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007569 // Can use both "@a" and "@a>".
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00007570 if (*arg == '>')
7571 arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007572 // Make register empty when not using @A-@Z and the
7573 // command is valid.
Bram Moolenaarc188b882007-10-19 14:20:54 +00007574 if (*arg == NUL && !isupper(redir_reg))
7575 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007576 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00007577 }
7578 if (*arg != NUL)
7579 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007580 redir_reg = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007581 semsg(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007582 }
7583 }
7584 else if (*arg == '=' && arg[1] == '>')
7585 {
7586 int append;
7587
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007588 // redirect to a variable
Bram Moolenaardf177f62005-02-22 08:39:57 +00007589 close_redir();
7590 arg += 2;
7591
7592 if (*arg == '>')
7593 {
7594 ++arg;
7595 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007596 }
7597 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00007598 append = FALSE;
7599
7600 if (var_redir_start(skipwhite(arg), append) == OK)
7601 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007602 }
7603#endif
7604
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007605 // TODO: redirect to a buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00007606
Bram Moolenaar071d4272004-06-13 20:20:40 +00007607 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007608 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007609 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00007610
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007611 // Make sure redirection is not off. Can happen for cmdline completion
7612 // that indirectly invokes a command to catch its output.
Bram Moolenaar29b2d262006-09-10 19:07:28 +00007613 if (redir_fd != NULL
7614#ifdef FEAT_EVAL
7615 || redir_reg || redir_vname
7616#endif
7617 )
7618 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007619}
7620
7621/*
7622 * ":redraw": force redraw
7623 */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01007624 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007625ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007626{
7627 int r = RedrawingDisabled;
7628 int p = p_lz;
7629
7630 RedrawingDisabled = 0;
7631 p_lz = FALSE;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01007632 validate_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007633 update_topline();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007634 update_screen(eap->forceit ? CLEAR : VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007635#ifdef FEAT_TITLE
7636 if (need_maketitle)
7637 maketitle();
7638#endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007639#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
7640# ifdef VIMDLL
7641 if (!gui.in_use)
7642# endif
7643 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01007644#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007645 RedrawingDisabled = r;
7646 p_lz = p;
7647
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007648 // Reset msg_didout, so that a message that's there is overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007649 msg_didout = FALSE;
7650 msg_col = 0;
7651
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007652 // No need to wait after an intentional redraw.
Bram Moolenaar56667a52013-07-17 11:54:28 +02007653 need_wait_return = FALSE;
7654
Bram Moolenaar071d4272004-06-13 20:20:40 +00007655 out_flush();
7656}
7657
7658/*
7659 * ":redrawstatus": force redraw of status line(s)
7660 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007661 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007662ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007663{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007664 int r = RedrawingDisabled;
7665 int p = p_lz;
7666
7667 RedrawingDisabled = 0;
7668 p_lz = FALSE;
7669 if (eap->forceit)
7670 status_redraw_all();
7671 else
7672 status_redraw_curbuf();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007673 update_screen(VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007674 RedrawingDisabled = r;
7675 p_lz = p;
7676 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007677}
7678
Bram Moolenaare12bab32019-01-08 22:02:56 +01007679/*
7680 * ":redrawtabline": force redraw of the tabline
7681 */
7682 static void
7683ex_redrawtabline(exarg_T *eap UNUSED)
7684{
7685 int r = RedrawingDisabled;
7686 int p = p_lz;
7687
7688 RedrawingDisabled = 0;
7689 p_lz = FALSE;
7690
7691 draw_tabline();
7692
7693 RedrawingDisabled = r;
7694 p_lz = p;
7695 out_flush();
7696}
7697
Bram Moolenaar071d4272004-06-13 20:20:40 +00007698 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007699close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007700{
7701 if (redir_fd != NULL)
7702 {
7703 fclose(redir_fd);
7704 redir_fd = NULL;
7705 }
7706#ifdef FEAT_EVAL
7707 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007708 if (redir_vname)
7709 {
7710 var_redir_stop();
7711 redir_vname = 0;
7712 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007713#endif
7714}
7715
Bram Moolenaarf96ae0b2019-07-28 15:21:55 +02007716#if (defined(FEAT_SESSION) || defined(FEAT_EVAL)) || defined(PROTO)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007717 int
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02007718vim_mkdir_emsg(char_u *name, int prot UNUSED)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007719{
7720 if (vim_mkdir(name, prot) != 0)
7721 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007722 semsg(_("E739: Cannot create directory: %s"), name);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007723 return FAIL;
7724 }
7725 return OK;
7726}
7727#endif
7728
Bram Moolenaar071d4272004-06-13 20:20:40 +00007729/*
7730 * Open a file for writing for an Ex command, with some checks.
7731 * Return file descriptor, or NULL on failure.
7732 */
7733 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007734open_exfile(
7735 char_u *fname,
7736 int forceit,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007737 char *mode) // "w" for create new file or "a" for append
Bram Moolenaar071d4272004-06-13 20:20:40 +00007738{
7739 FILE *fd;
7740
7741#ifdef UNIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007742 // with Unix it is possible to open a directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007743 if (mch_isdir(fname))
7744 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007745 semsg(_(e_isadir2), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007746 return NULL;
7747 }
7748#endif
7749 if (!forceit && *mode != 'a' && vim_fexists(fname))
7750 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007751 semsg(_("E189: \"%s\" exists (add ! to override)"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007752 return NULL;
7753 }
7754
7755 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007756 semsg(_("E190: Cannot open \"%s\" for writing"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007757
7758 return fd;
7759}
7760
7761/*
7762 * ":mark" and ":k".
7763 */
7764 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007765ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007766{
7767 pos_T pos;
7768
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007769 if (*eap->arg == NUL) // No argument?
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007770 emsg(_(e_argreq));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007771 else if (eap->arg[1] != NUL) // more than one character?
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02007772 semsg(_(e_trailing_arg), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007773 else
7774 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007775 pos = curwin->w_cursor; // save curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00007776 curwin->w_cursor.lnum = eap->line2;
7777 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007778 if (setmark(*eap->arg) == FAIL) // set mark
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007779 emsg(_("E191: Argument must be a letter or forward/backward quote"));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007780 curwin->w_cursor = pos; // restore curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00007781 }
7782}
7783
7784/*
7785 * Update w_topline, w_leftcol and the cursor position.
7786 */
7787 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007788update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007789{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007790 check_cursor(); // put cursor on valid line
Bram Moolenaar071d4272004-06-13 20:20:40 +00007791 update_topline();
7792 if (!curwin->w_p_wrap)
7793 validate_cursor();
7794 update_curswant();
7795}
7796
Bram Moolenaar071d4272004-06-13 20:20:40 +00007797/*
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007798 * Save the current State and go to Normal mode.
7799 * Return TRUE if the typeahead could be saved.
7800 */
7801 int
7802save_current_state(save_state_T *sst)
7803{
7804 sst->save_msg_scroll = msg_scroll;
7805 sst->save_restart_edit = restart_edit;
7806 sst->save_msg_didout = msg_didout;
7807 sst->save_State = State;
7808 sst->save_insertmode = p_im;
7809 sst->save_finish_op = finish_op;
7810 sst->save_opcount = opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01007811 sst->save_reg_executing = reg_executing;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007812
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007813 msg_scroll = FALSE; // no msg scrolling in Normal mode
7814 restart_edit = 0; // don't go to Insert mode
7815 p_im = FALSE; // don't use 'insertmode'
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007816
7817 /*
7818 * Save the current typeahead. This is required to allow using ":normal"
7819 * from an event handler and makes sure we don't hang when the argument
7820 * ends with half a command.
7821 */
7822 save_typeahead(&sst->tabuf);
7823 return sst->tabuf.typebuf_valid;
7824}
7825
7826 void
7827restore_current_state(save_state_T *sst)
7828{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007829 // Restore the previous typeahead.
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007830 restore_typeahead(&sst->tabuf);
7831
7832 msg_scroll = sst->save_msg_scroll;
7833 restart_edit = sst->save_restart_edit;
7834 p_im = sst->save_insertmode;
7835 finish_op = sst->save_finish_op;
7836 opcount = sst->save_opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01007837 reg_executing = sst->save_reg_executing;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007838 msg_didout |= sst->save_msg_didout; // don't reset msg_didout now
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007839
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007840 // Restore the state (needed when called from a function executed for
7841 // 'indentexpr'). Update the mouse and cursor, they may have changed.
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007842 State = sst->save_State;
7843#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007844 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007845#endif
7846}
7847
7848/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007849 * ":normal[!] {commands}": Execute normal mode commands.
7850 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01007851 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007852ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007853{
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007854 save_state_T save_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007855 char_u *arg = NULL;
7856 int l;
7857 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007858
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00007859 if (ex_normal_lock > 0)
7860 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007861 emsg(_(e_secure));
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00007862 return;
7863 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007864 if (ex_normal_busy >= p_mmd)
7865 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007866 emsg(_("E192: Recursive use of :normal too deep"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007867 return;
7868 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007869
Bram Moolenaar071d4272004-06-13 20:20:40 +00007870 /*
7871 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
7872 * this for the K_SPECIAL leading byte, otherwise special keys will not
7873 * work.
7874 */
7875 if (has_mbyte)
7876 {
7877 int len = 0;
7878
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007879 // Count the number of characters to be escaped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007880 for (p = eap->arg; *p != NUL; ++p)
7881 {
Bram Moolenaar13505972019-01-24 15:04:48 +01007882#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007883 if (*p == CSI) // leadbyte CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007884 len += 2;
Bram Moolenaar13505972019-01-24 15:04:48 +01007885#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007886 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007887 if (*++p == K_SPECIAL // trailbyte K_SPECIAL or CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01007888#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007889 || *p == CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01007890#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007891 )
7892 len += 2;
7893 }
7894 if (len > 0)
7895 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02007896 arg = alloc(STRLEN(eap->arg) + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007897 if (arg != NULL)
7898 {
7899 len = 0;
7900 for (p = eap->arg; *p != NUL; ++p)
7901 {
7902 arg[len++] = *p;
Bram Moolenaar13505972019-01-24 15:04:48 +01007903#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007904 if (*p == CSI)
7905 {
7906 arg[len++] = KS_EXTRA;
7907 arg[len++] = (int)KE_CSI;
7908 }
Bram Moolenaar13505972019-01-24 15:04:48 +01007909#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007910 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007911 {
7912 arg[len++] = *++p;
7913 if (*p == K_SPECIAL)
7914 {
7915 arg[len++] = KS_SPECIAL;
7916 arg[len++] = KE_FILLER;
7917 }
Bram Moolenaar13505972019-01-24 15:04:48 +01007918#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007919 else if (*p == CSI)
7920 {
7921 arg[len++] = KS_EXTRA;
7922 arg[len++] = (int)KE_CSI;
7923 }
Bram Moolenaar13505972019-01-24 15:04:48 +01007924#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007925 }
7926 arg[len] = NUL;
7927 }
7928 }
7929 }
7930 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007931
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007932 ++ex_normal_busy;
7933 if (save_current_state(&save_state))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007934 {
7935 /*
7936 * Repeat the :normal command for each line in the range. When no
7937 * range given, execute it just once, without positioning the cursor
7938 * first.
7939 */
7940 do
7941 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007942 if (eap->addr_count != 0)
7943 {
7944 curwin->w_cursor.lnum = eap->line1++;
7945 curwin->w_cursor.col = 0;
Bram Moolenaard5d37532017-03-27 23:02:07 +02007946 check_cursor_moved(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007947 }
7948
Bram Moolenaar13505972019-01-24 15:04:48 +01007949 exec_normal_cmd(arg != NULL
7950 ? arg
7951 : eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007952 }
7953 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
7954 }
7955
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007956 // Might not return to the main loop when in an event handler.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007957 update_topline_cursor();
7958
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007959 restore_current_state(&save_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007960 --ex_normal_busy;
Bram Moolenaareda73602014-11-05 09:53:23 +01007961 setmouse();
Bram Moolenaareda73602014-11-05 09:53:23 +01007962#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007963 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaareda73602014-11-05 09:53:23 +01007964#endif
7965
Bram Moolenaar071d4272004-06-13 20:20:40 +00007966 vim_free(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007967}
7968
7969/*
Bram Moolenaar64969662005-12-14 21:59:55 +00007970 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007971 */
7972 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007973ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007974{
Bram Moolenaarfd371682005-01-14 21:42:54 +00007975 if (eap->forceit)
7976 {
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007977 // cursor line can be zero on startup
Bram Moolenaar09ca9322017-09-26 17:40:45 +02007978 if (!curwin->w_cursor.lnum)
7979 curwin->w_cursor.lnum = 1;
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007980 set_cursor_for_append_to_line();
Bram Moolenaarfd371682005-01-14 21:42:54 +00007981 }
Bram Moolenaareb58a242020-04-19 18:13:19 +02007982#ifdef FEAT_TERMINAL
7983 // Ignore this when running in an active terminal.
7984 if (term_job_running(curbuf->b_term))
7985 return;
7986#endif
Bram Moolenaarfd371682005-01-14 21:42:54 +00007987
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007988 // Ignore the command when already in Insert mode. Inserting an
7989 // expression register that invokes a function can do this.
Bram Moolenaara40c5002005-01-09 21:16:21 +00007990 if (State & INSERT)
7991 return;
7992
Bram Moolenaar64969662005-12-14 21:59:55 +00007993 if (eap->cmdidx == CMD_startinsert)
7994 restart_edit = 'a';
7995 else if (eap->cmdidx == CMD_startreplace)
7996 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00007997 else
Bram Moolenaar64969662005-12-14 21:59:55 +00007998 restart_edit = 'V';
7999
8000 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008001 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00008002 if (eap->cmdidx == CMD_startinsert)
8003 restart_edit = 'i';
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008004 curwin->w_curswant = 0; // avoid MAXCOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008005 }
8006}
8007
8008/*
8009 * ":stopinsert"
8010 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008011 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008012ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008013{
8014 restart_edit = 0;
8015 stop_insert_mode = TRUE;
Bram Moolenaarfd773e92016-04-02 19:39:16 +02008016 clearmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008017}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008018
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008019/*
8020 * Execute normal mode command "cmd".
8021 * "remap" can be REMAP_NONE or REMAP_YES.
8022 */
8023 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008024exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008025{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008026 // Stuff the argument into the typeahead buffer.
Bram Moolenaar25281632016-01-21 23:32:32 +01008027 ins_typebuf(cmd, remap, 0, TRUE, silent);
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008028 exec_normal(FALSE, FALSE, FALSE);
Bram Moolenaar25281632016-01-21 23:32:32 +01008029}
Bram Moolenaar25281632016-01-21 23:32:32 +01008030
Bram Moolenaar25281632016-01-21 23:32:32 +01008031/*
8032 * Execute normal_cmd() until there is no typeahead left.
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008033 * When "use_vpeekc" is TRUE use vpeekc() to check for available chars.
Bram Moolenaar25281632016-01-21 23:32:32 +01008034 */
8035 void
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008036exec_normal(int was_typed, int use_vpeekc, int may_use_terminal_loop UNUSED)
Bram Moolenaar25281632016-01-21 23:32:32 +01008037{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008038 oparg_T oa;
Bram Moolenaar905dd902019-04-07 14:21:47 +02008039 int c;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008040
Bram Moolenaar905dd902019-04-07 14:21:47 +02008041 // When calling vpeekc() from feedkeys() it will return Ctrl_C when there
8042 // is nothing to get, so also check for Ctrl_C.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008043 clear_oparg(&oa);
8044 finish_op = FALSE;
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008045 while ((!stuff_empty()
8046 || ((was_typed || !typebuf_typed()) && typebuf.tb_len > 0)
Bram Moolenaar905dd902019-04-07 14:21:47 +02008047 || (use_vpeekc && (c = vpeekc()) != NUL && c != Ctrl_C))
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008048 && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008049 {
8050 update_topline_cursor();
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008051#ifdef FEAT_TERMINAL
Bram Moolenaar655a82a2018-05-08 22:01:07 +02008052 if (may_use_terminal_loop && term_use_loop()
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008053 && oa.op_type == OP_NOP && oa.regname == NUL
8054 && !VIsual_active)
8055 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008056 // If terminal_loop() returns OK we got a key that is handled
8057 // in Normal model. With FAIL we first need to position the
8058 // cursor and the screen needs to be redrawn.
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008059 if (terminal_loop(TRUE) == OK)
8060 normal_cmd(&oa, TRUE);
8061 }
8062 else
8063#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008064 // execute a Normal mode cmd
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008065 normal_cmd(&oa, TRUE);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008066 }
8067}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008068
Bram Moolenaar071d4272004-06-13 20:20:40 +00008069#ifdef FEAT_FIND_ID
8070 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008071ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008072{
8073 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
8074 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
8075 (linenr_T)1, (linenr_T)MAXLNUM);
8076}
8077
Bram Moolenaar4033c552017-09-16 20:54:51 +02008078#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008079/*
8080 * ":psearch"
8081 */
8082 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008083ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008084{
8085 g_do_tagpreview = p_pvh;
8086 ex_findpat(eap);
8087 g_do_tagpreview = 0;
8088}
8089#endif
8090
8091 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008092ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008093{
8094 int whole = TRUE;
8095 long n;
8096 char_u *p;
8097 int action;
8098
8099 switch (cmdnames[eap->cmdidx].cmd_name[2])
8100 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008101 case 'e': // ":psearch", ":isearch" and ":dsearch"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008102 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
8103 action = ACTION_GOTO;
8104 else
8105 action = ACTION_SHOW;
8106 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008107 case 'i': // ":ilist" and ":dlist"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008108 action = ACTION_SHOW_ALL;
8109 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008110 case 'u': // ":ijump" and ":djump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008111 action = ACTION_GOTO;
8112 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008113 default: // ":isplit" and ":dsplit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008114 action = ACTION_SPLIT;
8115 break;
8116 }
8117
8118 n = 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008119 if (vim_isdigit(*eap->arg)) // get count
Bram Moolenaar071d4272004-06-13 20:20:40 +00008120 {
8121 n = getdigits(&eap->arg);
8122 eap->arg = skipwhite(eap->arg);
8123 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008124 if (*eap->arg == '/') // Match regexp, not just whole words
Bram Moolenaar071d4272004-06-13 20:20:40 +00008125 {
8126 whole = FALSE;
8127 ++eap->arg;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02008128 p = skip_regexp(eap->arg, '/', p_magic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008129 if (*p)
8130 {
8131 *p++ = NUL;
8132 p = skipwhite(p);
8133
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008134 // Check for trailing illegal characters
Bram Moolenaarfaac4102020-04-20 17:46:14 +02008135 if (!ends_excmd2(eap->arg, p))
Bram Moolenaar8930caa2020-07-23 16:37:03 +02008136 eap->errmsg = ex_errmsg(e_trailing_arg, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008137 else
8138 eap->nextcmd = check_nextcmd(p);
8139 }
8140 }
8141 if (!eap->skip)
8142 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
8143 whole, !eap->forceit,
8144 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
8145 n, action, eap->line1, eap->line2);
8146}
8147#endif
8148
Bram Moolenaar071d4272004-06-13 20:20:40 +00008149
Bram Moolenaar4033c552017-09-16 20:54:51 +02008150#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00008151/*
8152 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
8153 */
8154 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008155ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008156{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008157 g_do_tagpreview = p_pvh; // will be reset to 0 in ex_tag_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00008158 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
8159}
8160
8161/*
8162 * ":pedit"
8163 */
8164 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008165ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008166{
8167 win_T *curwin_save = curwin;
8168
Bram Moolenaar3a2505c2020-03-09 16:40:41 +01008169 if (ERROR_IF_ANY_POPUP_WINDOW)
8170 return;
8171
Bram Moolenaar026587b2019-08-17 15:08:00 +02008172 // Open the preview window or popup and make it the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008173 g_do_tagpreview = p_pvh;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02008174 prepare_tagpreview(TRUE, TRUE, FALSE);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008175
Bram Moolenaar026587b2019-08-17 15:08:00 +02008176 // Edit the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008177 do_exedit(eap, NULL);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008178
Bram Moolenaar071d4272004-06-13 20:20:40 +00008179 if (curwin != curwin_save && win_valid(curwin_save))
8180 {
Bram Moolenaar026587b2019-08-17 15:08:00 +02008181 // Return cursor to where we were
Bram Moolenaar071d4272004-06-13 20:20:40 +00008182 validate_cursor();
8183 redraw_later(VALID);
8184 win_enter(curwin_save, TRUE);
8185 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01008186# ifdef FEAT_PROP_POPUP
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008187 else if (WIN_IS_POPUP(curwin))
8188 {
8189 // can't keep focus in popup window
8190 win_enter(firstwin, TRUE);
8191 }
8192# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008193 g_do_tagpreview = 0;
8194}
Bram Moolenaar4033c552017-09-16 20:54:51 +02008195#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008196
8197/*
8198 * ":stag", ":stselect" and ":stjump".
8199 */
8200 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008201ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008202{
8203 postponed_split = -1;
8204 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008205 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008206 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
8207 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008208 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008209}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008210
8211/*
8212 * ":tag", ":tselect", ":tjump", ":tnext", etc.
8213 */
8214 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008215ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008216{
8217 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
8218}
8219
8220 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008221ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008222{
8223 int cmd;
8224
8225 switch (name[1])
8226 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008227 case 'j': cmd = DT_JUMP; // ":tjump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008228 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008229 case 's': cmd = DT_SELECT; // ":tselect"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008230 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008231 case 'p': cmd = DT_PREV; // ":tprevious"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008232 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008233 case 'N': cmd = DT_PREV; // ":tNext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008234 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008235 case 'n': cmd = DT_NEXT; // ":tnext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008236 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008237 case 'o': cmd = DT_POP; // ":pop"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008238 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008239 case 'f': // ":tfirst"
8240 case 'r': cmd = DT_FIRST; // ":trewind"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008241 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008242 case 'l': cmd = DT_LAST; // ":tlast"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008243 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008244 default: // ":tag"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008245#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +00008246 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008247 {
Bram Moolenaard4db7712016-11-12 19:16:46 +01008248 ex_cstag(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008249 return;
8250 }
8251#endif
8252 cmd = DT_TAG;
8253 break;
8254 }
8255
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00008256 if (name[0] == 'l')
8257 {
8258#ifndef FEAT_QUICKFIX
8259 ex_ni(eap);
8260 return;
8261#else
8262 cmd = DT_LTAG;
8263#endif
8264 }
8265
Bram Moolenaar071d4272004-06-13 20:20:40 +00008266 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
8267 eap->forceit, TRUE);
8268}
8269
8270/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008271 * Check "str" for starting with a special cmdline variable.
8272 * If found return one of the SPEC_ values and set "*usedlen" to the length of
8273 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
8274 */
8275 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008276find_cmdline_var(char_u *src, int *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008277{
8278 int len;
8279 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +00008280 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008281 "%",
8282#define SPEC_PERC 0
8283 "#",
Bram Moolenaar65f08472017-09-10 18:16:20 +02008284#define SPEC_HASH (SPEC_PERC + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008285 "<cword>", // cursor word
Bram Moolenaar65f08472017-09-10 18:16:20 +02008286#define SPEC_CWORD (SPEC_HASH + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008287 "<cWORD>", // cursor WORD
Bram Moolenaar65f08472017-09-10 18:16:20 +02008288#define SPEC_CCWORD (SPEC_CWORD + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008289 "<cexpr>", // expr under cursor
Bram Moolenaar65f08472017-09-10 18:16:20 +02008290#define SPEC_CEXPR (SPEC_CCWORD + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008291 "<cfile>", // cursor path name
Bram Moolenaar65f08472017-09-10 18:16:20 +02008292#define SPEC_CFILE (SPEC_CEXPR + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008293 "<sfile>", // ":so" file name
Bram Moolenaar65f08472017-09-10 18:16:20 +02008294#define SPEC_SFILE (SPEC_CFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008295 "<slnum>", // ":so" file line number
Bram Moolenaar65f08472017-09-10 18:16:20 +02008296#define SPEC_SLNUM (SPEC_SFILE + 1)
Bram Moolenaara5d04232020-07-26 15:37:02 +02008297 "<stack>", // call stack
8298#define SPEC_STACK (SPEC_SLNUM + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008299 "<afile>", // autocommand file name
Bram Moolenaara5d04232020-07-26 15:37:02 +02008300#define SPEC_AFILE (SPEC_STACK + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008301 "<abuf>", // autocommand buffer number
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008302#define SPEC_ABUF (SPEC_AFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008303 "<amatch>", // autocommand match name
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008304#define SPEC_AMATCH (SPEC_ABUF + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008305 "<sflnum>", // script file line number
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008306#define SPEC_SFLNUM (SPEC_AMATCH + 1)
Bram Moolenaar90944302020-08-01 20:45:11 +02008307 "<SID>", // script ID: <SNR>123_
8308#define SPEC_SID (SPEC_SFLNUM + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008309#ifdef FEAT_CLIENTSERVER
8310 "<client>"
Bram Moolenaar90944302020-08-01 20:45:11 +02008311# define SPEC_CLIENT (SPEC_SID + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008312#endif
8313 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008314
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008315 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008316 {
8317 len = (int)STRLEN(spec_str[i]);
8318 if (STRNCMP(src, spec_str[i], len) == 0)
8319 {
8320 *usedlen = len;
8321 return i;
8322 }
8323 }
8324 return -1;
8325}
8326
8327/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008328 * Evaluate cmdline variables.
8329 *
8330 * change '%' to curbuf->b_ffname
8331 * '#' to curwin->w_altfile
8332 * '<cword>' to word under the cursor
8333 * '<cWORD>' to WORD under the cursor
Bram Moolenaar8071cb22019-07-12 17:58:01 +02008334 * '<cexpr>' to C-expression under the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00008335 * '<cfile>' to path name under the cursor
8336 * '<sfile>' to sourced file name
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008337 * '<slnum>' to sourced file line number
Bram Moolenaar071d4272004-06-13 20:20:40 +00008338 * '<afile>' to file name for autocommand
8339 * '<abuf>' to buffer number for autocommand
8340 * '<amatch>' to matching name for autocommand
8341 *
8342 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
8343 * "" for error without a message) and NULL is returned.
8344 * Returns an allocated string if a valid match was found.
8345 * Returns NULL if no match was found. "usedlen" then still contains the
8346 * number of characters to skip.
8347 */
8348 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008349eval_vars(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008350 char_u *src, // pointer into commandline
8351 char_u *srcstart, // beginning of valid memory for src
8352 int *usedlen, // characters after src that are used
8353 linenr_T *lnump, // line number for :e command, or NULL
8354 char **errormsg, // pointer to error message
8355 int *escaped) // return value has escaped white space (can
8356 // be NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008357{
8358 int i;
8359 char_u *s;
8360 char_u *result;
8361 char_u *resultbuf = NULL;
8362 int resultlen;
8363 buf_T *buf;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008364 int valid = VALID_HEAD + VALID_PATH; // assume valid result
Bram Moolenaar071d4272004-06-13 20:20:40 +00008365 int spec_idx;
Bram Moolenaarfd249462018-07-28 16:14:30 +02008366 int tilde_file = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008367 int skip_mod = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008368 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008369
8370 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +00008371 if (escaped != NULL)
8372 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008373
8374 /*
8375 * Check if there is something to do.
8376 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008377 spec_idx = find_cmdline_var(src, usedlen);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008378 if (spec_idx < 0) // no match
Bram Moolenaar071d4272004-06-13 20:20:40 +00008379 {
8380 *usedlen = 1;
8381 return NULL;
8382 }
8383
8384 /*
8385 * Skip when preceded with a backslash "\%" and "\#".
8386 * Note: In "\\%" the % is also not recognized!
8387 */
8388 if (src > srcstart && src[-1] == '\\')
8389 {
8390 *usedlen = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008391 STRMOVE(src - 1, src); // remove backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00008392 return NULL;
8393 }
8394
8395 /*
8396 * word or WORD under cursor
8397 */
Bram Moolenaar65f08472017-09-10 18:16:20 +02008398 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD
8399 || spec_idx == SPEC_CEXPR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008400 {
Bram Moolenaar65f08472017-09-10 18:16:20 +02008401 resultlen = find_ident_under_cursor(&result,
8402 spec_idx == SPEC_CWORD ? (FIND_IDENT | FIND_STRING)
8403 : spec_idx == SPEC_CEXPR ? (FIND_IDENT | FIND_STRING | FIND_EVAL)
8404 : FIND_STRING);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008405 if (resultlen == 0)
8406 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008407 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008408 return NULL;
8409 }
8410 }
8411
8412 /*
8413 * '#': Alternate file name
8414 * '%': Current file name
8415 * File name under the cursor
8416 * File name for autocommand
8417 * and following modifiers
8418 */
8419 else
8420 {
8421 switch (spec_idx)
8422 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008423 case SPEC_PERC: // '%': current file
Bram Moolenaar071d4272004-06-13 20:20:40 +00008424 if (curbuf->b_fname == NULL)
8425 {
8426 result = (char_u *)"";
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008427 valid = 0; // Must have ":p:h" to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00008428 }
8429 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008430 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008431 result = curbuf->b_fname;
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008432 tilde_file = STRCMP(result, "~") == 0;
8433 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008434 break;
8435
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008436 case SPEC_HASH: // '#' or "#99": alternate file
8437 if (src[1] == '#') // "##": the argument list
Bram Moolenaar071d4272004-06-13 20:20:40 +00008438 {
8439 result = arg_all();
8440 resultbuf = result;
8441 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +00008442 if (escaped != NULL)
8443 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008444 skip_mod = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008445 break;
8446 }
8447 s = src + 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008448 if (*s == '<') // "#<99" uses v:oldfiles
Bram Moolenaard812df62008-11-09 12:46:09 +00008449 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008450 i = (int)getdigits(&s);
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008451 if (s == src + 2 && src[1] == '-')
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008452 // just a minus sign, don't skip over it
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008453 s--;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008454 *usedlen = (int)(s - src); // length of what we expand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008455
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008456 if (src[1] == '<' && i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008457 {
Bram Moolenaard812df62008-11-09 12:46:09 +00008458 if (*usedlen < 2)
8459 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008460 // Should we give an error message for #<text?
Bram Moolenaard812df62008-11-09 12:46:09 +00008461 *usedlen = 1;
8462 return NULL;
8463 }
8464#ifdef FEAT_EVAL
8465 result = list_find_str(get_vim_var_list(VV_OLDFILES),
8466 (long)i);
8467 if (result == NULL)
8468 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008469 *errormsg = "";
Bram Moolenaard812df62008-11-09 12:46:09 +00008470 return NULL;
8471 }
8472#else
Bram Moolenaar8e481e82019-01-15 20:07:48 +01008473 *errormsg = _("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008474 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00008475#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008476 }
8477 else
Bram Moolenaard812df62008-11-09 12:46:09 +00008478 {
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008479 if (i == 0 && src[1] == '<' && *usedlen > 1)
8480 *usedlen = 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00008481 buf = buflist_findnr(i);
8482 if (buf == NULL)
8483 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008484 *errormsg = _("E194: No alternate file name to substitute for '#'");
Bram Moolenaard812df62008-11-09 12:46:09 +00008485 return NULL;
8486 }
8487 if (lnump != NULL)
8488 *lnump = ECMD_LAST;
8489 if (buf->b_fname == NULL)
8490 {
8491 result = (char_u *)"";
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008492 valid = 0; // Must have ":p:h" to be valid
Bram Moolenaard812df62008-11-09 12:46:09 +00008493 }
8494 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008495 {
Bram Moolenaard812df62008-11-09 12:46:09 +00008496 result = buf->b_fname;
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008497 tilde_file = STRCMP(result, "~") == 0;
8498 }
Bram Moolenaard812df62008-11-09 12:46:09 +00008499 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008500 break;
8501
8502#ifdef FEAT_SEARCHPATH
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008503 case SPEC_CFILE: // file name under cursor
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008504 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008505 if (result == NULL)
8506 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008507 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008508 return NULL;
8509 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008510 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00008511 break;
8512#endif
8513
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008514 case SPEC_AFILE: // file name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008515 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +00008516 if (result != NULL && !autocmd_fname_full)
8517 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008518 // Still need to turn the fname into a full path. It is
8519 // postponed to avoid a delay when <afile> is not used.
Bram Moolenaarf6dad432008-09-18 19:29:58 +00008520 autocmd_fname_full = TRUE;
8521 result = FullName_save(autocmd_fname, FALSE);
8522 vim_free(autocmd_fname);
8523 autocmd_fname = result;
8524 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008525 if (result == NULL)
8526 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008527 *errormsg = _("E495: no autocommand file name to substitute for \"<afile>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008528 return NULL;
8529 }
Bram Moolenaara0174af2008-01-02 20:08:25 +00008530 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008531 break;
8532
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008533 case SPEC_ABUF: // buffer number for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008534 if (autocmd_bufnr <= 0)
8535 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008536 *errormsg = _("E496: no autocommand buffer number to substitute for \"<abuf>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008537 return NULL;
8538 }
8539 sprintf((char *)strbuf, "%d", autocmd_bufnr);
8540 result = strbuf;
8541 break;
8542
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008543 case SPEC_AMATCH: // match name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008544 result = autocmd_match;
8545 if (result == NULL)
8546 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008547 *errormsg = _("E497: no autocommand match name to substitute for \"<amatch>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008548 return NULL;
8549 }
8550 break;
8551
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008552 case SPEC_SFILE: // file name for ":so" command
Bram Moolenaara5d04232020-07-26 15:37:02 +02008553 case SPEC_STACK: // call stack
8554 result = estack_sfile(spec_idx == SPEC_SFILE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008555 if (result == NULL)
8556 {
Bram Moolenaara5d04232020-07-26 15:37:02 +02008557 *errormsg = spec_idx == SPEC_SFILE
8558 ? _("E498: no :source file name to substitute for \"<sfile>\"")
8559 : _("E489: no call stack to substitute for \"<stack>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008560 return NULL;
8561 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008562 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00008563 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008564
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008565 case SPEC_SLNUM: // line in file for ":so" command
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008566 if (SOURCING_NAME == NULL || SOURCING_LNUM == 0)
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008567 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008568 *errormsg = _("E842: no line number to use for \"<slnum>\"");
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008569 return NULL;
8570 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008571 sprintf((char *)strbuf, "%ld", SOURCING_LNUM);
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008572 result = strbuf;
8573 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008574
8575#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008576 case SPEC_SFLNUM: // line in script file
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008577 if (current_sctx.sc_lnum + SOURCING_LNUM == 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008578 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008579 *errormsg = _("E961: no line number to use for \"<sflnum>\"");
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008580 return NULL;
8581 }
8582 sprintf((char *)strbuf, "%ld",
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008583 (long)(current_sctx.sc_lnum + SOURCING_LNUM));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008584 result = strbuf;
8585 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008586
Bram Moolenaar90944302020-08-01 20:45:11 +02008587 case SPEC_SID:
8588 if (current_sctx.sc_sid <= 0)
8589 {
8590 *errormsg = _(e_usingsid);
8591 return NULL;
8592 }
8593 sprintf((char *)strbuf, "<SNR>%d_", current_sctx.sc_sid);
8594 result = strbuf;
8595 break;
Bram Moolenaare4218b92020-08-01 21:11:38 +02008596#endif
Bram Moolenaar90944302020-08-01 20:45:11 +02008597
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008598#ifdef FEAT_CLIENTSERVER
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008599 case SPEC_CLIENT: // Source of last submitted input
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008600 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
8601 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008602 result = strbuf;
8603 break;
8604#endif
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008605
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02008606 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008607 result = (char_u *)""; // avoid gcc warning
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02008608 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008609 }
8610
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008611 resultlen = (int)STRLEN(result); // length of new string
8612 if (src[*usedlen] == '<') // remove the file name extension
Bram Moolenaar071d4272004-06-13 20:20:40 +00008613 {
8614 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008615 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008616 resultlen = (int)(s - result);
8617 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008618 else if (!skip_mod)
8619 {
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008620 valid |= modify_fname(src, tilde_file, usedlen, &result, &resultbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008621 &resultlen);
8622 if (result == NULL)
8623 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008624 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008625 return NULL;
8626 }
8627 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008628 }
8629
8630 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
8631 {
8632 if (valid != VALID_HEAD + VALID_PATH)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008633 // xgettext:no-c-format
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008634 *errormsg = _("E499: Empty file name for '%' or '#', only works with \":p:h\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008635 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008636 *errormsg = _("E500: Evaluates to an empty string");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008637 result = NULL;
8638 }
8639 else
8640 result = vim_strnsave(result, resultlen);
8641 vim_free(resultbuf);
8642 return result;
8643}
8644
8645/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008646 * Expand the <sfile> string in "arg".
8647 *
8648 * Returns an allocated string, or NULL for any error.
8649 */
8650 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008651expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008652{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008653 char *errormsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008654 int len;
8655 char_u *result;
8656 char_u *newres;
8657 char_u *repl;
8658 int srclen;
8659 char_u *p;
8660
8661 result = vim_strsave(arg);
8662 if (result == NULL)
8663 return NULL;
8664
8665 for (p = result; *p; )
8666 {
8667 if (STRNCMP(p, "<sfile>", 7) != 0)
8668 ++p;
8669 else
8670 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008671 // replace "<sfile>" with the sourced file name, and do ":" stuff
Bram Moolenaar63b92542007-03-27 14:57:09 +00008672 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008673 if (errormsg != NULL)
8674 {
8675 if (*errormsg)
8676 emsg(errormsg);
8677 vim_free(result);
8678 return NULL;
8679 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008680 if (repl == NULL) // no match (cannot happen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008681 {
8682 p += srclen;
8683 continue;
8684 }
8685 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
8686 newres = alloc(len);
8687 if (newres == NULL)
8688 {
8689 vim_free(repl);
8690 vim_free(result);
8691 return NULL;
8692 }
8693 mch_memmove(newres, result, (size_t)(p - result));
8694 STRCPY(newres + (p - result), repl);
8695 len = (int)STRLEN(newres);
8696 STRCAT(newres, p + srclen);
8697 vim_free(repl);
8698 vim_free(result);
8699 result = newres;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008700 p = newres + len; // continue after the match
Bram Moolenaar071d4272004-06-13 20:20:40 +00008701 }
8702 }
8703
8704 return result;
8705}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008706
Bram Moolenaar071d4272004-06-13 20:20:40 +00008707#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00008708/*
Bram Moolenaard9462e32011-04-11 21:35:11 +02008709 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +00008710 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +00008711 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008712 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008713dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008714{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008715 if (fname == NULL)
8716 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +02008717 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008718}
8719#endif
8720
8721/*
8722 * ":behave {mswin,xterm}"
8723 */
8724 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008725ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008726{
8727 if (STRCMP(eap->arg, "mswin") == 0)
8728 {
8729 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
8730 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
8731 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
8732 set_option_value((char_u *)"keymodel", 0L,
8733 (char_u *)"startsel,stopsel", 0);
8734 }
8735 else if (STRCMP(eap->arg, "xterm") == 0)
8736 {
8737 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
8738 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
8739 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
8740 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
8741 }
8742 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008743 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008744}
8745
Bram Moolenaar071d4272004-06-13 20:20:40 +00008746static int filetype_detect = FALSE;
8747static int filetype_plugin = FALSE;
8748static int filetype_indent = FALSE;
8749
8750/*
8751 * ":filetype [plugin] [indent] {on,off,detect}"
8752 * on: Load the filetype.vim file to install autocommands for file types.
8753 * off: Load the ftoff.vim file to remove all autocommands for file types.
8754 * plugin on: load filetype.vim and ftplugin.vim
8755 * plugin off: load ftplugof.vim
8756 * indent on: load filetype.vim and indent.vim
8757 * indent off: load indoff.vim
8758 */
8759 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008760ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008761{
8762 char_u *arg = eap->arg;
8763 int plugin = FALSE;
8764 int indent = FALSE;
8765
8766 if (*eap->arg == NUL)
8767 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008768 // Print current status.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008769 smsg("filetype detection:%s plugin:%s indent:%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00008770 filetype_detect ? "ON" : "OFF",
8771 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
8772 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
8773 return;
8774 }
8775
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008776 // Accept "plugin" and "indent" in any order.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008777 for (;;)
8778 {
8779 if (STRNCMP(arg, "plugin", 6) == 0)
8780 {
8781 plugin = TRUE;
8782 arg = skipwhite(arg + 6);
8783 continue;
8784 }
8785 if (STRNCMP(arg, "indent", 6) == 0)
8786 {
8787 indent = TRUE;
8788 arg = skipwhite(arg + 6);
8789 continue;
8790 }
8791 break;
8792 }
8793 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
8794 {
8795 if (*arg == 'o' || !filetype_detect)
8796 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008797 source_runtime((char_u *)FILETYPE_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008798 filetype_detect = TRUE;
8799 if (plugin)
8800 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008801 source_runtime((char_u *)FTPLUGIN_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008802 filetype_plugin = TRUE;
8803 }
8804 if (indent)
8805 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008806 source_runtime((char_u *)INDENT_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008807 filetype_indent = TRUE;
8808 }
8809 }
8810 if (*arg == 'd')
8811 {
Bram Moolenaar1610d052016-06-09 22:53:01 +02008812 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +00008813 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008814 }
8815 }
8816 else if (STRCMP(arg, "off") == 0)
8817 {
8818 if (plugin || indent)
8819 {
8820 if (plugin)
8821 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008822 source_runtime((char_u *)FTPLUGOF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008823 filetype_plugin = FALSE;
8824 }
8825 if (indent)
8826 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008827 source_runtime((char_u *)INDOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008828 filetype_indent = FALSE;
8829 }
8830 }
8831 else
8832 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008833 source_runtime((char_u *)FTOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008834 filetype_detect = FALSE;
8835 }
8836 }
8837 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008838 semsg(_(e_invarg2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008839}
8840
8841/*
Bram Moolenaar3e545692017-06-04 19:00:32 +02008842 * ":setfiletype [FALLBACK] {name}"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008843 */
8844 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008845ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008846{
8847 if (!did_filetype)
Bram Moolenaar3e545692017-06-04 19:00:32 +02008848 {
8849 char_u *arg = eap->arg;
8850
8851 if (STRNCMP(arg, "FALLBACK ", 9) == 0)
8852 arg += 9;
8853
8854 set_option_value((char_u *)"filetype", 0L, arg, OPT_LOCAL);
8855 if (arg != eap->arg)
8856 did_filetype = FALSE;
8857 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008858}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008859
Bram Moolenaar071d4272004-06-13 20:20:40 +00008860 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008861ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008862{
8863#ifdef FEAT_DIGRAPHS
8864 if (*eap->arg != NUL)
8865 putdigraph(eap->arg);
8866 else
Bram Moolenaareae8ae12018-12-14 18:53:02 +01008867 listdigraphs(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008868#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008869 emsg(_("E196: No digraphs in this version"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008870#endif
8871}
8872
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02008873#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
8874 void
8875set_no_hlsearch(int flag)
8876{
8877 no_hlsearch = flag;
8878# ifdef FEAT_EVAL
8879 set_vim_var_nr(VV_HLSEARCH, !no_hlsearch && p_hls);
8880# endif
8881}
8882
Bram Moolenaar071d4272004-06-13 20:20:40 +00008883/*
8884 * ":nohlsearch"
8885 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008886 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008887ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008888{
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02008889 set_no_hlsearch(TRUE);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00008890 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008891}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008892#endif
8893
8894#ifdef FEAT_CRYPT
8895/*
8896 * ":X": Get crypt key
8897 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008898 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008899ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008900{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +01008901 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02008902 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008903}
8904#endif
8905
8906#ifdef FEAT_FOLDING
8907 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008908ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008909{
8910 if (foldManualAllowed(TRUE))
8911 foldCreate(eap->line1, eap->line2);
8912}
8913
8914 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008915ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008916{
8917 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
8918 eap->forceit, FALSE);
8919}
8920
8921 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008922ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008923{
8924 linenr_T lnum;
8925
Bram Moolenaar4facea32019-10-12 20:17:40 +02008926# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02008927 start_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +02008928# endif
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02008929
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008930 // First set the marks for all lines closed/open.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008931 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
8932 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
8933 ml_setmarked(lnum);
8934
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008935 // Execute the command on the marked lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008936 global_exe(eap->arg);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008937 ml_clearmarked(); // clear rest of the marks
Bram Moolenaar4facea32019-10-12 20:17:40 +02008938# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02008939 end_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +02008940# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008941}
8942#endif
Bram Moolenaarf5291f32017-09-14 22:55:37 +02008943
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008944#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar39665952018-08-15 20:59:48 +02008945/*
8946 * Returns TRUE if the supplied Ex cmdidx is for a location list command
8947 * instead of a quickfix command.
8948 */
8949 int
8950is_loclist_cmd(int cmdidx)
8951{
Bram Moolenaar74c8be22018-08-23 22:51:40 +02008952 if (cmdidx < 0 || cmdidx >= CMD_SIZE)
Bram Moolenaar39665952018-08-15 20:59:48 +02008953 return FALSE;
8954 return cmdnames[cmdidx].cmd_name[0] == 'l';
8955}
8956#endif
8957
Bram Moolenaar4facea32019-10-12 20:17:40 +02008958#if defined(FEAT_TIMERS) || defined(PROTO)
Bram Moolenaarf5291f32017-09-14 22:55:37 +02008959 int
8960get_pressedreturn(void)
8961{
8962 return ex_pressedreturn;
8963}
8964
8965 void
8966set_pressedreturn(int val)
8967{
8968 ex_pressedreturn = val;
8969}
8970#endif