blob: 4f27898a199ec739aaeb927f8c7dda87bddbb8fb [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * ex_docmd.c: functions for executing an Ex command line.
12 */
13
14#include "vim.h"
15
Bram Moolenaar071d4272004-06-13 20:20:40 +000016static int quitmore = 0;
17static int ex_pressedreturn = FALSE;
18#ifndef FEAT_PRINTER
19# define ex_hardcopy ex_ni
20#endif
21
Bram Moolenaar071d4272004-06-13 20:20:40 +000022#ifdef FEAT_EVAL
Bram Moolenaarddef1292019-12-16 17:10:33 +010023static char_u *do_one_cmd(char_u **, int, cstack_T *, char_u *(*fgetline)(int, void *, int, int), void *cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024#else
Bram Moolenaare96a2492019-06-25 04:12:16 +020025static char_u *do_one_cmd(char_u **, int, char_u *(*fgetline)(int, void *, int, int), void *cookie);
Bram Moolenaar217e1b82019-12-01 21:41:28 +010026static int if_level = 0; // depth in :if
Bram Moolenaar071d4272004-06-13 20:20:40 +000027#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010028static void append_command(char_u *cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000029
Bram Moolenaar071d4272004-06-13 20:20:40 +000030#ifndef FEAT_MENU
31# define ex_emenu ex_ni
32# define ex_menu ex_ni
33# define ex_menutranslate ex_ni
34#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010035static void ex_autocmd(exarg_T *eap);
36static void ex_doautocmd(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010037static void ex_bunload(exarg_T *eap);
38static void ex_buffer(exarg_T *eap);
39static void ex_bmodified(exarg_T *eap);
40static void ex_bnext(exarg_T *eap);
41static void ex_bprevious(exarg_T *eap);
42static void ex_brewind(exarg_T *eap);
43static void ex_blast(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010044static char_u *getargcmd(char_u **);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010045static int getargopt(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000046#ifndef FEAT_QUICKFIX
47# define ex_make ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +000048# define ex_cbuffer ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000049# define ex_cc ex_ni
50# define ex_cnext ex_ni
Bram Moolenaar3ff33112019-05-03 21:56:35 +020051# define ex_cbelow ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000052# define ex_cfile ex_ni
53# define qf_list ex_ni
54# define qf_age ex_ni
Bram Moolenaarf6acffb2016-07-16 16:54:24 +020055# define qf_history ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000056# define ex_helpgrep ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +000057# define ex_vimgrep ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000058#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +020059#if !defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +000060# define ex_cclose ex_ni
61# define ex_copen ex_ni
62# define ex_cwindow ex_ni
Bram Moolenaardcb17002016-07-07 18:58:59 +020063# define ex_cbottom ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000064#endif
Bram Moolenaar1e015462005-09-25 22:16:38 +000065#if !defined(FEAT_QUICKFIX) || !defined(FEAT_EVAL)
66# define ex_cexpr ex_ni
67#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000068
Bram Moolenaarb7316892019-05-01 18:08:42 +020069static linenr_T get_address(exarg_T *, char_u **, cmd_addr_T addr_type, int skip, int silent, int to_other_file, int address_count);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010070static void get_flags(exarg_T *eap);
Bram Moolenaar85363ab2010-07-18 13:58:26 +020071#if !defined(FEAT_PERL) \
72 || !defined(FEAT_PYTHON) || !defined(FEAT_PYTHON3) \
73 || !defined(FEAT_TCL) \
74 || !defined(FEAT_RUBY) \
75 || !defined(FEAT_LUA) \
76 || !defined(FEAT_MZSCHEME)
Bram Moolenaar7bb75552007-07-16 18:39:49 +000077# define HAVE_EX_SCRIPT_NI
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010078static void ex_script_ni(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000079#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010080static char *invalid_range(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010081static void correct_range(exarg_T *eap);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000082#ifdef FEAT_QUICKFIX
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010083static char_u *replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000084#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010085static char_u *repl_cmdline(exarg_T *eap, char_u *src, int srclen, char_u *repl, char_u **cmdlinep);
86static void ex_highlight(exarg_T *eap);
87static void ex_colorscheme(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010088static void ex_cquit(exarg_T *eap);
89static void ex_quit_all(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010090static void ex_close(exarg_T *eap);
91static void ex_win_close(int forceit, win_T *win, tabpage_T *tp);
92static void ex_only(exarg_T *eap);
93static void ex_resize(exarg_T *eap);
94static void ex_stag(exarg_T *eap);
95static void ex_tabclose(exarg_T *eap);
96static void ex_tabonly(exarg_T *eap);
97static void ex_tabnext(exarg_T *eap);
98static void ex_tabmove(exarg_T *eap);
99static void ex_tabs(exarg_T *eap);
Bram Moolenaar4033c552017-09-16 20:54:51 +0200100#if defined(FEAT_QUICKFIX)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100101static void ex_pclose(exarg_T *eap);
102static void ex_ptag(exarg_T *eap);
103static void ex_pedit(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000104#else
105# define ex_pclose ex_ni
106# define ex_ptag ex_ni
107# define ex_pedit ex_ni
108#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100109static void ex_hide(exarg_T *eap);
110static void ex_stop(exarg_T *eap);
111static void ex_exit(exarg_T *eap);
112static void ex_print(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000113#ifdef FEAT_BYTEOFF
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100114static void ex_goto(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000115#else
116# define ex_goto ex_ni
117#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100118static void ex_shell(exarg_T *eap);
119static void ex_preserve(exarg_T *eap);
120static void ex_recover(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100121static void ex_mode(exarg_T *eap);
122static void ex_wrongmodifier(exarg_T *eap);
123static void ex_find(exarg_T *eap);
124static void ex_open(exarg_T *eap);
125static void ex_edit(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000126#ifndef FEAT_GUI
127# define ex_gui ex_nogui
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100128static void ex_nogui(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000129#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +0100130#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100131static void ex_tearoff(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000132#else
133# define ex_tearoff ex_ni
134#endif
Bram Moolenaar29a2c082018-03-05 21:06:23 +0100135#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
136 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100137static void ex_popup(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000138#else
139# define ex_popup ex_ni
140#endif
141#ifndef FEAT_GUI_MSWIN
142# define ex_simalt ex_ni
143#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000144#if !defined(FEAT_GUI_MSWIN) && !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000145# define gui_mch_find_dialog ex_ni
146# define gui_mch_replace_dialog ex_ni
147#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000148#if !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000149# define ex_helpfind ex_ni
150#endif
151#ifndef FEAT_CSCOPE
Bram Moolenaard4db7712016-11-12 19:16:46 +0100152# define ex_cscope ex_ni
153# define ex_scscope ex_ni
154# define ex_cstag ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000155#endif
156#ifndef FEAT_SYN_HL
157# define ex_syntax ex_ni
Bram Moolenaar860cae12010-06-05 23:22:07 +0200158# define ex_ownsyntax ex_ni
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000159#endif
Bram Moolenaarf7512552013-06-06 14:55:19 +0200160#if !defined(FEAT_SYN_HL) || !defined(FEAT_PROFILE)
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +0200161# define ex_syntime ex_ni
162#endif
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000163#ifndef FEAT_SPELL
Bram Moolenaarb765d632005-06-07 21:00:02 +0000164# define ex_spell ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000165# define ex_mkspell ex_ni
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000166# define ex_spelldump ex_ni
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000167# define ex_spellinfo ex_ni
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000168# define ex_spellrepall ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000169#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200170#ifndef FEAT_PERSISTENT_UNDO
171# define ex_rundo ex_ni
172# define ex_wundo ex_ni
173#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200174#ifndef FEAT_LUA
175# define ex_lua ex_script_ni
176# define ex_luado ex_ni
177# define ex_luafile ex_ni
178#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000179#ifndef FEAT_MZSCHEME
180# define ex_mzscheme ex_script_ni
181# define ex_mzfile ex_ni
182#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000183#ifndef FEAT_PERL
184# define ex_perl ex_script_ni
185# define ex_perldo ex_ni
186#endif
187#ifndef FEAT_PYTHON
188# define ex_python ex_script_ni
Bram Moolenaard620aa92013-05-17 16:40:06 +0200189# define ex_pydo ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000190# define ex_pyfile ex_ni
191#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200192#ifndef FEAT_PYTHON3
Bram Moolenaar368373e2010-07-19 20:46:22 +0200193# define ex_py3 ex_script_ni
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200194# define ex_py3do ex_ni
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200195# define ex_py3file ex_ni
196#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100197#if !defined(FEAT_PYTHON) && !defined(FEAT_PYTHON3)
198# define ex_pyx ex_script_ni
199# define ex_pyxdo ex_ni
200# define ex_pyxfile ex_ni
201#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000202#ifndef FEAT_TCL
203# define ex_tcl ex_script_ni
204# define ex_tcldo ex_ni
205# define ex_tclfile ex_ni
206#endif
207#ifndef FEAT_RUBY
208# define ex_ruby ex_script_ni
209# define ex_rubydo ex_ni
210# define ex_rubyfile ex_ni
211#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000212#ifndef FEAT_KEYMAP
213# define ex_loadkeymap ex_ni
214#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100215static void ex_swapname(exarg_T *eap);
216static void ex_syncbind(exarg_T *eap);
217static void ex_read(exarg_T *eap);
218static void ex_pwd(exarg_T *eap);
219static void ex_equal(exarg_T *eap);
220static void ex_sleep(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100221static void ex_winsize(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100222static void ex_wincmd(exarg_T *eap);
Bram Moolenaar843ee412004-06-30 16:16:41 +0000223#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100224static void ex_winpos(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000225#else
226# define ex_winpos ex_ni
227#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100228static void ex_operators(exarg_T *eap);
229static void ex_put(exarg_T *eap);
230static void ex_copymove(exarg_T *eap);
231static void ex_submagic(exarg_T *eap);
232static void ex_join(exarg_T *eap);
233static void ex_at(exarg_T *eap);
234static void ex_bang(exarg_T *eap);
235static void ex_undo(exarg_T *eap);
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200236#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100237static void ex_wundo(exarg_T *eap);
238static void ex_rundo(exarg_T *eap);
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200239#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100240static void ex_redo(exarg_T *eap);
241static void ex_later(exarg_T *eap);
242static void ex_redir(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100243static void ex_redrawstatus(exarg_T *eap);
Bram Moolenaare12bab32019-01-08 22:02:56 +0100244static void ex_redrawtabline(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100245static void close_redir(void);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100246static void ex_mark(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100247static void ex_startinsert(exarg_T *eap);
248static void ex_stopinsert(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000249#ifdef FEAT_FIND_ID
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100250static void ex_checkpath(exarg_T *eap);
251static void ex_findpat(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000252#else
253# define ex_findpat ex_ni
254# define ex_checkpath ex_ni
255#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +0200256#if defined(FEAT_FIND_ID) && defined(FEAT_QUICKFIX)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100257static void ex_psearch(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000258#else
259# define ex_psearch ex_ni
260#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100261static void ex_tag(exarg_T *eap);
262static void ex_tag_cmd(exarg_T *eap, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000263#ifndef FEAT_EVAL
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200264# define ex_break ex_ni
265# define ex_breakadd ex_ni
266# define ex_breakdel ex_ni
267# define ex_breaklist ex_ni
268# define ex_call ex_ni
269# define ex_catch ex_ni
270# define ex_compiler ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200271# define ex_continue ex_ni
272# define ex_debug ex_ni
273# define ex_debuggreedy ex_ni
Bram Moolenaar822ba242020-05-24 23:00:18 +0200274# define ex_def ex_ni
275# define ex_defcompile ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200276# define ex_delfunction ex_ni
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100277# define ex_disassemble ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000278# define ex_echo ex_ni
279# define ex_echohl ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000280# define ex_else ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200281# define ex_endfunction ex_ni
282# define ex_endif ex_ni
283# define ex_endtry ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000284# define ex_endwhile ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200285# define ex_eval ex_ni
286# define ex_execute ex_ni
287# define ex_finally ex_ni
288# define ex_finish ex_ni
289# define ex_function ex_ni
290# define ex_if ex_ni
291# define ex_let ex_ni
292# define ex_lockvar ex_ni
293# define ex_oldfiles ex_ni
294# define ex_options ex_ni
295# define ex_packadd ex_ni
296# define ex_packloadall ex_ni
297# define ex_return ex_ni
298# define ex_scriptnames ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000299# define ex_throw ex_ni
300# define ex_try ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000301# define ex_unlet ex_ni
Bram Moolenaar65c1b012005-01-31 19:02:28 +0000302# define ex_unlockvar ex_ni
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100303# define ex_vim9script ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200304# define ex_while ex_ni
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100305# define ex_import ex_ni
306# define ex_export ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000307#endif
Bram Moolenaar84538072019-07-28 14:15:42 +0200308#ifndef FEAT_SESSION
Bram Moolenaar071d4272004-06-13 20:20:40 +0000309# define ex_loadview ex_ni
310#endif
Bram Moolenaardefa0672019-07-21 19:25:37 +0200311#ifndef FEAT_VIMINFO
Bram Moolenaar071d4272004-06-13 20:20:40 +0000312# define ex_viminfo ex_ni
313#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100314static void ex_behave(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100315static void ex_filetype(exarg_T *eap);
316static void ex_setfiletype(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000317#ifndef FEAT_DIFF
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000318# define ex_diffoff ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000319# define ex_diffpatch ex_ni
320# define ex_diffgetput ex_ni
321# define ex_diffsplit ex_ni
322# define ex_diffthis ex_ni
323# define ex_diffupdate ex_ni
324#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100325static void ex_digraphs(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000326#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100327static void ex_nohlsearch(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000328#else
329# define ex_nohlsearch ex_ni
330# define ex_match ex_ni
331#endif
332#ifdef FEAT_CRYPT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100333static void ex_X(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000334#else
335# define ex_X ex_ni
336#endif
337#ifdef FEAT_FOLDING
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100338static void ex_fold(exarg_T *eap);
339static void ex_foldopen(exarg_T *eap);
340static void ex_folddo(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000341#else
342# define ex_fold ex_ni
343# define ex_foldopen ex_ni
344# define ex_folddo ex_ni
345#endif
Bram Moolenaar13505972019-01-24 15:04:48 +0100346#if !(defined(HAVE_LOCALE_H) || defined(X_LOCALE))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000347# define ex_language ex_ni
348#endif
349#ifndef FEAT_SIGNS
350# define ex_sign ex_ni
351#endif
Bram Moolenaar009b2592004-10-24 19:18:58 +0000352#ifndef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200353# define ex_nbclose ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000354# define ex_nbkey ex_ni
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200355# define ex_nbstart ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000356#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000357
Bram Moolenaar071d4272004-06-13 20:20:40 +0000358#ifndef FEAT_JUMPLIST
359# define ex_jumps ex_ni
Bram Moolenaar2d358992016-06-12 21:20:54 +0200360# define ex_clearjumps ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000361# define ex_changes ex_ni
362#endif
363
Bram Moolenaar05159a02005-02-26 23:04:13 +0000364#ifndef FEAT_PROFILE
365# define ex_profile ex_ni
366#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200367#ifndef FEAT_TERMINAL
368# define ex_terminal ex_ni
369#endif
Bram Moolenaard4aa83a2019-05-09 18:59:31 +0200370#if !defined(FEAT_X11) || !defined(FEAT_XCLIPBOARD)
371# define ex_xrestore ex_ni
372#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100373#if !defined(FEAT_PROP_POPUP)
Bram Moolenaar682725c2019-05-25 20:10:37 +0200374# define ex_popupclear ex_ni
375#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +0000376
Bram Moolenaar071d4272004-06-13 20:20:40 +0000377/*
378 * Declare cmdnames[].
379 */
380#define DO_DECLARE_EXCMD
381#include "ex_cmds.h"
Bram Moolenaar6de5e122017-04-20 21:55:44 +0200382#include "ex_cmdidxs.h"
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +0100383
Bram Moolenaar071d4272004-06-13 20:20:40 +0000384static char_u dollar_command[2] = {'$', 0};
385
386
387#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100388// Struct for storing a line inside a while/for loop
Bram Moolenaar071d4272004-06-13 20:20:40 +0000389typedef struct
390{
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100391 char_u *line; // command line
392 linenr_T lnum; // sourcing_lnum of the line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000393} wcmd_T;
394
395/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000396 * Structure used to store info for line position in a while or for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000397 * This is required, because do_one_cmd() may invoke ex_function(), which
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000398 * reads more lines that may come from the while/for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000399 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000400struct loop_cookie
Bram Moolenaar071d4272004-06-13 20:20:40 +0000401{
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100402 garray_T *lines_gap; // growarray with line info
403 int current_line; // last read line from growarray
404 int repeating; // TRUE when looping a second time
405 // When "repeating" is FALSE use "getline" and "cookie" to get lines
Bram Moolenaare96a2492019-06-25 04:12:16 +0200406 char_u *(*getline)(int, void *, int, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000407 void *cookie;
408};
409
Bram Moolenaare96a2492019-06-25 04:12:16 +0200410static char_u *get_loop_line(int c, void *cookie, int indent, int do_concat);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100411static int store_loop_line(garray_T *gap, char_u *line);
412static void free_cmdlines(garray_T *gap);
Bram Moolenaared203462004-06-16 11:19:22 +0000413
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100414// Struct to save a few things while debugging. Used in do_cmdline() only.
Bram Moolenaared203462004-06-16 11:19:22 +0000415struct dbg_stuff
416{
417 int trylevel;
418 int force_abort;
419 except_T *caught_stack;
420 char_u *vv_exception;
421 char_u *vv_throwpoint;
422 int did_emsg;
423 int got_int;
424 int did_throw;
425 int need_rethrow;
426 int check_cstack;
427 except_T *current_exception;
428};
429
Bram Moolenaared203462004-06-16 11:19:22 +0000430 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100431save_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000432{
433 dsp->trylevel = trylevel; trylevel = 0;
434 dsp->force_abort = force_abort; force_abort = FALSE;
435 dsp->caught_stack = caught_stack; caught_stack = NULL;
436 dsp->vv_exception = v_exception(NULL);
437 dsp->vv_throwpoint = v_throwpoint(NULL);
438
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100439 // Necessary for debugging an inactive ":catch", ":finally", ":endtry"
Bram Moolenaared203462004-06-16 11:19:22 +0000440 dsp->did_emsg = did_emsg; did_emsg = FALSE;
441 dsp->got_int = got_int; got_int = FALSE;
442 dsp->did_throw = did_throw; did_throw = FALSE;
443 dsp->need_rethrow = need_rethrow; need_rethrow = FALSE;
444 dsp->check_cstack = check_cstack; check_cstack = FALSE;
445 dsp->current_exception = current_exception; current_exception = NULL;
446}
447
448 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100449restore_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000450{
451 suppress_errthrow = FALSE;
452 trylevel = dsp->trylevel;
453 force_abort = dsp->force_abort;
454 caught_stack = dsp->caught_stack;
455 (void)v_exception(dsp->vv_exception);
456 (void)v_throwpoint(dsp->vv_throwpoint);
457 did_emsg = dsp->did_emsg;
458 got_int = dsp->got_int;
459 did_throw = dsp->did_throw;
460 need_rethrow = dsp->need_rethrow;
461 check_cstack = dsp->check_cstack;
462 current_exception = dsp->current_exception;
463}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000464#endif
465
Bram Moolenaar071d4272004-06-13 20:20:40 +0000466/*
467 * do_exmode(): Repeatedly get commands for the "Ex" mode, until the ":vi"
468 * command is given.
469 */
470 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100471do_exmode(
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100472 int improved) // TRUE for "improved Ex" mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000473{
474 int save_msg_scroll;
475 int prev_msg_row;
476 linenr_T prev_line;
Bram Moolenaar79518e22017-02-17 16:31:35 +0100477 varnumber_T changedtick;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000478
479 if (improved)
480 exmode_active = EXMODE_VIM;
481 else
482 exmode_active = EXMODE_NORMAL;
483 State = NORMAL;
484
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100485 // When using ":global /pat/ visual" and then "Q" we return to continue
486 // the :global command.
Bram Moolenaardf177f62005-02-22 08:39:57 +0000487 if (global_busy)
488 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000489
490 save_msg_scroll = msg_scroll;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100491 ++RedrawingDisabled; // don't redisplay the window
492 ++no_wait_return; // don't wait for return
Bram Moolenaar071d4272004-06-13 20:20:40 +0000493#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100494 // Ignore scrollbar and mouse events in Ex mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000495 ++hold_gui_events;
496#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000497
Bram Moolenaar32526b32019-01-19 17:43:09 +0100498 msg(_("Entering Ex mode. Type \"visual\" to go to Normal mode."));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000499 while (exmode_active)
500 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100501 // Check for a ":normal" command and no more characters left.
Bram Moolenaar7c626922005-02-07 22:01:03 +0000502 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
503 {
504 exmode_active = FALSE;
505 break;
506 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000507 msg_scroll = TRUE;
508 need_wait_return = FALSE;
509 ex_pressedreturn = FALSE;
510 ex_no_reprint = FALSE;
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100511 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000512 prev_msg_row = msg_row;
513 prev_line = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000514 if (improved)
515 {
516 cmdline_row = msg_row;
517 do_cmdline(NULL, getexline, NULL, 0);
518 }
519 else
520 do_cmdline(NULL, getexmodeline, NULL, DOCMD_NOWAIT);
521 lines_left = Rows - 1;
522
Bram Moolenaardf177f62005-02-22 08:39:57 +0000523 if ((prev_line != curwin->w_cursor.lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100524 || changedtick != CHANGEDTICK(curbuf)) && !ex_no_reprint)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000525 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000526 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100527 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000528 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000529 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000530 if (ex_pressedreturn)
531 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100532 // go up one line, to overwrite the ":<CR>" line, so the
533 // output doesn't contain empty lines.
Bram Moolenaardf177f62005-02-22 08:39:57 +0000534 msg_row = prev_msg_row;
535 if (prev_msg_row == Rows - 1)
536 msg_row--;
537 }
538 msg_col = 0;
539 print_line_no_prefix(curwin->w_cursor.lnum, FALSE, FALSE);
540 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000541 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000542 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100543 else if (ex_pressedreturn && !ex_no_reprint) // must be at EOF
Bram Moolenaardf177f62005-02-22 08:39:57 +0000544 {
545 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100546 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000547 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100548 emsg(_("E501: At end-of-file"));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000549 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000550 }
551
552#ifdef FEAT_GUI
553 --hold_gui_events;
554#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000555 --RedrawingDisabled;
556 --no_wait_return;
557 update_screen(CLEAR);
558 need_wait_return = FALSE;
559 msg_scroll = save_msg_scroll;
560}
561
562/*
Bram Moolenaar4facea32019-10-12 20:17:40 +0200563 * Print the executed command for when 'verbose' is set.
564 * When "lnum" is 0 only print the command.
565 */
566 static void
567msg_verbose_cmd(linenr_T lnum, char_u *cmd)
568{
569 ++no_wait_return;
570 verbose_enter_scroll();
571
572 if (lnum == 0)
573 smsg(_("Executing: %s"), cmd);
574 else
575 smsg(_("line %ld: %s"), (long)lnum, cmd);
576 if (msg_silent == 0)
577 msg_puts("\n"); // don't overwrite this
578
579 verbose_leave_scroll();
580 --no_wait_return;
581}
582
583/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000584 * Execute a simple command line. Used for translated commands like "*".
585 */
586 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100587do_cmdline_cmd(char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000588{
589 return do_cmdline(cmd, NULL, NULL,
590 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
591}
592
593/*
594 * do_cmdline(): execute one Ex command line
595 *
596 * 1. Execute "cmdline" when it is not NULL.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100597 * If "cmdline" is NULL, or more lines are needed, fgetline() is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000598 * 2. Split up in parts separated with '|'.
599 *
600 * This function can be called recursively!
601 *
602 * flags:
603 * DOCMD_VERBOSE - The command will be included in the error message.
604 * DOCMD_NOWAIT - Don't call wait_return() and friends.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100605 * DOCMD_REPEAT - Repeat execution until fgetline() returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000606 * DOCMD_KEYTYPED - Don't reset KeyTyped.
607 * DOCMD_EXCRESET - Reset the exception environment (used for debugging).
608 * DOCMD_KEEPLINE - Store first typed line (for repeating with ".").
609 *
610 * return FAIL if cmdline could not be executed, OK otherwise
611 */
612 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100613do_cmdline(
614 char_u *cmdline,
Bram Moolenaare96a2492019-06-25 04:12:16 +0200615 char_u *(*fgetline)(int, void *, int, int),
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100616 void *cookie, // argument for fgetline()
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100617 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000618{
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100619 char_u *next_cmdline; // next cmd to execute
620 char_u *cmdline_copy = NULL; // copy of cmd line
621 int used_getline = FALSE; // used "fgetline" to obtain command
622 static int recursive = 0; // recursive depth
Bram Moolenaar071d4272004-06-13 20:20:40 +0000623 int msg_didout_before_start = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100624 int count = 0; // line number count
625 int did_inc = FALSE; // incremented RedrawingDisabled
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626 int retval = OK;
627#ifdef FEAT_EVAL
Bram Moolenaarddef1292019-12-16 17:10:33 +0100628 cstack_T cstack; // conditional stack
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100629 garray_T lines_ga; // keep lines for ":while"/":for"
630 int current_line = 0; // active line in lines_ga
Bram Moolenaard5053d02020-06-28 15:51:16 +0200631 int current_line_before = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100632 char_u *fname = NULL; // function or script name
633 linenr_T *breakpoint = NULL; // ptr to breakpoint field in cookie
634 int *dbg_tick = NULL; // ptr to dbg_tick field in cookie
635 struct dbg_stuff debug_saved; // saved things for debug mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000636 int initial_trylevel;
Bram Moolenaar25e0f582020-05-25 22:36:50 +0200637 msglist_T **saved_msg_list = NULL;
638 msglist_T *private_msg_list;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000639
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100640 // "fgetline" and "cookie" passed to do_one_cmd()
Bram Moolenaare96a2492019-06-25 04:12:16 +0200641 char_u *(*cmd_getline)(int, void *, int, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642 void *cmd_cookie;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000643 struct loop_cookie cmd_loop_cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000644 void *real_cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000645 int getline_is_func;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000646#else
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100647# define cmd_getline fgetline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000648# define cmd_cookie cookie
649#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100650 static int call_depth = 0; // recursiveness
Bram Moolenaar2196bce2020-04-12 20:01:11 +0200651#ifdef FEAT_EVAL
Bram Moolenaare31ee862020-01-07 20:59:34 +0100652 ESTACK_CHECK_DECLARATION
Bram Moolenaar071d4272004-06-13 20:20:40 +0000653
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100654 // For every pair of do_cmdline()/do_one_cmd() calls, use an extra memory
655 // location for storing error messages to be converted to an exception.
656 // This ensures that the do_errthrow() call in do_one_cmd() does not
657 // combine the messages stored by an earlier invocation of do_one_cmd()
658 // with the command name of the later one. This would happen when
659 // BufWritePost autocommands are executed after a write error.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000660 saved_msg_list = msg_list;
661 msg_list = &private_msg_list;
662 private_msg_list = NULL;
663#endif
664
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100665 // It's possible to create an endless loop with ":execute", catch that
666 // here. The value of 200 allows nested function calls, ":source", etc.
667 // Allow 200 or 'maxfuncdepth', whatever is larger.
Bram Moolenaarb094ff42017-01-02 16:16:39 +0100668 if (call_depth >= 200
669#ifdef FEAT_EVAL
670 && call_depth >= p_mfd
671#endif
672 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000673 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100674 emsg(_("E169: Command too recursive"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100676 // When converting to an exception, we do not include the command name
677 // since this is not an error of the specific command.
Bram Moolenaarddef1292019-12-16 17:10:33 +0100678 do_errthrow((cstack_T *)NULL, (char_u *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679 msg_list = saved_msg_list;
680#endif
681 return FAIL;
682 }
683 ++call_depth;
684
685#ifdef FEAT_EVAL
686 cstack.cs_idx = -1;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000687 cstack.cs_looplevel = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688 cstack.cs_trylevel = 0;
689 cstack.cs_emsg_silent_list = NULL;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000690 cstack.cs_lflags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691 ga_init2(&lines_ga, (int)sizeof(wcmd_T), 10);
692
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100693 real_cookie = getline_cookie(fgetline, cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100695 // Inside a function use a higher nesting level.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100696 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000697 if (getline_is_func && ex_nesting_level == func_level(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698 ++ex_nesting_level;
699
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100700 // Get the function or script name and the address where the next breakpoint
701 // line and the debug tick for a function or script are stored.
Bram Moolenaar05159a02005-02-26 23:04:13 +0000702 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000703 {
704 fname = func_name(real_cookie);
705 breakpoint = func_breakpoint(real_cookie);
706 dbg_tick = func_dbg_tick(real_cookie);
707 }
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100708 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100710 fname = SOURCING_NAME;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000711 breakpoint = source_breakpoint(real_cookie);
712 dbg_tick = source_dbg_tick(real_cookie);
713 }
714
715 /*
716 * Initialize "force_abort" and "suppress_errthrow" at the top level.
717 */
718 if (!recursive)
719 {
720 force_abort = FALSE;
721 suppress_errthrow = FALSE;
722 }
723
724 /*
725 * If requested, store and reset the global values controlling the
Bram Moolenaar89d40322006-08-29 15:30:07 +0000726 * exception handling (used when debugging). Otherwise clear it to avoid
727 * a bogus compiler warning when the optimizer uses inline functions...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000728 */
Bram Moolenaarb4872942006-05-13 10:32:52 +0000729 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +0000730 save_dbg_stuff(&debug_saved);
Bram Moolenaar89d40322006-08-29 15:30:07 +0000731 else
Bram Moolenaara80faa82020-04-12 19:37:17 +0200732 CLEAR_FIELD(debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000733
734 initial_trylevel = trylevel;
735
736 /*
737 * "did_throw" will be set to TRUE when an exception is being thrown.
738 */
739 did_throw = FALSE;
740#endif
741 /*
742 * "did_emsg" will be set to TRUE when emsg() is used, in which case we
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000743 * cancel the whole command line, and any if/endif or loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000744 * If force_abort is set, we cancel everything.
745 */
746 did_emsg = FALSE;
747
748 /*
749 * KeyTyped is only set when calling vgetc(). Reset it here when not
750 * calling vgetc() (sourced command lines).
751 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100752 if (!(flags & DOCMD_KEYTYPED)
753 && !getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754 KeyTyped = FALSE;
755
756 /*
757 * Continue executing command lines:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000758 * - when inside an ":if", ":while" or ":for"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000759 * - for multiple commands on one line, separated with '|'
760 * - when repeating until there are no more lines (for ":source")
761 */
762 next_cmdline = cmdline;
763 do
764 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000765#ifdef FEAT_EVAL
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100766 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000767#endif
768
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100769 // stop skipping cmds for an error msg after all endif/while/for
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770 if (next_cmdline == NULL
771#ifdef FEAT_EVAL
772 && !force_abort
773 && cstack.cs_idx < 0
Bram Moolenaar05159a02005-02-26 23:04:13 +0000774 && !(getline_is_func && func_has_abort(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775#endif
776 )
777 did_emsg = FALSE;
778
779 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000780 * 1. If repeating a line in a loop, get a line from lines_ga.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100781 * 2. If no line given: Get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782 * 3. If a line is given: Make a copy, so we can mess with it.
783 */
784
785#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100786 // 1. If repeating, get a previous line from lines_ga.
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000787 if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100789 // Each '|' separated command is stored separately in lines_ga, to
790 // be able to jump to it. Don't use next_cmdline now.
Bram Moolenaard23a8232018-02-10 18:45:26 +0100791 VIM_CLEAR(cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100793 // Check if a function has returned or, unless it has an unclosed
794 // try conditional, aborted.
Bram Moolenaar05159a02005-02-26 23:04:13 +0000795 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000797# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000798 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000799 func_line_end(real_cookie);
800# endif
801 if (func_has_ended(real_cookie))
802 {
803 retval = FAIL;
804 break;
805 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000807#ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000808 else if (do_profiling == PROF_YES
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100809 && getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000810 script_line_end();
811#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000812
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100813 // Check if a sourced file hit a ":finish" command.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100814 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815 {
816 retval = FAIL;
817 break;
818 }
819
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100820 // If breakpoints have been added/deleted need to check for it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000821 if (breakpoint != NULL && dbg_tick != NULL
822 && *dbg_tick != debug_tick)
823 {
824 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100825 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100826 fname, SOURCING_LNUM);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827 *dbg_tick = debug_tick;
828 }
829
830 next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100831 SOURCING_LNUM = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100833 // Did we encounter a breakpoint?
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834 if (breakpoint != NULL && *breakpoint != 0
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100835 && *breakpoint <= SOURCING_LNUM)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000836 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100837 dbg_breakpoint(fname, SOURCING_LNUM);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100838 // Find next breakpoint.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100840 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100841 fname, SOURCING_LNUM);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842 *dbg_tick = debug_tick;
843 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000844# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000845 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000846 {
847 if (getline_is_func)
848 func_line_start(real_cookie);
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100849 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000850 script_line_start();
851 }
852# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000853 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854#endif
855
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100856 // 2. If no line given, get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000857 if (next_cmdline == NULL)
858 {
859 /*
860 * Need to set msg_didout for the first line after an ":if",
861 * otherwise the ":if" will be overwritten.
862 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100863 if (count == 1 && getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000864 msg_didout = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100865 if (fgetline == NULL || (next_cmdline = fgetline(':', cookie,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000866#ifdef FEAT_EVAL
867 cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2
868#else
869 0
870#endif
Bram Moolenaare96a2492019-06-25 04:12:16 +0200871 , TRUE)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000872 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100873 // Don't call wait_return for aborted command line. The NULL
874 // returned for the end of a sourced file or executed function
875 // doesn't do this.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876 if (KeyTyped && !(flags & DOCMD_REPEAT))
877 need_wait_return = FALSE;
878 retval = FAIL;
879 break;
880 }
881 used_getline = TRUE;
882
883 /*
884 * Keep the first typed line. Clear it when more lines are typed.
885 */
886 if (flags & DOCMD_KEEPLINE)
887 {
888 vim_free(repeat_cmdline);
889 if (count == 0)
890 repeat_cmdline = vim_strsave(next_cmdline);
891 else
892 repeat_cmdline = NULL;
893 }
894 }
895
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100896 // 3. Make a copy of the command so we can mess with it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000897 else if (cmdline_copy == NULL)
898 {
899 next_cmdline = vim_strsave(next_cmdline);
900 if (next_cmdline == NULL)
901 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100902 emsg(_(e_outofmem));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000903 retval = FAIL;
904 break;
905 }
906 }
907 cmdline_copy = next_cmdline;
908
909#ifdef FEAT_EVAL
910 /*
Bram Moolenaard5053d02020-06-28 15:51:16 +0200911 * Inside a while/for loop, and when the command looks like a ":while"
912 * or ":for", the line is stored, because we may need it later when
913 * looping.
914 *
915 * When there is a '|' and another command, it is stored separately,
916 * because we need to be able to jump back to it from an
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000917 * :endwhile/:endfor.
Bram Moolenaard5053d02020-06-28 15:51:16 +0200918 *
919 * Pass a different "fgetline" function to do_one_cmd() below,
920 * that it stores lines in or reads them from "lines_ga". Makes it
921 * possible to define a function inside a while/for loop and handles
922 * line continuation.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000923 */
Bram Moolenaard5053d02020-06-28 15:51:16 +0200924 if ((cstack.cs_looplevel > 0 || has_loop_cmd(next_cmdline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000925 {
Bram Moolenaard5053d02020-06-28 15:51:16 +0200926 cmd_getline = get_loop_line;
927 cmd_cookie = (void *)&cmd_loop_cookie;
928 cmd_loop_cookie.lines_gap = &lines_ga;
929 cmd_loop_cookie.current_line = current_line;
930 cmd_loop_cookie.getline = fgetline;
931 cmd_loop_cookie.cookie = cookie;
932 cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len);
933
934 // Save the current line when encountering it the first time.
935 if (current_line == lines_ga.ga_len
936 && store_loop_line(&lines_ga, next_cmdline) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937 {
938 retval = FAIL;
939 break;
940 }
Bram Moolenaard5053d02020-06-28 15:51:16 +0200941 current_line_before = current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000942 }
Bram Moolenaard5053d02020-06-28 15:51:16 +0200943 else
944 {
945 cmd_getline = fgetline;
946 cmd_cookie = cookie;
947 }
948
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949 did_endif = FALSE;
950#endif
951
952 if (count++ == 0)
953 {
954 /*
955 * All output from the commands is put below each other, without
956 * waiting for a return. Don't do this when executing commands
957 * from a script or when being called recursive (e.g. for ":e
958 * +command file").
959 */
960 if (!(flags & DOCMD_NOWAIT) && !recursive)
961 {
962 msg_didout_before_start = msg_didout;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100963 msg_didany = FALSE; // no output yet
Bram Moolenaar071d4272004-06-13 20:20:40 +0000964 msg_start();
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100965 msg_scroll = TRUE; // put messages below each other
966 ++no_wait_return; // don't wait for return until finished
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967 ++RedrawingDisabled;
968 did_inc = TRUE;
969 }
970 }
971
Bram Moolenaar823654b2020-05-29 23:03:09 +0200972 if ((p_verbose >= 15 && SOURCING_NAME != NULL) || p_verbose >= 16)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100973 msg_verbose_cmd(SOURCING_LNUM, cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000974
975 /*
976 * 2. Execute one '|' separated command.
977 * do_one_cmd() will return NULL if there is no trailing '|'.
978 * "cmdline_copy" can change, e.g. for '%' and '#' expansion.
979 */
980 ++recursive;
981 next_cmdline = do_one_cmd(&cmdline_copy, flags & DOCMD_VERBOSE,
982#ifdef FEAT_EVAL
983 &cstack,
984#endif
985 cmd_getline, cmd_cookie);
986 --recursive;
987
988#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000989 if (cmd_cookie == (void *)&cmd_loop_cookie)
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100990 // Use "current_line" from "cmd_loop_cookie", it may have been
991 // incremented when defining a function.
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000992 current_line = cmd_loop_cookie.current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993#endif
994
995 if (next_cmdline == NULL)
996 {
Bram Moolenaard23a8232018-02-10 18:45:26 +0100997 VIM_CLEAR(cmdline_copy);
Bram Moolenaard7663c22019-08-06 21:59:57 +0200998
Bram Moolenaar071d4272004-06-13 20:20:40 +0000999 /*
1000 * If the command was typed, remember it for the ':' register.
1001 * Do this AFTER executing the command to make :@: work.
1002 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001003 if (getline_equal(fgetline, cookie, getexline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001004 && new_last_cmdline != NULL)
1005 {
1006 vim_free(last_cmdline);
1007 last_cmdline = new_last_cmdline;
1008 new_last_cmdline = NULL;
1009 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010 }
1011 else
1012 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001013 // need to copy the command after the '|' to cmdline_copy, for the
1014 // next do_one_cmd()
Bram Moolenaara7241f52008-06-24 20:39:31 +00001015 STRMOVE(cmdline_copy, next_cmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016 next_cmdline = cmdline_copy;
1017 }
1018
1019
1020#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001021 // reset did_emsg for a function that is not aborted by an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022 if (did_emsg && !force_abort
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001023 && getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001024 && !func_has_abort(real_cookie))
1025 did_emsg = FALSE;
1026
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001027 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028 {
1029 ++current_line;
1030
1031 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001032 * An ":endwhile", ":endfor" and ":continue" is handled here.
1033 * If we were executing commands, jump back to the ":while" or
1034 * ":for".
1035 * If we were not executing commands, decrement cs_looplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001037 if (cstack.cs_lflags & (CSL_HAD_CONT | CSL_HAD_ENDLOOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001039 cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001041 // Jump back to the matching ":while" or ":for". Be careful
1042 // not to use a cs_line[] from an entry that isn't a ":while"
1043 // or ":for": It would make "current_line" invalid and can
1044 // cause a crash.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045 if (!did_emsg && !got_int && !did_throw
1046 && cstack.cs_idx >= 0
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001047 && (cstack.cs_flags[cstack.cs_idx]
1048 & (CSF_WHILE | CSF_FOR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049 && cstack.cs_line[cstack.cs_idx] >= 0
1050 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE))
1051 {
1052 current_line = cstack.cs_line[cstack.cs_idx];
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001053 // remember we jumped there
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001054 cstack.cs_lflags |= CSL_HAD_LOOP;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001055 line_breakcheck(); // check if CTRL-C typed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001057 // Check for the next breakpoint at or after the ":while"
1058 // or ":for".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059 if (breakpoint != NULL)
1060 {
1061 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001062 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063 fname,
1064 ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1);
1065 *dbg_tick = debug_tick;
1066 }
1067 }
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001068 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001070 // can only get here with ":endwhile" or ":endfor"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071 if (cstack.cs_idx >= 0)
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001072 rewind_conditionals(&cstack, cstack.cs_idx - 1,
1073 CSF_WHILE | CSF_FOR, &cstack.cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074 }
1075 }
1076
1077 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001078 * For a ":while" or ":for" we need to remember the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001080 else if (cstack.cs_lflags & CSL_HAD_LOOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001082 cstack.cs_lflags &= ~CSL_HAD_LOOP;
Bram Moolenaard5053d02020-06-28 15:51:16 +02001083 cstack.cs_line[cstack.cs_idx] = current_line_before;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084 }
1085 }
1086
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001087 // Check for the next breakpoint after a watchexpression
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01001088 if (breakpoint != NULL && has_watchexpr())
1089 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001090 *breakpoint = dbg_find_breakpoint(FALSE, fname, SOURCING_LNUM);
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01001091 *dbg_tick = debug_tick;
1092 }
1093
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094 /*
1095 * When not inside any ":while" loop, clear remembered lines.
1096 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001097 if (cstack.cs_looplevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098 {
1099 if (lines_ga.ga_len > 0)
1100 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001101 SOURCING_LNUM =
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102 ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum;
1103 free_cmdlines(&lines_ga);
1104 }
1105 current_line = 0;
1106 }
1107
1108 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001109 * A ":finally" makes did_emsg, got_int, and did_throw pending for
1110 * being restored at the ":endtry". Reset them here and set the
1111 * ACTIVE and FINALLY flags, so that the finally clause gets executed.
1112 * This includes the case where a missing ":endif", ":endwhile" or
1113 * ":endfor" was detected by the ":finally" itself.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001115 if (cstack.cs_lflags & CSL_HAD_FINA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001116 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001117 cstack.cs_lflags &= ~CSL_HAD_FINA;
1118 report_make_pending(cstack.cs_pending[cstack.cs_idx]
1119 & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001120 did_throw ? (void *)current_exception : NULL);
1121 did_emsg = got_int = did_throw = FALSE;
1122 cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY;
1123 }
1124
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001125 // Update global "trylevel" for recursive calls to do_cmdline() from
1126 // within this loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001127 trylevel = initial_trylevel + cstack.cs_trylevel;
1128
1129 /*
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001130 * If the outermost try conditional (across function calls and sourced
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131 * files) is aborted because of an error, an interrupt, or an uncaught
1132 * exception, cancel everything. If it is left normally, reset
1133 * force_abort to get the non-EH compatible abortion behavior for
1134 * the rest of the script.
1135 */
1136 if (trylevel == 0 && !did_emsg && !got_int && !did_throw)
1137 force_abort = FALSE;
1138
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001139 // Convert an interrupt to an exception if appropriate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001140 (void)do_intthrow(&cstack);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001141#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142
1143 }
1144 /*
1145 * Continue executing command lines when:
1146 * - no CTRL-C typed, no aborting error, no exception thrown or try
1147 * conditionals need to be checked for executing finally clauses or
1148 * catching an interrupt exception
1149 * - didn't get an error message or lines are not typed
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001150 * - there is a command after '|', inside a :if, :while, :for or :try, or
Bram Moolenaar071d4272004-06-13 20:20:40 +00001151 * looping for ":source" command or function call.
1152 */
1153 while (!((got_int
1154#ifdef FEAT_EVAL
1155 || (did_emsg && force_abort) || did_throw
1156#endif
1157 )
1158#ifdef FEAT_EVAL
1159 && cstack.cs_trylevel == 0
1160#endif
1161 )
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001162 && !(did_emsg
1163#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001164 // Keep going when inside try/catch, so that the error can be
1165 // deal with, except when it is a syntax error, it may cause
1166 // the :endtry to be missed.
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001167 && (cstack.cs_trylevel == 0 || did_emsg_syntax)
1168#endif
1169 && used_getline
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001170 && (getline_equal(fgetline, cookie, getexmodeline)
1171 || getline_equal(fgetline, cookie, getexline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172 && (next_cmdline != NULL
1173#ifdef FEAT_EVAL
1174 || cstack.cs_idx >= 0
1175#endif
1176 || (flags & DOCMD_REPEAT)));
1177
1178 vim_free(cmdline_copy);
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001179 did_emsg_syntax = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180#ifdef FEAT_EVAL
1181 free_cmdlines(&lines_ga);
1182 ga_clear(&lines_ga);
1183
1184 if (cstack.cs_idx >= 0)
1185 {
1186 /*
1187 * If a sourced file or executed function ran to its end, report the
1188 * unclosed conditional.
1189 */
1190 if (!got_int && !did_throw
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001191 && ((getline_equal(fgetline, cookie, getsourceline)
1192 && !source_finished(fgetline, cookie))
1193 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194 && !func_has_ended(real_cookie))))
1195 {
1196 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001197 emsg(_(e_endtry));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001199 emsg(_(e_endwhile));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001200 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001201 emsg(_(e_endfor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001203 emsg(_(e_endif));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204 }
1205
1206 /*
1207 * Reset "trylevel" in case of a ":finish" or ":return" or a missing
1208 * ":endtry" in a sourced file or executed function. If the try
1209 * conditional is in its finally clause, ignore anything pending.
1210 * If it is in a catch clause, finish the caught exception.
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001211 * Also cleanup any "cs_forinfo" structures.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212 */
1213 do
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001214 {
1215 int idx = cleanup_conditionals(&cstack, 0, TRUE);
1216
Bram Moolenaar89e5d682005-01-17 22:06:23 +00001217 if (idx >= 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001218 --idx; // remove try block not in its finally clause
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001219 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
1220 &cstack.cs_looplevel);
1221 }
1222 while (cstack.cs_idx >= 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223 trylevel = initial_trylevel;
1224 }
1225
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001226 // If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
1227 // lack was reported above and the error message is to be converted to an
1228 // exception, do this now after rewinding the cstack.
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001229 do_errthrow(&cstack, getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001230 ? (char_u *)"endfunction" : (char_u *)NULL);
1231
1232 if (trylevel == 0)
1233 {
1234 /*
1235 * When an exception is being thrown out of the outermost try
1236 * conditional, discard the uncaught exception, disable the conversion
1237 * of interrupts or errors to exceptions, and ensure that no more
1238 * commands are executed.
1239 */
1240 if (did_throw)
1241 {
1242 void *p = NULL;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02001243 msglist_T *messages = NULL, *next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244
1245 /*
1246 * If the uncaught exception is a user exception, report it as an
1247 * error. If it is an error exception, display the saved error
1248 * message now. For an interrupt exception, do nothing; the
1249 * interrupt message is given elsewhere.
1250 */
1251 switch (current_exception->type)
1252 {
1253 case ET_USER:
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001254 vim_snprintf((char *)IObuff, IOSIZE,
1255 _("E605: Exception not caught: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256 current_exception->value);
1257 p = vim_strsave(IObuff);
1258 break;
1259 case ET_ERROR:
1260 messages = current_exception->messages;
1261 current_exception->messages = NULL;
1262 break;
1263 case ET_INTERRUPT:
1264 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265 }
1266
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001267 estack_push(ETYPE_EXCEPT, current_exception->throw_name,
1268 current_exception->throw_lnum);
Bram Moolenaare31ee862020-01-07 20:59:34 +01001269 ESTACK_CHECK_SETUP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270 current_exception->throw_name = NULL;
1271
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001272 discard_current_exception(); // uses IObuff if 'verbose'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273 suppress_errthrow = TRUE;
1274 force_abort = TRUE;
1275
1276 if (messages != NULL)
1277 {
1278 do
1279 {
1280 next = messages->next;
1281 emsg(messages->msg);
1282 vim_free(messages->msg);
Bram Moolenaar5fbf3bc2020-06-01 21:13:11 +02001283 vim_free(messages->sfile);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284 vim_free(messages);
1285 messages = next;
1286 }
1287 while (messages != NULL);
1288 }
1289 else if (p != NULL)
1290 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01001291 emsg(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292 vim_free(p);
1293 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001294 vim_free(SOURCING_NAME);
Bram Moolenaare31ee862020-01-07 20:59:34 +01001295 ESTACK_CHECK_NOW
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001296 estack_pop();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297 }
1298
1299 /*
1300 * On an interrupt or an aborting error not converted to an exception,
1301 * disable the conversion of errors to exceptions. (Interrupts are not
Bram Moolenaar2196bce2020-04-12 20:01:11 +02001302 * converted anymore, here.) This enables also the interrupt message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303 * when force_abort is set and did_emsg unset in case of an interrupt
1304 * from a finally clause after an error.
1305 */
1306 else if (got_int || (did_emsg && force_abort))
1307 suppress_errthrow = TRUE;
1308 }
1309
1310 /*
1311 * The current cstack will be freed when do_cmdline() returns. An uncaught
1312 * exception will have to be rethrown in the previous cstack. If a function
1313 * has just returned or a script file was just finished and the previous
1314 * cstack belongs to the same function or, respectively, script file, it
1315 * will have to be checked for finally clauses to be executed due to the
1316 * ":return" or ":finish". This is done in do_one_cmd().
1317 */
1318 if (did_throw)
1319 need_rethrow = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001320 if ((getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321 && ex_nesting_level > source_level(real_cookie))
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001322 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323 && ex_nesting_level > func_level(real_cookie) + 1))
1324 {
1325 if (!did_throw)
1326 check_cstack = TRUE;
1327 }
1328 else
1329 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001330 // When leaving a function, reduce nesting level.
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001331 if (getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332 --ex_nesting_level;
1333 /*
1334 * Go to debug mode when returning from a function in which we are
1335 * single-stepping.
1336 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001337 if ((getline_equal(fgetline, cookie, getsourceline)
1338 || getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339 && ex_nesting_level + 1 <= debug_break_level)
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001340 do_debug(getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341 ? (char_u *)_("End of sourced file")
1342 : (char_u *)_("End of function"));
1343 }
1344
1345 /*
1346 * Restore the exception environment (done after returning from the
1347 * debugger).
1348 */
1349 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001350 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351
1352 msg_list = saved_msg_list;
Bram Moolenaar292b90d2020-03-18 15:23:16 +01001353
1354 // Cleanup if "cs_emsg_silent_list" remains.
1355 if (cstack.cs_emsg_silent_list != NULL)
1356 {
1357 eslist_T *elem, *temp;
1358
1359 for (elem = cstack.cs_emsg_silent_list; elem != NULL; elem = temp)
1360 {
1361 temp = elem->next;
1362 vim_free(elem);
1363 }
1364 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001365#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366
1367 /*
1368 * If there was too much output to fit on the command line, ask the user to
1369 * hit return before redrawing the screen. With the ":global" command we do
1370 * this only once after the command is finished.
1371 */
1372 if (did_inc)
1373 {
1374 --RedrawingDisabled;
1375 --no_wait_return;
1376 msg_scroll = FALSE;
1377
1378 /*
1379 * When just finished an ":if"-":else" which was typed, no need to
1380 * wait for hit-return. Also for an error situation.
1381 */
1382 if (retval == FAIL
1383#ifdef FEAT_EVAL
1384 || (did_endif && KeyTyped && !did_emsg)
1385#endif
1386 )
1387 {
1388 need_wait_return = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001389 msg_didany = FALSE; // don't wait when restarting edit
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390 }
1391 else if (need_wait_return)
1392 {
1393 /*
1394 * The msg_start() above clears msg_didout. The wait_return we do
1395 * here should not overwrite the command that may be shown before
1396 * doing that.
1397 */
1398 msg_didout |= msg_didout_before_start;
1399 wait_return(FALSE);
1400 }
1401 }
1402
Bram Moolenaar2a942252012-11-28 23:03:07 +01001403#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001404 did_endif = FALSE; // in case do_cmdline used recursively
Bram Moolenaar2a942252012-11-28 23:03:07 +01001405#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406 /*
1407 * Reset if_level, in case a sourced script file contains more ":if" than
1408 * ":endif" (could be ":if x | foo | endif").
1409 */
1410 if_level = 0;
Bram Moolenaar2e18a122012-11-28 19:10:54 +01001411#endif
Bram Moolenaard4ad0d42012-11-28 17:34:48 +01001412
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413 --call_depth;
1414 return retval;
1415}
1416
1417#ifdef FEAT_EVAL
1418/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001419 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420 */
1421 static char_u *
Bram Moolenaare96a2492019-06-25 04:12:16 +02001422get_loop_line(int c, void *cookie, int indent, int do_concat)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001424 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425 wcmd_T *wp;
1426 char_u *line;
1427
1428 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1429 {
1430 if (cp->repeating)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001431 return NULL; // trying to read past ":endwhile"/":endfor"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001433 // First time inside the ":while"/":for": get line normally.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001434 if (cp->getline == NULL)
Bram Moolenaare96a2492019-06-25 04:12:16 +02001435 line = getcmdline(c, 0L, indent, do_concat);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001436 else
Bram Moolenaare96a2492019-06-25 04:12:16 +02001437 line = cp->getline(c, cp->cookie, indent, do_concat);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001438 if (line != NULL && store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001439 ++cp->current_line;
1440
1441 return line;
1442 }
1443
1444 KeyTyped = FALSE;
1445 ++cp->current_line;
1446 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001447 SOURCING_LNUM = wp->lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001448 return vim_strsave(wp->line);
1449}
1450
1451/*
1452 * Store a line in "gap" so that a ":while" loop can execute it again.
1453 */
1454 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001455store_loop_line(garray_T *gap, char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001456{
1457 if (ga_grow(gap, 1) == FAIL)
1458 return FAIL;
1459 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001460 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = SOURCING_LNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001461 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001462 return OK;
1463}
1464
1465/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001466 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001467 */
1468 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001469free_cmdlines(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470{
1471 while (gap->ga_len > 0)
1472 {
1473 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1474 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001475 }
1476}
1477#endif
1478
1479/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001480 * If "fgetline" is get_loop_line(), return TRUE if the getline it uses equals
1481 * "func". * Otherwise return TRUE when "fgetline" equals "func".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001484getline_equal(
Bram Moolenaare96a2492019-06-25 04:12:16 +02001485 char_u *(*fgetline)(int, void *, int, int),
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001486 void *cookie UNUSED, // argument for fgetline()
Bram Moolenaare96a2492019-06-25 04:12:16 +02001487 char_u *(*func)(int, void *, int, int))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488{
1489#ifdef FEAT_EVAL
Bram Moolenaare96a2492019-06-25 04:12:16 +02001490 char_u *(*gp)(int, void *, int, int);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001491 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001492
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001493 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1494 // function that's originally used to obtain the lines. This may be
1495 // nested several levels.
Bram Moolenaar89d40322006-08-29 15:30:07 +00001496 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001497 cp = (struct loop_cookie *)cookie;
1498 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001499 {
1500 gp = cp->getline;
1501 cp = cp->cookie;
1502 }
1503 return gp == func;
1504#else
Bram Moolenaar89d40322006-08-29 15:30:07 +00001505 return fgetline == func;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001506#endif
1507}
1508
Bram Moolenaar071d4272004-06-13 20:20:40 +00001509/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001510 * If "fgetline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511 * getline function. Otherwise return "cookie".
1512 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513 void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001514getline_cookie(
Bram Moolenaare96a2492019-06-25 04:12:16 +02001515 char_u *(*fgetline)(int, void *, int, int) UNUSED,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001516 void *cookie) // argument for fgetline()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517{
Bram Moolenaar13505972019-01-24 15:04:48 +01001518#ifdef FEAT_EVAL
Bram Moolenaare96a2492019-06-25 04:12:16 +02001519 char_u *(*gp)(int, void *, int, int);
Bram Moolenaard5053d02020-06-28 15:51:16 +02001520 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001521
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001522 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1523 // cookie that's originally used to obtain the lines. This may be nested
1524 // several levels.
Bram Moolenaar89d40322006-08-29 15:30:07 +00001525 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001526 cp = (struct loop_cookie *)cookie;
1527 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001528 {
1529 gp = cp->getline;
1530 cp = cp->cookie;
1531 }
1532 return cp;
Bram Moolenaar13505972019-01-24 15:04:48 +01001533#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001534 return cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001535#endif
Bram Moolenaar13505972019-01-24 15:04:48 +01001536}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537
Bram Moolenaard5053d02020-06-28 15:51:16 +02001538#if defined(FEAT_EVAL) || defined(PROT)
1539/*
1540 * Get the next line source line without advancing.
1541 */
1542 char_u *
1543getline_peek(
1544 char_u *(*fgetline)(int, void *, int, int) UNUSED,
1545 void *cookie) // argument for fgetline()
1546{
1547 char_u *(*gp)(int, void *, int, int);
1548 struct loop_cookie *cp;
1549 wcmd_T *wp;
1550
1551 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1552 // cookie that's originally used to obtain the lines. This may be nested
1553 // several levels.
1554 gp = fgetline;
1555 cp = (struct loop_cookie *)cookie;
1556 while (gp == get_loop_line)
1557 {
1558 if (cp->current_line + 1 < cp->lines_gap->ga_len)
1559 {
1560 // executing lines a second time, use the stored copy
1561 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line + 1;
1562 return wp->line;
1563 }
1564 gp = cp->getline;
1565 cp = cp->cookie;
1566 }
1567 if (gp == getsourceline)
1568 return source_nextline(cp);
1569 return NULL;
1570}
1571#endif
1572
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001573
1574/*
1575 * Helper function to apply an offset for buffer commands, i.e. ":bdelete",
1576 * ":bwipeout", etc.
1577 * Returns the buffer number.
1578 */
1579 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001580compute_buffer_local_count(int addr_type, int lnum, int offset)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001581{
1582 buf_T *buf;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001583 buf_T *nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001584 int count = offset;
1585
1586 buf = firstbuf;
1587 while (buf->b_next != NULL && buf->b_fnum < lnum)
1588 buf = buf->b_next;
1589 while (count != 0)
1590 {
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001591 count += (offset < 0) ? 1 : -1;
1592 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1593 if (nextbuf == NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001594 break;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001595 buf = nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001596 if (addr_type == ADDR_LOADED_BUFFERS)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001597 // skip over unloaded buffers
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001598 while (buf->b_ml.ml_mfp == NULL)
1599 {
1600 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1601 if (nextbuf == NULL)
1602 break;
1603 buf = nextbuf;
1604 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001605 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001606 // we might have gone too far, last buffer is not loadedd
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001607 if (addr_type == ADDR_LOADED_BUFFERS)
1608 while (buf->b_ml.ml_mfp == NULL)
1609 {
1610 nextbuf = (offset >= 0) ? buf->b_prev : buf->b_next;
1611 if (nextbuf == NULL)
1612 break;
1613 buf = nextbuf;
1614 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001615 return buf->b_fnum;
1616}
1617
Bram Moolenaarb7316892019-05-01 18:08:42 +02001618/*
1619 * Return the window number of "win".
1620 * When "win" is NULL return the number of windows.
1621 */
Bram Moolenaarf240e182014-11-27 18:33:02 +01001622 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001623current_win_nr(win_T *win)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001624{
1625 win_T *wp;
1626 int nr = 0;
1627
Bram Moolenaar29323592016-07-24 22:04:11 +02001628 FOR_ALL_WINDOWS(wp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001629 {
1630 ++nr;
1631 if (wp == win)
1632 break;
1633 }
1634 return nr;
1635}
1636
1637 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001638current_tab_nr(tabpage_T *tab)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001639{
1640 tabpage_T *tp;
1641 int nr = 0;
1642
Bram Moolenaar29323592016-07-24 22:04:11 +02001643 FOR_ALL_TABPAGES(tp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001644 {
1645 ++nr;
1646 if (tp == tab)
1647 break;
1648 }
1649 return nr;
1650}
1651
Bram Moolenaar6e36b1c2020-07-17 20:47:51 +02001652 static int
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001653comment_start(char_u *p, int starts_with_colon UNUSED)
1654{
1655#ifdef FEAT_EVAL
1656 if (in_vim9script())
1657 return p[0] == '#' && p[1] != '{' && !starts_with_colon;
1658#endif
1659 return *p == '"';
1660}
1661
Bram Moolenaarf240e182014-11-27 18:33:02 +01001662# define CURRENT_WIN_NR current_win_nr(curwin)
1663# define LAST_WIN_NR current_win_nr(NULL)
1664# define CURRENT_TAB_NR current_tab_nr(curtab)
1665# define LAST_TAB_NR current_tab_nr(NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001666
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667/*
1668 * Execute one Ex command.
1669 *
1670 * If 'sourcing' is TRUE, the command will be included in the error message.
1671 *
1672 * 1. skip comment lines and leading space
1673 * 2. handle command modifiers
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001674 * 3. find the command
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001675 * 4. parse range
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001676 * 5. Parse the command.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001677 * 6. parse arguments
1678 * 7. switch on command name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001679 *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001680 * Note: "fgetline" can be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681 *
1682 * This function may be called recursively!
1683 */
1684#if (_MSC_VER == 1200)
1685/*
Bram Moolenaared203462004-06-16 11:19:22 +00001686 * Avoid optimisation bug in VC++ version 6.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687 */
Bram Moolenaar281bdce2005-01-25 21:53:18 +00001688 #pragma optimize( "g", off )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689#endif
1690 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001691do_one_cmd(
Bram Moolenaarddef1292019-12-16 17:10:33 +01001692 char_u **cmdlinep,
1693 int sourcing,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001694#ifdef FEAT_EVAL
Bram Moolenaarddef1292019-12-16 17:10:33 +01001695 cstack_T *cstack,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001696#endif
Bram Moolenaarddef1292019-12-16 17:10:33 +01001697 char_u *(*fgetline)(int, void *, int, int),
1698 void *cookie) // argument for fgetline()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001699{
Bram Moolenaarddef1292019-12-16 17:10:33 +01001700 char_u *p;
1701 linenr_T lnum;
1702 long n;
1703 char *errormsg = NULL; // error message
1704 char_u *after_modifier = NULL;
1705 exarg_T ea; // Ex command arguments
1706 int save_msg_scroll = msg_scroll;
1707 cmdmod_T save_cmdmod;
1708 int save_reg_executing = reg_executing;
1709 int ni; // set when Not Implemented
1710 char_u *cmd;
Bram Moolenaar6e36b1c2020-07-17 20:47:51 +02001711 int starts_with_colon = FALSE;
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001712#ifdef FEAT_EVAL
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001713 int starts_with_quote;
1714 int vim9script = in_vim9script();
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001715#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716
Bram Moolenaara80faa82020-04-12 19:37:17 +02001717 CLEAR_FIELD(ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718 ea.line1 = 1;
1719 ea.line2 = 1;
1720#ifdef FEAT_EVAL
1721 ++ex_nesting_level;
1722#endif
1723
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001724 // When the last file has not been edited :q has to be typed twice.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725 if (quitmore
1726#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001727 // avoid that a function call in 'statusline' does this
Bram Moolenaar89d40322006-08-29 15:30:07 +00001728 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001729#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001730 // avoid that an autocommand, e.g. QuitPre, does this
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001731 && !getline_equal(fgetline, cookie, getnextac))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732 --quitmore;
1733
1734 /*
1735 * Reset browse, confirm, etc.. They are restored when returning, for
1736 * recursive calls.
1737 */
1738 save_cmdmod = cmdmod;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001740 // "#!anything" is handled like a comment.
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001741 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1742 goto doend;
1743
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02001744/*
1745 * 1. Skip comment lines and leading white space and colons.
1746 * 2. Handle command modifiers.
1747 */
1748 // The "ea" structure holds the arguments that can be used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749 ea.cmd = *cmdlinep;
Bram Moolenaareffed932018-08-14 13:38:17 +02001750 ea.cmdlinep = cmdlinep;
1751 ea.getline = fgetline;
1752 ea.cookie = cookie;
1753#ifdef FEAT_EVAL
1754 ea.cstack = cstack;
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001755 starts_with_colon = *skipwhite(ea.cmd) == ':';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001756#endif
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02001757 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaareffed932018-08-14 13:38:17 +02001758 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001760 after_modifier = ea.cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001761
1762#ifdef FEAT_EVAL
1763 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
1764 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
1765#else
1766 ea.skip = (if_level > 0);
1767#endif
1768
Bram Moolenaar071d4272004-06-13 20:20:40 +00001769/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001770 * 3. Skip over the range to find the command. Let "p" point to after it.
1771 *
1772 * We need the command to know what kind of range it uses.
1773 */
1774 cmd = ea.cmd;
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001775#ifdef FEAT_EVAL
1776 starts_with_quote = vim9script && *ea.cmd == '\'';
1777 if (!starts_with_quote)
1778#endif
1779 ea.cmd = skip_range(ea.cmd, NULL);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001780 if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
1781 ea.cmd = skipwhite(ea.cmd + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001782
1783#ifdef FEAT_EVAL
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001784 if (vim9script && !starts_with_colon)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02001785 {
1786 if (ea.cmd > cmd)
1787 {
1788 emsg(_(e_colon_required));
1789 goto doend;
1790 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001791 p = find_ex_command(&ea, NULL, lookup_scriptvar, NULL);
Bram Moolenaardf069ee2020-06-22 23:02:51 +02001792 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001793 else
1794#endif
1795 p = find_ex_command(&ea, NULL, NULL, NULL);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001796
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001797#ifdef FEAT_EVAL
1798# ifdef FEAT_PROFILE
1799 // Count this line for profiling if skip is TRUE.
1800 if (do_profiling == PROF_YES
1801 && (!ea.skip || cstack->cs_idx == 0 || (cstack->cs_idx > 0
1802 && (cstack->cs_flags[cstack->cs_idx - 1] & CSF_ACTIVE))))
1803 {
1804 int skip = did_emsg || got_int || did_throw;
1805
1806 if (ea.cmdidx == CMD_catch)
1807 skip = !skip && !(cstack->cs_idx >= 0
1808 && (cstack->cs_flags[cstack->cs_idx] & CSF_THROWN)
1809 && !(cstack->cs_flags[cstack->cs_idx] & CSF_CAUGHT));
1810 else if (ea.cmdidx == CMD_else || ea.cmdidx == CMD_elseif)
1811 skip = skip || !(cstack->cs_idx >= 0
1812 && !(cstack->cs_flags[cstack->cs_idx]
1813 & (CSF_ACTIVE | CSF_TRUE)));
1814 else if (ea.cmdidx == CMD_finally)
1815 skip = FALSE;
1816 else if (ea.cmdidx != CMD_endif
1817 && ea.cmdidx != CMD_endfor
1818 && ea.cmdidx != CMD_endtry
1819 && ea.cmdidx != CMD_endwhile)
1820 skip = ea.skip;
1821
1822 if (!skip)
1823 {
1824 if (getline_equal(fgetline, cookie, get_func_line))
1825 func_line_exec(getline_cookie(fgetline, cookie));
1826 else if (getline_equal(fgetline, cookie, getsourceline))
1827 script_line_exec();
1828 }
1829 }
1830# endif
1831
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001832 // May go to debug mode. If this happens and the ">quit" debug command is
1833 // used, throw an interrupt exception and skip the next command.
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001834 dbg_check_breakpoint(&ea);
1835 if (!ea.skip && got_int)
1836 {
1837 ea.skip = TRUE;
1838 (void)do_intthrow(cstack);
1839 }
1840#endif
1841
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001842/*
1843 * 4. parse a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844 *
1845 * where 'addr' is:
1846 *
1847 * % (entire file)
1848 * $ [+-NUM]
1849 * 'x [+-NUM] (where x denotes a currently defined mark)
1850 * . [+-NUM]
1851 * [+-NUM]..
1852 * NUM
1853 *
1854 * The ea.cmd pointer is updated to point to the first character following the
1855 * range spec. If an initial address is found, but no second, the upper bound
1856 * is equal to the lower.
1857 */
1858
Bram Moolenaar25190db2019-05-04 15:05:28 +02001859 // ea.addr_type for user commands is set by find_ucmd
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001860 if (!IS_USER_CMDIDX(ea.cmdidx))
1861 {
1862 if (ea.cmdidx != CMD_SIZE)
1863 ea.addr_type = cmdnames[(int)ea.cmdidx].cmd_addr_type;
1864 else
1865 ea.addr_type = ADDR_LINES;
1866
Bram Moolenaar25190db2019-05-04 15:05:28 +02001867 // :wincmd range depends on the argument.
Bram Moolenaar164f3262015-01-14 21:22:01 +01001868 if (ea.cmdidx == CMD_wincmd && p != NULL)
1869 get_wincmd_addr_type(skipwhite(p), &ea);
Bram Moolenaar25190db2019-05-04 15:05:28 +02001870#ifdef FEAT_QUICKFIX
1871 // :.cc in quickfix window uses line number
1872 if ((ea.cmdidx == CMD_cc || ea.cmdidx == CMD_ll) && bt_quickfix(curbuf))
1873 ea.addr_type = ADDR_OTHER;
1874#endif
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001875 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001876
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001877 ea.cmd = cmd;
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001878#ifdef FEAT_EVAL
1879 if (!starts_with_quote)
1880#endif
1881 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
1882 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883
Bram Moolenaar071d4272004-06-13 20:20:40 +00001884/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001885 * 5. Parse the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886 */
1887
1888 /*
1889 * Skip ':' and any white space
1890 */
1891 ea.cmd = skipwhite(ea.cmd);
1892 while (*ea.cmd == ':')
1893 ea.cmd = skipwhite(ea.cmd + 1);
1894
1895 /*
1896 * If we got a line, but no command, then go to the line.
1897 * If we find a '|' or '\n' we set ea.nextcmd.
1898 */
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001899 if (*ea.cmd == NUL || comment_start(ea.cmd, starts_with_colon)
1900 || (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001901 {
1902 /*
1903 * strange vi behaviour:
1904 * ":3" jumps to line 3
1905 * ":3|..." prints line 3
1906 * ":|" prints current line
1907 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001908 if (ea.skip) // skip this if inside :if
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001910 if (*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001911 {
1912 ea.cmdidx = CMD_print;
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001913 ea.argt = EX_RANGE+EX_COUNT+EX_TRLBAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001914 if ((errormsg = invalid_range(&ea)) == NULL)
1915 {
1916 correct_range(&ea);
1917 ex_print(&ea);
1918 }
1919 }
1920 else if (ea.addr_count != 0)
1921 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001922 if (ea.line2 > curbuf->b_ml.ml_line_count)
1923 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001924 // With '-' in 'cpoptions' a line number past the file is an
1925 // error, otherwise put it at the end of the file.
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001926 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
1927 ea.line2 = -1;
1928 else
1929 ea.line2 = curbuf->b_ml.ml_line_count;
1930 }
1931
1932 if (ea.line2 < 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001933 errormsg = _(e_invrange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001934 else
1935 {
1936 if (ea.line2 == 0)
1937 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938 else
1939 curwin->w_cursor.lnum = ea.line2;
1940 beginline(BL_SOL | BL_FIX);
1941 }
1942 }
1943 goto doend;
1944 }
1945
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001946 // If this looks like an undefined user command and there are CmdUndefined
1947 // autocommands defined, trigger the matching autocommands.
Bram Moolenaard5005162014-08-22 23:05:54 +02001948 if (p != NULL && ea.cmdidx == CMD_SIZE && !ea.skip
1949 && ASCII_ISUPPER(*ea.cmd)
1950 && has_cmdundefined())
1951 {
Bram Moolenaard5005162014-08-22 23:05:54 +02001952 int ret;
1953
Bram Moolenaar5a31b462014-08-23 14:16:20 +02001954 p = ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02001955 while (ASCII_ISALNUM(*p))
1956 ++p;
Bram Moolenaar158ea172020-06-18 17:28:39 +02001957 p = vim_strnsave(ea.cmd, p - ea.cmd);
Bram Moolenaard5005162014-08-22 23:05:54 +02001958 ret = apply_autocmds(EVENT_CMDUNDEFINED, p, p, TRUE, NULL);
1959 vim_free(p);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001960 // If the autocommands did something and didn't cause an error, try
1961 // finding the command again.
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001962 p = (ret
1963#ifdef FEAT_EVAL
1964 && !aborting()
Bram Moolenaard5005162014-08-22 23:05:54 +02001965#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001966 ) ? find_ex_command(&ea, NULL, NULL, NULL) : ea.cmd;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001967 }
Bram Moolenaard5005162014-08-22 23:05:54 +02001968
Bram Moolenaar071d4272004-06-13 20:20:40 +00001969 if (p == NULL)
1970 {
1971 if (!ea.skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001972 errormsg = _("E464: Ambiguous use of user-defined command");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001973 goto doend;
1974 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001975 // Check for wrong commands.
Bram Moolenaar6f9a4762017-06-22 20:39:17 +02001976 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78
1977 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978 {
1979 errormsg = uc_fun_cmd();
1980 goto doend;
1981 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001982
Bram Moolenaar071d4272004-06-13 20:20:40 +00001983 if (ea.cmdidx == CMD_SIZE)
1984 {
1985 if (!ea.skip)
1986 {
1987 STRCPY(IObuff, _("E492: Not an editor command"));
1988 if (!sourcing)
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001989 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001990 // If the modifier was parsed OK the error must be in the
1991 // following command
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001992 if (after_modifier != NULL)
1993 append_command(after_modifier);
1994 else
1995 append_command(*cmdlinep);
1996 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001997 errormsg = (char *)IObuff;
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001998 did_emsg_syntax = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999 }
2000 goto doend;
2001 }
2002
Bram Moolenaar958636c2014-10-21 20:01:58 +02002003 ni = (!IS_USER_CMDIDX(ea.cmdidx)
2004 && (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00002005#ifdef HAVE_EX_SCRIPT_NI
2006 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
2007#endif
2008 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009
2010#ifndef FEAT_EVAL
2011 /*
2012 * When the expression evaluation is disabled, recognize the ":if" and
2013 * ":endif" commands and ignore everything in between it.
2014 */
2015 if (ea.cmdidx == CMD_if)
2016 ++if_level;
2017 if (if_level)
2018 {
2019 if (ea.cmdidx == CMD_endif)
2020 --if_level;
2021 goto doend;
2022 }
2023
2024#endif
2025
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002026 // forced commands
Bram Moolenaar196b3b02008-06-20 16:51:41 +00002027 if (*p == '!' && ea.cmdidx != CMD_substitute
2028 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002029 {
2030 ++p;
2031 ea.forceit = TRUE;
2032 }
2033 else
2034 ea.forceit = FALSE;
2035
2036/*
Bram Moolenaarb7316892019-05-01 18:08:42 +02002037 * 6. Parse arguments. Then check for errors.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002038 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02002039 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002040 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002041
2042 if (!ea.skip)
2043 {
2044#ifdef HAVE_SANDBOX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002045 if (sandbox != 0 && !(ea.argt & EX_SBOXOK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046 {
Bram Moolenaar8c62a082019-02-08 14:34:10 +01002047 // Command not allowed in sandbox.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002048 errormsg = _(e_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002049 goto doend;
2050 }
2051#endif
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002052 if (restricted != 0 && (ea.argt & EX_RESTRICT))
Bram Moolenaar8c62a082019-02-08 14:34:10 +01002053 {
2054 errormsg = _("E981: Command not allowed in rvim");
2055 goto doend;
2056 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002057 if (!curbuf->b_p_ma && (ea.argt & EX_MODIFY))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002058 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002059 // Command not allowed in non-'modifiable' buffer
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002060 errormsg = _(e_modifiable);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002061 goto doend;
2062 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002063
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002064 if (text_locked() && !(ea.argt & EX_CMDWIN)
Bram Moolenaar958636c2014-10-21 20:01:58 +02002065 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002067 // Command not allowed when editing the command line.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002068 errormsg = _(get_text_locked_msg());
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069 goto doend;
2070 }
Bram Moolenaar379fb762018-08-30 15:58:28 +02002071
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002072 // Disallow editing another buffer when "curbuf_lock" is set.
2073 // Do allow ":checktime" (it is postponed).
2074 // Do allow ":edit" (check for an argument later).
2075 // Do allow ":file" with no arguments (check for an argument later).
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002076 if (!(ea.argt & EX_CMDWIN)
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002077 && ea.cmdidx != CMD_checktime
Bram Moolenaar379fb762018-08-30 15:58:28 +02002078 && ea.cmdidx != CMD_edit
2079 && ea.cmdidx != CMD_file
Bram Moolenaar958636c2014-10-21 20:01:58 +02002080 && !IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002081 && curbuf_locked())
2082 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002083
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002084 if (!ni && !(ea.argt & EX_RANGE) && ea.addr_count > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002085 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002086 // no range allowed
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002087 errormsg = _(e_norange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002088 goto doend;
2089 }
2090 }
2091
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002092 if (!ni && !(ea.argt & EX_BANG) && ea.forceit) // no <!> allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002093 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002094 errormsg = _(e_nobang);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002095 goto doend;
2096 }
2097
2098 /*
2099 * Don't complain about the range if it is not used
2100 * (could happen if line_count is accidentally set to 0).
2101 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002102 if (!ea.skip && !ni && (ea.argt & EX_RANGE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002103 {
2104 /*
2105 * If the range is backwards, ask for confirmation and, if given, swap
2106 * ea.line1 & ea.line2 so it's forwards again.
2107 * When global command is busy, don't ask, will fail below.
2108 */
2109 if (!global_busy && ea.line1 > ea.line2)
2110 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002111 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002112 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002113 if (sourcing || exmode_active)
2114 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002115 errormsg = _("E493: Backwards range given");
Bram Moolenaara5792f52005-11-23 21:25:05 +00002116 goto doend;
2117 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118 if (ask_yesno((char_u *)
2119 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002120 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002121 }
2122 lnum = ea.line1;
2123 ea.line1 = ea.line2;
2124 ea.line2 = lnum;
2125 }
2126 if ((errormsg = invalid_range(&ea)) != NULL)
2127 goto doend;
2128 }
2129
Bram Moolenaarb7316892019-05-01 18:08:42 +02002130 if ((ea.addr_type == ADDR_OTHER) && ea.addr_count == 0)
2131 // default is 1, not cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132 ea.line2 = 1;
2133
2134 correct_range(&ea);
2135
2136#ifdef FEAT_FOLDING
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002137 if (((ea.argt & EX_WHOLEFOLD) || ea.addr_count >= 2) && !global_busy
Bram Moolenaara3306952016-01-02 21:41:06 +01002138 && ea.addr_type == ADDR_LINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002139 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002140 // Put the first line at the start of a closed fold, put the last line
2141 // at the end of a closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142 (void)hasFolding(ea.line1, &ea.line1, NULL);
2143 (void)hasFolding(ea.line2, NULL, &ea.line2);
2144 }
2145#endif
2146
2147#ifdef FEAT_QUICKFIX
2148 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002149 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002150 * option here, so things like % get expanded.
2151 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002152 p = replace_makeprg(&ea, p, cmdlinep);
2153 if (p == NULL)
2154 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155#endif
2156
2157 /*
2158 * Skip to start of argument.
2159 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2160 */
2161 if (ea.cmdidx == CMD_bang)
2162 ea.arg = p;
2163 else
2164 ea.arg = skipwhite(p);
2165
Bram Moolenaar379fb762018-08-30 15:58:28 +02002166 // ":file" cannot be run with an argument when "curbuf_lock" is set
2167 if (ea.cmdidx == CMD_file && *ea.arg != NUL && curbuf_locked())
2168 goto doend;
2169
Bram Moolenaar071d4272004-06-13 20:20:40 +00002170 /*
2171 * Check for "++opt=val" argument.
2172 * Must be first, allow ":w ++enc=utf8 !cmd"
2173 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002174 if (ea.argt & EX_ARGOPT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2176 if (getargopt(&ea) == FAIL && !ni)
2177 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002178 errormsg = _(e_invarg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002179 goto doend;
2180 }
2181
2182 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2183 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002184 if (*ea.arg == '>') // append
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002186 if (*++ea.arg != '>') // typed wrong
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002188 errormsg = _("E494: Use w or w>>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002189 goto doend;
2190 }
2191 ea.arg = skipwhite(ea.arg + 1);
2192 ea.append = TRUE;
2193 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002194 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) // :w !filter
Bram Moolenaar071d4272004-06-13 20:20:40 +00002195 {
2196 ++ea.arg;
2197 ea.usefilter = TRUE;
2198 }
2199 }
2200
2201 if (ea.cmdidx == CMD_read)
2202 {
2203 if (ea.forceit)
2204 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002205 ea.usefilter = TRUE; // :r! filter if ea.forceit
Bram Moolenaar071d4272004-06-13 20:20:40 +00002206 ea.forceit = FALSE;
2207 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002208 else if (*ea.arg == '!') // :r !filter
Bram Moolenaar071d4272004-06-13 20:20:40 +00002209 {
2210 ++ea.arg;
2211 ea.usefilter = TRUE;
2212 }
2213 }
2214
2215 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2216 {
2217 ea.amount = 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002218 while (*ea.arg == *ea.cmd) // count number of '>' or '<'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002219 {
2220 ++ea.arg;
2221 ++ea.amount;
2222 }
2223 ea.arg = skipwhite(ea.arg);
2224 }
2225
2226 /*
2227 * Check for "+command" argument, before checking for next command.
2228 * Don't do this for ":read !cmd" and ":write !cmd".
2229 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002230 if ((ea.argt & EX_CMDARG) && !ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2232
2233 /*
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002234 * Check for '|' to separate commands and '"' or '#' to start comments.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002235 * Don't do this for ":read !cmd" and ":write !cmd".
2236 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002237 if ((ea.argt & EX_TRLBAR) && !ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002238 separate_nextcmd(&ea);
2239
2240 /*
2241 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002242 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2243 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002244 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002245 else if (ea.cmdidx == CMD_bang
Bram Moolenaar67883b42017-07-27 22:57:00 +02002246 || ea.cmdidx == CMD_terminal
Bram Moolenaardf177f62005-02-22 08:39:57 +00002247 || ea.cmdidx == CMD_global
2248 || ea.cmdidx == CMD_vglobal
2249 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002250 {
2251 for (p = ea.arg; *p; ++p)
2252 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002253 // Remove one backslash before a newline, so that it's possible to
2254 // pass a newline to the shell and also a newline that is preceded
2255 // with a backslash. This makes it impossible to end a shell
2256 // command in a backslash, but that doesn't appear useful.
2257 // Halving the number of backslashes is incompatible with previous
2258 // versions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002260 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002261 else if (*p == '\n')
2262 {
2263 ea.nextcmd = p + 1;
2264 *p = NUL;
2265 break;
2266 }
2267 }
2268 }
2269
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002270 if ((ea.argt & EX_DFLALL) && ea.addr_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271 {
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002272 buf_T *buf;
2273
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274 ea.line1 = 1;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002275 switch (ea.addr_type)
2276 {
2277 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02002278 case ADDR_OTHER:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002279 ea.line2 = curbuf->b_ml.ml_line_count;
2280 break;
2281 case ADDR_LOADED_BUFFERS:
2282 buf = firstbuf;
2283 while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL)
2284 buf = buf->b_next;
2285 ea.line1 = buf->b_fnum;
2286 buf = lastbuf;
2287 while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL)
2288 buf = buf->b_prev;
2289 ea.line2 = buf->b_fnum;
2290 break;
2291 case ADDR_BUFFERS:
2292 ea.line1 = firstbuf->b_fnum;
2293 ea.line2 = lastbuf->b_fnum;
2294 break;
2295 case ADDR_WINDOWS:
2296 ea.line2 = LAST_WIN_NR;
2297 break;
2298 case ADDR_TABS:
2299 ea.line2 = LAST_TAB_NR;
2300 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01002301 case ADDR_TABS_RELATIVE:
2302 ea.line2 = 1;
2303 break;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002304 case ADDR_ARGUMENTS:
2305 if (ARGCOUNT == 0)
2306 ea.line1 = ea.line2 = 0;
2307 else
2308 ea.line2 = ARGCOUNT;
2309 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02002310 case ADDR_QUICKFIX_VALID:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002311#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02002312 ea.line2 = qf_get_valid_size(&ea);
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002313 if (ea.line2 == 0)
2314 ea.line2 = 1;
Bram Moolenaare906c502015-09-09 21:10:39 +02002315#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002316 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02002317 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02002318 case ADDR_UNSIGNED:
2319 case ADDR_QUICKFIX:
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002320 iemsg(_("INTERNAL: Cannot use EX_DFLALL with ADDR_NONE, ADDR_UNSIGNED or ADDR_QUICKFIX"));
Bram Moolenaarb7316892019-05-01 18:08:42 +02002321 break;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002322 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323 }
2324
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002325 // accept numbered register only when no count allowed (:put)
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002326 if ( (ea.argt & EX_REGSTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327 && *ea.arg != NUL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002328 // Do not allow register = for user commands
Bram Moolenaar958636c2014-10-21 20:01:58 +02002329 && (!IS_USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002330 && !((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002331 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002332#ifndef FEAT_CLIPBOARD
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002333 // check these explicitly for a more specific error message
Bram Moolenaar85de2062011-05-05 14:26:41 +02002334 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002335 {
Bram Moolenaar99b12722019-01-14 20:16:40 +01002336 errormsg = _(e_invalidreg);
Bram Moolenaar85de2062011-05-05 14:26:41 +02002337 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002338 }
2339#endif
Bram Moolenaar958636c2014-10-21 20:01:58 +02002340 if (valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2341 && !IS_USER_CMDIDX(ea.cmdidx))))
Bram Moolenaar85de2062011-05-05 14:26:41 +02002342 {
2343 ea.regname = *ea.arg++;
2344#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002345 // for '=' register: accept the rest of the line as an expression
Bram Moolenaar85de2062011-05-05 14:26:41 +02002346 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2347 {
2348 set_expr_line(vim_strsave(ea.arg));
2349 ea.arg += STRLEN(ea.arg);
2350 }
2351#endif
2352 ea.arg = skipwhite(ea.arg);
2353 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002354 }
2355
2356 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002357 * Check for a count. When accepting a EX_BUFNAME, don't use "123foo" as a
Bram Moolenaar071d4272004-06-13 20:20:40 +00002358 * count, it's a buffer name.
2359 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002360 if ((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)
2361 && (!(ea.argt & EX_BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01002362 || VIM_ISWHITE(*p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363 {
2364 n = getdigits(&ea.arg);
2365 ea.arg = skipwhite(ea.arg);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002366 if (n <= 0 && !ni && (ea.argt & EX_ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002368 errormsg = _(e_zerocount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369 goto doend;
2370 }
Bram Moolenaarb7316892019-05-01 18:08:42 +02002371 if (ea.addr_type != ADDR_LINES) // e.g. :buffer 2, :sleep 3
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372 {
2373 ea.line2 = n;
2374 if (ea.addr_count == 0)
2375 ea.addr_count = 1;
2376 }
2377 else
2378 {
2379 ea.line1 = ea.line2;
2380 ea.line2 += n - 1;
2381 ++ea.addr_count;
2382 /*
2383 * Be vi compatible: no error message for out of range.
2384 */
Bram Moolenaarb7316892019-05-01 18:08:42 +02002385 if (ea.line2 > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002386 ea.line2 = curbuf->b_ml.ml_line_count;
2387 }
2388 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002389
2390 /*
2391 * Check for flags: 'l', 'p' and '#'.
2392 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002393 if (ea.argt & EX_FLAGS)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002394 get_flags(&ea);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002395 if (!ni && !(ea.argt & EX_EXTRA) && *ea.arg != NUL
2396 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & EX_TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002398 // no arguments allowed but there is something
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002399 errormsg = _(e_trailing);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400 goto doend;
2401 }
2402
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002403 if (!ni && (ea.argt & EX_NEEDARG) && *ea.arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002404 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002405 errormsg = _(e_argreq);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406 goto doend;
2407 }
2408
2409#ifdef FEAT_EVAL
2410 /*
2411 * Skip the command when it's not going to be executed.
2412 * The commands like :if, :endif, etc. always need to be executed.
2413 * Also make an exception for commands that handle a trailing command
2414 * themselves.
2415 */
2416 if (ea.skip)
2417 {
2418 switch (ea.cmdidx)
2419 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002420 // commands that need evaluation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421 case CMD_while:
2422 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002423 case CMD_for:
2424 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425 case CMD_if:
2426 case CMD_elseif:
2427 case CMD_else:
2428 case CMD_endif:
2429 case CMD_try:
2430 case CMD_catch:
2431 case CMD_finally:
2432 case CMD_endtry:
2433 case CMD_function:
Bram Moolenaarab55c682020-03-01 19:41:43 +01002434 case CMD_def:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435 break;
2436
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002437 // Commands that handle '|' themselves. Check: A command should
2438 // either have the EX_TRLBAR flag, appear in this list or appear in
2439 // the list at ":help :bar".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002440 case CMD_aboveleft:
2441 case CMD_and:
2442 case CMD_belowright:
2443 case CMD_botright:
2444 case CMD_browse:
2445 case CMD_call:
2446 case CMD_confirm:
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01002447 case CMD_const:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002448 case CMD_delfunction:
2449 case CMD_djump:
2450 case CMD_dlist:
2451 case CMD_dsearch:
2452 case CMD_dsplit:
2453 case CMD_echo:
2454 case CMD_echoerr:
2455 case CMD_echomsg:
2456 case CMD_echon:
Bram Moolenaara76b3152020-02-16 16:20:21 +01002457 case CMD_eval:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002458 case CMD_execute:
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002459 case CMD_filter:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002460 case CMD_help:
2461 case CMD_hide:
2462 case CMD_ijump:
2463 case CMD_ilist:
2464 case CMD_isearch:
2465 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002466 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002467 case CMD_keepjumps:
2468 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01002469 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002470 case CMD_leftabove:
2471 case CMD_let:
2472 case CMD_lockmarks:
Bram Moolenaarcc4423a2019-11-26 17:05:00 +01002473 case CMD_lockvar:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002474 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002475 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002476 case CMD_mzscheme:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01002477 case CMD_noautocmd:
2478 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002479 case CMD_perl:
2480 case CMD_psearch:
2481 case CMD_python:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002482 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002483 case CMD_python3:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484 case CMD_return:
2485 case CMD_rightbelow:
2486 case CMD_ruby:
2487 case CMD_silent:
2488 case CMD_smagic:
2489 case CMD_snomagic:
2490 case CMD_substitute:
2491 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002492 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493 case CMD_tcl:
2494 case CMD_throw:
2495 case CMD_tilde:
2496 case CMD_topleft:
2497 case CMD_unlet:
Bram Moolenaarcc4423a2019-11-26 17:05:00 +01002498 case CMD_unlockvar:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002499 case CMD_verbose:
2500 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002501 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002502 break;
2503
2504 default: goto doend;
2505 }
2506 }
2507#endif
2508
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002509 if (ea.argt & EX_XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002510 {
2511 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2512 goto doend;
2513 }
2514
Bram Moolenaar071d4272004-06-13 20:20:40 +00002515 /*
2516 * Accept buffer name. Cannot be used at the same time with a buffer
2517 * number. Don't do this for a user command.
2518 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002519 if ((ea.argt & EX_BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
Bram Moolenaar958636c2014-10-21 20:01:58 +02002520 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002521 {
2522 /*
2523 * :bdelete, :bwipeout and :bunload take several arguments, separated
2524 * by spaces: find next space (skipping over escaped characters).
2525 * The others take one argument: ignore trailing spaces.
2526 */
2527 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2528 || ea.cmdidx == CMD_bunload)
2529 p = skiptowhite_esc(ea.arg);
2530 else
2531 {
2532 p = ea.arg + STRLEN(ea.arg);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002533 while (p > ea.arg && VIM_ISWHITE(p[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002534 --p;
2535 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002536 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & EX_BUFUNL) != 0,
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002537 FALSE, FALSE);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002538 if (ea.line2 < 0) // failed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002539 goto doend;
2540 ea.addr_count = 1;
2541 ea.arg = skipwhite(p);
2542 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002543
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002544 // The :try command saves the emsg_silent flag, reset it here when
2545 // ":silent! try" was used, it should only apply to :try itself.
Bram Moolenaareffed932018-08-14 13:38:17 +02002546 if (ea.cmdidx == CMD_try && ea.did_esilent > 0)
Bram Moolenaar2be57332018-02-13 18:05:18 +01002547 {
Bram Moolenaareffed932018-08-14 13:38:17 +02002548 emsg_silent -= ea.did_esilent;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002549 if (emsg_silent < 0)
2550 emsg_silent = 0;
Bram Moolenaareffed932018-08-14 13:38:17 +02002551 ea.did_esilent = 0;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002552 }
2553
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554/*
Bram Moolenaar2be57332018-02-13 18:05:18 +01002555 * 7. Execute the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002556 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002557
Bram Moolenaar958636c2014-10-21 20:01:58 +02002558 if (IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002559 {
2560 /*
2561 * Execute a user-defined command.
2562 */
2563 do_ucmd(&ea);
2564 }
2565 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002566 {
2567 /*
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002568 * Call the function to execute the builtin command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569 */
2570 ea.errmsg = NULL;
2571 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2572 if (ea.errmsg != NULL)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02002573 errormsg = ea.errmsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002574 }
2575
2576#ifdef FEAT_EVAL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002577 // Set flag that any command was executed, used by ex_vim9script().
Bram Moolenaar16531552020-02-08 16:00:46 +01002578 if (getline_equal(ea.getline, ea.cookie, getsourceline)
2579 && current_sctx.sc_sid > 0)
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002580 SCRIPT_ITEM(current_sctx.sc_sid)->sn_had_command = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002581
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582 /*
2583 * If the command just executed called do_cmdline(), any throw or ":return"
2584 * or ":finish" encountered there must also check the cstack of the still
2585 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2586 * exception, or reanimate a returned function or finished script file and
2587 * return or finish it again.
2588 */
2589 if (need_rethrow)
2590 do_throw(cstack);
2591 else if (check_cstack)
2592 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002593 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002594 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002595 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002596 && current_func_returned())
2597 do_return(&ea, TRUE, FALSE, NULL);
2598 }
2599 need_rethrow = check_cstack = FALSE;
2600#endif
2601
2602doend:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002603 if (curwin->w_cursor.lnum == 0) // can happen with zero line number
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002604 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002605 curwin->w_cursor.lnum = 1;
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002606 curwin->w_cursor.col = 0;
2607 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002608
2609 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2610 {
2611 if (sourcing)
2612 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002613 if (errormsg != (char *)IObuff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002614 {
2615 STRCPY(IObuff, errormsg);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002616 errormsg = (char *)IObuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002618 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002619 }
2620 emsg(errormsg);
2621 }
2622#ifdef FEAT_EVAL
2623 do_errthrow(cstack,
Bram Moolenaar958636c2014-10-21 20:01:58 +02002624 (ea.cmdidx != CMD_SIZE && !IS_USER_CMDIDX(ea.cmdidx))
2625 ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002626#endif
2627
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002628 undo_cmdmod(&ea, save_msg_scroll);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002629 cmdmod = save_cmdmod;
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01002630 reg_executing = save_reg_executing;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002632#ifdef HAVE_SANDBOX
Bram Moolenaareffed932018-08-14 13:38:17 +02002633 if (ea.did_sandbox)
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002634 --sandbox;
2635#endif
2636
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002637 if (ea.nextcmd && *ea.nextcmd == NUL) // not really a next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00002638 ea.nextcmd = NULL;
2639
2640#ifdef FEAT_EVAL
2641 --ex_nesting_level;
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002642 vim_free(ea.cmdline_tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643#endif
2644
2645 return ea.nextcmd;
2646}
2647#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00002648 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649#endif
2650
Bram Moolenaar8930caa2020-07-23 16:37:03 +02002651static char ex_error_buf[MSG_BUF_LEN];
2652
2653/*
2654 * Return an error message with argument included.
2655 * Uses a static buffer, only the last error will be kept.
2656 * "msg" will be translated, caller should use N_().
2657 */
2658 char *
2659ex_errmsg(char *msg, char_u *arg)
2660{
2661 vim_snprintf(ex_error_buf, MSG_BUF_LEN, _(msg), arg);
2662 return ex_error_buf;
2663}
2664
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665/*
Bram Moolenaareffed932018-08-14 13:38:17 +02002666 * Parse and skip over command modifiers:
2667 * - update eap->cmd
2668 * - store flags in "cmdmod".
2669 * - Set ex_pressedreturn for an empty command line.
2670 * - set msg_silent for ":silent"
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002671 * - set 'eventignore' to "all" for ":noautocmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002672 * - set p_verbose for ":verbose"
2673 * - Increment "sandbox" for ":sandbox"
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002674 * When "skip_only" is TRUE the global variables are not changed, except for
2675 * "cmdmod".
Bram Moolenaareffed932018-08-14 13:38:17 +02002676 * Return FAIL when the command is not to be executed.
2677 * May set "errormsg" to an error message.
2678 */
2679 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002680parse_command_modifiers(exarg_T *eap, char **errormsg, int skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002681{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002682 char_u *p;
2683 int starts_with_colon = FALSE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002684
Bram Moolenaara80faa82020-04-12 19:37:17 +02002685 CLEAR_FIELD(cmdmod);
Bram Moolenaareffed932018-08-14 13:38:17 +02002686 eap->verbose_save = -1;
2687 eap->save_msg_silent = -1;
2688
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002689 // Repeat until no more command modifiers are found.
Bram Moolenaareffed932018-08-14 13:38:17 +02002690 for (;;)
2691 {
Bram Moolenaareffed932018-08-14 13:38:17 +02002692 while (*eap->cmd == ' ' || *eap->cmd == '\t' || *eap->cmd == ':')
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002693 {
2694 if (*eap->cmd == ':')
2695 starts_with_colon = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002696 ++eap->cmd;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002697 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002698
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002699 // in ex mode, an empty line works like :+
Bram Moolenaareffed932018-08-14 13:38:17 +02002700 if (*eap->cmd == NUL && exmode_active
2701 && (getline_equal(eap->getline, eap->cookie, getexmodeline)
2702 || getline_equal(eap->getline, eap->cookie, getexline))
2703 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2704 {
2705 eap->cmd = (char_u *)"+";
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002706 if (!skip_only)
2707 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002708 }
2709
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002710 // ignore comment and empty lines
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002711 if (comment_start(eap->cmd, starts_with_colon))
Bram Moolenaareffed932018-08-14 13:38:17 +02002712 return FAIL;
2713 if (*eap->cmd == NUL)
2714 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002715 if (!skip_only)
2716 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002717 return FAIL;
2718 }
2719
Bram Moolenaareffed932018-08-14 13:38:17 +02002720 p = skip_range(eap->cmd, NULL);
2721 switch (*p)
2722 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002723 // When adding an entry, also modify cmd_exists().
Bram Moolenaareffed932018-08-14 13:38:17 +02002724 case 'a': if (!checkforcmd(&eap->cmd, "aboveleft", 3))
2725 break;
2726 cmdmod.split |= WSP_ABOVE;
2727 continue;
2728
2729 case 'b': if (checkforcmd(&eap->cmd, "belowright", 3))
2730 {
2731 cmdmod.split |= WSP_BELOW;
2732 continue;
2733 }
2734 if (checkforcmd(&eap->cmd, "browse", 3))
2735 {
2736#ifdef FEAT_BROWSE_CMD
2737 cmdmod.browse = TRUE;
2738#endif
2739 continue;
2740 }
2741 if (!checkforcmd(&eap->cmd, "botright", 2))
2742 break;
2743 cmdmod.split |= WSP_BOT;
2744 continue;
2745
2746 case 'c': if (!checkforcmd(&eap->cmd, "confirm", 4))
2747 break;
2748#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2749 cmdmod.confirm = TRUE;
2750#endif
2751 continue;
2752
2753 case 'k': if (checkforcmd(&eap->cmd, "keepmarks", 3))
2754 {
2755 cmdmod.keepmarks = TRUE;
2756 continue;
2757 }
2758 if (checkforcmd(&eap->cmd, "keepalt", 5))
2759 {
2760 cmdmod.keepalt = TRUE;
2761 continue;
2762 }
2763 if (checkforcmd(&eap->cmd, "keeppatterns", 5))
2764 {
2765 cmdmod.keeppatterns = TRUE;
2766 continue;
2767 }
2768 if (!checkforcmd(&eap->cmd, "keepjumps", 5))
2769 break;
2770 cmdmod.keepjumps = TRUE;
2771 continue;
2772
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002773 case 'f': // only accept ":filter {pat} cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002774 {
2775 char_u *reg_pat;
2776
2777 if (!checkforcmd(&p, "filter", 4)
2778 || *p == NUL || ends_excmd(*p))
2779 break;
2780 if (*p == '!')
2781 {
2782 cmdmod.filter_force = TRUE;
2783 p = skipwhite(p + 1);
2784 if (*p == NUL || ends_excmd(*p))
2785 break;
2786 }
Bram Moolenaar1e624c92020-07-11 14:08:04 +02002787#ifdef FEAT_EVAL
Bram Moolenaarb074e8b2020-07-11 13:40:45 +02002788 // Avoid that "filter(arg)" is recognized.
2789 if (in_vim9script() && !VIM_ISWHITE(*p))
2790 break;
Bram Moolenaar1e624c92020-07-11 14:08:04 +02002791#endif
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002792 if (skip_only)
2793 p = skip_vimgrep_pat(p, NULL, NULL);
2794 else
2795 // NOTE: This puts a NUL after the pattern.
2796 p = skip_vimgrep_pat(p, &reg_pat, NULL);
Bram Moolenaareffed932018-08-14 13:38:17 +02002797 if (p == NULL || *p == NUL)
2798 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002799 if (!skip_only)
2800 {
2801 cmdmod.filter_regmatch.regprog =
Bram Moolenaareffed932018-08-14 13:38:17 +02002802 vim_regcomp(reg_pat, RE_MAGIC);
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002803 if (cmdmod.filter_regmatch.regprog == NULL)
2804 break;
2805 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002806 eap->cmd = p;
2807 continue;
2808 }
2809
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002810 // ":hide" and ":hide | cmd" are not modifiers
Bram Moolenaareffed932018-08-14 13:38:17 +02002811 case 'h': if (p != eap->cmd || !checkforcmd(&p, "hide", 3)
2812 || *p == NUL || ends_excmd(*p))
2813 break;
2814 eap->cmd = p;
2815 cmdmod.hide = TRUE;
2816 continue;
2817
2818 case 'l': if (checkforcmd(&eap->cmd, "lockmarks", 3))
2819 {
2820 cmdmod.lockmarks = TRUE;
2821 continue;
2822 }
2823
2824 if (!checkforcmd(&eap->cmd, "leftabove", 5))
2825 break;
2826 cmdmod.split |= WSP_ABOVE;
2827 continue;
2828
2829 case 'n': if (checkforcmd(&eap->cmd, "noautocmd", 3))
2830 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002831 if (cmdmod.save_ei == NULL && !skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002832 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002833 // Set 'eventignore' to "all". Restore the
2834 // existing option value later.
Bram Moolenaareffed932018-08-14 13:38:17 +02002835 cmdmod.save_ei = vim_strsave(p_ei);
2836 set_string_option_direct((char_u *)"ei", -1,
2837 (char_u *)"all", OPT_FREE, SID_NONE);
2838 }
2839 continue;
2840 }
2841 if (!checkforcmd(&eap->cmd, "noswapfile", 3))
2842 break;
2843 cmdmod.noswapfile = TRUE;
2844 continue;
2845
2846 case 'r': if (!checkforcmd(&eap->cmd, "rightbelow", 6))
2847 break;
2848 cmdmod.split |= WSP_BELOW;
2849 continue;
2850
2851 case 's': if (checkforcmd(&eap->cmd, "sandbox", 3))
2852 {
2853#ifdef HAVE_SANDBOX
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002854 if (!skip_only)
2855 {
2856 if (!eap->did_sandbox)
2857 ++sandbox;
2858 eap->did_sandbox = TRUE;
2859 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002860#endif
2861 continue;
2862 }
2863 if (!checkforcmd(&eap->cmd, "silent", 3))
2864 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002865 if (!skip_only)
2866 {
2867 if (eap->save_msg_silent == -1)
2868 eap->save_msg_silent = msg_silent;
2869 ++msg_silent;
2870 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002871 if (*eap->cmd == '!' && !VIM_ISWHITE(eap->cmd[-1]))
2872 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002873 // ":silent!", but not "silent !cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002874 eap->cmd = skipwhite(eap->cmd + 1);
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002875 if (!skip_only)
2876 {
2877 ++emsg_silent;
2878 ++eap->did_esilent;
2879 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002880 }
2881 continue;
2882
2883 case 't': if (checkforcmd(&p, "tab", 3))
2884 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002885 if (!skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002886 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002887 long tabnr = get_address(eap, &eap->cmd,
2888 ADDR_TABS, eap->skip,
2889 skip_only, FALSE, 1);
2890 if (tabnr == MAXLNUM)
2891 cmdmod.tab = tabpage_index(curtab) + 1;
2892 else
Bram Moolenaareffed932018-08-14 13:38:17 +02002893 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002894 if (tabnr < 0 || tabnr > LAST_TAB_NR)
2895 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002896 *errormsg = _(e_invrange);
Bram Moolenaar548e5982018-12-26 21:45:00 +01002897 return FAIL;
2898 }
2899 cmdmod.tab = tabnr + 1;
Bram Moolenaareffed932018-08-14 13:38:17 +02002900 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002901 }
2902 eap->cmd = p;
2903 continue;
2904 }
2905 if (!checkforcmd(&eap->cmd, "topleft", 2))
2906 break;
2907 cmdmod.split |= WSP_TOP;
2908 continue;
2909
2910 case 'u': if (!checkforcmd(&eap->cmd, "unsilent", 3))
2911 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002912 if (!skip_only)
2913 {
2914 if (eap->save_msg_silent == -1)
2915 eap->save_msg_silent = msg_silent;
2916 msg_silent = 0;
2917 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002918 continue;
2919
2920 case 'v': if (checkforcmd(&eap->cmd, "vertical", 4))
2921 {
2922 cmdmod.split |= WSP_VERT;
2923 continue;
2924 }
2925 if (!checkforcmd(&p, "verbose", 4))
2926 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002927 if (!skip_only)
2928 {
2929 if (eap->verbose_save < 0)
2930 eap->verbose_save = p_verbose;
2931 if (vim_isdigit(*eap->cmd))
2932 p_verbose = atoi((char *)eap->cmd);
2933 else
2934 p_verbose = 1;
2935 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002936 eap->cmd = p;
2937 continue;
2938 }
2939 break;
2940 }
2941
2942 return OK;
2943}
2944
2945/*
Bram Moolenaarb074e8b2020-07-11 13:40:45 +02002946 * Undo and free contents of "cmdmod".
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002947 */
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002948 void
2949undo_cmdmod(exarg_T *eap, int save_msg_scroll)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002950{
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002951 if (eap->verbose_save >= 0)
2952 p_verbose = eap->verbose_save;
2953
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002954 if (cmdmod.save_ei != NULL)
2955 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002956 // Restore 'eventignore' to the value before ":noautocmd".
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002957 set_string_option_direct((char_u *)"ei", -1, cmdmod.save_ei,
2958 OPT_FREE, SID_NONE);
2959 free_string_option(cmdmod.save_ei);
2960 }
2961
2962 if (cmdmod.filter_regmatch.regprog != NULL)
2963 vim_regfree(cmdmod.filter_regmatch.regprog);
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002964
2965 if (eap->save_msg_silent != -1)
2966 {
2967 // messages could be enabled for a serious error, need to check if the
2968 // counters don't become negative
2969 if (!did_emsg || msg_silent > eap->save_msg_silent)
2970 msg_silent = eap->save_msg_silent;
2971 emsg_silent -= eap->did_esilent;
2972 if (emsg_silent < 0)
2973 emsg_silent = 0;
2974 // Restore msg_scroll, it's set by file I/O commands, even when no
2975 // message is actually displayed.
2976 msg_scroll = save_msg_scroll;
2977
2978 // "silent reg" or "silent echo x" inside "redir" leaves msg_col
2979 // somewhere in the line. Put it back in the first column.
2980 if (redirecting())
2981 msg_col = 0;
2982 }
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002983}
2984
2985/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002986 * Parse the address range, if any, in "eap".
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02002987 * May set the last search pattern, unless "silent" is TRUE.
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002988 * Return FAIL and set "errormsg" or return OK.
2989 */
2990 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002991parse_cmd_address(exarg_T *eap, char **errormsg, int silent)
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002992{
2993 int address_count = 1;
2994 linenr_T lnum;
2995
2996 // Repeat for all ',' or ';' separated addresses.
2997 for (;;)
2998 {
2999 eap->line1 = eap->line2;
3000 switch (eap->addr_type)
3001 {
3002 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003003 case ADDR_OTHER:
Bram Moolenaarb5383b12020-05-18 19:46:48 +02003004 // Default is the cursor line number. Avoid using an invalid
3005 // line number though.
3006 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
3007 eap->line2 = curbuf->b_ml.ml_line_count;
3008 else
3009 eap->line2 = curwin->w_cursor.lnum;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003010 break;
3011 case ADDR_WINDOWS:
3012 eap->line2 = CURRENT_WIN_NR;
3013 break;
3014 case ADDR_ARGUMENTS:
3015 eap->line2 = curwin->w_arg_idx + 1;
3016 if (eap->line2 > ARGCOUNT)
3017 eap->line2 = ARGCOUNT;
3018 break;
3019 case ADDR_LOADED_BUFFERS:
3020 case ADDR_BUFFERS:
3021 eap->line2 = curbuf->b_fnum;
3022 break;
3023 case ADDR_TABS:
3024 eap->line2 = CURRENT_TAB_NR;
3025 break;
3026 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003027 case ADDR_UNSIGNED:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003028 eap->line2 = 1;
3029 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003030 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003031#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02003032 eap->line2 = qf_get_cur_idx(eap);
3033#endif
3034 break;
3035 case ADDR_QUICKFIX_VALID:
3036#ifdef FEAT_QUICKFIX
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003037 eap->line2 = qf_get_cur_valid_idx(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003038#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003039 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003040 case ADDR_NONE:
3041 // Will give an error later if a range is found.
3042 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003043 }
3044 eap->cmd = skipwhite(eap->cmd);
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003045 lnum = get_address(eap, &eap->cmd, eap->addr_type, eap->skip, silent,
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003046 eap->addr_count == 0, address_count++);
3047 if (eap->cmd == NULL) // error detected
3048 return FAIL;
3049 if (lnum == MAXLNUM)
3050 {
3051 if (*eap->cmd == '%') // '%' - all lines
3052 {
3053 ++eap->cmd;
3054 switch (eap->addr_type)
3055 {
3056 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003057 case ADDR_OTHER:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003058 eap->line1 = 1;
3059 eap->line2 = curbuf->b_ml.ml_line_count;
3060 break;
3061 case ADDR_LOADED_BUFFERS:
3062 {
3063 buf_T *buf = firstbuf;
3064
3065 while (buf->b_next != NULL
3066 && buf->b_ml.ml_mfp == NULL)
3067 buf = buf->b_next;
3068 eap->line1 = buf->b_fnum;
3069 buf = lastbuf;
3070 while (buf->b_prev != NULL
3071 && buf->b_ml.ml_mfp == NULL)
3072 buf = buf->b_prev;
3073 eap->line2 = buf->b_fnum;
3074 break;
3075 }
3076 case ADDR_BUFFERS:
3077 eap->line1 = firstbuf->b_fnum;
3078 eap->line2 = lastbuf->b_fnum;
3079 break;
3080 case ADDR_WINDOWS:
3081 case ADDR_TABS:
3082 if (IS_USER_CMDIDX(eap->cmdidx))
3083 {
3084 eap->line1 = 1;
3085 eap->line2 = eap->addr_type == ADDR_WINDOWS
3086 ? LAST_WIN_NR : LAST_TAB_NR;
3087 }
3088 else
3089 {
3090 // there is no Vim command which uses '%' and
3091 // ADDR_WINDOWS or ADDR_TABS
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003092 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003093 return FAIL;
3094 }
3095 break;
3096 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003097 case ADDR_UNSIGNED:
3098 case ADDR_QUICKFIX:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003099 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003100 return FAIL;
3101 case ADDR_ARGUMENTS:
3102 if (ARGCOUNT == 0)
3103 eap->line1 = eap->line2 = 0;
3104 else
3105 {
3106 eap->line1 = 1;
3107 eap->line2 = ARGCOUNT;
3108 }
3109 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003110 case ADDR_QUICKFIX_VALID:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003111#ifdef FEAT_QUICKFIX
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003112 eap->line1 = 1;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003113 eap->line2 = qf_get_valid_size(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003114 if (eap->line2 == 0)
3115 eap->line2 = 1;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003116#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003117 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003118 case ADDR_NONE:
3119 // Will give an error later if a range is found.
3120 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003121 }
3122 ++eap->addr_count;
3123 }
3124 else if (*eap->cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
3125 {
3126 pos_T *fp;
3127
3128 // '*' - visual area
3129 if (eap->addr_type != ADDR_LINES)
3130 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003131 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003132 return FAIL;
3133 }
3134
3135 ++eap->cmd;
3136 if (!eap->skip)
3137 {
3138 fp = getmark('<', FALSE);
3139 if (check_mark(fp) == FAIL)
3140 return FAIL;
3141 eap->line1 = fp->lnum;
3142 fp = getmark('>', FALSE);
3143 if (check_mark(fp) == FAIL)
3144 return FAIL;
3145 eap->line2 = fp->lnum;
3146 ++eap->addr_count;
3147 }
3148 }
3149 }
3150 else
3151 eap->line2 = lnum;
3152 eap->addr_count++;
3153
3154 if (*eap->cmd == ';')
3155 {
3156 if (!eap->skip)
3157 {
3158 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar0e717042020-04-27 19:29:01 +02003159 // Don't leave the cursor on an illegal line or column, but do
3160 // accept zero as address, so 0;/PATTERN/ works correctly.
3161 if (eap->line2 > 0)
3162 check_cursor();
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003163 }
3164 }
3165 else if (*eap->cmd != ',')
3166 break;
3167 ++eap->cmd;
3168 }
3169
3170 // One address given: set start and end lines.
3171 if (eap->addr_count == 1)
3172 {
3173 eap->line1 = eap->line2;
3174 // ... but only implicit: really no address given
3175 if (lnum == MAXLNUM)
3176 eap->addr_count = 0;
3177 }
3178 return OK;
3179}
3180
3181/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003182 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003183 * If there is a match advance "pp" to the argument and return TRUE.
3184 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003185 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003186checkforcmd(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003187 char_u **pp, // start of command
3188 char *cmd, // name of command
3189 int len) // required length
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190{
3191 int i;
3192
3193 for (i = 0; cmd[i] != NUL; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003194 if (((char_u *)cmd)[i] != (*pp)[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195 break;
3196 if (i >= len && !isalpha((*pp)[i]))
3197 {
3198 *pp = skipwhite(*pp + i);
3199 return TRUE;
3200 }
3201 return FALSE;
3202}
3203
3204/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003205 * Append "cmd" to the error message in IObuff.
3206 * Takes care of limiting the length and handling 0xa0, which would be
3207 * invisible otherwise.
3208 */
3209 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003210append_command(char_u *cmd)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003211{
3212 char_u *s = cmd;
3213 char_u *d;
3214
3215 STRCAT(IObuff, ": ");
3216 d = IObuff + STRLEN(IObuff);
3217 while (*s != NUL && d - IObuff < IOSIZE - 7)
3218 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003219 if (enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) : *s == 0xa0)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003220 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003221 s += enc_utf8 ? 2 : 1;
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003222 STRCPY(d, "<a0>");
3223 d += 4;
3224 }
3225 else
3226 MB_COPY_CHAR(s, d);
3227 }
3228 *d = NUL;
3229}
3230
3231/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003232 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003233 * Start of the name can be found at eap->cmd.
Bram Moolenaar25190db2019-05-04 15:05:28 +02003234 * Sets eap->cmdidx and returns a pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003235 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003236 *
3237 * If "lookup" is not NULL recognize expression without "eval" or "call" and
3238 * assignment without "let". Sets eap->cmdidx to the command while returning
3239 * "eap->cmd".
3240 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003241 * Returns NULL for an ambiguous user command.
3242 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003243 char_u *
3244find_ex_command(
3245 exarg_T *eap,
Bram Moolenaara494f562020-04-18 17:45:38 +02003246 int *full UNUSED,
Bram Moolenaarb84a3812020-05-01 15:44:29 +02003247 void *(*lookup)(char_u *, size_t, cctx_T *) UNUSED,
Bram Moolenaara494f562020-04-18 17:45:38 +02003248 cctx_T *cctx UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249{
3250 int len;
3251 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003252 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003253
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003254#ifdef FEAT_EVAL
3255 /*
3256 * Recognize a Vim9 script function/method call and assignment:
3257 * "lvar = value", "lvar(arg)", "[1, 2 3]->Func()"
3258 */
Bram Moolenaar0c6ceaf2020-02-22 18:36:32 +01003259 p = eap->cmd;
Bram Moolenaar3d48e252020-07-15 14:15:52 +02003260 if (lookup != NULL && (vim_strchr((char_u *)"{('[", *p) != NULL
3261 || ((p = to_name_const_end(eap->cmd)) > eap->cmd
3262 && *p != NUL)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003263 {
3264 int oplen;
3265 int heredoc;
3266
Bram Moolenaar3d48e252020-07-15 14:15:52 +02003267 if (
3268 // "(..." is an expression.
3269 // "funcname(" is always a function call.
3270 *p == '('
3271 || (p == eap->cmd
3272 ? (
3273 // "{..." is an dict expression.
3274 *eap->cmd == '{'
3275 // "'string'->func()" is an expression.
3276 || *eap->cmd == '\''
3277 // "g:varname" is an expression.
3278 || eap->cmd[1] == ':'
3279 )
3280 : (
3281 // "varname[]" is an expression.
3282 *p == '['
3283 // "varname->func()" is an expression.
3284 || (*p == '-' && p[1] == '>')
3285 // "varname.expr" is an expression.
3286 || (*p == '.' && ASCII_ISALPHA(p[1]))
3287 )))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003288 {
3289 eap->cmdidx = CMD_eval;
3290 return eap->cmd;
3291 }
3292
Bram Moolenaard2ef6b32020-07-02 21:11:34 +02003293 // "[...]->Method()" is a list expression, but "[a, b] = Func()" is
3294 // an assignment.
3295 // If there is no line break inside the "[...]" then "p" is advanced to
3296 // after the "]" by to_name_const_end(): check if a "=" follows.
3297 // If "[...]" has a line break "p" still points at the "[" and it can't
3298 // be an assignment.
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003299 if (*eap->cmd == '[')
Bram Moolenaard2ef6b32020-07-02 21:11:34 +02003300 {
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003301 p = to_name_const_end(eap->cmd);
3302 if (p == eap->cmd || *skipwhite(p) != '=')
3303 {
3304 eap->cmdidx = CMD_eval;
3305 return eap->cmd;
3306 }
3307 if (p > eap->cmd && *skipwhite(p) == '=')
3308 {
3309 eap->cmdidx = CMD_let;
3310 return eap->cmd;
3311 }
Bram Moolenaard2ef6b32020-07-02 21:11:34 +02003312 }
3313
Bram Moolenaar5f195932020-07-01 20:07:14 +02003314 // Recognize an assignment if we recognize the variable name:
3315 // "g:var = expr"
3316 // "var = expr" where "var" is a local var name.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003317 oplen = assignment_len(skipwhite(p), &heredoc);
3318 if (oplen > 0)
3319 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003320 if (((p - eap->cmd) > 2 && eap->cmd[1] == ':')
Bram Moolenaarb84a3812020-05-01 15:44:29 +02003321 || lookup(eap->cmd, p - eap->cmd, cctx) != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003322 {
3323 eap->cmdidx = CMD_let;
3324 return eap->cmd;
3325 }
3326 }
3327 }
3328#endif
3329
Bram Moolenaar071d4272004-06-13 20:20:40 +00003330 /*
3331 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00003332 * Exceptions:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333 * - the 'k' command can directly be followed by any character.
3334 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003335 * but :sre[wind] is another command, as are :scr[iptnames],
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00003337 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338 */
3339 p = eap->cmd;
3340 if (*p == 'k')
3341 {
3342 eap->cmdidx = CMD_k;
3343 ++p;
3344 }
3345 else if (p[0] == 's'
Bram Moolenaar204b93f2015-08-04 22:02:51 +02003346 && ((p[1] == 'c' && (p[2] == NUL || (p[2] != 's' && p[2] != 'r'
3347 && (p[3] == NUL || (p[3] != 'i' && p[4] != 'p')))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348 || p[1] == 'g'
3349 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
3350 || p[1] == 'I'
3351 || (p[1] == 'r' && p[2] != 'e')))
3352 {
3353 eap->cmdidx = CMD_substitute;
3354 ++p;
3355 }
3356 else
3357 {
3358 while (ASCII_ISALPHA(*p))
3359 ++p;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003360 // for python 3.x support ":py3", ":python3", ":py3file", etc.
Bram Moolenaar55d5c032010-07-17 23:52:29 +02003361 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003362 {
Bram Moolenaarb6590522010-07-21 16:00:43 +02003363 while (ASCII_ISALNUM(*p))
3364 ++p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003365 }
3366 else if (*p == '9' && STRNCMP("vim9", eap->cmd, 4) == 0)
3367 {
3368 // include "9" for "vim9script"
3369 ++p;
3370 while (ASCII_ISALPHA(*p))
3371 ++p;
3372 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02003373
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003374 // check for non-alpha command
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3376 ++p;
3377 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003378 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
3379 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003380 // Check for ":dl", ":dell", etc. to ":deletel": that's
3381 // :delete with the 'l' flag. Same for 'p'.
Bram Moolenaardf177f62005-02-22 08:39:57 +00003382 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003383 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00003384 break;
3385 if (i == len - 1)
3386 {
3387 --len;
3388 if (p[-1] == 'l')
3389 eap->flags |= EXFLAG_LIST;
3390 else
3391 eap->flags |= EXFLAG_PRINT;
3392 }
3393 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003395 if (ASCII_ISLOWER(eap->cmd[0]))
3396 {
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003397 int c1 = eap->cmd[0];
Bram Moolenaar94f82cb2019-07-24 22:30:27 +02003398 int c2 = len == 1 ? NUL : eap->cmd[1];
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003399
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003400 if (command_count != (int)CMD_SIZE)
3401 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003402 iemsg(_("E943: Command table needs to be updated, run 'make cmdidxs'"));
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003403 getout(1);
3404 }
3405
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003406 // Use a precomputed index for fast look-up in cmdnames[]
3407 // taking into account the first 2 letters of eap->cmd.
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003408 eap->cmdidx = cmdidxs1[CharOrdLow(c1)];
3409 if (ASCII_ISLOWER(c2))
3410 eap->cmdidx += cmdidxs2[CharOrdLow(c1)][CharOrdLow(c2)];
3411 }
Bram Moolenaara494f562020-04-18 17:45:38 +02003412 else if (ASCII_ISUPPER(eap->cmd[0]))
3413 eap->cmdidx = CMD_Next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414 else
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003415 eap->cmdidx = CMD_bang;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416
3417 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
3418 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
3419 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
3420 (size_t)len) == 0)
3421 {
3422#ifdef FEAT_EVAL
3423 if (full != NULL
3424 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
3425 *full = TRUE;
3426#endif
3427 break;
3428 }
3429
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003430 // Look for a user defined command as a last resort. Let ":Print" be
3431 // overruled by a user defined command.
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01003432 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
3433 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434 {
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003435 // User defined commands may contain digits.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436 while (ASCII_ISALNUM(*p))
3437 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003438 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003440 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003441 eap->cmdidx = CMD_SIZE;
3442 }
3443
3444 return p;
3445}
3446
3447#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003448static struct cmdmod
3449{
3450 char *name;
3451 int minlen;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003452 int has_count; // :123verbose :3tab
Bram Moolenaared53fb92007-11-24 20:50:24 +00003453} cmdmods[] = {
3454 {"aboveleft", 3, FALSE},
3455 {"belowright", 3, FALSE},
3456 {"botright", 2, FALSE},
3457 {"browse", 3, FALSE},
3458 {"confirm", 4, FALSE},
Bram Moolenaar7b668e82016-08-23 23:51:21 +02003459 {"filter", 4, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003460 {"hide", 3, FALSE},
3461 {"keepalt", 5, FALSE},
3462 {"keepjumps", 5, FALSE},
3463 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01003464 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003465 {"leftabove", 5, FALSE},
3466 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003467 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003468 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003469 {"rightbelow", 6, FALSE},
3470 {"sandbox", 3, FALSE},
3471 {"silent", 3, FALSE},
3472 {"tab", 3, TRUE},
3473 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003474 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003475 {"verbose", 4, TRUE},
3476 {"vertical", 4, FALSE},
3477};
3478
3479/*
3480 * Return length of a command modifier (including optional count).
3481 * Return zero when it's not a modifier.
3482 */
3483 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003484modifier_len(char_u *cmd)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003485{
3486 int i, j;
3487 char_u *p = cmd;
3488
3489 if (VIM_ISDIGIT(*cmd))
3490 p = skipwhite(skipdigits(cmd));
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003491 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003492 {
3493 for (j = 0; p[j] != NUL; ++j)
3494 if (p[j] != cmdmods[i].name[j])
3495 break;
Bram Moolenaar2d473ab2013-06-12 17:12:24 +02003496 if (!ASCII_ISALPHA(p[j]) && j >= cmdmods[i].minlen
Bram Moolenaared53fb92007-11-24 20:50:24 +00003497 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003498 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003499 }
3500 return 0;
3501}
3502
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503/*
3504 * Return > 0 if an Ex command "name" exists.
3505 * Return 2 if there is an exact match.
3506 * Return 3 if there is an ambiguous match.
3507 */
3508 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003509cmd_exists(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510{
3511 exarg_T ea;
3512 int full = FALSE;
3513 int i;
3514 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003515 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003517 // Check command modifiers.
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003518 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003519 {
3520 for (j = 0; name[j] != NUL; ++j)
3521 if (name[j] != cmdmods[i].name[j])
3522 break;
3523 if (name[j] == NUL && j >= cmdmods[i].minlen)
3524 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3525 }
3526
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003527 // Check built-in commands and user defined commands.
3528 // For ":2match" and ":3match" we need to skip the number.
Bram Moolenaara9587612006-05-04 21:47:50 +00003529 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003531 p = find_ex_command(&ea, &full, NULL, NULL);
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003532 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003534 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3535 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003536 if (*skipwhite(p) != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003537 return 0; // trailing garbage
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3539}
3540#endif
3541
Bram Moolenaard0190392019-08-23 21:17:35 +02003542 cmdidx_T
3543excmd_get_cmdidx(char_u *cmd, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544{
Bram Moolenaard0190392019-08-23 21:17:35 +02003545 cmdidx_T idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546
Bram Moolenaard0190392019-08-23 21:17:35 +02003547 for (idx = (cmdidx_T)0; (int)idx < (int)CMD_SIZE;
3548 idx = (cmdidx_T)((int)idx + 1))
3549 if (STRNCMP(cmdnames[(int)idx].cmd_name, cmd, (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550 break;
3551
Bram Moolenaard0190392019-08-23 21:17:35 +02003552 return idx;
3553}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554
Bram Moolenaard0190392019-08-23 21:17:35 +02003555 long
3556excmd_get_argt(cmdidx_T idx)
3557{
3558 return (long)cmdnames[(int)idx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559}
3560
3561/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003562 * Skip a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563 *
3564 * Backslashed delimiters after / or ? will be skipped, and commands will
3565 * not be expanded between /'s and ?'s or after "'".
3566 *
Bram Moolenaardf069ee2020-06-22 23:02:51 +02003567 * Also skip white space and ":" characters after the range.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568 * Returns the "cmd" pointer advanced to beyond the range.
3569 */
3570 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003571skip_range(
3572 char_u *cmd,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003573 int *ctx) // pointer to xp_context or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003575 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003576
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01003577 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;\\", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578 {
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01003579 if (*cmd == '\\')
3580 {
3581 if (cmd[1] == '?' || cmd[1] == '/' || cmd[1] == '&')
3582 ++cmd;
3583 else
3584 break;
3585 }
3586 else if (*cmd == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587 {
3588 if (*++cmd == NUL && ctx != NULL)
3589 *ctx = EXPAND_NOTHING;
3590 }
3591 else if (*cmd == '/' || *cmd == '?')
3592 {
3593 delim = *cmd++;
3594 while (*cmd != NUL && *cmd != delim)
3595 if (*cmd++ == '\\' && *cmd != NUL)
3596 ++cmd;
3597 if (*cmd == NUL && ctx != NULL)
3598 *ctx = EXPAND_NOTHING;
3599 }
3600 if (*cmd != NUL)
3601 ++cmd;
3602 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00003603
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003604 // Skip ":" and white space.
Bram Moolenaardf177f62005-02-22 08:39:57 +00003605 while (*cmd == ':')
3606 cmd = skipwhite(cmd + 1);
3607
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608 return cmd;
3609}
3610
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003611 static void
3612addr_error(cmd_addr_T addr_type)
3613{
3614 if (addr_type == ADDR_NONE)
3615 emsg(_(e_norange));
3616 else
3617 emsg(_(e_invrange));
3618}
3619
Bram Moolenaar071d4272004-06-13 20:20:40 +00003620/*
Bram Moolenaar198cb662018-09-06 21:44:17 +02003621 * Get a single EX address.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622 *
3623 * Set ptr to the next character after the part that was interpreted.
3624 * Set ptr to NULL when an error is encountered.
Bram Moolenaar198cb662018-09-06 21:44:17 +02003625 * This may set the last used search pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626 *
3627 * Return MAXLNUM when no Ex address was found.
3628 */
3629 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003630get_address(
3631 exarg_T *eap UNUSED,
3632 char_u **ptr,
Bram Moolenaar1b03a192019-12-08 17:08:29 +01003633 cmd_addr_T addr_type,
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003634 int skip, // only skip the address, don't use it
3635 int silent, // no errors or side effects
3636 int to_other_file, // flag: may jump to other file
3637 int address_count UNUSED) // 1 for first address, >1 after comma
Bram Moolenaar071d4272004-06-13 20:20:40 +00003638{
3639 int c;
3640 int i;
3641 long n;
3642 char_u *cmd;
3643 pos_T pos;
3644 pos_T *fp;
3645 linenr_T lnum;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003646 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647
3648 cmd = skipwhite(*ptr);
3649 lnum = MAXLNUM;
3650 do
3651 {
3652 switch (*cmd)
3653 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003654 case '.': // '.' - Cursor position
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003655 ++cmd;
3656 switch (addr_type)
3657 {
3658 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003659 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003660 lnum = curwin->w_cursor.lnum;
3661 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003662 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003663 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003664 break;
3665 case ADDR_ARGUMENTS:
3666 lnum = curwin->w_arg_idx + 1;
3667 break;
3668 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003669 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003670 lnum = curbuf->b_fnum;
3671 break;
3672 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003673 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003674 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003675 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003676 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003677 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003678 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003679 cmd = NULL;
3680 goto error;
3681 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003682 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003683#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02003684 lnum = qf_get_cur_idx(eap);
3685#endif
3686 break;
3687 case ADDR_QUICKFIX_VALID:
3688#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003689 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02003690#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003691 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003692 }
3693 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003694
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003695 case '$': // '$' - last line
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003696 ++cmd;
3697 switch (addr_type)
3698 {
3699 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003700 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003701 lnum = curbuf->b_ml.ml_line_count;
3702 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003703 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003704 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003705 break;
3706 case ADDR_ARGUMENTS:
3707 lnum = ARGCOUNT;
3708 break;
3709 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003710 buf = lastbuf;
3711 while (buf->b_ml.ml_mfp == NULL)
3712 {
3713 if (buf->b_prev == NULL)
3714 break;
3715 buf = buf->b_prev;
3716 }
3717 lnum = buf->b_fnum;
3718 break;
3719 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003720 lnum = lastbuf->b_fnum;
3721 break;
3722 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003723 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003724 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003725 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003726 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003727 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003728 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003729 cmd = NULL;
3730 goto error;
3731 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003732 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003733#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003734 lnum = qf_get_size(eap);
3735 if (lnum == 0)
3736 lnum = 1;
Bram Moolenaare906c502015-09-09 21:10:39 +02003737#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003738 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003739 case ADDR_QUICKFIX_VALID:
3740#ifdef FEAT_QUICKFIX
3741 lnum = qf_get_valid_size(eap);
3742 if (lnum == 0)
3743 lnum = 1;
3744#endif
3745 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003746 }
3747 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003748
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003749 case '\'': // ''' - mark
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003750 if (*++cmd == NUL)
3751 {
3752 cmd = NULL;
3753 goto error;
3754 }
3755 if (addr_type != ADDR_LINES)
3756 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003757 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003758 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003759 goto error;
3760 }
3761 if (skip)
3762 ++cmd;
3763 else
3764 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003765 // Only accept a mark in another file when it is
3766 // used by itself: ":'M".
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003767 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
3768 ++cmd;
3769 if (fp == (pos_T *)-1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003770 // Jumped to another file.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003771 lnum = curwin->w_cursor.lnum;
3772 else
3773 {
3774 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003775 {
3776 cmd = NULL;
3777 goto error;
3778 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003779 lnum = fp->lnum;
3780 }
3781 }
3782 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003783
3784 case '/':
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003785 case '?': // '/' or '?' - search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003786 c = *cmd++;
3787 if (addr_type != ADDR_LINES)
3788 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003789 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003790 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003791 goto error;
3792 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003793 if (skip) // skip "/pat/"
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003794 {
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02003795 cmd = skip_regexp(cmd, c, (int)p_magic);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003796 if (*cmd == c)
3797 ++cmd;
3798 }
3799 else
3800 {
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003801 int flags;
3802
3803 pos = curwin->w_cursor; // save curwin->w_cursor
3804
3805 // When '/' or '?' follows another address, start from
3806 // there.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003807 if (lnum != MAXLNUM)
3808 curwin->w_cursor.lnum = lnum;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003809
3810 // Start a forward search at the end of the line (unless
3811 // before the first line).
3812 // Start a backward search at the start of the line.
3813 // This makes sure we never match in the current
3814 // line, and can match anywhere in the
3815 // next/previous line.
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01003816 if (c == '/' && curwin->w_cursor.lnum > 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003817 curwin->w_cursor.col = MAXCOL;
3818 else
3819 curwin->w_cursor.col = 0;
3820 searchcmdlen = 0;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003821 flags = silent ? 0 : SEARCH_HIS | SEARCH_MSG;
Bram Moolenaarc036e872020-02-21 21:30:52 +01003822 if (!do_search(NULL, c, c, cmd, 1L, flags, NULL))
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003823 {
3824 curwin->w_cursor = pos;
3825 cmd = NULL;
3826 goto error;
3827 }
3828 lnum = curwin->w_cursor.lnum;
3829 curwin->w_cursor = pos;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003830 // adjust command string pointer
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003831 cmd += searchcmdlen;
3832 }
3833 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003834
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003835 case '\\': // "\?", "\/" or "\&", repeat search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003836 ++cmd;
3837 if (addr_type != ADDR_LINES)
3838 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003839 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003840 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003841 goto error;
3842 }
3843 if (*cmd == '&')
3844 i = RE_SUBST;
3845 else if (*cmd == '?' || *cmd == '/')
3846 i = RE_SEARCH;
3847 else
3848 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003849 emsg(_(e_backslash));
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003850 cmd = NULL;
3851 goto error;
3852 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003853
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003854 if (!skip)
3855 {
3856 /*
3857 * When search follows another address, start from
3858 * there.
3859 */
3860 if (lnum != MAXLNUM)
3861 pos.lnum = lnum;
3862 else
3863 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003864
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003865 /*
3866 * Start the search just like for the above
3867 * do_search().
3868 */
3869 if (*cmd != '?')
3870 pos.col = MAXCOL;
3871 else
3872 pos.col = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02003873 pos.coladd = 0;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01003874 if (searchit(curwin, curbuf, &pos, NULL,
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003875 *cmd == '?' ? BACKWARD : FORWARD,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02003876 (char_u *)"", 1L, SEARCH_MSG, i, NULL) != FAIL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003877 lnum = pos.lnum;
3878 else
3879 {
3880 cmd = NULL;
3881 goto error;
3882 }
3883 }
3884 ++cmd;
3885 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886
3887 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003888 if (VIM_ISDIGIT(*cmd)) // absolute line number
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003889 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003890 }
3891
3892 for (;;)
3893 {
3894 cmd = skipwhite(cmd);
3895 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
3896 break;
3897
3898 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003899 {
3900 switch (addr_type)
3901 {
3902 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003903 case ADDR_OTHER:
3904 // "+1" is same as ".+1"
Bram Moolenaarf240e182014-11-27 18:33:02 +01003905 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003906 break;
3907 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003908 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003909 break;
3910 case ADDR_ARGUMENTS:
3911 lnum = curwin->w_arg_idx + 1;
3912 break;
3913 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003914 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003915 lnum = curbuf->b_fnum;
3916 break;
3917 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003918 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003919 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003920 case ADDR_TABS_RELATIVE:
3921 lnum = 1;
3922 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003923 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003924#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02003925 lnum = qf_get_cur_idx(eap);
3926#endif
3927 break;
3928 case ADDR_QUICKFIX_VALID:
3929#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003930 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02003931#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003932 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003933 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003934 case ADDR_UNSIGNED:
3935 lnum = 0;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003936 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003937 }
3938 }
3939
Bram Moolenaar071d4272004-06-13 20:20:40 +00003940 if (VIM_ISDIGIT(*cmd))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003941 i = '+'; // "number" is same as "+number"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942 else
3943 i = *cmd++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003944 if (!VIM_ISDIGIT(*cmd)) // '+' is '+1', but '+0' is not '+1'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945 n = 1;
3946 else
3947 n = getdigits(&cmd);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003948
3949 if (addr_type == ADDR_TABS_RELATIVE)
3950 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003951 emsg(_(e_invrange));
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003952 cmd = NULL;
3953 goto error;
3954 }
3955 else if (addr_type == ADDR_LOADED_BUFFERS
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003956 || addr_type == ADDR_BUFFERS)
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003957 lnum = compute_buffer_local_count(
3958 addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003959 else
Bram Moolenaarded27822017-01-02 14:27:34 +01003960 {
3961#ifdef FEAT_FOLDING
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003962 // Relative line addressing, need to adjust for folded lines
3963 // now, but only do it after the first address.
Bram Moolenaarded27822017-01-02 14:27:34 +01003964 if (addr_type == ADDR_LINES && (i == '-' || i == '+')
3965 && address_count >= 2)
3966 (void)hasFolding(lnum, NULL, &lnum);
3967#endif
3968 if (i == '-')
3969 lnum -= n;
3970 else
3971 lnum += n;
3972 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003973 }
3974 } while (*cmd == '/' || *cmd == '?');
3975
3976error:
3977 *ptr = cmd;
3978 return lnum;
3979}
3980
3981/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00003982 * Get flags from an Ex command argument.
3983 */
3984 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003985get_flags(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00003986{
3987 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
3988 {
3989 if (*eap->arg == 'l')
3990 eap->flags |= EXFLAG_LIST;
3991 else if (*eap->arg == 'p')
3992 eap->flags |= EXFLAG_PRINT;
3993 else
3994 eap->flags |= EXFLAG_NR;
3995 eap->arg = skipwhite(eap->arg + 1);
3996 }
3997}
3998
3999/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000 * Function called for command which is Not Implemented. NI!
4001 */
4002 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004003ex_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004{
4005 if (!eap->skip)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02004006 eap->errmsg =
4007 _("E319: Sorry, the command is not available in this version");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008}
4009
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004010#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004011/*
4012 * Function called for script command which is Not Implemented. NI!
4013 * Skips over ":perl <<EOF" constructs.
4014 */
4015 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004016ex_script_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004017{
4018 if (!eap->skip)
4019 ex_ni(eap);
4020 else
4021 vim_free(script_get(eap, eap->arg));
4022}
4023#endif
4024
4025/*
4026 * Check range in Ex command for validity.
4027 * Return NULL when valid, error message when invalid.
4028 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004029 static char *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004030invalid_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004031{
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004032 buf_T *buf;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004033
Bram Moolenaar071d4272004-06-13 20:20:40 +00004034 if ( eap->line1 < 0
4035 || eap->line2 < 0
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004036 || eap->line1 > eap->line2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004037 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004038
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004039 if (eap->argt & EX_RANGE)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004040 {
Bram Moolenaarb7316892019-05-01 18:08:42 +02004041 switch (eap->addr_type)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004042 {
4043 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004044 if (eap->line2 > curbuf->b_ml.ml_line_count
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004045#ifdef FEAT_DIFF
4046 + (eap->cmdidx == CMD_diffget)
4047#endif
4048 )
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004049 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004050 break;
4051 case ADDR_ARGUMENTS:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004052 // add 1 if ARGCOUNT is 0
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004053 if (eap->line2 > ARGCOUNT + (!ARGCOUNT))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004054 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004055 break;
4056 case ADDR_BUFFERS:
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02004057 // Only a boundary check, not whether the buffers actually
4058 // exist.
4059 if (eap->line1 < 1 || eap->line2 > get_highest_fnum())
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004060 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004061 break;
4062 case ADDR_LOADED_BUFFERS:
4063 buf = firstbuf;
4064 while (buf->b_ml.ml_mfp == NULL)
4065 {
4066 if (buf->b_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004067 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004068 buf = buf->b_next;
4069 }
4070 if (eap->line1 < buf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004071 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004072 buf = lastbuf;
4073 while (buf->b_ml.ml_mfp == NULL)
4074 {
4075 if (buf->b_prev == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004076 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004077 buf = buf->b_prev;
4078 }
4079 if (eap->line2 > buf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004080 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004081 break;
4082 case ADDR_WINDOWS:
Bram Moolenaar8be63882015-01-14 11:25:05 +01004083 if (eap->line2 > LAST_WIN_NR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004084 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004085 break;
4086 case ADDR_TABS:
4087 if (eap->line2 > LAST_TAB_NR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004088 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004089 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004090 case ADDR_TABS_RELATIVE:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004091 case ADDR_OTHER:
4092 // Any range is OK.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004093 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004094 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004095#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004096 // No error for value that is too big, will use the last entry.
4097 if (eap->line2 <= 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004098 return _(e_invrange);
Bram Moolenaare906c502015-09-09 21:10:39 +02004099#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004100 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004101 case ADDR_QUICKFIX_VALID:
4102#ifdef FEAT_QUICKFIX
4103 if ((eap->line2 != 1 && eap->line2 > qf_get_valid_size(eap))
4104 || eap->line2 < 0)
4105 return _(e_invrange);
4106#endif
4107 break;
4108 case ADDR_UNSIGNED:
4109 if (eap->line2 < 0)
4110 return _(e_invrange);
4111 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004112 case ADDR_NONE:
4113 // Will give an error elsewhere.
4114 break;
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004115 }
4116 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117 return NULL;
4118}
4119
4120/*
4121 * Correct the range for zero line number, if required.
4122 */
4123 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004124correct_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004125{
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004126 if (!(eap->argt & EX_ZEROR)) // zero in range not allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004127 {
4128 if (eap->line1 == 0)
4129 eap->line1 = 1;
4130 if (eap->line2 == 0)
4131 eap->line2 = 1;
4132 }
4133}
4134
Bram Moolenaar748bf032005-02-02 23:04:36 +00004135#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004136/*
4137 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4138 * pattern. Otherwise return eap->arg.
4139 */
4140 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004141skip_grep_pat(exarg_T *eap)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004142{
4143 char_u *p = eap->arg;
4144
Bram Moolenaara37420f2006-02-04 22:37:47 +00004145 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4146 || eap->cmdidx == CMD_vimgrepadd
4147 || eap->cmdidx == CMD_lvimgrepadd
4148 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004149 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004150 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004151 if (p == NULL)
4152 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004153 }
4154 return p;
4155}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004156
4157/*
4158 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4159 * in the command line, so that things like % get expanded.
4160 */
4161 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004162replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004163{
4164 char_u *new_cmdline;
4165 char_u *program;
4166 char_u *pos;
4167 char_u *ptr;
4168 int len;
4169 int i;
4170
4171 /*
4172 * Don't do it when ":vimgrep" is used for ":grep".
4173 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004174 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4175 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4176 || eap->cmdidx == CMD_grepadd
4177 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004178 && !grep_internal(eap->cmdidx))
4179 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004180 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4181 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004182 {
4183 if (*curbuf->b_p_gp == NUL)
4184 program = p_gp;
4185 else
4186 program = curbuf->b_p_gp;
4187 }
4188 else
4189 {
4190 if (*curbuf->b_p_mp == NUL)
4191 program = p_mp;
4192 else
4193 program = curbuf->b_p_mp;
4194 }
4195
4196 p = skipwhite(p);
4197
4198 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4199 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004200 // replace $* by given arguments
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004201 i = 1;
4202 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4203 ++i;
4204 len = (int)STRLEN(p);
Bram Moolenaar51e14382019-05-25 20:21:28 +02004205 new_cmdline = alloc(STRLEN(program) + i * (len - 2) + 1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004206 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004207 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004208 ptr = new_cmdline;
4209 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4210 {
4211 i = (int)(pos - program);
4212 STRNCPY(ptr, program, i);
4213 STRCPY(ptr += i, p);
4214 ptr += len;
4215 program = pos + 2;
4216 }
4217 STRCPY(ptr, program);
4218 }
4219 else
4220 {
Bram Moolenaar51e14382019-05-25 20:21:28 +02004221 new_cmdline = alloc(STRLEN(program) + STRLEN(p) + 2);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004222 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004223 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004224 STRCPY(new_cmdline, program);
4225 STRCAT(new_cmdline, " ");
4226 STRCAT(new_cmdline, p);
4227 }
4228 msg_make(p);
4229
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004230 // 'eap->cmd' is not set here, because it is not used at CMD_make
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004231 vim_free(*cmdlinep);
4232 *cmdlinep = new_cmdline;
4233 p = new_cmdline;
4234 }
4235 return p;
4236}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004237#endif
4238
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239/*
4240 * Expand file name in Ex command argument.
Bram Moolenaar0abb4272019-06-15 16:06:00 +02004241 * When an error is detected, "errormsgp" is set to a non-NULL pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242 * Return FAIL for failure, OK otherwise.
4243 */
4244 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004245expand_filename(
4246 exarg_T *eap,
4247 char_u **cmdlinep,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004248 char **errormsgp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004250 int has_wildcards; // need to expand wildcards
Bram Moolenaar071d4272004-06-13 20:20:40 +00004251 char_u *repl;
4252 int srclen;
4253 char_u *p;
4254 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004255 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256
Bram Moolenaar748bf032005-02-02 23:04:36 +00004257#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004258 // Skip a regexp pattern for ":vimgrep[add] pat file..."
Bram Moolenaar748bf032005-02-02 23:04:36 +00004259 p = skip_grep_pat(eap);
4260#else
4261 p = eap->arg;
4262#endif
4263
Bram Moolenaar071d4272004-06-13 20:20:40 +00004264 /*
4265 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4266 * the file name contains a wildcard it should not cause expanding.
4267 * (it will be expanded anyway if there is a wildcard before replacing).
4268 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004269 has_wildcards = mch_has_wildcard(p);
4270 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004272#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004273 // Skip over `=expr`, wildcards in it are not expanded.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004274 if (p[0] == '`' && p[1] == '=')
4275 {
4276 p += 2;
4277 (void)skip_expr(&p);
4278 if (*p == '`')
4279 ++p;
4280 continue;
4281 }
4282#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004283 /*
4284 * Quick check if this cannot be the start of a special string.
4285 * Also removes backslash before '%', '#' and '<'.
4286 */
4287 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4288 {
4289 ++p;
4290 continue;
4291 }
4292
4293 /*
4294 * Try to find a match at this position.
4295 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00004296 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
4297 errormsgp, &escaped);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004298 if (*errormsgp != NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00004299 return FAIL;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004300 if (repl == NULL) // no match found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004301 {
4302 p += srclen;
4303 continue;
4304 }
4305
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004306 // Wildcards won't be expanded below, the replacement is taken
4307 // literally. But do expand "~/file", "~user/file" and "$HOME/file".
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004308 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4309 {
4310 char_u *l = repl;
4311
4312 repl = expand_env_save(repl);
4313 vim_free(l);
4314 }
4315
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004316 // Need to escape white space et al. with a backslash.
4317 // Don't do this for:
4318 // - replacement that already has been escaped: "##"
4319 // - shell commands (may have to use quotes instead).
4320 // - non-unix systems when there is a single argument (spaces don't
4321 // separate arguments then).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00004323 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00004324 && eap->cmdidx != CMD_bang
Bram Moolenaar071d4272004-06-13 20:20:40 +00004325 && eap->cmdidx != CMD_grep
4326 && eap->cmdidx != CMD_grepadd
Bram Moolenaarbf15b8d2017-06-04 20:43:48 +02004327 && eap->cmdidx != CMD_hardcopy
Bram Moolenaar67883b42017-07-27 22:57:00 +02004328 && eap->cmdidx != CMD_lgrep
4329 && eap->cmdidx != CMD_lgrepadd
4330 && eap->cmdidx != CMD_lmake
4331 && eap->cmdidx != CMD_make
4332 && eap->cmdidx != CMD_terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00004333#ifndef UNIX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004334 && !(eap->argt & EX_NOSPC)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004335#endif
4336 )
4337 {
4338 char_u *l;
4339#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004340 // Don't escape a backslash here, because rem_backslash() doesn't
4341 // remove it later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004342 static char_u *nobslash = (char_u *)" \t\"|";
4343# define ESCAPE_CHARS nobslash
4344#else
4345# define ESCAPE_CHARS escape_chars
4346#endif
4347
4348 for (l = repl; *l; ++l)
4349 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
4350 {
4351 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
4352 if (l != NULL)
4353 {
4354 vim_free(repl);
4355 repl = l;
4356 }
4357 break;
4358 }
4359 }
4360
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004361 // For a shell command a '!' must be escaped.
Bram Moolenaar67883b42017-07-27 22:57:00 +02004362 if ((eap->usefilter || eap->cmdidx == CMD_bang
4363 || eap->cmdidx == CMD_terminal)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004364 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004365 {
4366 char_u *l;
4367
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004368 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004369 if (l != NULL)
4370 {
4371 vim_free(repl);
4372 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004373 }
4374 }
4375
4376 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
4377 vim_free(repl);
4378 if (p == NULL)
4379 return FAIL;
4380 }
4381
4382 /*
4383 * One file argument: Expand wildcards.
4384 * Don't do this with ":r !command" or ":w !command".
4385 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004386 if ((eap->argt & EX_NOSPC) && !eap->usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004387 {
4388 /*
4389 * May do this twice:
4390 * 1. Replace environment variables.
4391 * 2. Replace any other wildcards, remove backslashes.
4392 */
4393 for (n = 1; n <= 2; ++n)
4394 {
4395 if (n == 2)
4396 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004397 /*
4398 * Halve the number of backslashes (this is Vi compatible).
4399 * For Unix and OS/2, when wildcards are expanded, this is
4400 * done by ExpandOne() below.
4401 */
Bram Moolenaare7fedb62015-12-31 19:07:19 +01004402#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004403 if (!has_wildcards)
4404#endif
4405 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004406 }
4407
4408 if (has_wildcards)
4409 {
4410 if (n == 1)
4411 {
4412 /*
4413 * First loop: May expand environment variables. This
4414 * can be done much faster with expand_env() than with
4415 * something else (e.g., calling a shell).
4416 * After expanding environment variables, check again
4417 * if there are still wildcards present.
4418 */
4419 if (vim_strchr(eap->arg, '$') != NULL
4420 || vim_strchr(eap->arg, '~') != NULL)
4421 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00004422 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004423 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004424 has_wildcards = mch_has_wildcard(NameBuff);
4425 p = NameBuff;
4426 }
4427 else
4428 p = NULL;
4429 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004430 else // n == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004431 {
4432 expand_T xpc;
Bram Moolenaarb40c2572019-10-19 21:01:05 +02004433 int options = WILD_LIST_NOTFOUND
4434 | WILD_NOERROR | WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004435
4436 ExpandInit(&xpc);
4437 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004438 if (p_wic)
4439 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01004441 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004442 if (p == NULL)
4443 return FAIL;
4444 }
4445 if (p != NULL)
4446 {
4447 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
4448 p, cmdlinep);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004449 if (n == 2) // p came from ExpandOne()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004450 vim_free(p);
4451 }
4452 }
4453 }
4454 }
4455 return OK;
4456}
4457
4458/*
4459 * Replace part of the command line, keeping eap->cmd, eap->arg and
4460 * eap->nextcmd correct.
4461 * "src" points to the part that is to be replaced, of length "srclen".
4462 * "repl" is the replacement string.
4463 * Returns a pointer to the character after the replaced string.
4464 * Returns NULL for failure.
4465 */
4466 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004467repl_cmdline(
4468 exarg_T *eap,
4469 char_u *src,
4470 int srclen,
4471 char_u *repl,
4472 char_u **cmdlinep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004473{
4474 int len;
4475 int i;
4476 char_u *new_cmdline;
4477
4478 /*
4479 * The new command line is build in new_cmdline[].
4480 * First allocate it.
4481 * Careful: a "+cmd" argument may have been NUL terminated.
4482 */
4483 len = (int)STRLEN(repl);
4484 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004485 if (eap->nextcmd != NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004486 i += (int)STRLEN(eap->nextcmd);// add space for next command
Bram Moolenaar964b3742019-05-24 18:54:09 +02004487 if ((new_cmdline = alloc(i)) == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004488 return NULL; // out of memory!
Bram Moolenaar071d4272004-06-13 20:20:40 +00004489
4490 /*
4491 * Copy the stuff before the expanded part.
4492 * Copy the expanded stuff.
4493 * Copy what came after the expanded part.
4494 * Copy the next commands, if there are any.
4495 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004496 i = (int)(src - *cmdlinep); // length of part before match
Bram Moolenaar071d4272004-06-13 20:20:40 +00004497 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00004498
Bram Moolenaar071d4272004-06-13 20:20:40 +00004499 mch_memmove(new_cmdline + i, repl, (size_t)len);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004500 i += len; // remember the end of the string
Bram Moolenaar071d4272004-06-13 20:20:40 +00004501 STRCPY(new_cmdline + i, src + srclen);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004502 src = new_cmdline + i; // remember where to continue
Bram Moolenaar071d4272004-06-13 20:20:40 +00004503
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004504 if (eap->nextcmd != NULL) // append next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00004505 {
4506 i = (int)STRLEN(new_cmdline) + 1;
4507 STRCPY(new_cmdline + i, eap->nextcmd);
4508 eap->nextcmd = new_cmdline + i;
4509 }
4510 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
4511 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
4512 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
4513 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
4514 vim_free(*cmdlinep);
4515 *cmdlinep = new_cmdline;
4516
4517 return src;
4518}
4519
4520/*
4521 * Check for '|' to separate commands and '"' to start comments.
4522 */
4523 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004524separate_nextcmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525{
4526 char_u *p;
4527
Bram Moolenaar86b68352004-12-27 21:59:20 +00004528#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004529 p = skip_grep_pat(eap);
4530#else
4531 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004532#endif
4533
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004534 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004535 {
4536 if (*p == Ctrl_V)
4537 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004538 if (eap->argt & (EX_CTRLV | EX_XFILE))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004539 ++p; // skip CTRL-V and next char
Bram Moolenaar071d4272004-06-13 20:20:40 +00004540 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004541 // remove CTRL-V and skip next char
Bram Moolenaara7241f52008-06-24 20:39:31 +00004542 STRMOVE(p, p + 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004543 if (*p == NUL) // stop at NUL after CTRL-V
Bram Moolenaar071d4272004-06-13 20:20:40 +00004544 break;
4545 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004546
4547#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004548 // Skip over `=expr` when wildcards are expanded.
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004549 else if (p[0] == '`' && p[1] == '=' && (eap->argt & EX_XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004550 {
4551 p += 2;
4552 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004553 }
4554#endif
4555
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004556 // Check for '"': start of comment or '|': next command
4557 // :@" and :*" do not start a comment!
4558 // :redir @" doesn't either.
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004559 else if ((*p == '"'
Bram Moolenaara26b9702020-04-18 19:53:28 +02004560#ifdef FEAT_EVAL
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004561 && !in_vim9script()
4562#endif
4563 && !(eap->argt & EX_NOTRLCOM)
4564 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
4565 || p != eap->arg)
4566 && (eap->cmdidx != CMD_redir
4567 || p != eap->arg + 1 || p[-1] != '@'))
4568#ifdef FEAT_EVAL
4569 || (*p == '#'
4570 && in_vim9script()
4571 && p[1] != '{'
4572 && p > eap->cmd && VIM_ISWHITE(p[-1]))
Bram Moolenaara26b9702020-04-18 19:53:28 +02004573#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004574 || *p == '|' || *p == '\n')
4575 {
4576 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004577 * We remove the '\' before the '|', unless EX_CTRLV is used
Bram Moolenaar071d4272004-06-13 20:20:40 +00004578 * AND 'b' is present in 'cpoptions'.
4579 */
4580 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004581 || !(eap->argt & EX_CTRLV)) && *(p - 1) == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004582 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004583 STRMOVE(p - 1, p); // remove the '\'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004584 --p;
4585 }
4586 else
4587 {
4588 eap->nextcmd = check_nextcmd(p);
4589 *p = NUL;
4590 break;
4591 }
4592 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004593 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004594
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004595 if (!(eap->argt & EX_NOTRLCOM)) // remove trailing spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00004596 del_trailing_spaces(eap->arg);
4597}
4598
4599/*
4600 * get + command from ex argument
4601 */
4602 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004603getargcmd(char_u **argp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004604{
4605 char_u *arg = *argp;
4606 char_u *command = NULL;
4607
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004608 if (*arg == '+') // +[command]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004609 {
4610 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02004611 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004612 command = dollar_command;
4613 else
4614 {
4615 command = arg;
4616 arg = skip_cmd_arg(command, TRUE);
4617 if (*arg != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004618 *arg++ = NUL; // terminate command with NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004619 }
4620
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004621 arg = skipwhite(arg); // skip over spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00004622 *argp = arg;
4623 }
4624 return command;
4625}
4626
4627/*
4628 * Find end of "+command" argument. Skip over "\ " and "\\".
4629 */
Bram Moolenaard0190392019-08-23 21:17:35 +02004630 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004631skip_cmd_arg(
4632 char_u *p,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004633 int rembs) // TRUE to halve the number of backslashes
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634{
4635 while (*p && !vim_isspace(*p))
4636 {
4637 if (*p == '\\' && p[1] != NUL)
4638 {
4639 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00004640 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004641 else
4642 ++p;
4643 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004644 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004645 }
4646 return p;
4647}
4648
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004649 int
4650get_bad_opt(char_u *p, exarg_T *eap)
4651{
4652 if (STRICMP(p, "keep") == 0)
4653 eap->bad_char = BAD_KEEP;
4654 else if (STRICMP(p, "drop") == 0)
4655 eap->bad_char = BAD_DROP;
4656 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
4657 eap->bad_char = *p;
Bram Moolenaar75808492018-06-12 12:39:41 +02004658 else
4659 return FAIL;
4660 return OK;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004661}
4662
Bram Moolenaar071d4272004-06-13 20:20:40 +00004663/*
4664 * Get "++opt=arg" argument.
4665 * Return FAIL or OK.
4666 */
4667 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004668getargopt(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004669{
4670 char_u *arg = eap->arg + 2;
4671 int *pp = NULL;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004672 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004673 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004674
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004675 // ":edit ++[no]bin[ary] file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004676 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
4677 {
4678 if (*arg == 'n')
4679 {
4680 arg += 2;
4681 eap->force_bin = FORCE_NOBIN;
4682 }
4683 else
4684 eap->force_bin = FORCE_BIN;
4685 if (!checkforcmd(&arg, "binary", 3))
4686 return FAIL;
4687 eap->arg = skipwhite(arg);
4688 return OK;
4689 }
4690
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004691 // ":read ++edit file"
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004692 if (STRNCMP(arg, "edit", 4) == 0)
4693 {
4694 eap->read_edit = TRUE;
4695 eap->arg = skipwhite(arg + 4);
4696 return OK;
4697 }
4698
Bram Moolenaar071d4272004-06-13 20:20:40 +00004699 if (STRNCMP(arg, "ff", 2) == 0)
4700 {
4701 arg += 2;
4702 pp = &eap->force_ff;
4703 }
4704 else if (STRNCMP(arg, "fileformat", 10) == 0)
4705 {
4706 arg += 10;
4707 pp = &eap->force_ff;
4708 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004709 else if (STRNCMP(arg, "enc", 3) == 0)
4710 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01004711 if (STRNCMP(arg, "encoding", 8) == 0)
4712 arg += 8;
4713 else
4714 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004715 pp = &eap->force_enc;
4716 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004717 else if (STRNCMP(arg, "bad", 3) == 0)
4718 {
4719 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004720 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004721 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004722
4723 if (pp == NULL || *arg != '=')
4724 return FAIL;
4725
4726 ++arg;
4727 *pp = (int)(arg - eap->cmd);
4728 arg = skip_cmd_arg(arg, FALSE);
4729 eap->arg = skipwhite(arg);
4730 *arg = NUL;
4731
Bram Moolenaar071d4272004-06-13 20:20:40 +00004732 if (pp == &eap->force_ff)
4733 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004734 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
4735 return FAIL;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004736 eap->force_ff = eap->cmd[eap->force_ff];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004737 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004738 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004739 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004740 // Make 'fileencoding' lower case.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004741 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
4742 *p = TOLOWER_ASC(*p);
4743 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004744 else
4745 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004746 // Check ++bad= argument. Must be a single-byte character, "keep" or
4747 // "drop".
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004748 if (get_bad_opt(eap->cmd + bad_char_idx, eap) == FAIL)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004749 return FAIL;
4750 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004751
4752 return OK;
4753}
4754
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004756ex_autocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004757{
4758 /*
Bram Moolenaar26d4b892018-07-04 22:26:28 +02004759 * Disallow autocommands from .exrc and .vimrc in current
Bram Moolenaar071d4272004-06-13 20:20:40 +00004760 * directory for security reasons.
4761 */
4762 if (secure)
4763 {
4764 secure = 2;
Bram Moolenaar8930caa2020-07-23 16:37:03 +02004765 eap->errmsg = _(e_curdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004766 }
4767 else if (eap->cmdidx == CMD_autocmd)
4768 do_autocmd(eap->arg, eap->forceit);
4769 else
4770 do_augroup(eap->arg, eap->forceit);
4771}
4772
4773/*
4774 * ":doautocmd": Apply the automatic commands to the current buffer.
4775 */
4776 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004777ex_doautocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004778{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01004779 char_u *arg = eap->arg;
4780 int call_do_modelines = check_nomodeline(&arg);
Bram Moolenaar1610d052016-06-09 22:53:01 +02004781 int did_aucmd;
Bram Moolenaar60542ac2012-02-12 20:14:01 +01004782
Bram Moolenaar1610d052016-06-09 22:53:01 +02004783 (void)do_doautocmd(arg, TRUE, &did_aucmd);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004784 // Only when there is no <nomodeline>.
Bram Moolenaar1610d052016-06-09 22:53:01 +02004785 if (call_do_modelines && did_aucmd)
Bram Moolenaar60542ac2012-02-12 20:14:01 +01004786 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004787}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004788
Bram Moolenaar071d4272004-06-13 20:20:40 +00004789/*
4790 * :[N]bunload[!] [N] [bufname] unload buffer
4791 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
4792 * :[N]bwipeout[!] [N] [bufname] delete buffer really
4793 */
4794 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004795ex_bunload(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004796{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004797 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02004798 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004799 eap->errmsg = do_bufdel(
4800 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
4801 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
4802 : DOBUF_UNLOAD, eap->arg,
4803 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
4804}
4805
4806/*
4807 * :[N]buffer [N] to buffer N
4808 * :[N]sbuffer [N] to buffer N
4809 */
4810 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004811ex_buffer(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004812{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004813 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02004814 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004815 if (*eap->arg)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02004816 eap->errmsg = ex_errmsg(e_trailing_arg, eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004817 else
4818 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004819 if (eap->addr_count == 0) // default is current buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00004820 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
4821 else
4822 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004823 if (eap->do_ecmd_cmd != NULL)
4824 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004825 }
4826}
4827
4828/*
4829 * :[N]bmodified [N] to next mod. buffer
4830 * :[N]sbmodified [N] to next mod. buffer
4831 */
4832 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004833ex_bmodified(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004834{
4835 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004836 if (eap->do_ecmd_cmd != NULL)
4837 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004838}
4839
4840/*
4841 * :[N]bnext [N] to next buffer
4842 * :[N]sbnext [N] split and to next buffer
4843 */
4844 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004845ex_bnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004846{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004847 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01004848 return;
4849
Bram Moolenaar071d4272004-06-13 20:20:40 +00004850 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004851 if (eap->do_ecmd_cmd != NULL)
4852 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004853}
4854
4855/*
4856 * :[N]bNext [N] to previous buffer
4857 * :[N]bprevious [N] to previous buffer
4858 * :[N]sbNext [N] split and to previous buffer
4859 * :[N]sbprevious [N] split and to previous buffer
4860 */
4861 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004862ex_bprevious(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004863{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004864 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01004865 return;
4866
Bram Moolenaar071d4272004-06-13 20:20:40 +00004867 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004868 if (eap->do_ecmd_cmd != NULL)
4869 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004870}
4871
4872/*
4873 * :brewind to first buffer
4874 * :bfirst to first buffer
4875 * :sbrewind split and to first buffer
4876 * :sbfirst split and to first buffer
4877 */
4878 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004879ex_brewind(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004880{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004881 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01004882 return;
4883
Bram Moolenaar071d4272004-06-13 20:20:40 +00004884 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004885 if (eap->do_ecmd_cmd != NULL)
4886 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004887}
4888
4889/*
4890 * :blast to last buffer
4891 * :sblast split and to last buffer
4892 */
4893 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004894ex_blast(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004895{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004896 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01004897 return;
4898
Bram Moolenaar071d4272004-06-13 20:20:40 +00004899 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004900 if (eap->do_ecmd_cmd != NULL)
4901 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004902}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004903
Bram Moolenaar7a092242020-04-16 22:10:49 +02004904/*
4905 * Check if "c" ends an Ex command.
Bram Moolenaara494f562020-04-18 17:45:38 +02004906 * In Vim9 script does not check for white space before # or #{.
Bram Moolenaar7a092242020-04-16 22:10:49 +02004907 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004908 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004909ends_excmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004910{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004911 int comment_char = '"';
4912
Bram Moolenaarfbda69b2020-04-13 15:06:53 +02004913#ifdef FEAT_EVAL
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004914 if (in_vim9script())
4915 comment_char = '#';
Bram Moolenaar7a092242020-04-16 22:10:49 +02004916#endif
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004917 return (c == NUL || c == '|' || c == comment_char || c == '\n');
Bram Moolenaar7a092242020-04-16 22:10:49 +02004918}
4919
4920/*
4921 * Like ends_excmd() but checks that a # in Vim9 script either has "cmd" equal
4922 * to "cmd_start" or has a white space character before it.
4923 */
4924 int
Bram Moolenaar21cfe502020-04-16 23:01:50 +02004925ends_excmd2(char_u *cmd_start UNUSED, char_u *cmd)
Bram Moolenaar7a092242020-04-16 22:10:49 +02004926{
4927 int c = *cmd;
4928
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004929 if (c == NUL || c == '|' || c == '\n')
4930 return TRUE;
Bram Moolenaar7a092242020-04-16 22:10:49 +02004931#ifdef FEAT_EVAL
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004932 if (in_vim9script())
4933 return c == '#' && cmd[1] != '{'
4934 && (cmd == cmd_start || VIM_ISWHITE(cmd[-1]));
Bram Moolenaarfbda69b2020-04-13 15:06:53 +02004935#endif
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004936 return c == '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004937}
4938
4939#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
4940 || defined(PROTO)
4941/*
4942 * Return the next command, after the first '|' or '\n'.
4943 * Return NULL if not found.
4944 */
4945 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004946find_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004947{
4948 while (*p != '|' && *p != '\n')
4949 {
4950 if (*p == NUL)
4951 return NULL;
4952 ++p;
4953 }
4954 return (p + 1);
4955}
4956#endif
4957
4958/*
Bram Moolenaar2256c992016-11-15 21:17:07 +01004959 * Check if *p is a separator between Ex commands, skipping over white space.
4960 * Return NULL if it isn't, the following character if it is.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004961 */
4962 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004963check_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004964{
Bram Moolenaar2256c992016-11-15 21:17:07 +01004965 char_u *s = skipwhite(p);
4966
4967 if (*s == '|' || *s == '\n')
4968 return (s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004969 else
4970 return NULL;
4971}
4972
4973/*
4974 * - if there are more files to edit
4975 * - and this is the last window
4976 * - and forceit not used
4977 * - and not repeated twice on a row
4978 * return FAIL and give error message if 'message' TRUE
4979 * return OK otherwise
4980 */
4981 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004982check_more(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004983 int message, // when FALSE check only, no messages
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004984 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985{
4986 int n = ARGCOUNT - curwin->w_arg_idx - 1;
4987
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00004988 if (!forceit && only_one_window()
4989 && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004990 {
4991 if (message)
4992 {
4993#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
4994 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
4995 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02004996 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004997
Bram Moolenaarda6e8912018-08-21 15:12:14 +02004998 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
4999 NGETTEXT("%d more file to edit. Quit anyway?",
5000 "%d more files to edit. Quit anyway?", n), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005001 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5002 return OK;
5003 return FAIL;
5004 }
5005#endif
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01005006 semsg(NGETTEXT("E173: %d more file to edit",
5007 "E173: %d more files to edit", n), n);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005008 quitmore = 2; // next try to quit is allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00005009 }
5010 return FAIL;
5011 }
5012 return OK;
5013}
5014
Bram Moolenaar071d4272004-06-13 20:20:40 +00005015/*
5016 * Function given to ExpandGeneric() to obtain the list of command names.
5017 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005018 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005019get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005020{
5021 if (idx >= (int)CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005022 return get_user_command_name(idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023 return cmdnames[idx].cmd_name;
5024}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005025
Bram Moolenaar071d4272004-06-13 20:20:40 +00005026 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005027ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005028{
Bram Moolenaare6850792010-05-14 15:28:44 +02005029 if (*eap->arg == NUL)
5030 {
5031#ifdef FEAT_EVAL
5032 char_u *expr = vim_strsave((char_u *)"g:colors_name");
5033 char_u *p = NULL;
5034
5035 if (expr != NULL)
5036 {
5037 ++emsg_off;
Bram Moolenaarb171fb12020-06-24 20:34:03 +02005038 p = eval_to_string(expr, FALSE);
Bram Moolenaare6850792010-05-14 15:28:44 +02005039 --emsg_off;
5040 vim_free(expr);
5041 }
5042 if (p != NULL)
5043 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005044 msg((char *)p);
Bram Moolenaare6850792010-05-14 15:28:44 +02005045 vim_free(p);
5046 }
5047 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005048 msg("default");
Bram Moolenaare6850792010-05-14 15:28:44 +02005049#else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005050 msg(_("unknown"));
Bram Moolenaare6850792010-05-14 15:28:44 +02005051#endif
5052 }
5053 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005054 semsg(_("E185: Cannot find color scheme '%s'"), eap->arg);
Bram Moolenaarf58d81a2019-01-28 20:19:05 +01005055
5056#ifdef FEAT_VTP
5057 else if (has_vtp_working())
5058 {
5059 // background color change requires clear + redraw
5060 update_screen(CLEAR);
5061 redrawcmd();
5062 }
5063#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005064}
5065
5066 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005067ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005068{
5069 if (*eap->arg == NUL && eap->cmd[2] == '!')
Bram Moolenaar32526b32019-01-19 17:43:09 +01005070 msg(_("Greetings, Vim user!"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005071 do_highlight(eap->arg, eap->forceit, FALSE);
5072}
5073
5074
5075/*
5076 * Call this function if we thought we were going to exit, but we won't
5077 * (because of an error). May need to restore the terminal mode.
5078 */
5079 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005080not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005081{
5082 exiting = FALSE;
5083 settmode(TMODE_RAW);
5084}
5085
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005086 static int
5087before_quit_autocmds(win_T *wp, int quit_all, int forceit)
5088{
5089 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, wp->w_buffer);
5090
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02005091 // Bail out when autocommands closed the window.
5092 // Refuse to quit when the buffer in the last window is being closed (can
5093 // only happen in autocommands).
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005094 if (!win_valid(wp)
5095 || curbuf_locked()
5096 || (wp->w_buffer->b_nwindows == 1 && wp->w_buffer->b_locked > 0))
5097 return TRUE;
5098
5099 if (quit_all || (check_more(FALSE, forceit) == OK && only_one_window()))
5100 {
5101 apply_autocmds(EVENT_EXITPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02005102 // Refuse to quit when locked or when the window was closed or the
5103 // buffer in the last window is being closed (can only happen in
5104 // autocommands).
5105 if (!win_valid(wp) || curbuf_locked()
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005106 || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
5107 return TRUE;
5108 }
5109
5110 return FALSE;
5111}
5112
Bram Moolenaar071d4272004-06-13 20:20:40 +00005113/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01005114 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005115 * ":{nr}quit": quit window {nr}
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02005116 * Also used when closing a terminal window that's the last one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005117 */
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02005118 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005119ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005121 win_T *wp;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005122
Bram Moolenaar071d4272004-06-13 20:20:40 +00005123#ifdef FEAT_CMDWIN
5124 if (cmdwin_type != 0)
5125 {
5126 cmdwin_result = Ctrl_C;
5127 return;
5128 }
5129#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005130 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005131 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005132 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005133 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005134 return;
5135 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005136 if (eap->addr_count > 0)
5137 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01005138 int wnr = eap->line2;
5139
5140 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
5141 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005142 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005143 }
5144 else
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005145 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01005146
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005147 // Refuse to quit when locked.
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02005148 if (curbuf_locked())
5149 return;
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005150
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005151 // Trigger QuitPre and maybe ExitPre
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005152 if (before_quit_autocmds(wp, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005153 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005154
5155#ifdef FEAT_NETBEANS_INTG
5156 netbeansForcedQuit = eap->forceit;
5157#endif
5158
5159 /*
5160 * If there are more files or windows we won't exit.
5161 */
5162 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
5163 exiting = TRUE;
Bram Moolenaarff930ca2017-10-19 17:12:10 +02005164 if ((!buf_hide(wp->w_buffer)
5165 && check_changed(wp->w_buffer, (p_awa ? CCGD_AW : 0)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01005166 | (eap->forceit ? CCGD_FORCEIT : 0)
5167 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005168 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01005169 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005170 {
5171 not_exiting();
5172 }
5173 else
5174 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005175 // quit last window
5176 // Note: only_one_window() returns true, even so a help window is
5177 // still open. In that case only quit, if no address has been
5178 // specified. Example:
5179 // :h|wincmd w|1q - don't quit
5180 // :h|wincmd w|q - quit
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01005181 if (only_one_window() && (ONE_WINDOW || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005182 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02005183 not_exiting();
Bram Moolenaar4033c552017-09-16 20:54:51 +02005184#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005186#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005187 // close window; may free buffer
Bram Moolenaareb44a682017-08-03 22:44:55 +02005188 win_close(wp, !buf_hide(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005189 }
5190}
5191
5192/*
5193 * ":cquit".
5194 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005195 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005196ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005197{
Bram Moolenaar1860bde2020-01-06 21:47:21 +01005198 // this does not always pass on the exit code to the Manx compiler. why?
5199 getout(eap->addr_count > 0 ? (int)eap->line2 : EXIT_FAILURE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005200}
5201
5202/*
5203 * ":qall": try to quit all windows
5204 */
5205 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005206ex_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207{
5208# ifdef FEAT_CMDWIN
5209 if (cmdwin_type != 0)
5210 {
5211 if (eap->forceit)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005212 cmdwin_result = K_XF1; // ex_window() takes care of this
Bram Moolenaar071d4272004-06-13 20:20:40 +00005213 else
5214 cmdwin_result = K_XF2;
5215 return;
5216 }
5217# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005218
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005219 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005220 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005221 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005222 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005223 return;
5224 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005225
5226 if (before_quit_autocmds(curwin, TRUE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005227 return;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005228
Bram Moolenaar071d4272004-06-13 20:20:40 +00005229 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01005230 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005231 getout(0);
5232 not_exiting();
5233}
5234
Bram Moolenaar071d4272004-06-13 20:20:40 +00005235/*
5236 * ":close": close current window, unless it is the last one
5237 */
5238 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005239ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005240{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005241 win_T *win;
5242 int winnr = 0;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005243#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00005244 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02005245 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005246 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005247#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005248 if (!text_locked() && !curbuf_locked())
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005249 {
5250 if (eap->addr_count == 0)
5251 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar6d41c782018-06-06 09:11:12 +02005252 else
5253 {
Bram Moolenaar29323592016-07-24 22:04:11 +02005254 FOR_ALL_WINDOWS(win)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005255 {
5256 winnr++;
5257 if (winnr == eap->line2)
5258 break;
5259 }
5260 if (win == NULL)
5261 win = lastwin;
5262 ex_win_close(eap->forceit, win, NULL);
5263 }
5264 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005265}
5266
Bram Moolenaar4033c552017-09-16 20:54:51 +02005267#ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005268/*
5269 * ":pclose": Close any preview window.
5270 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005271 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005272ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005273{
5274 win_T *win;
5275
Bram Moolenaar79648732019-07-18 21:43:07 +02005276 // First close any normal window.
Bram Moolenaar29323592016-07-24 22:04:11 +02005277 FOR_ALL_WINDOWS(win)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005278 if (win->w_p_pvw)
5279 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005280 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar79648732019-07-18 21:43:07 +02005281 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005282 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005283# ifdef FEAT_PROP_POPUP
Bram Moolenaar79648732019-07-18 21:43:07 +02005284 // Also when 'previewpopup' is empty, it might have been cleared.
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02005285 popup_close_preview();
Bram Moolenaar79648732019-07-18 21:43:07 +02005286# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005287}
Bram Moolenaar4033c552017-09-16 20:54:51 +02005288#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005289
Bram Moolenaarf740b292006-02-16 22:11:02 +00005290/*
5291 * Close window "win" and take care of handling closing the last window for a
5292 * modified buffer.
5293 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005294 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005295ex_win_close(
5296 int forceit,
5297 win_T *win,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005298 tabpage_T *tp) // NULL or the tab page "win" is in
Bram Moolenaar071d4272004-06-13 20:20:40 +00005299{
5300 int need_hide;
5301 buf_T *buf = win->w_buffer;
5302
Bram Moolenaarcf844172020-06-26 19:44:06 +02005303 // Never close the autocommand window.
5304 if (win == aucmd_win)
5305 {
5306 emsg(_(e_autocmd_close));
5307 return;
5308 }
5309
Bram Moolenaar071d4272004-06-13 20:20:40 +00005310 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02005311 if (need_hide && !buf_hide(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005312 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02005313#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005314 if ((p_confirm || cmdmod.confirm) && p_write)
5315 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005316 bufref_T bufref;
5317
5318 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005319 dialog_changed(buf, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005320 if (bufref_valid(&bufref) && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005321 return;
5322 need_hide = FALSE;
5323 }
5324 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005325#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005326 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02005327 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005328 return;
5329 }
5330 }
5331
Bram Moolenaar4033c552017-09-16 20:54:51 +02005332#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005333 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005334#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005335
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005336 // free buffer when not hiding it or when it's a scratch buffer
Bram Moolenaarf740b292006-02-16 22:11:02 +00005337 if (tp == NULL)
Bram Moolenaareb44a682017-08-03 22:44:55 +02005338 win_close(win, !need_hide && !buf_hide(buf));
Bram Moolenaarf740b292006-02-16 22:11:02 +00005339 else
Bram Moolenaareb44a682017-08-03 22:44:55 +02005340 win_close_othertab(win, !need_hide && !buf_hide(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005341}
5342
Bram Moolenaar071d4272004-06-13 20:20:40 +00005343/*
Bram Moolenaardea25702017-01-29 15:18:10 +01005344 * Handle the argument for a tabpage related ex command.
5345 * Returns a tabpage number.
5346 * When an error is encountered then eap->errmsg is set.
5347 */
5348 static int
5349get_tabpage_arg(exarg_T *eap)
5350{
5351 int tab_number;
5352 int unaccept_arg0 = (eap->cmdidx == CMD_tabmove) ? 0 : 1;
5353
5354 if (eap->arg && *eap->arg != NUL)
5355 {
5356 char_u *p = eap->arg;
5357 char_u *p_save;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005358 int relative = 0; // argument +N/-N means: go to N places to the
5359 // right/left relative to the current position.
Bram Moolenaardea25702017-01-29 15:18:10 +01005360
5361 if (*p == '-')
5362 {
5363 relative = -1;
5364 p++;
5365 }
5366 else if (*p == '+')
5367 {
5368 relative = 1;
5369 p++;
5370 }
5371
5372 p_save = p;
5373 tab_number = getdigits(&p);
5374
5375 if (relative == 0)
5376 {
5377 if (STRCMP(p, "$") == 0)
5378 tab_number = LAST_TAB_NR;
5379 else if (p == p_save || *p_save == '-' || *p != NUL
5380 || tab_number > LAST_TAB_NR)
5381 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005382 // No numbers as argument.
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005383 eap->errmsg = ex_errmsg(e_invarg2, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01005384 goto theend;
5385 }
5386 }
5387 else
5388 {
5389 if (*p_save == NUL)
5390 tab_number = 1;
5391 else if (p == p_save || *p_save == '-' || *p != NUL
5392 || tab_number == 0)
5393 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005394 // No numbers as argument.
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005395 eap->errmsg = ex_errmsg(e_invarg2, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01005396 goto theend;
5397 }
5398 tab_number = tab_number * relative + tabpage_index(curtab);
5399 if (!unaccept_arg0 && relative == -1)
5400 --tab_number;
5401 }
5402 if (tab_number < unaccept_arg0 || tab_number > LAST_TAB_NR)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005403 eap->errmsg = ex_errmsg(e_invarg2, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01005404 }
5405 else if (eap->addr_count > 0)
5406 {
5407 if (unaccept_arg0 && eap->line2 == 0)
Bram Moolenaarc6251552017-01-29 21:42:20 +01005408 {
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005409 eap->errmsg = _(e_invrange);
Bram Moolenaarc6251552017-01-29 21:42:20 +01005410 tab_number = 0;
5411 }
Bram Moolenaardea25702017-01-29 15:18:10 +01005412 else
5413 {
5414 tab_number = eap->line2;
Bram Moolenaar82a12462019-01-22 22:41:42 +01005415 if (!unaccept_arg0 && *skipwhite(*eap->cmdlinep) == '-')
Bram Moolenaardea25702017-01-29 15:18:10 +01005416 {
5417 --tab_number;
5418 if (tab_number < unaccept_arg0)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005419 eap->errmsg = _(e_invrange);
Bram Moolenaardea25702017-01-29 15:18:10 +01005420 }
5421 }
5422 }
5423 else
5424 {
5425 switch (eap->cmdidx)
5426 {
5427 case CMD_tabnext:
5428 tab_number = tabpage_index(curtab) + 1;
5429 if (tab_number > LAST_TAB_NR)
5430 tab_number = 1;
5431 break;
5432 case CMD_tabmove:
5433 tab_number = LAST_TAB_NR;
5434 break;
5435 default:
5436 tab_number = tabpage_index(curtab);
5437 }
5438 }
5439
5440theend:
5441 return tab_number;
5442}
5443
5444/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00005445 * ":tabclose": close current tab page, unless it is the last one.
5446 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005447 */
5448 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005449ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005450{
Bram Moolenaarf740b292006-02-16 22:11:02 +00005451 tabpage_T *tp;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005452 int tab_number;
Bram Moolenaarf740b292006-02-16 22:11:02 +00005453
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005454# ifdef FEAT_CMDWIN
5455 if (cmdwin_type != 0)
5456 cmdwin_result = K_IGNORE;
5457 else
5458# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005459 if (first_tabpage->tp_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005460 emsg(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00005461 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005462 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005463 tab_number = get_tabpage_arg(eap);
5464 if (eap->errmsg == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005465 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005466 tp = find_tabpage(tab_number);
Bram Moolenaarf740b292006-02-16 22:11:02 +00005467 if (tp == NULL)
5468 {
5469 beep_flush();
5470 return;
5471 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005472 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005473 {
5474 tabpage_close_other(tp, eap->forceit);
5475 return;
5476 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005477 else if (!text_locked() && !curbuf_locked())
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005478 tabpage_close(eap->forceit);
5479 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005480 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005481}
5482
5483/*
5484 * ":tabonly": close all tab pages except the current one
5485 */
5486 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005487ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005488{
5489 tabpage_T *tp;
5490 int done;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005491 int tab_number;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005492
5493# ifdef FEAT_CMDWIN
5494 if (cmdwin_type != 0)
5495 cmdwin_result = K_IGNORE;
5496 else
5497# endif
5498 if (first_tabpage->tp_next == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005499 msg(_("Already only one tab page"));
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005500 else
5501 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005502 tab_number = get_tabpage_arg(eap);
5503 if (eap->errmsg == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005504 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005505 goto_tabpage(tab_number);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005506 // Repeat this up to a 1000 times, because autocommands may
5507 // mess up the lists.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005508 for (done = 0; done < 1000; ++done)
5509 {
5510 FOR_ALL_TABPAGES(tp)
5511 if (tp->tp_topframe != topframe)
5512 {
5513 tabpage_close_other(tp, eap->forceit);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005514 // if we failed to close it quit
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005515 if (valid_tabpage(tp))
5516 done = 1000;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005517 // start over, "tp" is now invalid
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005518 break;
5519 }
5520 if (first_tabpage->tp_next == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005521 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005522 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005523 }
5524 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005525}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005526
5527/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00005528 * Close the current tab page.
5529 */
5530 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005531tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005532{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005533 // First close all the windows but the current one. If that worked then
5534 // close the last window in this tab, that will close it.
Bram Moolenaar459ca562016-11-10 18:16:33 +01005535 if (!ONE_WINDOW)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005536 close_others(TRUE, forceit);
Bram Moolenaar459ca562016-11-10 18:16:33 +01005537 if (ONE_WINDOW)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005538 ex_win_close(forceit, curwin, NULL);
5539# ifdef FEAT_GUI
5540 need_mouse_correct = TRUE;
5541# endif
5542}
5543
5544/*
5545 * Close tab page "tp", which is not the current tab page.
5546 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005547 * Also takes care of the tab pages line disappearing when closing the
5548 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00005549 */
5550 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005551tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005552{
5553 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005554 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005555 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00005556
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005557 // Limit to 1000 windows, autocommands may add a window while we close
5558 // one. OK, so I'm paranoid...
Bram Moolenaarf740b292006-02-16 22:11:02 +00005559 while (++done < 1000)
5560 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005561 wp = tp->tp_firstwin;
5562 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00005563
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005564 // Autocommands may delete the tab page under our fingers and we may
5565 // fail to close a window with a modified buffer.
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005566 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005567 break;
5568 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005569
Bram Moolenaar29323592016-07-24 22:04:11 +02005570 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar29323592016-07-24 22:04:11 +02005571
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005572 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005573 if (h != tabline_height())
5574 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00005575}
5576
5577/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005578 * ":only".
5579 */
5580 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005581ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005582{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005583 win_T *wp;
5584 int wnr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005585# ifdef FEAT_GUI
5586 need_mouse_correct = TRUE;
5587# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005588 if (eap->addr_count > 0)
5589 {
5590 wnr = eap->line2;
5591 for (wp = firstwin; --wnr > 0; )
5592 {
5593 if (wp->w_next == NULL)
5594 break;
5595 else
5596 wp = wp->w_next;
5597 }
5598 win_goto(wp);
5599 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005600 close_others(TRUE, eap->forceit);
5601}
5602
Bram Moolenaar071d4272004-06-13 20:20:40 +00005603 static void
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01005604ex_hide(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005605{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005606 // ":hide" or ":hide | cmd": hide current window
Bram Moolenaar2256c992016-11-15 21:17:07 +01005607 if (!eap->skip)
5608 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02005609#ifdef FEAT_GUI
Bram Moolenaar2256c992016-11-15 21:17:07 +01005610 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005611#endif
Bram Moolenaar2256c992016-11-15 21:17:07 +01005612 if (eap->addr_count == 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005613 win_close(curwin, FALSE); // don't free buffer
Bram Moolenaar2256c992016-11-15 21:17:07 +01005614 else
5615 {
5616 int winnr = 0;
5617 win_T *win;
Bram Moolenaarf240e182014-11-27 18:33:02 +01005618
Bram Moolenaar2256c992016-11-15 21:17:07 +01005619 FOR_ALL_WINDOWS(win)
5620 {
5621 winnr++;
5622 if (winnr == eap->line2)
5623 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005624 }
Bram Moolenaar2256c992016-11-15 21:17:07 +01005625 if (win == NULL)
5626 win = lastwin;
5627 win_close(win, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005628 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005629 }
5630}
5631
5632/*
5633 * ":stop" and ":suspend": Suspend Vim.
5634 */
5635 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005636ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005637{
5638 /*
5639 * Disallow suspending for "rvim".
5640 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005641 if (!check_restricted())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005642 {
5643 if (!eap->forceit)
5644 autowrite_all();
5645 windgoto((int)Rows - 1, 0);
5646 out_char('\n');
5647 out_flush();
5648 stoptermcap();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005649 out_flush(); // needed for SUN to restore xterm buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00005650#ifdef FEAT_TITLE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005651 mch_restore_title(SAVE_RESTORE_BOTH); // restore window titles
Bram Moolenaar071d4272004-06-13 20:20:40 +00005652#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005653 ui_suspend(); // call machine specific function
Bram Moolenaar071d4272004-06-13 20:20:40 +00005654#ifdef FEAT_TITLE
5655 maketitle();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005656 resettitle(); // force updating the title
Bram Moolenaar071d4272004-06-13 20:20:40 +00005657#endif
5658 starttermcap();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005659 scroll_start(); // scroll screen before redrawing
Bram Moolenaar071d4272004-06-13 20:20:40 +00005660 redraw_later_clear();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005661 shell_resized(); // may have resized window
Bram Moolenaar071d4272004-06-13 20:20:40 +00005662 }
5663}
5664
5665/*
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005666 * ":exit", ":xit" and ":wq": Write file and quite the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005667 */
5668 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005669ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005670{
5671#ifdef FEAT_CMDWIN
5672 if (cmdwin_type != 0)
5673 {
5674 cmdwin_result = Ctrl_C;
5675 return;
5676 }
5677#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005678 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005679 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005680 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005681 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005682 return;
5683 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005684
5685 if (before_quit_autocmds(curwin, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005686 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005687
5688 /*
5689 * if more files or windows we won't exit
5690 */
5691 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
5692 exiting = TRUE;
5693 if ( ((eap->cmdidx == CMD_wq
5694 || curbufIsChanged())
5695 && do_write(eap) == FAIL)
5696 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01005697 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005698 {
5699 not_exiting();
5700 }
5701 else
5702 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005703 if (only_one_window()) // quit last window, exit Vim
Bram Moolenaar071d4272004-06-13 20:20:40 +00005704 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02005705 not_exiting();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005706# ifdef FEAT_GUI
5707 need_mouse_correct = TRUE;
5708# endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005709 // Quit current window, may free the buffer.
Bram Moolenaareb44a682017-08-03 22:44:55 +02005710 win_close(curwin, !buf_hide(curwin->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005711 }
5712}
5713
5714/*
5715 * ":print", ":list", ":number".
5716 */
5717 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005718ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005719{
Bram Moolenaardf177f62005-02-22 08:39:57 +00005720 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005721 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +00005722 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005723 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00005724 for ( ;!got_int; ui_breakcheck())
5725 {
5726 print_line(eap->line1,
5727 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
5728 || (eap->flags & EXFLAG_NR)),
5729 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
5730 if (++eap->line1 > eap->line2)
5731 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005732 out_flush(); // show one line at a time
Bram Moolenaardf177f62005-02-22 08:39:57 +00005733 }
5734 setpcmark();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005735 // put cursor at last line
Bram Moolenaardf177f62005-02-22 08:39:57 +00005736 curwin->w_cursor.lnum = eap->line2;
5737 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005738 }
5739
Bram Moolenaar071d4272004-06-13 20:20:40 +00005740 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005741}
5742
5743#ifdef FEAT_BYTEOFF
5744 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005745ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005746{
5747 goto_byte(eap->line2);
5748}
5749#endif
5750
5751/*
5752 * ":shell".
5753 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005754 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005755ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005756{
5757 do_shell(NULL, 0);
5758}
5759
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005760#if defined(HAVE_DROP_FILE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005761
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005762static int drop_busy = FALSE;
5763static int drop_filec;
5764static char_u **drop_filev = NULL;
5765static int drop_split;
5766static void (*drop_callback)(void *);
5767static void *drop_cookie;
5768
5769 static void
5770handle_drop_internal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005771{
5772 exarg_T ea;
5773 int save_msg_scroll = msg_scroll;
5774
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005775 // Setting the argument list may cause screen updates and being called
5776 // recursively. Avoid that by setting drop_busy.
5777 drop_busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005778
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005779 // Check whether the current buffer is changed. If so, we will need
5780 // to split the current window or data could be lost.
5781 // We don't need to check if the 'hidden' option is set, as in this
5782 // case the buffer won't be lost.
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005783 if (!buf_hide(curbuf) && !drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005784 {
5785 ++emsg_off;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005786 drop_split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005787 --emsg_off;
5788 }
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005789 if (drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005790 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005791 if (win_split(0, 0) == FAIL)
5792 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02005793 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005794
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005795 // When splitting the window, create a new alist. Otherwise the
5796 // existing one is overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005797 alist_unlink(curwin->w_alist);
5798 alist_new();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005799 }
5800
5801 /*
5802 * Set up the new argument list.
5803 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005804 alist_set(ALIST(curwin), drop_filec, drop_filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005805
5806 /*
5807 * Move to the first file.
5808 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005809 // Fake up a minimal "next" command for do_argfile()
Bram Moolenaara80faa82020-04-12 19:37:17 +02005810 CLEAR_FIELD(ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005811 ea.cmd = (char_u *)"next";
5812 do_argfile(&ea, 0);
5813
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005814 // do_ecmd() may set need_start_insertmode, but since we never left Insert
5815 // mode that is not needed here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005816 need_start_insertmode = FALSE;
5817
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005818 // Restore msg_scroll, otherwise a following command may cause scrolling
5819 // unexpectedly. The screen will be redrawn by the caller, thus
5820 // msg_scroll being set by displaying a message is irrelevant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005821 msg_scroll = save_msg_scroll;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005822
5823 if (drop_callback != NULL)
5824 drop_callback(drop_cookie);
5825
5826 drop_filev = NULL;
5827 drop_busy = FALSE;
5828}
5829
5830/*
5831 * Handle a file drop. The code is here because a drop is *nearly* like an
5832 * :args command, but not quite (we have a list of exact filenames, so we
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01005833 * don't want to (a) parse a command line, or (b) expand wildcards). So the
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005834 * code is very similar to :args and hence needs access to a lot of the static
5835 * functions in this file.
5836 *
5837 * The "filev" list must have been allocated using alloc(), as should each item
5838 * in the list. This function takes over responsibility for freeing the "filev"
5839 * list.
5840 */
5841 void
5842handle_drop(
5843 int filec, // the number of files dropped
5844 char_u **filev, // the list of files dropped
5845 int split, // force splitting the window
5846 void (*callback)(void *), // to be called after setting the argument
5847 // list
5848 void *cookie) // argument for "callback" (allocated)
5849{
5850 // Cannot handle recursive drops, finish the pending one.
5851 if (drop_busy)
5852 {
5853 FreeWild(filec, filev);
5854 vim_free(cookie);
5855 return;
5856 }
5857
5858 // When calling handle_drop() more than once in a row we only use the last
5859 // one.
5860 if (drop_filev != NULL)
5861 {
5862 FreeWild(drop_filec, drop_filev);
5863 vim_free(drop_cookie);
5864 }
5865
5866 drop_filec = filec;
5867 drop_filev = filev;
5868 drop_split = split;
5869 drop_callback = callback;
5870 drop_cookie = cookie;
5871
5872 // Postpone this when:
5873 // - editing the command line
5874 // - not possible to change the current buffer
5875 // - updating the screen
5876 // As it may change buffers and window structures that are in use and cause
5877 // freed memory to be used.
5878 if (text_locked() || curbuf_locked() || updating_screen)
5879 return;
5880
5881 handle_drop_internal();
5882}
5883
5884/*
5885 * To be called when text is unlocked, curbuf is unlocked or updating_screen is
5886 * reset: Handle a postponed drop.
5887 */
5888 void
5889handle_any_postponed_drop(void)
5890{
5891 if (!drop_busy && drop_filev != NULL
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02005892 && !text_locked() && !curbuf_locked() && !updating_screen)
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005893 handle_drop_internal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005894}
5895#endif
5896
Bram Moolenaar071d4272004-06-13 20:20:40 +00005897/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005898 * ":preserve".
5899 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005900 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005901ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005902{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005903 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005904 ml_preserve(curbuf, TRUE);
5905}
5906
5907/*
5908 * ":recover".
5909 */
5910 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005911ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005912{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005913 // Set recoverymode right away to avoid the ATTENTION prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005914 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01005915 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
5916 | CCGD_MULTWIN
5917 | (eap->forceit ? CCGD_FORCEIT : 0)
5918 | CCGD_EXCMD)
5919
Bram Moolenaar071d4272004-06-13 20:20:40 +00005920 && (*eap->arg == NUL
5921 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
Bram Moolenaar99499b12019-05-23 21:35:48 +02005922 ml_recover(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005923 recoverymode = FALSE;
5924}
5925
5926/*
5927 * Command modifier used in a wrong way.
5928 */
5929 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005930ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005931{
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005932 eap->errmsg = _(e_invcmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005933}
5934
Bram Moolenaar071d4272004-06-13 20:20:40 +00005935/*
5936 * :sview [+command] file split window with new file, read-only
5937 * :split [[+command] file] split window with current or new file
5938 * :vsplit [[+command] file] split window vertically with current or new file
5939 * :new [[+command] file] split window with no or new file
5940 * :vnew [[+command] file] split vertically window with no or new file
5941 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005942 *
5943 * :tabedit open new Tab page with empty window
5944 * :tabedit [+command] file open new Tab page and edit "file"
5945 * :tabnew [[+command] file] just like :tabedit
5946 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005947 */
5948 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005949ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005950{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005951 win_T *old_curwin = curwin;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005952#if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005953 char_u *fname = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005954#endif
5955#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005956 int browse_flag = cmdmod.browse;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005957#endif
Bram Moolenaar39902a02018-06-21 22:10:08 +02005958 int use_tab = eap->cmdidx == CMD_tabedit
5959 || eap->cmdidx == CMD_tabfind
5960 || eap->cmdidx == CMD_tabnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005961
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005962 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005963 return;
5964
Bram Moolenaar4033c552017-09-16 20:54:51 +02005965#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005966 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005967#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005968
Bram Moolenaar4033c552017-09-16 20:54:51 +02005969#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005970 // A ":split" in the quickfix window works like ":new". Don't want two
5971 // quickfix windows. But it's OK when doing ":tab split".
Bram Moolenaar05bb9532008-07-04 09:44:11 +00005972 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005973 {
5974 if (eap->cmdidx == CMD_split)
5975 eap->cmdidx = CMD_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005976 if (eap->cmdidx == CMD_vsplit)
5977 eap->cmdidx = CMD_vnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005978 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02005979#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005980
Bram Moolenaar4033c552017-09-16 20:54:51 +02005981#ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005982 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005983 {
5984 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
5985 FNAME_MESS, TRUE, curbuf->b_ffname);
5986 if (fname == NULL)
5987 goto theend;
5988 eap->arg = fname;
5989 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005990# ifdef FEAT_BROWSE
Bram Moolenaar4033c552017-09-16 20:54:51 +02005991 else
5992# endif
5993#endif
5994#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005995 if (cmdmod.browse
Bram Moolenaar071d4272004-06-13 20:20:40 +00005996 && eap->cmdidx != CMD_vnew
Bram Moolenaar071d4272004-06-13 20:20:40 +00005997 && eap->cmdidx != CMD_new)
5998 {
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005999 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006000# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006001 !gui.in_use &&
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006002# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006003 au_has_group((char_u *)"FileExplorer"))
6004 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006005 // No browsing supported but we do have the file explorer:
6006 // Edit the directory.
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006007 if (*eap->arg == NUL || !mch_isdir(eap->arg))
6008 eap->arg = (char_u *)".";
6009 }
6010 else
6011 {
Bram Moolenaar39902a02018-06-21 22:10:08 +02006012 fname = do_browse(0, (char_u *)(use_tab
6013 ? _("Edit File in new tab page")
6014 : _("Edit File in new window")),
Bram Moolenaar071d4272004-06-13 20:20:40 +00006015 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006016 if (fname == NULL)
6017 goto theend;
6018 eap->arg = fname;
6019 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006020 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006021 cmdmod.browse = FALSE; // Don't browse again in do_ecmd().
Bram Moolenaar4033c552017-09-16 20:54:51 +02006022#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006023
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006024 /*
6025 * Either open new tab page or split the window.
6026 */
Bram Moolenaar39902a02018-06-21 22:10:08 +02006027 if (use_tab)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006028 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006029 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
6030 : eap->addr_count == 0 ? 0
6031 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006032 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00006033 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006034
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006035 // set the alternate buffer for the window we came from
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006036 if (curwin != old_curwin
6037 && win_valid(old_curwin)
6038 && old_curwin->w_buffer != curbuf
6039 && !cmdmod.keepalt)
6040 old_curwin->w_alt_fnum = curbuf->b_fnum;
6041 }
6042 }
6043 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006044 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
6045 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006046 // Reset 'scrollbind' when editing another file, but keep it when
6047 // doing ":split" without arguments.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006048 if (*eap->arg != NUL
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01006049# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006050 || cmdmod.browse
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01006051# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006052 )
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006053 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006054 else
6055 do_check_scrollbind(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006056 do_exedit(eap, old_curwin);
6057 }
6058
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006059# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006060 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006061# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006062
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006063# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006064theend:
6065 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006066# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006067}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006068
6069/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006070 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006071 */
6072 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006073tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006074{
6075 exarg_T ea;
6076
Bram Moolenaara80faa82020-04-12 19:37:17 +02006077 CLEAR_FIELD(ea);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006078 ea.cmdidx = CMD_tabnew;
6079 ea.cmd = (char_u *)"tabn";
6080 ea.arg = (char_u *)"";
6081 ex_splitview(&ea);
6082}
6083
6084/*
6085 * :tabnext command
6086 */
6087 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006088ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006089{
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006090 int tab_number;
6091
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02006092 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006093 return;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006094 switch (eap->cmdidx)
6095 {
6096 case CMD_tabfirst:
6097 case CMD_tabrewind:
6098 goto_tabpage(1);
6099 break;
6100 case CMD_tablast:
6101 goto_tabpage(9999);
6102 break;
6103 case CMD_tabprevious:
6104 case CMD_tabNext:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006105 if (eap->arg && *eap->arg != NUL)
6106 {
6107 char_u *p = eap->arg;
6108 char_u *p_save = p;
6109
6110 tab_number = getdigits(&p);
6111 if (p == p_save || *p_save == '-' || *p != NUL
6112 || tab_number == 0)
6113 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006114 // No numbers as argument.
Bram Moolenaar8930caa2020-07-23 16:37:03 +02006115 eap->errmsg = ex_errmsg(e_invarg2, eap->arg);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006116 return;
6117 }
6118 }
6119 else
6120 {
6121 if (eap->addr_count == 0)
6122 tab_number = 1;
6123 else
6124 {
6125 tab_number = eap->line2;
6126 if (tab_number < 1)
6127 {
Bram Moolenaar8930caa2020-07-23 16:37:03 +02006128 eap->errmsg = _(e_invrange);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006129 return;
6130 }
6131 }
6132 }
6133 goto_tabpage(-tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006134 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006135 default: // CMD_tabnext
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006136 tab_number = get_tabpage_arg(eap);
6137 if (eap->errmsg == NULL)
6138 goto_tabpage(tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006139 break;
6140 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006141}
6142
6143/*
6144 * :tabmove command
6145 */
6146 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006147ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006148{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02006149 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02006150
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006151 tab_number = get_tabpage_arg(eap);
6152 if (eap->errmsg == NULL)
6153 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006154}
6155
6156/*
6157 * :tabs command: List tabs and their contents.
6158 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006159 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006160ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006161{
6162 tabpage_T *tp;
6163 win_T *wp;
6164 int tabcount = 1;
6165
6166 msg_start();
6167 msg_scroll = TRUE;
6168 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
6169 {
6170 msg_putchar('\n');
6171 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
Bram Moolenaar8820b482017-03-16 17:23:31 +01006172 msg_outtrans_attr(IObuff, HL_ATTR(HLF_T));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006173 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006174 ui_breakcheck();
6175
Bram Moolenaar030f0df2006-02-21 22:02:53 +00006176 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006177 wp = firstwin;
6178 else
6179 wp = tp->tp_firstwin;
6180 for ( ; wp != NULL && !got_int; wp = wp->w_next)
6181 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006182 msg_putchar('\n');
6183 msg_putchar(wp == curwin ? '>' : ' ');
6184 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006185 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
6186 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006187 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02006188 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006189 else
6190 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
6191 IObuff, IOSIZE, TRUE);
6192 msg_outtrans(IObuff);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006193 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006194 ui_breakcheck();
6195 }
6196 }
6197}
6198
Bram Moolenaar071d4272004-06-13 20:20:40 +00006199/*
6200 * ":mode": Set screen mode.
6201 * If no argument given, just get the screen size and redraw.
6202 */
6203 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006204ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006205{
6206 if (*eap->arg == NUL)
6207 shell_resized();
6208 else
Bram Moolenaar3c71aec2020-01-17 19:32:20 +01006209 emsg(_(e_screenmode));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006210}
6211
Bram Moolenaar071d4272004-06-13 20:20:40 +00006212/*
6213 * ":resize".
6214 * set, increment or decrement current window height
6215 */
6216 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006217ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006218{
6219 int n;
6220 win_T *wp = curwin;
6221
6222 if (eap->addr_count > 0)
6223 {
6224 n = eap->line2;
6225 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
6226 ;
6227 }
6228
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006229# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006230 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006231# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006232 n = atol((char *)eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006233 if (cmdmod.split & WSP_VERT)
6234 {
6235 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar02631462017-09-22 15:20:32 +02006236 n += curwin->w_width;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006237 else if (n == 0 && eap->arg[0] == NUL) // default is very wide
Bram Moolenaar071d4272004-06-13 20:20:40 +00006238 n = 9999;
6239 win_setwidth_win((int)n, wp);
6240 }
6241 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006242 {
6243 if (*eap->arg == '-' || *eap->arg == '+')
6244 n += curwin->w_height;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006245 else if (n == 0 && eap->arg[0] == NUL) // default is very high
Bram Moolenaar071d4272004-06-13 20:20:40 +00006246 n = 9999;
6247 win_setheight_win((int)n, wp);
6248 }
6249}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006250
6251/*
6252 * ":find [+command] <file>" command.
6253 */
6254 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006255ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006256{
6257#ifdef FEAT_SEARCHPATH
6258 char_u *fname;
6259 int count;
6260
6261 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
6262 TRUE, curbuf->b_ffname);
6263 if (eap->addr_count > 0)
6264 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006265 // Repeat finding the file "count" times. This matters when it
6266 // appears several times in the path.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006267 count = eap->line2;
6268 while (fname != NULL && --count > 0)
6269 {
6270 vim_free(fname);
6271 fname = find_file_in_path(NULL, 0, FNAME_MESS,
6272 FALSE, curbuf->b_ffname);
6273 }
6274 }
6275
6276 if (fname != NULL)
6277 {
6278 eap->arg = fname;
6279#endif
6280 do_exedit(eap, NULL);
6281#ifdef FEAT_SEARCHPATH
6282 vim_free(fname);
6283 }
6284#endif
6285}
6286
6287/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00006288 * ":open" simulation: for now just work like ":visual".
6289 */
6290 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006291ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006292{
6293 regmatch_T regmatch;
6294 char_u *p;
6295
6296 curwin->w_cursor.lnum = eap->line2;
6297 beginline(BL_SOL | BL_FIX);
6298 if (*eap->arg == '/')
6299 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006300 // ":open /pattern/": put cursor in column found with pattern
Bram Moolenaardf177f62005-02-22 08:39:57 +00006301 ++eap->arg;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006302 p = skip_regexp(eap->arg, '/', p_magic);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006303 *p = NUL;
6304 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
6305 if (regmatch.regprog != NULL)
6306 {
6307 regmatch.rm_ic = p_ic;
6308 p = ml_get_curline();
6309 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006310 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006311 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006312 emsg(_(e_nomatch));
Bram Moolenaar473de612013-06-08 18:19:48 +02006313 vim_regfree(regmatch.regprog);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006314 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006315 // Move to the NUL, ignore any other arguments.
Bram Moolenaardf177f62005-02-22 08:39:57 +00006316 eap->arg += STRLEN(eap->arg);
6317 }
6318 check_cursor();
6319
6320 eap->cmdidx = CMD_visual;
6321 do_exedit(eap, NULL);
6322}
6323
6324/*
6325 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006326 */
6327 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006328ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006329{
6330 do_exedit(eap, NULL);
6331}
6332
6333/*
Bram Moolenaar4b96df52020-01-26 22:00:26 +01006334 * ":edit <file>" command and alike.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006335 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006336 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006337do_exedit(
6338 exarg_T *eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006339 win_T *old_curwin) // curwin before doing a split or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006340{
6341 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006342 int need_hide;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006343 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006344
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01006345 if ((eap->cmdidx != CMD_pedit && ERROR_IF_POPUP_WINDOW)
6346 || ERROR_IF_TERM_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006347 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006348 /*
6349 * ":vi" command ends Ex mode.
6350 */
6351 if (exmode_active && (eap->cmdidx == CMD_visual
6352 || eap->cmdidx == CMD_view))
6353 {
6354 exmode_active = FALSE;
Bram Moolenaar158ea172020-06-18 17:28:39 +02006355 ex_pressedreturn = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006356 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006357 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006358 // Special case: ":global/pat/visual\NLvi-commands"
Bram Moolenaardf177f62005-02-22 08:39:57 +00006359 if (global_busy)
6360 {
6361 int rd = RedrawingDisabled;
6362 int nwr = no_wait_return;
6363 int ms = msg_scroll;
6364#ifdef FEAT_GUI
6365 int he = hold_gui_events;
6366#endif
6367
6368 if (eap->nextcmd != NULL)
6369 {
6370 stuffReadbuff(eap->nextcmd);
6371 eap->nextcmd = NULL;
6372 }
6373
6374 if (exmode_was != EXMODE_VIM)
6375 settmode(TMODE_RAW);
6376 RedrawingDisabled = 0;
6377 no_wait_return = 0;
6378 need_wait_return = FALSE;
6379 msg_scroll = 0;
6380#ifdef FEAT_GUI
6381 hold_gui_events = 0;
6382#endif
6383 must_redraw = CLEAR;
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01006384 pending_exmode_active = TRUE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006385
6386 main_loop(FALSE, TRUE);
6387
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01006388 pending_exmode_active = FALSE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006389 RedrawingDisabled = rd;
6390 no_wait_return = nwr;
6391 msg_scroll = ms;
6392#ifdef FEAT_GUI
6393 hold_gui_events = he;
6394#endif
6395 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006396 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006397 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006398 }
6399
6400 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006401 || eap->cmdidx == CMD_tabnew
6402 || eap->cmdidx == CMD_tabedit
Bram Moolenaar4033c552017-09-16 20:54:51 +02006403 || eap->cmdidx == CMD_vnew) && *eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006404 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006405 // ":new" or ":tabnew" without argument: edit an new empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006406 setpcmark();
6407 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006408 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
6409 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006410 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006411 else if ((eap->cmdidx != CMD_split && eap->cmdidx != CMD_vsplit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006412 || *eap->arg != NUL
6413#ifdef FEAT_BROWSE
6414 || cmdmod.browse
6415#endif
6416 )
6417 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006418 // Can't edit another file when "curbuf_lock" is set. Only ":edit"
6419 // can bring us here, others are stopped earlier.
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006420 if (*eap->arg != NUL && curbuf_locked())
6421 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006422
Bram Moolenaar071d4272004-06-13 20:20:40 +00006423 n = readonlymode;
6424 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
6425 readonlymode = TRUE;
6426 else if (eap->cmdidx == CMD_enew)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006427 readonlymode = FALSE; // 'readonly' doesn't make sense in an
6428 // empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006429 setpcmark();
6430 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
6431 NULL, eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006432 // ":edit" goes to first line if Vi compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00006433 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
6434 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
6435 ? ECMD_ONE : eap->do_ecmd_lnum,
Bram Moolenaareb44a682017-08-03 22:44:55 +02006436 (buf_hide(curbuf) ? ECMD_HIDE : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006437 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006438 // after a split we can use an existing buffer
Bram Moolenaar7b449342014-03-25 13:03:48 +01006439 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006440 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006441 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006442 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006443 // Editing the file failed. If the window was split, close it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006444 if (old_curwin != NULL)
6445 {
6446 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02006447 if (!need_hide || buf_hide(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006448 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006449#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006450 cleanup_T cs;
6451
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006452 // Reset the error/interrupt/exception state here so that
6453 // aborting() returns FALSE when closing a window.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006454 enter_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02006455#endif
6456#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006457 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006458#endif
Bram Moolenaareb44a682017-08-03 22:44:55 +02006459 win_close(curwin, !need_hide && !buf_hide(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006460
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006461#if defined(FEAT_EVAL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006462 // Restore the error/interrupt/exception state if not
6463 // discarded by a new aborting error, interrupt, or
6464 // uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006465 leave_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02006466#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006467 }
6468 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006469 }
6470 else if (readonlymode && curbuf->b_nwindows == 1)
6471 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006472 // When editing an already visited buffer, 'readonly' won't be set
6473 // but the previous value is kept. With ":view" and ":sview" we
6474 // want the file to be readonly, except when another window is
6475 // editing the same buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006476 curbuf->b_p_ro = TRUE;
6477 }
6478 readonlymode = n;
6479 }
6480 else
6481 {
6482 if (eap->do_ecmd_cmd != NULL)
6483 do_cmdline_cmd(eap->do_ecmd_cmd);
6484#ifdef FEAT_TITLE
6485 n = curwin->w_arg_idx_invalid;
6486#endif
6487 check_arg_idx(curwin);
6488#ifdef FEAT_TITLE
6489 if (n != curwin->w_arg_idx_invalid)
6490 maketitle();
6491#endif
6492 }
6493
Bram Moolenaar071d4272004-06-13 20:20:40 +00006494 /*
6495 * if ":split file" worked, set alternate file name in old window to new
6496 * file
6497 */
6498 if (old_curwin != NULL
6499 && *eap->arg != NUL
6500 && curwin != old_curwin
6501 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00006502 && old_curwin->w_buffer != curbuf
6503 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006504 old_curwin->w_alt_fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006505
6506 ex_no_reprint = TRUE;
6507}
6508
6509#ifndef FEAT_GUI
6510/*
6511 * ":gui" and ":gvim" when there is no GUI.
6512 */
6513 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006514ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006515{
Bram Moolenaar8930caa2020-07-23 16:37:03 +02006516 eap->errmsg = _(e_nogvim);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006517}
6518#endif
6519
Bram Moolenaar4f974752019-02-17 17:44:42 +01006520#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006521 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006522ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006523{
6524 gui_make_tearoff(eap->arg);
6525}
6526#endif
6527
Bram Moolenaar29a2c082018-03-05 21:06:23 +01006528#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
6529 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006530 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006531ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006532{
Bram Moolenaar29a2c082018-03-05 21:06:23 +01006533# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
6534 if (gui.in_use)
6535 gui_make_popup(eap->arg, eap->forceit);
6536# ifdef FEAT_TERM_POPUP_MENU
6537 else
6538# endif
6539# endif
6540# ifdef FEAT_TERM_POPUP_MENU
6541 pum_make_popup(eap->arg, eap->forceit);
6542# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006543}
6544#endif
6545
Bram Moolenaar071d4272004-06-13 20:20:40 +00006546 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006547ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006548{
6549 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006550 msg(_("No swap file"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006551 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01006552 msg((char *)curbuf->b_ml.ml_mfp->mf_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006553}
6554
6555/*
6556 * ":syncbind" forces all 'scrollbind' windows to have the same relative
6557 * offset.
6558 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
6559 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006560 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006561ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006562{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006563 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006564 win_T *save_curwin = curwin;
6565 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006566 long topline;
6567 long y;
6568 linenr_T old_linenr = curwin->w_cursor.lnum;
6569
6570 setpcmark();
6571
6572 /*
6573 * determine max topline
6574 */
6575 if (curwin->w_p_scb)
6576 {
6577 topline = curwin->w_topline;
Bram Moolenaar29323592016-07-24 22:04:11 +02006578 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006579 {
6580 if (wp->w_p_scb && wp->w_buffer)
6581 {
Bram Moolenaar375e3392019-01-31 18:26:10 +01006582 y = wp->w_buffer->b_ml.ml_line_count - get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006583 if (topline > y)
6584 topline = y;
6585 }
6586 }
6587 if (topline < 1)
6588 topline = 1;
6589 }
6590 else
6591 {
6592 topline = 1;
6593 }
6594
6595
6596 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006597 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006598 */
Bram Moolenaar29323592016-07-24 22:04:11 +02006599 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006600 {
6601 if (curwin->w_p_scb)
6602 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006603 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006604 y = topline - curwin->w_topline;
6605 if (y > 0)
6606 scrollup(y, TRUE);
6607 else
6608 scrolldown(-y, TRUE);
6609 curwin->w_scbind_pos = topline;
6610 redraw_later(VALID);
6611 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006612 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006613 }
6614 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006615 curwin = save_curwin;
6616 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006617 if (curwin->w_p_scb)
6618 {
6619 did_syncbind = TRUE;
6620 checkpcmark();
6621 if (old_linenr != curwin->w_cursor.lnum)
6622 {
6623 char_u ctrl_o[2];
6624
6625 ctrl_o[0] = Ctrl_O;
6626 ctrl_o[1] = 0;
6627 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
6628 }
6629 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006630}
6631
6632
6633 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006634ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006635{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006636 int i;
6637 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
6638 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006639
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006640 if (eap->usefilter) // :r!cmd
Bram Moolenaar071d4272004-06-13 20:20:40 +00006641 do_bang(1, eap, FALSE, FALSE, TRUE);
6642 else
6643 {
6644 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
6645 return;
6646
6647#ifdef FEAT_BROWSE
6648 if (cmdmod.browse)
6649 {
6650 char_u *browseFile;
6651
Bram Moolenaar7171abe2004-10-11 10:06:20 +00006652 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006653 NULL, NULL, NULL, curbuf);
6654 if (browseFile != NULL)
6655 {
6656 i = readfile(browseFile, NULL,
6657 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
6658 vim_free(browseFile);
6659 }
6660 else
6661 i = OK;
6662 }
6663 else
6664#endif
6665 if (*eap->arg == NUL)
6666 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006667 if (check_fname() == FAIL) // check for no file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00006668 return;
6669 i = readfile(curbuf->b_ffname, curbuf->b_fname,
6670 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
6671 }
6672 else
6673 {
6674 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
6675 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
6676 i = readfile(eap->arg, NULL,
6677 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
6678
6679 }
Bram Moolenaare13b9af2017-01-13 22:01:02 +01006680 if (i != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006681 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006682#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006683 if (!aborting())
6684#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006685 semsg(_(e_notopen), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006686 }
6687 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00006688 {
6689 if (empty && exmode_active)
6690 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006691 // Delete the empty line that remains. Historically ex does
6692 // this but vi doesn't.
Bram Moolenaardf177f62005-02-22 08:39:57 +00006693 if (eap->line2 == 0)
6694 lnum = curbuf->b_ml.ml_line_count;
6695 else
6696 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00006697 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006698 {
Bram Moolenaarca70c072020-05-30 20:30:46 +02006699 ml_delete(lnum);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00006700 if (curwin->w_cursor.lnum > 1
6701 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006702 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00006703 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006704 }
6705 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006706 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006707 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006708 }
6709}
6710
Bram Moolenaarea408852005-06-25 22:49:46 +00006711static char_u *prev_dir = NULL;
6712
6713#if defined(EXITFREE) || defined(PROTO)
6714 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006715free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00006716{
Bram Moolenaard23a8232018-02-10 18:45:26 +01006717 VIM_CLEAR(prev_dir);
6718 VIM_CLEAR(globaldir);
Bram Moolenaarea408852005-06-25 22:49:46 +00006719}
6720#endif
6721
Bram Moolenaarf4258302013-06-02 18:20:17 +02006722/*
Bram Moolenaar002bc792020-06-05 22:33:42 +02006723 * Get the previous directory for the given chdir scope.
6724 */
6725 static char_u *
6726get_prevdir(cdscope_T scope)
6727{
6728 if (scope == CDSCOPE_WINDOW)
6729 return curwin->w_prevdir;
6730 else if (scope == CDSCOPE_TABPAGE)
6731 return curtab->tp_prevdir;
6732 return prev_dir;
6733}
6734
6735/*
Bram Moolenaarf4258302013-06-02 18:20:17 +02006736 * Deal with the side effects of changing the current directory.
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006737 * When 'scope' is CDSCOPE_TABPAGE then this was after an ":tcd" command.
6738 * When 'scope' is CDSCOPE_WINDOW then this was after an ":lcd" command.
Bram Moolenaarf4258302013-06-02 18:20:17 +02006739 */
6740 void
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006741post_chdir(cdscope_T scope)
Bram Moolenaarf4258302013-06-02 18:20:17 +02006742{
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006743 if (scope != CDSCOPE_WINDOW)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006744 // Clear tab local directory for both :cd and :tcd
6745 VIM_CLEAR(curtab->tp_localdir);
Bram Moolenaard23a8232018-02-10 18:45:26 +01006746 VIM_CLEAR(curwin->w_localdir);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006747 if (scope != CDSCOPE_GLOBAL)
Bram Moolenaarf4258302013-06-02 18:20:17 +02006748 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02006749 char_u *pdir = get_prevdir(scope);
6750
6751 // If still in the global directory, need to remember current
6752 // directory as the global directory.
6753 if (globaldir == NULL && pdir != NULL)
6754 globaldir = vim_strsave(pdir);
6755
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006756 // Remember this local directory for the window.
Bram Moolenaarf4258302013-06-02 18:20:17 +02006757 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006758 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006759 if (scope == CDSCOPE_TABPAGE)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006760 curtab->tp_localdir = vim_strsave(NameBuff);
6761 else
6762 curwin->w_localdir = vim_strsave(NameBuff);
6763 }
Bram Moolenaarf4258302013-06-02 18:20:17 +02006764 }
6765 else
6766 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02006767 // We are now in the global directory, no need to remember its name.
Bram Moolenaard23a8232018-02-10 18:45:26 +01006768 VIM_CLEAR(globaldir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006769 }
6770
6771 shorten_fnames(TRUE);
6772}
6773
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006774/*
6775 * Change directory function used by :cd/:tcd/:lcd Ex commands and the
Bram Moolenaar95058722020-06-01 16:26:19 +02006776 * chdir() function.
6777 * scope == CDSCOPE_WINDOW: changes the window-local directory
6778 * scope == CDSCOPE_TABPAGE: changes the tab-local directory
6779 * Otherwise: changes the global directory
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006780 * Returns TRUE if the directory is successfully changed.
6781 */
6782 int
6783changedir_func(
6784 char_u *new_dir,
6785 int forceit,
6786 cdscope_T scope)
6787{
6788 char_u *tofree;
Bram Moolenaar002bc792020-06-05 22:33:42 +02006789 char_u *pdir = NULL;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006790 int dir_differs;
6791 int retval = FALSE;
6792
Bram Moolenaar7cc96922020-01-31 22:41:38 +01006793 if (new_dir == NULL || allbuf_locked())
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006794 return FALSE;
6795
6796 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged() && !forceit)
6797 {
6798 emsg(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
6799 return FALSE;
6800 }
6801
6802 // ":cd -": Change to previous directory
6803 if (STRCMP(new_dir, "-") == 0)
6804 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02006805 pdir = get_prevdir(scope);
6806 if (pdir == NULL)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006807 {
6808 emsg(_("E186: No previous directory"));
6809 return FALSE;
6810 }
Bram Moolenaar002bc792020-06-05 22:33:42 +02006811 new_dir = pdir;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006812 }
6813
Bram Moolenaar002bc792020-06-05 22:33:42 +02006814 // Free the previous directory
6815 tofree = get_prevdir(scope);
6816
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006817 // Save current directory for next ":cd -"
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006818 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar002bc792020-06-05 22:33:42 +02006819 pdir = vim_strsave(NameBuff);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006820 else
Bram Moolenaar002bc792020-06-05 22:33:42 +02006821 pdir = NULL;
6822 if (scope == CDSCOPE_WINDOW)
6823 curwin->w_prevdir = pdir;
6824 else if (scope == CDSCOPE_TABPAGE)
6825 curtab->tp_prevdir = pdir;
6826 else
6827 prev_dir = pdir;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006828
6829#if defined(UNIX) || defined(VMS)
6830 // for UNIX ":cd" means: go to home directory
6831 if (*new_dir == NUL)
6832 {
6833 // use NameBuff for home directory name
6834# ifdef VMS
6835 char_u *p;
6836
6837 p = mch_getenv((char_u *)"SYS$LOGIN");
6838 if (p == NULL || *p == NUL) // empty is the same as not set
6839 NameBuff[0] = NUL;
6840 else
6841 vim_strncpy(NameBuff, p, MAXPATHL - 1);
6842# else
6843 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
6844# endif
6845 new_dir = NameBuff;
6846 }
6847#endif
Bram Moolenaar002bc792020-06-05 22:33:42 +02006848 dir_differs = new_dir == NULL || pdir == NULL
6849 || pathcmp((char *)pdir, (char *)new_dir, -1) != 0;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006850 if (new_dir == NULL || (dir_differs && vim_chdir(new_dir)))
6851 emsg(_(e_failed));
6852 else
6853 {
6854 char_u *acmd_fname;
6855
6856 post_chdir(scope);
6857
6858 if (dir_differs)
6859 {
6860 if (scope == CDSCOPE_WINDOW)
6861 acmd_fname = (char_u *)"window";
6862 else if (scope == CDSCOPE_TABPAGE)
6863 acmd_fname = (char_u *)"tabpage";
6864 else
6865 acmd_fname = (char_u *)"global";
6866 apply_autocmds(EVENT_DIRCHANGED, acmd_fname, new_dir, FALSE,
6867 curbuf);
6868 }
6869 retval = TRUE;
6870 }
6871 vim_free(tofree);
6872
6873 return retval;
6874}
Bram Moolenaarea408852005-06-25 22:49:46 +00006875
Bram Moolenaar071d4272004-06-13 20:20:40 +00006876/*
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006877 * ":cd", ":tcd", ":lcd", ":chdir" ":tchdir" and ":lchdir".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006878 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00006879 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006880ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006881{
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01006882 char_u *new_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006883
6884 new_dir = eap->arg;
6885#if !defined(UNIX) && !defined(VMS)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006886 // for non-UNIX ":cd" means: print current directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006887 if (*new_dir == NUL)
6888 ex_pwd(NULL);
6889 else
6890#endif
6891 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006892 cdscope_T scope = CDSCOPE_GLOBAL;
6893
6894 if (eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir)
6895 scope = CDSCOPE_WINDOW;
6896 else if (eap->cmdidx == CMD_tcd || eap->cmdidx == CMD_tchdir)
6897 scope = CDSCOPE_TABPAGE;
6898
6899 if (changedir_func(new_dir, eap->forceit, scope))
Bram Moolenaardf177f62005-02-22 08:39:57 +00006900 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006901 // Echo the new current directory if the command was typed.
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00006902 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006903 ex_pwd(eap);
6904 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006905 }
6906}
6907
6908/*
6909 * ":pwd".
6910 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006911 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006912ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006913{
6914 if (mch_dirname(NameBuff, MAXPATHL) == OK)
6915 {
6916#ifdef BACKSLASH_IN_FILENAME
6917 slash_adjust(NameBuff);
6918#endif
Bram Moolenaar95058722020-06-01 16:26:19 +02006919 if (p_verbose > 0)
6920 {
6921 char *context = "global";
6922
6923 if (curwin->w_localdir != NULL)
6924 context = "window";
6925 else if (curtab->tp_localdir != NULL)
6926 context = "tabpage";
6927 smsg("[%s] %s", context, (char *)NameBuff);
6928 }
6929 else
6930 msg((char *)NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006931 }
6932 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006933 emsg(_("E187: Unknown"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006934}
6935
6936/*
6937 * ":=".
6938 */
6939 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006940ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006941{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006942 smsg("%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006943 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006944}
6945
6946 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006947ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006948{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006949 int n;
6950 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006951
6952 if (cursor_valid())
6953 {
6954 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
6955 if (n >= 0)
Bram Moolenaar53f81742017-09-22 14:35:51 +02006956 windgoto((int)n, curwin->w_wincol + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006957 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006958
6959 len = eap->line2;
6960 switch (*eap->arg)
6961 {
6962 case 'm': break;
6963 case NUL: len *= 1000L; break;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006964 default: semsg(_(e_invarg2), eap->arg); return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006965 }
6966 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006967}
6968
6969/*
6970 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
6971 */
6972 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006973do_sleep(long msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006974{
Bram Moolenaar52953192019-08-16 10:27:13 +02006975 long done = 0;
Bram Moolenaar975b5272016-03-15 23:10:59 +01006976 long wait_now;
Bram Moolenaar52953192019-08-16 10:27:13 +02006977# ifdef ELAPSED_FUNC
6978 elapsed_T start_tv;
6979
6980 // Remember at what time we started, so that we know how much longer we
6981 // should wait after waiting for a bit.
6982 ELAPSED_INIT(start_tv);
6983# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006984
6985 cursor_on();
Bram Moolenaarf45d7472018-09-30 18:22:26 +02006986 out_flush_cursor(FALSE, FALSE);
Bram Moolenaar52953192019-08-16 10:27:13 +02006987 while (!got_int && done < msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006988 {
Bram Moolenaar975b5272016-03-15 23:10:59 +01006989 wait_now = msec - done > 1000L ? 1000L : msec - done;
6990#ifdef FEAT_TIMERS
6991 {
6992 long due_time = check_due_timer();
6993
6994 if (due_time > 0 && due_time < wait_now)
6995 wait_now = due_time;
6996 }
6997#endif
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02006998#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar28e67e02019-08-15 23:05:49 +02006999 if (has_any_channel() && wait_now > 20L)
7000 wait_now = 20L;
7001#endif
7002#ifdef FEAT_SOUND
7003 if (has_any_sound_callback() && wait_now > 20L)
7004 wait_now = 20L;
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007005#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +01007006 ui_delay(wait_now, TRUE);
Bram Moolenaar52953192019-08-16 10:27:13 +02007007
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007008#ifdef FEAT_JOB_CHANNEL
7009 if (has_any_channel())
7010 ui_breakcheck_force(TRUE);
7011 else
7012#endif
7013 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007014#ifdef MESSAGE_QUEUE
Bram Moolenaar52953192019-08-16 10:27:13 +02007015 // Process the netbeans and clientserver messages that may have been
7016 // received in the call to ui_breakcheck() when the GUI is in use. This
7017 // may occur when running a test case.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007018 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02007019#endif
Bram Moolenaar52953192019-08-16 10:27:13 +02007020
7021# ifdef ELAPSED_FUNC
7022 // actual time passed
7023 done = ELAPSED_FUNC(start_tv);
7024# else
7025 // guestimate time passed (will actually be more)
7026 done += wait_now;
7027# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007028 }
Bram Moolenaar1ebff3d2018-07-07 16:18:13 +02007029
7030 // If CTRL-C was typed to interrupt the sleep, drop the CTRL-C from the
7031 // input buffer, otherwise a following call to input() fails.
7032 if (got_int)
7033 (void)vpeekc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007034}
7035
Bram Moolenaar071d4272004-06-13 20:20:40 +00007036/*
7037 * ":winsize" command (obsolete).
7038 */
7039 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007040ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007041{
7042 int w, h;
7043 char_u *arg = eap->arg;
7044 char_u *p;
7045
7046 w = getdigits(&arg);
7047 arg = skipwhite(arg);
7048 p = arg;
7049 h = getdigits(&arg);
7050 if (*p != NUL && *arg == NUL)
7051 set_shellsize(w, h, TRUE);
7052 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007053 emsg(_("E465: :winsize requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007054}
7055
Bram Moolenaar071d4272004-06-13 20:20:40 +00007056 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007057ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007058{
7059 int xchar = NUL;
7060 char_u *p;
7061
7062 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
7063 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007064 // CTRL-W g and CTRL-W CTRL-G have an extra command character
Bram Moolenaar071d4272004-06-13 20:20:40 +00007065 if (eap->arg[1] == NUL)
7066 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007067 emsg(_(e_invarg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007068 return;
7069 }
7070 xchar = eap->arg[1];
7071 p = eap->arg + 2;
7072 }
7073 else
7074 p = eap->arg + 1;
7075
7076 eap->nextcmd = check_nextcmd(p);
7077 p = skipwhite(p);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02007078 if (*p != NUL && *p != (
7079#ifdef FEAT_EVAL
7080 in_vim9script() ? '#' :
7081#endif
7082 '"')
7083 && eap->nextcmd == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007084 emsg(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02007085 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007086 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007087 // Pass flags on for ":vertical wincmd ]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007088 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00007089 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007090 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
7091 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00007092 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007093 }
7094}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007095
Bram Moolenaar843ee412004-06-30 16:16:41 +00007096#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007097/*
7098 * ":winpos".
7099 */
7100 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007101ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007102{
7103 int x, y;
7104 char_u *arg = eap->arg;
7105 char_u *p;
7106
7107 if (*arg == NUL)
7108 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00007109# if defined(FEAT_GUI) || defined(MSWIN)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007110# ifdef VIMDLL
7111 if (gui.in_use ? gui_mch_get_winpos(&x, &y) != FAIL :
7112 mch_get_winpos(&x, &y) != FAIL)
7113# elif defined(FEAT_GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007114 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00007115# else
7116 if (mch_get_winpos(&x, &y) != FAIL)
7117# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007118 {
7119 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
Bram Moolenaar32526b32019-01-19 17:43:09 +01007120 msg((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007121 }
7122 else
7123# endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007124 emsg(_("E188: Obtaining window position not implemented for this platform"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007125 }
7126 else
7127 {
7128 x = getdigits(&arg);
7129 arg = skipwhite(arg);
7130 p = arg;
7131 y = getdigits(&arg);
7132 if (*p == NUL || *arg != NUL)
7133 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007134 emsg(_("E466: :winpos requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007135 return;
7136 }
7137# ifdef FEAT_GUI
7138 if (gui.in_use)
7139 gui_mch_set_winpos(x, y);
7140 else if (gui.starting)
7141 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007142 // Remember the coordinates for when the window is opened.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007143 gui_win_x = x;
7144 gui_win_y = y;
7145 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007146# if defined(HAVE_TGETENT) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007147 else
7148# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007149# endif
7150# if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar843ee412004-06-30 16:16:41 +00007151 mch_set_winpos(x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007152# endif
7153# ifdef HAVE_TGETENT
7154 if (*T_CWP)
7155 term_set_winpos(x, y);
7156# endif
7157 }
7158}
7159#endif
7160
7161/*
7162 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
7163 */
7164 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007165ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007166{
7167 oparg_T oa;
7168
7169 clear_oparg(&oa);
7170 oa.regname = eap->regname;
7171 oa.start.lnum = eap->line1;
7172 oa.end.lnum = eap->line2;
7173 oa.line_count = eap->line2 - eap->line1 + 1;
7174 oa.motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007175 virtual_op = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007176 if (eap->cmdidx != CMD_yank) // position cursor for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00007177 {
7178 setpcmark();
7179 curwin->w_cursor.lnum = eap->line1;
7180 beginline(BL_SOL | BL_FIX);
7181 }
7182
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007183 if (VIsual_active)
7184 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007185
Bram Moolenaar071d4272004-06-13 20:20:40 +00007186 switch (eap->cmdidx)
7187 {
7188 case CMD_delete:
7189 oa.op_type = OP_DELETE;
7190 op_delete(&oa);
7191 break;
7192
7193 case CMD_yank:
7194 oa.op_type = OP_YANK;
7195 (void)op_yank(&oa, FALSE, TRUE);
7196 break;
7197
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007198 default: // CMD_rshift or CMD_lshift
Bram Moolenaar6e707362013-06-14 19:15:58 +02007199 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007200#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02007201 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
7202#else
7203 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00007204#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02007205 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007206 oa.op_type = OP_RSHIFT;
7207 else
7208 oa.op_type = OP_LSHIFT;
7209 op_shift(&oa, FALSE, eap->amount);
7210 break;
7211 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007212 virtual_op = MAYBE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007213 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007214}
7215
7216/*
7217 * ":put".
7218 */
7219 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007220ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007221{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007222 // ":0put" works like ":1put!".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007223 if (eap->line2 == 0)
7224 {
7225 eap->line2 = 1;
7226 eap->forceit = TRUE;
7227 }
7228 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007229 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
7230 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007231}
7232
7233/*
7234 * Handle ":copy" and ":move".
7235 */
7236 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007237ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007238{
7239 long n;
7240
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02007241 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE, FALSE, 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007242 if (eap->arg == NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00007243 {
7244 eap->nextcmd = NULL;
7245 return;
7246 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00007247 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007248
7249 /*
7250 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
7251 */
7252 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
7253 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02007254 emsg(_(e_invrange));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007255 return;
7256 }
7257
7258 if (eap->cmdidx == CMD_move)
7259 {
7260 if (do_move(eap->line1, eap->line2, n) == FAIL)
7261 return;
7262 }
7263 else
7264 ex_copy(eap->line1, eap->line2, n);
7265 u_clearline();
7266 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007267 ex_may_print(eap);
7268}
7269
7270/*
7271 * Print the current line if flags were given to the Ex command.
7272 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02007273 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007274ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007275{
7276 if (eap->flags != 0)
7277 {
7278 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
7279 (eap->flags & EXFLAG_LIST));
7280 ex_no_reprint = TRUE;
7281 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007282}
7283
7284/*
7285 * ":smagic" and ":snomagic".
7286 */
7287 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007288ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007289{
7290 int magic_save = p_magic;
7291
7292 p_magic = (eap->cmdidx == CMD_smagic);
7293 do_sub(eap);
7294 p_magic = magic_save;
7295}
7296
7297/*
7298 * ":join".
7299 */
7300 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007301ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007302{
7303 curwin->w_cursor.lnum = eap->line1;
7304 if (eap->line1 == eap->line2)
7305 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007306 if (eap->addr_count >= 2) // :2,2join does nothing
Bram Moolenaar071d4272004-06-13 20:20:40 +00007307 return;
7308 if (eap->line2 == curbuf->b_ml.ml_line_count)
7309 {
7310 beep_flush();
7311 return;
7312 }
7313 ++eap->line2;
7314 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02007315 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007316 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007317 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007318}
7319
7320/*
7321 * ":[addr]@r" or ":[addr]*r": execute register
7322 */
7323 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007324ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007325{
7326 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00007327 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007328
7329 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4930a762016-09-11 14:39:53 +02007330 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007331
7332#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007333 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00007334#endif
7335
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007336 // get the register name. No name means to use the previous one
Bram Moolenaar071d4272004-06-13 20:20:40 +00007337 c = *eap->arg;
7338 if (c == NUL || (c == '*' && *eap->cmd == '*'))
7339 c = '@';
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007340 // Put the register in the typeahead buffer with the "silent" flag.
Bram Moolenaard333d1e2006-11-07 17:43:47 +00007341 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
7342 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007343 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007344 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00007345 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007346 else
7347 {
7348 int save_efr = exec_from_reg;
7349
7350 exec_from_reg = TRUE;
7351
7352 /*
7353 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00007354 * Continue until the stuff buffer is empty and all added characters
7355 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007356 */
Bram Moolenaar60462872009-11-03 11:40:19 +00007357 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007358 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
7359
7360 exec_from_reg = save_efr;
7361 }
7362}
7363
7364/*
7365 * ":!".
7366 */
7367 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007368ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007369{
7370 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
7371}
7372
7373/*
7374 * ":undo".
7375 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007376 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01007377ex_undo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007378{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007379 if (eap->addr_count == 1) // :undo 123
Bram Moolenaar730cde92010-06-27 05:18:54 +02007380 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00007381 else
7382 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007383}
7384
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007385#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02007386 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007387ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007388{
7389 char_u hash[UNDO_HASH_SIZE];
7390
7391 u_compute_hash(hash);
7392 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
7393}
7394
Bram Moolenaar6df6f472010-07-18 18:04:50 +02007395 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007396ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007397{
7398 char_u hash[UNDO_HASH_SIZE];
7399
7400 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02007401 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007402}
7403#endif
7404
Bram Moolenaar071d4272004-06-13 20:20:40 +00007405/*
7406 * ":redo".
7407 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007408 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007409ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007410{
7411 u_redo(1);
7412}
7413
7414/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007415 * ":earlier" and ":later".
7416 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007417 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007418ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007419{
7420 long count = 0;
7421 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02007422 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007423 char_u *p = eap->arg;
7424
7425 if (*p == NUL)
7426 count = 1;
7427 else if (isdigit(*p))
7428 {
7429 count = getdigits(&p);
7430 switch (*p)
7431 {
7432 case 's': ++p; sec = TRUE; break;
7433 case 'm': ++p; sec = TRUE; count *= 60; break;
7434 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02007435 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
7436 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007437 }
7438 }
7439
7440 if (*p != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007441 semsg(_(e_invarg2), eap->arg);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007442 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02007443 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
7444 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007445}
7446
7447/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007448 * ":redir": start/stop redirection.
7449 */
7450 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007451ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007452{
7453 char *mode;
7454 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00007455 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007456
Bram Moolenaarba768492016-07-08 15:32:54 +02007457#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02007458 if (redir_execute)
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007459 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007460 emsg(_("E930: Cannot use :redir inside execute()"));
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007461 return;
7462 }
Bram Moolenaarba768492016-07-08 15:32:54 +02007463#endif
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007464
Bram Moolenaar071d4272004-06-13 20:20:40 +00007465 if (STRICMP(eap->arg, "END") == 0)
7466 close_redir();
7467 else
7468 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007469 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007470 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007471 ++arg;
7472 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007473 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007474 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007475 mode = "a";
7476 }
7477 else
7478 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00007479 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007480
7481 close_redir();
7482
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007483 // Expand environment variables and "~/".
Bram Moolenaarca472992005-01-21 11:46:23 +00007484 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007485 if (fname == NULL)
7486 return;
7487#ifdef FEAT_BROWSE
7488 if (cmdmod.browse)
7489 {
7490 char_u *browseFile;
7491
Bram Moolenaar7171abe2004-10-11 10:06:20 +00007492 browseFile = do_browse(BROWSE_SAVE,
7493 (char_u *)_("Save Redirection"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +02007494 fname, NULL, NULL,
7495 (char_u *)_(BROWSE_FILTER_ALL_FILES), curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007496 if (browseFile == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007497 return; // operation cancelled
Bram Moolenaar071d4272004-06-13 20:20:40 +00007498 vim_free(fname);
7499 fname = browseFile;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007500 eap->forceit = TRUE; // since dialog already asked
Bram Moolenaar071d4272004-06-13 20:20:40 +00007501 }
7502#endif
7503
7504 redir_fd = open_exfile(fname, eap->forceit, mode);
7505 vim_free(fname);
7506 }
7507#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00007508 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007509 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007510 // redirect to a register a-z (resp. A-Z for appending)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007511 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00007512 ++arg;
7513 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007514# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00007515 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00007516 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00007517# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00007518 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007519 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007520 redir_reg = *arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007521 if (*arg == '>' && arg[1] == '>') // append
Bram Moolenaard9d30582005-05-18 22:10:28 +00007522 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00007523 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007524 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007525 // Can use both "@a" and "@a>".
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00007526 if (*arg == '>')
7527 arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007528 // Make register empty when not using @A-@Z and the
7529 // command is valid.
Bram Moolenaarc188b882007-10-19 14:20:54 +00007530 if (*arg == NUL && !isupper(redir_reg))
7531 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007532 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00007533 }
7534 if (*arg != NUL)
7535 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007536 redir_reg = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007537 semsg(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007538 }
7539 }
7540 else if (*arg == '=' && arg[1] == '>')
7541 {
7542 int append;
7543
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007544 // redirect to a variable
Bram Moolenaardf177f62005-02-22 08:39:57 +00007545 close_redir();
7546 arg += 2;
7547
7548 if (*arg == '>')
7549 {
7550 ++arg;
7551 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007552 }
7553 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00007554 append = FALSE;
7555
7556 if (var_redir_start(skipwhite(arg), append) == OK)
7557 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007558 }
7559#endif
7560
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007561 // TODO: redirect to a buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00007562
Bram Moolenaar071d4272004-06-13 20:20:40 +00007563 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007564 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007565 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00007566
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007567 // Make sure redirection is not off. Can happen for cmdline completion
7568 // that indirectly invokes a command to catch its output.
Bram Moolenaar29b2d262006-09-10 19:07:28 +00007569 if (redir_fd != NULL
7570#ifdef FEAT_EVAL
7571 || redir_reg || redir_vname
7572#endif
7573 )
7574 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007575}
7576
7577/*
7578 * ":redraw": force redraw
7579 */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01007580 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007581ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007582{
7583 int r = RedrawingDisabled;
7584 int p = p_lz;
7585
7586 RedrawingDisabled = 0;
7587 p_lz = FALSE;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01007588 validate_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007589 update_topline();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007590 update_screen(eap->forceit ? CLEAR : VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007591#ifdef FEAT_TITLE
7592 if (need_maketitle)
7593 maketitle();
7594#endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007595#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
7596# ifdef VIMDLL
7597 if (!gui.in_use)
7598# endif
7599 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01007600#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007601 RedrawingDisabled = r;
7602 p_lz = p;
7603
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007604 // Reset msg_didout, so that a message that's there is overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007605 msg_didout = FALSE;
7606 msg_col = 0;
7607
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007608 // No need to wait after an intentional redraw.
Bram Moolenaar56667a52013-07-17 11:54:28 +02007609 need_wait_return = FALSE;
7610
Bram Moolenaar071d4272004-06-13 20:20:40 +00007611 out_flush();
7612}
7613
7614/*
7615 * ":redrawstatus": force redraw of status line(s)
7616 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007617 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007618ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007619{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007620 int r = RedrawingDisabled;
7621 int p = p_lz;
7622
7623 RedrawingDisabled = 0;
7624 p_lz = FALSE;
7625 if (eap->forceit)
7626 status_redraw_all();
7627 else
7628 status_redraw_curbuf();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007629 update_screen(VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007630 RedrawingDisabled = r;
7631 p_lz = p;
7632 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007633}
7634
Bram Moolenaare12bab32019-01-08 22:02:56 +01007635/*
7636 * ":redrawtabline": force redraw of the tabline
7637 */
7638 static void
7639ex_redrawtabline(exarg_T *eap UNUSED)
7640{
7641 int r = RedrawingDisabled;
7642 int p = p_lz;
7643
7644 RedrawingDisabled = 0;
7645 p_lz = FALSE;
7646
7647 draw_tabline();
7648
7649 RedrawingDisabled = r;
7650 p_lz = p;
7651 out_flush();
7652}
7653
Bram Moolenaar071d4272004-06-13 20:20:40 +00007654 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007655close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007656{
7657 if (redir_fd != NULL)
7658 {
7659 fclose(redir_fd);
7660 redir_fd = NULL;
7661 }
7662#ifdef FEAT_EVAL
7663 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007664 if (redir_vname)
7665 {
7666 var_redir_stop();
7667 redir_vname = 0;
7668 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007669#endif
7670}
7671
Bram Moolenaarf96ae0b2019-07-28 15:21:55 +02007672#if (defined(FEAT_SESSION) || defined(FEAT_EVAL)) || defined(PROTO)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007673 int
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02007674vim_mkdir_emsg(char_u *name, int prot UNUSED)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007675{
7676 if (vim_mkdir(name, prot) != 0)
7677 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007678 semsg(_("E739: Cannot create directory: %s"), name);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007679 return FAIL;
7680 }
7681 return OK;
7682}
7683#endif
7684
Bram Moolenaar071d4272004-06-13 20:20:40 +00007685/*
7686 * Open a file for writing for an Ex command, with some checks.
7687 * Return file descriptor, or NULL on failure.
7688 */
7689 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007690open_exfile(
7691 char_u *fname,
7692 int forceit,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007693 char *mode) // "w" for create new file or "a" for append
Bram Moolenaar071d4272004-06-13 20:20:40 +00007694{
7695 FILE *fd;
7696
7697#ifdef UNIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007698 // with Unix it is possible to open a directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007699 if (mch_isdir(fname))
7700 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007701 semsg(_(e_isadir2), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007702 return NULL;
7703 }
7704#endif
7705 if (!forceit && *mode != 'a' && vim_fexists(fname))
7706 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007707 semsg(_("E189: \"%s\" exists (add ! to override)"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007708 return NULL;
7709 }
7710
7711 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007712 semsg(_("E190: Cannot open \"%s\" for writing"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007713
7714 return fd;
7715}
7716
7717/*
7718 * ":mark" and ":k".
7719 */
7720 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007721ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007722{
7723 pos_T pos;
7724
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007725 if (*eap->arg == NUL) // No argument?
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007726 emsg(_(e_argreq));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007727 else if (eap->arg[1] != NUL) // more than one character?
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007728 emsg(_(e_trailing));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007729 else
7730 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007731 pos = curwin->w_cursor; // save curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00007732 curwin->w_cursor.lnum = eap->line2;
7733 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007734 if (setmark(*eap->arg) == FAIL) // set mark
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007735 emsg(_("E191: Argument must be a letter or forward/backward quote"));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007736 curwin->w_cursor = pos; // restore curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00007737 }
7738}
7739
7740/*
7741 * Update w_topline, w_leftcol and the cursor position.
7742 */
7743 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007744update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007745{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007746 check_cursor(); // put cursor on valid line
Bram Moolenaar071d4272004-06-13 20:20:40 +00007747 update_topline();
7748 if (!curwin->w_p_wrap)
7749 validate_cursor();
7750 update_curswant();
7751}
7752
Bram Moolenaar071d4272004-06-13 20:20:40 +00007753/*
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007754 * Save the current State and go to Normal mode.
7755 * Return TRUE if the typeahead could be saved.
7756 */
7757 int
7758save_current_state(save_state_T *sst)
7759{
7760 sst->save_msg_scroll = msg_scroll;
7761 sst->save_restart_edit = restart_edit;
7762 sst->save_msg_didout = msg_didout;
7763 sst->save_State = State;
7764 sst->save_insertmode = p_im;
7765 sst->save_finish_op = finish_op;
7766 sst->save_opcount = opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01007767 sst->save_reg_executing = reg_executing;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007768
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007769 msg_scroll = FALSE; // no msg scrolling in Normal mode
7770 restart_edit = 0; // don't go to Insert mode
7771 p_im = FALSE; // don't use 'insertmode'
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007772
7773 /*
7774 * Save the current typeahead. This is required to allow using ":normal"
7775 * from an event handler and makes sure we don't hang when the argument
7776 * ends with half a command.
7777 */
7778 save_typeahead(&sst->tabuf);
7779 return sst->tabuf.typebuf_valid;
7780}
7781
7782 void
7783restore_current_state(save_state_T *sst)
7784{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007785 // Restore the previous typeahead.
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007786 restore_typeahead(&sst->tabuf);
7787
7788 msg_scroll = sst->save_msg_scroll;
7789 restart_edit = sst->save_restart_edit;
7790 p_im = sst->save_insertmode;
7791 finish_op = sst->save_finish_op;
7792 opcount = sst->save_opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01007793 reg_executing = sst->save_reg_executing;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007794 msg_didout |= sst->save_msg_didout; // don't reset msg_didout now
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007795
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007796 // Restore the state (needed when called from a function executed for
7797 // 'indentexpr'). Update the mouse and cursor, they may have changed.
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007798 State = sst->save_State;
7799#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007800 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007801#endif
7802}
7803
7804/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007805 * ":normal[!] {commands}": Execute normal mode commands.
7806 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01007807 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007808ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007809{
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007810 save_state_T save_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007811 char_u *arg = NULL;
7812 int l;
7813 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007814
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00007815 if (ex_normal_lock > 0)
7816 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007817 emsg(_(e_secure));
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00007818 return;
7819 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007820 if (ex_normal_busy >= p_mmd)
7821 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007822 emsg(_("E192: Recursive use of :normal too deep"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007823 return;
7824 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007825
Bram Moolenaar071d4272004-06-13 20:20:40 +00007826 /*
7827 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
7828 * this for the K_SPECIAL leading byte, otherwise special keys will not
7829 * work.
7830 */
7831 if (has_mbyte)
7832 {
7833 int len = 0;
7834
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007835 // Count the number of characters to be escaped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007836 for (p = eap->arg; *p != NUL; ++p)
7837 {
Bram Moolenaar13505972019-01-24 15:04:48 +01007838#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007839 if (*p == CSI) // leadbyte CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007840 len += 2;
Bram Moolenaar13505972019-01-24 15:04:48 +01007841#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007842 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007843 if (*++p == K_SPECIAL // trailbyte K_SPECIAL or CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01007844#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007845 || *p == CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01007846#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007847 )
7848 len += 2;
7849 }
7850 if (len > 0)
7851 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02007852 arg = alloc(STRLEN(eap->arg) + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007853 if (arg != NULL)
7854 {
7855 len = 0;
7856 for (p = eap->arg; *p != NUL; ++p)
7857 {
7858 arg[len++] = *p;
Bram Moolenaar13505972019-01-24 15:04:48 +01007859#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007860 if (*p == CSI)
7861 {
7862 arg[len++] = KS_EXTRA;
7863 arg[len++] = (int)KE_CSI;
7864 }
Bram Moolenaar13505972019-01-24 15:04:48 +01007865#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007866 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007867 {
7868 arg[len++] = *++p;
7869 if (*p == K_SPECIAL)
7870 {
7871 arg[len++] = KS_SPECIAL;
7872 arg[len++] = KE_FILLER;
7873 }
Bram Moolenaar13505972019-01-24 15:04:48 +01007874#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007875 else if (*p == CSI)
7876 {
7877 arg[len++] = KS_EXTRA;
7878 arg[len++] = (int)KE_CSI;
7879 }
Bram Moolenaar13505972019-01-24 15:04:48 +01007880#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007881 }
7882 arg[len] = NUL;
7883 }
7884 }
7885 }
7886 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007887
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007888 ++ex_normal_busy;
7889 if (save_current_state(&save_state))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007890 {
7891 /*
7892 * Repeat the :normal command for each line in the range. When no
7893 * range given, execute it just once, without positioning the cursor
7894 * first.
7895 */
7896 do
7897 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007898 if (eap->addr_count != 0)
7899 {
7900 curwin->w_cursor.lnum = eap->line1++;
7901 curwin->w_cursor.col = 0;
Bram Moolenaard5d37532017-03-27 23:02:07 +02007902 check_cursor_moved(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007903 }
7904
Bram Moolenaar13505972019-01-24 15:04:48 +01007905 exec_normal_cmd(arg != NULL
7906 ? arg
7907 : eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007908 }
7909 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
7910 }
7911
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007912 // Might not return to the main loop when in an event handler.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007913 update_topline_cursor();
7914
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007915 restore_current_state(&save_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007916 --ex_normal_busy;
Bram Moolenaareda73602014-11-05 09:53:23 +01007917 setmouse();
Bram Moolenaareda73602014-11-05 09:53:23 +01007918#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007919 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaareda73602014-11-05 09:53:23 +01007920#endif
7921
Bram Moolenaar071d4272004-06-13 20:20:40 +00007922 vim_free(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007923}
7924
7925/*
Bram Moolenaar64969662005-12-14 21:59:55 +00007926 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007927 */
7928 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007929ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007930{
Bram Moolenaarfd371682005-01-14 21:42:54 +00007931 if (eap->forceit)
7932 {
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007933 // cursor line can be zero on startup
Bram Moolenaar09ca9322017-09-26 17:40:45 +02007934 if (!curwin->w_cursor.lnum)
7935 curwin->w_cursor.lnum = 1;
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007936 set_cursor_for_append_to_line();
Bram Moolenaarfd371682005-01-14 21:42:54 +00007937 }
Bram Moolenaareb58a242020-04-19 18:13:19 +02007938#ifdef FEAT_TERMINAL
7939 // Ignore this when running in an active terminal.
7940 if (term_job_running(curbuf->b_term))
7941 return;
7942#endif
Bram Moolenaarfd371682005-01-14 21:42:54 +00007943
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007944 // Ignore the command when already in Insert mode. Inserting an
7945 // expression register that invokes a function can do this.
Bram Moolenaara40c5002005-01-09 21:16:21 +00007946 if (State & INSERT)
7947 return;
7948
Bram Moolenaar64969662005-12-14 21:59:55 +00007949 if (eap->cmdidx == CMD_startinsert)
7950 restart_edit = 'a';
7951 else if (eap->cmdidx == CMD_startreplace)
7952 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00007953 else
Bram Moolenaar64969662005-12-14 21:59:55 +00007954 restart_edit = 'V';
7955
7956 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007957 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00007958 if (eap->cmdidx == CMD_startinsert)
7959 restart_edit = 'i';
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007960 curwin->w_curswant = 0; // avoid MAXCOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007961 }
7962}
7963
7964/*
7965 * ":stopinsert"
7966 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007967 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007968ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007969{
7970 restart_edit = 0;
7971 stop_insert_mode = TRUE;
Bram Moolenaarfd773e92016-04-02 19:39:16 +02007972 clearmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007973}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007974
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007975/*
7976 * Execute normal mode command "cmd".
7977 * "remap" can be REMAP_NONE or REMAP_YES.
7978 */
7979 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007980exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007981{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007982 // Stuff the argument into the typeahead buffer.
Bram Moolenaar25281632016-01-21 23:32:32 +01007983 ins_typebuf(cmd, remap, 0, TRUE, silent);
Bram Moolenaar586c70c2018-10-02 16:23:58 +02007984 exec_normal(FALSE, FALSE, FALSE);
Bram Moolenaar25281632016-01-21 23:32:32 +01007985}
Bram Moolenaar25281632016-01-21 23:32:32 +01007986
Bram Moolenaar25281632016-01-21 23:32:32 +01007987/*
7988 * Execute normal_cmd() until there is no typeahead left.
Bram Moolenaar586c70c2018-10-02 16:23:58 +02007989 * When "use_vpeekc" is TRUE use vpeekc() to check for available chars.
Bram Moolenaar25281632016-01-21 23:32:32 +01007990 */
7991 void
Bram Moolenaar586c70c2018-10-02 16:23:58 +02007992exec_normal(int was_typed, int use_vpeekc, int may_use_terminal_loop UNUSED)
Bram Moolenaar25281632016-01-21 23:32:32 +01007993{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007994 oparg_T oa;
Bram Moolenaar905dd902019-04-07 14:21:47 +02007995 int c;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007996
Bram Moolenaar905dd902019-04-07 14:21:47 +02007997 // When calling vpeekc() from feedkeys() it will return Ctrl_C when there
7998 // is nothing to get, so also check for Ctrl_C.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007999 clear_oparg(&oa);
8000 finish_op = FALSE;
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008001 while ((!stuff_empty()
8002 || ((was_typed || !typebuf_typed()) && typebuf.tb_len > 0)
Bram Moolenaar905dd902019-04-07 14:21:47 +02008003 || (use_vpeekc && (c = vpeekc()) != NUL && c != Ctrl_C))
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008004 && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008005 {
8006 update_topline_cursor();
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008007#ifdef FEAT_TERMINAL
Bram Moolenaar655a82a2018-05-08 22:01:07 +02008008 if (may_use_terminal_loop && term_use_loop()
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008009 && oa.op_type == OP_NOP && oa.regname == NUL
8010 && !VIsual_active)
8011 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008012 // If terminal_loop() returns OK we got a key that is handled
8013 // in Normal model. With FAIL we first need to position the
8014 // cursor and the screen needs to be redrawn.
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008015 if (terminal_loop(TRUE) == OK)
8016 normal_cmd(&oa, TRUE);
8017 }
8018 else
8019#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008020 // execute a Normal mode cmd
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008021 normal_cmd(&oa, TRUE);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008022 }
8023}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008024
Bram Moolenaar071d4272004-06-13 20:20:40 +00008025#ifdef FEAT_FIND_ID
8026 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008027ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008028{
8029 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
8030 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
8031 (linenr_T)1, (linenr_T)MAXLNUM);
8032}
8033
Bram Moolenaar4033c552017-09-16 20:54:51 +02008034#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008035/*
8036 * ":psearch"
8037 */
8038 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008039ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008040{
8041 g_do_tagpreview = p_pvh;
8042 ex_findpat(eap);
8043 g_do_tagpreview = 0;
8044}
8045#endif
8046
8047 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008048ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008049{
8050 int whole = TRUE;
8051 long n;
8052 char_u *p;
8053 int action;
8054
8055 switch (cmdnames[eap->cmdidx].cmd_name[2])
8056 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008057 case 'e': // ":psearch", ":isearch" and ":dsearch"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008058 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
8059 action = ACTION_GOTO;
8060 else
8061 action = ACTION_SHOW;
8062 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008063 case 'i': // ":ilist" and ":dlist"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008064 action = ACTION_SHOW_ALL;
8065 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008066 case 'u': // ":ijump" and ":djump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008067 action = ACTION_GOTO;
8068 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008069 default: // ":isplit" and ":dsplit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008070 action = ACTION_SPLIT;
8071 break;
8072 }
8073
8074 n = 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008075 if (vim_isdigit(*eap->arg)) // get count
Bram Moolenaar071d4272004-06-13 20:20:40 +00008076 {
8077 n = getdigits(&eap->arg);
8078 eap->arg = skipwhite(eap->arg);
8079 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008080 if (*eap->arg == '/') // Match regexp, not just whole words
Bram Moolenaar071d4272004-06-13 20:20:40 +00008081 {
8082 whole = FALSE;
8083 ++eap->arg;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02008084 p = skip_regexp(eap->arg, '/', p_magic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008085 if (*p)
8086 {
8087 *p++ = NUL;
8088 p = skipwhite(p);
8089
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008090 // Check for trailing illegal characters
Bram Moolenaarfaac4102020-04-20 17:46:14 +02008091 if (!ends_excmd2(eap->arg, p))
Bram Moolenaar8930caa2020-07-23 16:37:03 +02008092 eap->errmsg = ex_errmsg(e_trailing_arg, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008093 else
8094 eap->nextcmd = check_nextcmd(p);
8095 }
8096 }
8097 if (!eap->skip)
8098 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
8099 whole, !eap->forceit,
8100 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
8101 n, action, eap->line1, eap->line2);
8102}
8103#endif
8104
Bram Moolenaar071d4272004-06-13 20:20:40 +00008105
Bram Moolenaar4033c552017-09-16 20:54:51 +02008106#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00008107/*
8108 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
8109 */
8110 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008111ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008112{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008113 g_do_tagpreview = p_pvh; // will be reset to 0 in ex_tag_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00008114 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
8115}
8116
8117/*
8118 * ":pedit"
8119 */
8120 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008121ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008122{
8123 win_T *curwin_save = curwin;
8124
Bram Moolenaar3a2505c2020-03-09 16:40:41 +01008125 if (ERROR_IF_ANY_POPUP_WINDOW)
8126 return;
8127
Bram Moolenaar026587b2019-08-17 15:08:00 +02008128 // Open the preview window or popup and make it the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008129 g_do_tagpreview = p_pvh;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02008130 prepare_tagpreview(TRUE, TRUE, FALSE);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008131
Bram Moolenaar026587b2019-08-17 15:08:00 +02008132 // Edit the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008133 do_exedit(eap, NULL);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008134
Bram Moolenaar071d4272004-06-13 20:20:40 +00008135 if (curwin != curwin_save && win_valid(curwin_save))
8136 {
Bram Moolenaar026587b2019-08-17 15:08:00 +02008137 // Return cursor to where we were
Bram Moolenaar071d4272004-06-13 20:20:40 +00008138 validate_cursor();
8139 redraw_later(VALID);
8140 win_enter(curwin_save, TRUE);
8141 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01008142# ifdef FEAT_PROP_POPUP
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008143 else if (WIN_IS_POPUP(curwin))
8144 {
8145 // can't keep focus in popup window
8146 win_enter(firstwin, TRUE);
8147 }
8148# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008149 g_do_tagpreview = 0;
8150}
Bram Moolenaar4033c552017-09-16 20:54:51 +02008151#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008152
8153/*
8154 * ":stag", ":stselect" and ":stjump".
8155 */
8156 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008157ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008158{
8159 postponed_split = -1;
8160 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008161 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008162 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
8163 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008164 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008165}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008166
8167/*
8168 * ":tag", ":tselect", ":tjump", ":tnext", etc.
8169 */
8170 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008171ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008172{
8173 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
8174}
8175
8176 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008177ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008178{
8179 int cmd;
8180
8181 switch (name[1])
8182 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008183 case 'j': cmd = DT_JUMP; // ":tjump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008184 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008185 case 's': cmd = DT_SELECT; // ":tselect"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008186 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008187 case 'p': cmd = DT_PREV; // ":tprevious"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008188 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008189 case 'N': cmd = DT_PREV; // ":tNext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008190 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008191 case 'n': cmd = DT_NEXT; // ":tnext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008192 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008193 case 'o': cmd = DT_POP; // ":pop"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008194 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008195 case 'f': // ":tfirst"
8196 case 'r': cmd = DT_FIRST; // ":trewind"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008197 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008198 case 'l': cmd = DT_LAST; // ":tlast"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008199 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008200 default: // ":tag"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008201#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +00008202 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008203 {
Bram Moolenaard4db7712016-11-12 19:16:46 +01008204 ex_cstag(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008205 return;
8206 }
8207#endif
8208 cmd = DT_TAG;
8209 break;
8210 }
8211
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00008212 if (name[0] == 'l')
8213 {
8214#ifndef FEAT_QUICKFIX
8215 ex_ni(eap);
8216 return;
8217#else
8218 cmd = DT_LTAG;
8219#endif
8220 }
8221
Bram Moolenaar071d4272004-06-13 20:20:40 +00008222 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
8223 eap->forceit, TRUE);
8224}
8225
8226/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008227 * Check "str" for starting with a special cmdline variable.
8228 * If found return one of the SPEC_ values and set "*usedlen" to the length of
8229 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
8230 */
8231 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008232find_cmdline_var(char_u *src, int *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008233{
8234 int len;
8235 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +00008236 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008237 "%",
8238#define SPEC_PERC 0
8239 "#",
Bram Moolenaar65f08472017-09-10 18:16:20 +02008240#define SPEC_HASH (SPEC_PERC + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008241 "<cword>", // cursor word
Bram Moolenaar65f08472017-09-10 18:16:20 +02008242#define SPEC_CWORD (SPEC_HASH + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008243 "<cWORD>", // cursor WORD
Bram Moolenaar65f08472017-09-10 18:16:20 +02008244#define SPEC_CCWORD (SPEC_CWORD + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008245 "<cexpr>", // expr under cursor
Bram Moolenaar65f08472017-09-10 18:16:20 +02008246#define SPEC_CEXPR (SPEC_CCWORD + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008247 "<cfile>", // cursor path name
Bram Moolenaar65f08472017-09-10 18:16:20 +02008248#define SPEC_CFILE (SPEC_CEXPR + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008249 "<sfile>", // ":so" file name
Bram Moolenaar65f08472017-09-10 18:16:20 +02008250#define SPEC_SFILE (SPEC_CFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008251 "<slnum>", // ":so" file line number
Bram Moolenaar65f08472017-09-10 18:16:20 +02008252#define SPEC_SLNUM (SPEC_SFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008253 "<afile>", // autocommand file name
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008254#define SPEC_AFILE (SPEC_SLNUM + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008255 "<abuf>", // autocommand buffer number
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008256#define SPEC_ABUF (SPEC_AFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008257 "<amatch>", // autocommand match name
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008258#define SPEC_AMATCH (SPEC_ABUF + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008259 "<sflnum>", // script file line number
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008260#define SPEC_SFLNUM (SPEC_AMATCH + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008261#ifdef FEAT_CLIENTSERVER
8262 "<client>"
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008263# define SPEC_CLIENT (SPEC_SFLNUM + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008264#endif
8265 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008266
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008267 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008268 {
8269 len = (int)STRLEN(spec_str[i]);
8270 if (STRNCMP(src, spec_str[i], len) == 0)
8271 {
8272 *usedlen = len;
8273 return i;
8274 }
8275 }
8276 return -1;
8277}
8278
8279/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008280 * Evaluate cmdline variables.
8281 *
8282 * change '%' to curbuf->b_ffname
8283 * '#' to curwin->w_altfile
8284 * '<cword>' to word under the cursor
8285 * '<cWORD>' to WORD under the cursor
Bram Moolenaar8071cb22019-07-12 17:58:01 +02008286 * '<cexpr>' to C-expression under the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00008287 * '<cfile>' to path name under the cursor
8288 * '<sfile>' to sourced file name
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008289 * '<slnum>' to sourced file line number
Bram Moolenaar071d4272004-06-13 20:20:40 +00008290 * '<afile>' to file name for autocommand
8291 * '<abuf>' to buffer number for autocommand
8292 * '<amatch>' to matching name for autocommand
8293 *
8294 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
8295 * "" for error without a message) and NULL is returned.
8296 * Returns an allocated string if a valid match was found.
8297 * Returns NULL if no match was found. "usedlen" then still contains the
8298 * number of characters to skip.
8299 */
8300 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008301eval_vars(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008302 char_u *src, // pointer into commandline
8303 char_u *srcstart, // beginning of valid memory for src
8304 int *usedlen, // characters after src that are used
8305 linenr_T *lnump, // line number for :e command, or NULL
8306 char **errormsg, // pointer to error message
8307 int *escaped) // return value has escaped white space (can
8308 // be NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008309{
8310 int i;
8311 char_u *s;
8312 char_u *result;
8313 char_u *resultbuf = NULL;
8314 int resultlen;
8315 buf_T *buf;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008316 int valid = VALID_HEAD + VALID_PATH; // assume valid result
Bram Moolenaar071d4272004-06-13 20:20:40 +00008317 int spec_idx;
Bram Moolenaarfd249462018-07-28 16:14:30 +02008318 int tilde_file = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008319 int skip_mod = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008320 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008321
8322 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +00008323 if (escaped != NULL)
8324 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008325
8326 /*
8327 * Check if there is something to do.
8328 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008329 spec_idx = find_cmdline_var(src, usedlen);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008330 if (spec_idx < 0) // no match
Bram Moolenaar071d4272004-06-13 20:20:40 +00008331 {
8332 *usedlen = 1;
8333 return NULL;
8334 }
8335
8336 /*
8337 * Skip when preceded with a backslash "\%" and "\#".
8338 * Note: In "\\%" the % is also not recognized!
8339 */
8340 if (src > srcstart && src[-1] == '\\')
8341 {
8342 *usedlen = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008343 STRMOVE(src - 1, src); // remove backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00008344 return NULL;
8345 }
8346
8347 /*
8348 * word or WORD under cursor
8349 */
Bram Moolenaar65f08472017-09-10 18:16:20 +02008350 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD
8351 || spec_idx == SPEC_CEXPR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008352 {
Bram Moolenaar65f08472017-09-10 18:16:20 +02008353 resultlen = find_ident_under_cursor(&result,
8354 spec_idx == SPEC_CWORD ? (FIND_IDENT | FIND_STRING)
8355 : spec_idx == SPEC_CEXPR ? (FIND_IDENT | FIND_STRING | FIND_EVAL)
8356 : FIND_STRING);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008357 if (resultlen == 0)
8358 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008359 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008360 return NULL;
8361 }
8362 }
8363
8364 /*
8365 * '#': Alternate file name
8366 * '%': Current file name
8367 * File name under the cursor
8368 * File name for autocommand
8369 * and following modifiers
8370 */
8371 else
8372 {
8373 switch (spec_idx)
8374 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008375 case SPEC_PERC: // '%': current file
Bram Moolenaar071d4272004-06-13 20:20:40 +00008376 if (curbuf->b_fname == NULL)
8377 {
8378 result = (char_u *)"";
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008379 valid = 0; // Must have ":p:h" to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00008380 }
8381 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008382 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008383 result = curbuf->b_fname;
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008384 tilde_file = STRCMP(result, "~") == 0;
8385 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008386 break;
8387
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008388 case SPEC_HASH: // '#' or "#99": alternate file
8389 if (src[1] == '#') // "##": the argument list
Bram Moolenaar071d4272004-06-13 20:20:40 +00008390 {
8391 result = arg_all();
8392 resultbuf = result;
8393 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +00008394 if (escaped != NULL)
8395 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008396 skip_mod = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008397 break;
8398 }
8399 s = src + 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008400 if (*s == '<') // "#<99" uses v:oldfiles
Bram Moolenaard812df62008-11-09 12:46:09 +00008401 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008402 i = (int)getdigits(&s);
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008403 if (s == src + 2 && src[1] == '-')
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008404 // just a minus sign, don't skip over it
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008405 s--;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008406 *usedlen = (int)(s - src); // length of what we expand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008407
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008408 if (src[1] == '<' && i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008409 {
Bram Moolenaard812df62008-11-09 12:46:09 +00008410 if (*usedlen < 2)
8411 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008412 // Should we give an error message for #<text?
Bram Moolenaard812df62008-11-09 12:46:09 +00008413 *usedlen = 1;
8414 return NULL;
8415 }
8416#ifdef FEAT_EVAL
8417 result = list_find_str(get_vim_var_list(VV_OLDFILES),
8418 (long)i);
8419 if (result == NULL)
8420 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008421 *errormsg = "";
Bram Moolenaard812df62008-11-09 12:46:09 +00008422 return NULL;
8423 }
8424#else
Bram Moolenaar8e481e82019-01-15 20:07:48 +01008425 *errormsg = _("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008426 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00008427#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008428 }
8429 else
Bram Moolenaard812df62008-11-09 12:46:09 +00008430 {
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008431 if (i == 0 && src[1] == '<' && *usedlen > 1)
8432 *usedlen = 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00008433 buf = buflist_findnr(i);
8434 if (buf == NULL)
8435 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008436 *errormsg = _("E194: No alternate file name to substitute for '#'");
Bram Moolenaard812df62008-11-09 12:46:09 +00008437 return NULL;
8438 }
8439 if (lnump != NULL)
8440 *lnump = ECMD_LAST;
8441 if (buf->b_fname == NULL)
8442 {
8443 result = (char_u *)"";
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008444 valid = 0; // Must have ":p:h" to be valid
Bram Moolenaard812df62008-11-09 12:46:09 +00008445 }
8446 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008447 {
Bram Moolenaard812df62008-11-09 12:46:09 +00008448 result = buf->b_fname;
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008449 tilde_file = STRCMP(result, "~") == 0;
8450 }
Bram Moolenaard812df62008-11-09 12:46:09 +00008451 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008452 break;
8453
8454#ifdef FEAT_SEARCHPATH
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008455 case SPEC_CFILE: // file name under cursor
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008456 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008457 if (result == NULL)
8458 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008459 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008460 return NULL;
8461 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008462 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00008463 break;
8464#endif
8465
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008466 case SPEC_AFILE: // file name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008467 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +00008468 if (result != NULL && !autocmd_fname_full)
8469 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008470 // Still need to turn the fname into a full path. It is
8471 // postponed to avoid a delay when <afile> is not used.
Bram Moolenaarf6dad432008-09-18 19:29:58 +00008472 autocmd_fname_full = TRUE;
8473 result = FullName_save(autocmd_fname, FALSE);
8474 vim_free(autocmd_fname);
8475 autocmd_fname = result;
8476 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008477 if (result == NULL)
8478 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008479 *errormsg = _("E495: no autocommand file name to substitute for \"<afile>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008480 return NULL;
8481 }
Bram Moolenaara0174af2008-01-02 20:08:25 +00008482 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008483 break;
8484
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008485 case SPEC_ABUF: // buffer number for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008486 if (autocmd_bufnr <= 0)
8487 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008488 *errormsg = _("E496: no autocommand buffer number to substitute for \"<abuf>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008489 return NULL;
8490 }
8491 sprintf((char *)strbuf, "%d", autocmd_bufnr);
8492 result = strbuf;
8493 break;
8494
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008495 case SPEC_AMATCH: // match name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008496 result = autocmd_match;
8497 if (result == NULL)
8498 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008499 *errormsg = _("E497: no autocommand match name to substitute for \"<amatch>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008500 return NULL;
8501 }
8502 break;
8503
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008504 case SPEC_SFILE: // file name for ":so" command
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008505 result = estack_sfile();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008506 if (result == NULL)
8507 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008508 *errormsg = _("E498: no :source file name to substitute for \"<sfile>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008509 return NULL;
8510 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008511 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00008512 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008513
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008514 case SPEC_SLNUM: // line in file for ":so" command
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008515 if (SOURCING_NAME == NULL || SOURCING_LNUM == 0)
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008516 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008517 *errormsg = _("E842: no line number to use for \"<slnum>\"");
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008518 return NULL;
8519 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008520 sprintf((char *)strbuf, "%ld", SOURCING_LNUM);
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008521 result = strbuf;
8522 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008523
8524#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008525 case SPEC_SFLNUM: // line in script file
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008526 if (current_sctx.sc_lnum + SOURCING_LNUM == 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008527 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008528 *errormsg = _("E961: no line number to use for \"<sflnum>\"");
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008529 return NULL;
8530 }
8531 sprintf((char *)strbuf, "%ld",
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008532 (long)(current_sctx.sc_lnum + SOURCING_LNUM));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008533 result = strbuf;
8534 break;
8535#endif
8536
8537#ifdef FEAT_CLIENTSERVER
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008538 case SPEC_CLIENT: // Source of last submitted input
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008539 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
8540 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008541 result = strbuf;
8542 break;
8543#endif
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008544
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02008545 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008546 result = (char_u *)""; // avoid gcc warning
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02008547 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008548 }
8549
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008550 resultlen = (int)STRLEN(result); // length of new string
8551 if (src[*usedlen] == '<') // remove the file name extension
Bram Moolenaar071d4272004-06-13 20:20:40 +00008552 {
8553 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008554 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008555 resultlen = (int)(s - result);
8556 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008557 else if (!skip_mod)
8558 {
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008559 valid |= modify_fname(src, tilde_file, usedlen, &result, &resultbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008560 &resultlen);
8561 if (result == NULL)
8562 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008563 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008564 return NULL;
8565 }
8566 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008567 }
8568
8569 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
8570 {
8571 if (valid != VALID_HEAD + VALID_PATH)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008572 // xgettext:no-c-format
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008573 *errormsg = _("E499: Empty file name for '%' or '#', only works with \":p:h\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008574 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008575 *errormsg = _("E500: Evaluates to an empty string");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008576 result = NULL;
8577 }
8578 else
8579 result = vim_strnsave(result, resultlen);
8580 vim_free(resultbuf);
8581 return result;
8582}
8583
8584/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008585 * Expand the <sfile> string in "arg".
8586 *
8587 * Returns an allocated string, or NULL for any error.
8588 */
8589 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008590expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008591{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008592 char *errormsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008593 int len;
8594 char_u *result;
8595 char_u *newres;
8596 char_u *repl;
8597 int srclen;
8598 char_u *p;
8599
8600 result = vim_strsave(arg);
8601 if (result == NULL)
8602 return NULL;
8603
8604 for (p = result; *p; )
8605 {
8606 if (STRNCMP(p, "<sfile>", 7) != 0)
8607 ++p;
8608 else
8609 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008610 // replace "<sfile>" with the sourced file name, and do ":" stuff
Bram Moolenaar63b92542007-03-27 14:57:09 +00008611 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008612 if (errormsg != NULL)
8613 {
8614 if (*errormsg)
8615 emsg(errormsg);
8616 vim_free(result);
8617 return NULL;
8618 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008619 if (repl == NULL) // no match (cannot happen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008620 {
8621 p += srclen;
8622 continue;
8623 }
8624 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
8625 newres = alloc(len);
8626 if (newres == NULL)
8627 {
8628 vim_free(repl);
8629 vim_free(result);
8630 return NULL;
8631 }
8632 mch_memmove(newres, result, (size_t)(p - result));
8633 STRCPY(newres + (p - result), repl);
8634 len = (int)STRLEN(newres);
8635 STRCAT(newres, p + srclen);
8636 vim_free(repl);
8637 vim_free(result);
8638 result = newres;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008639 p = newres + len; // continue after the match
Bram Moolenaar071d4272004-06-13 20:20:40 +00008640 }
8641 }
8642
8643 return result;
8644}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008645
Bram Moolenaar071d4272004-06-13 20:20:40 +00008646#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00008647/*
Bram Moolenaard9462e32011-04-11 21:35:11 +02008648 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +00008649 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +00008650 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008651 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008652dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008653{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008654 if (fname == NULL)
8655 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +02008656 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008657}
8658#endif
8659
8660/*
8661 * ":behave {mswin,xterm}"
8662 */
8663 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008664ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008665{
8666 if (STRCMP(eap->arg, "mswin") == 0)
8667 {
8668 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
8669 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
8670 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
8671 set_option_value((char_u *)"keymodel", 0L,
8672 (char_u *)"startsel,stopsel", 0);
8673 }
8674 else if (STRCMP(eap->arg, "xterm") == 0)
8675 {
8676 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
8677 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
8678 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
8679 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
8680 }
8681 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008682 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008683}
8684
Bram Moolenaar071d4272004-06-13 20:20:40 +00008685static int filetype_detect = FALSE;
8686static int filetype_plugin = FALSE;
8687static int filetype_indent = FALSE;
8688
8689/*
8690 * ":filetype [plugin] [indent] {on,off,detect}"
8691 * on: Load the filetype.vim file to install autocommands for file types.
8692 * off: Load the ftoff.vim file to remove all autocommands for file types.
8693 * plugin on: load filetype.vim and ftplugin.vim
8694 * plugin off: load ftplugof.vim
8695 * indent on: load filetype.vim and indent.vim
8696 * indent off: load indoff.vim
8697 */
8698 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008699ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008700{
8701 char_u *arg = eap->arg;
8702 int plugin = FALSE;
8703 int indent = FALSE;
8704
8705 if (*eap->arg == NUL)
8706 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008707 // Print current status.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008708 smsg("filetype detection:%s plugin:%s indent:%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00008709 filetype_detect ? "ON" : "OFF",
8710 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
8711 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
8712 return;
8713 }
8714
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008715 // Accept "plugin" and "indent" in any order.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008716 for (;;)
8717 {
8718 if (STRNCMP(arg, "plugin", 6) == 0)
8719 {
8720 plugin = TRUE;
8721 arg = skipwhite(arg + 6);
8722 continue;
8723 }
8724 if (STRNCMP(arg, "indent", 6) == 0)
8725 {
8726 indent = TRUE;
8727 arg = skipwhite(arg + 6);
8728 continue;
8729 }
8730 break;
8731 }
8732 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
8733 {
8734 if (*arg == 'o' || !filetype_detect)
8735 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008736 source_runtime((char_u *)FILETYPE_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008737 filetype_detect = TRUE;
8738 if (plugin)
8739 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008740 source_runtime((char_u *)FTPLUGIN_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008741 filetype_plugin = TRUE;
8742 }
8743 if (indent)
8744 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008745 source_runtime((char_u *)INDENT_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008746 filetype_indent = TRUE;
8747 }
8748 }
8749 if (*arg == 'd')
8750 {
Bram Moolenaar1610d052016-06-09 22:53:01 +02008751 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +00008752 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008753 }
8754 }
8755 else if (STRCMP(arg, "off") == 0)
8756 {
8757 if (plugin || indent)
8758 {
8759 if (plugin)
8760 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008761 source_runtime((char_u *)FTPLUGOF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008762 filetype_plugin = FALSE;
8763 }
8764 if (indent)
8765 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008766 source_runtime((char_u *)INDOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008767 filetype_indent = FALSE;
8768 }
8769 }
8770 else
8771 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008772 source_runtime((char_u *)FTOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008773 filetype_detect = FALSE;
8774 }
8775 }
8776 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008777 semsg(_(e_invarg2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008778}
8779
8780/*
Bram Moolenaar3e545692017-06-04 19:00:32 +02008781 * ":setfiletype [FALLBACK] {name}"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008782 */
8783 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008784ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008785{
8786 if (!did_filetype)
Bram Moolenaar3e545692017-06-04 19:00:32 +02008787 {
8788 char_u *arg = eap->arg;
8789
8790 if (STRNCMP(arg, "FALLBACK ", 9) == 0)
8791 arg += 9;
8792
8793 set_option_value((char_u *)"filetype", 0L, arg, OPT_LOCAL);
8794 if (arg != eap->arg)
8795 did_filetype = FALSE;
8796 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008797}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008798
Bram Moolenaar071d4272004-06-13 20:20:40 +00008799 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008800ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008801{
8802#ifdef FEAT_DIGRAPHS
8803 if (*eap->arg != NUL)
8804 putdigraph(eap->arg);
8805 else
Bram Moolenaareae8ae12018-12-14 18:53:02 +01008806 listdigraphs(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008807#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008808 emsg(_("E196: No digraphs in this version"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008809#endif
8810}
8811
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02008812#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
8813 void
8814set_no_hlsearch(int flag)
8815{
8816 no_hlsearch = flag;
8817# ifdef FEAT_EVAL
8818 set_vim_var_nr(VV_HLSEARCH, !no_hlsearch && p_hls);
8819# endif
8820}
8821
Bram Moolenaar071d4272004-06-13 20:20:40 +00008822/*
8823 * ":nohlsearch"
8824 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008825 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008826ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008827{
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02008828 set_no_hlsearch(TRUE);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00008829 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008830}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008831#endif
8832
8833#ifdef FEAT_CRYPT
8834/*
8835 * ":X": Get crypt key
8836 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008837 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008838ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008839{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +01008840 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02008841 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008842}
8843#endif
8844
8845#ifdef FEAT_FOLDING
8846 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008847ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008848{
8849 if (foldManualAllowed(TRUE))
8850 foldCreate(eap->line1, eap->line2);
8851}
8852
8853 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008854ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008855{
8856 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
8857 eap->forceit, FALSE);
8858}
8859
8860 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008861ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008862{
8863 linenr_T lnum;
8864
Bram Moolenaar4facea32019-10-12 20:17:40 +02008865# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02008866 start_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +02008867# endif
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02008868
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008869 // First set the marks for all lines closed/open.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008870 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
8871 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
8872 ml_setmarked(lnum);
8873
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008874 // Execute the command on the marked lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008875 global_exe(eap->arg);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008876 ml_clearmarked(); // clear rest of the marks
Bram Moolenaar4facea32019-10-12 20:17:40 +02008877# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02008878 end_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +02008879# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008880}
8881#endif
Bram Moolenaarf5291f32017-09-14 22:55:37 +02008882
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008883#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar39665952018-08-15 20:59:48 +02008884/*
8885 * Returns TRUE if the supplied Ex cmdidx is for a location list command
8886 * instead of a quickfix command.
8887 */
8888 int
8889is_loclist_cmd(int cmdidx)
8890{
Bram Moolenaar74c8be22018-08-23 22:51:40 +02008891 if (cmdidx < 0 || cmdidx >= CMD_SIZE)
Bram Moolenaar39665952018-08-15 20:59:48 +02008892 return FALSE;
8893 return cmdnames[cmdidx].cmd_name[0] == 'l';
8894}
8895#endif
8896
Bram Moolenaar4facea32019-10-12 20:17:40 +02008897#if defined(FEAT_TIMERS) || defined(PROTO)
Bram Moolenaarf5291f32017-09-14 22:55:37 +02008898 int
8899get_pressedreturn(void)
8900{
8901 return ex_pressedreturn;
8902}
8903
8904 void
8905set_pressedreturn(int val)
8906{
8907 ex_pressedreturn = val;
8908}
8909#endif