blob: a326d20d3cfe85b1b92837d5f46b52f3f6096259 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * ex_docmd.c: functions for executing an Ex command line.
12 */
13
14#include "vim.h"
15
Bram Moolenaar071d4272004-06-13 20:20:40 +000016static int quitmore = 0;
17static int ex_pressedreturn = FALSE;
18#ifndef FEAT_PRINTER
19# define ex_hardcopy ex_ni
20#endif
21
Bram Moolenaar071d4272004-06-13 20:20:40 +000022#ifdef FEAT_EVAL
Bram Moolenaarddef1292019-12-16 17:10:33 +010023static char_u *do_one_cmd(char_u **, int, cstack_T *, char_u *(*fgetline)(int, void *, int, int), void *cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024#else
Bram Moolenaare96a2492019-06-25 04:12:16 +020025static char_u *do_one_cmd(char_u **, int, char_u *(*fgetline)(int, void *, int, int), void *cookie);
Bram Moolenaar217e1b82019-12-01 21:41:28 +010026static int if_level = 0; // depth in :if
Bram Moolenaar071d4272004-06-13 20:20:40 +000027#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010028static void append_command(char_u *cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000029
Bram Moolenaar071d4272004-06-13 20:20:40 +000030#ifndef FEAT_MENU
31# define ex_emenu ex_ni
32# define ex_menu ex_ni
33# define ex_menutranslate ex_ni
34#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010035static void ex_autocmd(exarg_T *eap);
36static void ex_doautocmd(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010037static void ex_bunload(exarg_T *eap);
38static void ex_buffer(exarg_T *eap);
39static void ex_bmodified(exarg_T *eap);
40static void ex_bnext(exarg_T *eap);
41static void ex_bprevious(exarg_T *eap);
42static void ex_brewind(exarg_T *eap);
43static void ex_blast(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010044static char_u *getargcmd(char_u **);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010045static int getargopt(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000046#ifndef FEAT_QUICKFIX
47# define ex_make ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +000048# define ex_cbuffer ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000049# define ex_cc ex_ni
50# define ex_cnext ex_ni
Bram Moolenaar3ff33112019-05-03 21:56:35 +020051# define ex_cbelow ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000052# define ex_cfile ex_ni
53# define qf_list ex_ni
54# define qf_age ex_ni
Bram Moolenaarf6acffb2016-07-16 16:54:24 +020055# define qf_history ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000056# define ex_helpgrep ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +000057# define ex_vimgrep ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000058#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +020059#if !defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +000060# define ex_cclose ex_ni
61# define ex_copen ex_ni
62# define ex_cwindow ex_ni
Bram Moolenaardcb17002016-07-07 18:58:59 +020063# define ex_cbottom ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000064#endif
Bram Moolenaar1e015462005-09-25 22:16:38 +000065#if !defined(FEAT_QUICKFIX) || !defined(FEAT_EVAL)
66# define ex_cexpr ex_ni
67#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000068
Bram Moolenaarb7316892019-05-01 18:08:42 +020069static linenr_T get_address(exarg_T *, char_u **, cmd_addr_T addr_type, int skip, int silent, int to_other_file, int address_count);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010070static void get_flags(exarg_T *eap);
Bram Moolenaar85363ab2010-07-18 13:58:26 +020071#if !defined(FEAT_PERL) \
72 || !defined(FEAT_PYTHON) || !defined(FEAT_PYTHON3) \
73 || !defined(FEAT_TCL) \
74 || !defined(FEAT_RUBY) \
75 || !defined(FEAT_LUA) \
76 || !defined(FEAT_MZSCHEME)
Bram Moolenaar7bb75552007-07-16 18:39:49 +000077# define HAVE_EX_SCRIPT_NI
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010078static void ex_script_ni(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000079#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010080static char *invalid_range(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010081static void correct_range(exarg_T *eap);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000082#ifdef FEAT_QUICKFIX
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010083static char_u *replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000084#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010085static char_u *repl_cmdline(exarg_T *eap, char_u *src, int srclen, char_u *repl, char_u **cmdlinep);
86static void ex_highlight(exarg_T *eap);
87static void ex_colorscheme(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010088static void ex_cquit(exarg_T *eap);
89static void ex_quit_all(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010090static void ex_close(exarg_T *eap);
91static void ex_win_close(int forceit, win_T *win, tabpage_T *tp);
92static void ex_only(exarg_T *eap);
93static void ex_resize(exarg_T *eap);
94static void ex_stag(exarg_T *eap);
95static void ex_tabclose(exarg_T *eap);
96static void ex_tabonly(exarg_T *eap);
97static void ex_tabnext(exarg_T *eap);
98static void ex_tabmove(exarg_T *eap);
99static void ex_tabs(exarg_T *eap);
Bram Moolenaar4033c552017-09-16 20:54:51 +0200100#if defined(FEAT_QUICKFIX)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100101static void ex_pclose(exarg_T *eap);
102static void ex_ptag(exarg_T *eap);
103static void ex_pedit(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000104#else
105# define ex_pclose ex_ni
106# define ex_ptag ex_ni
107# define ex_pedit ex_ni
108#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100109static void ex_hide(exarg_T *eap);
110static void ex_stop(exarg_T *eap);
111static void ex_exit(exarg_T *eap);
112static void ex_print(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000113#ifdef FEAT_BYTEOFF
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100114static void ex_goto(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000115#else
116# define ex_goto ex_ni
117#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100118static void ex_shell(exarg_T *eap);
119static void ex_preserve(exarg_T *eap);
120static void ex_recover(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100121static void ex_mode(exarg_T *eap);
122static void ex_wrongmodifier(exarg_T *eap);
123static void ex_find(exarg_T *eap);
124static void ex_open(exarg_T *eap);
125static void ex_edit(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000126#ifndef FEAT_GUI
127# define ex_gui ex_nogui
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100128static void ex_nogui(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000129#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +0100130#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100131static void ex_tearoff(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000132#else
133# define ex_tearoff ex_ni
134#endif
Bram Moolenaar29a2c082018-03-05 21:06:23 +0100135#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
136 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100137static void ex_popup(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000138#else
139# define ex_popup ex_ni
140#endif
141#ifndef FEAT_GUI_MSWIN
142# define ex_simalt ex_ni
143#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000144#if !defined(FEAT_GUI_MSWIN) && !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000145# define gui_mch_find_dialog ex_ni
146# define gui_mch_replace_dialog ex_ni
147#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000148#if !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000149# define ex_helpfind ex_ni
150#endif
151#ifndef FEAT_CSCOPE
Bram Moolenaard4db7712016-11-12 19:16:46 +0100152# define ex_cscope ex_ni
153# define ex_scscope ex_ni
154# define ex_cstag ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000155#endif
156#ifndef FEAT_SYN_HL
157# define ex_syntax ex_ni
Bram Moolenaar860cae12010-06-05 23:22:07 +0200158# define ex_ownsyntax ex_ni
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000159#endif
Bram Moolenaarf7512552013-06-06 14:55:19 +0200160#if !defined(FEAT_SYN_HL) || !defined(FEAT_PROFILE)
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +0200161# define ex_syntime ex_ni
162#endif
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000163#ifndef FEAT_SPELL
Bram Moolenaarb765d632005-06-07 21:00:02 +0000164# define ex_spell ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000165# define ex_mkspell ex_ni
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000166# define ex_spelldump ex_ni
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000167# define ex_spellinfo ex_ni
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000168# define ex_spellrepall ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000169#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200170#ifndef FEAT_PERSISTENT_UNDO
171# define ex_rundo ex_ni
172# define ex_wundo ex_ni
173#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200174#ifndef FEAT_LUA
175# define ex_lua ex_script_ni
176# define ex_luado ex_ni
177# define ex_luafile ex_ni
178#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000179#ifndef FEAT_MZSCHEME
180# define ex_mzscheme ex_script_ni
181# define ex_mzfile ex_ni
182#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000183#ifndef FEAT_PERL
184# define ex_perl ex_script_ni
185# define ex_perldo ex_ni
186#endif
187#ifndef FEAT_PYTHON
188# define ex_python ex_script_ni
Bram Moolenaard620aa92013-05-17 16:40:06 +0200189# define ex_pydo ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000190# define ex_pyfile ex_ni
191#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200192#ifndef FEAT_PYTHON3
Bram Moolenaar368373e2010-07-19 20:46:22 +0200193# define ex_py3 ex_script_ni
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200194# define ex_py3do ex_ni
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200195# define ex_py3file ex_ni
196#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100197#if !defined(FEAT_PYTHON) && !defined(FEAT_PYTHON3)
198# define ex_pyx ex_script_ni
199# define ex_pyxdo ex_ni
200# define ex_pyxfile ex_ni
201#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000202#ifndef FEAT_TCL
203# define ex_tcl ex_script_ni
204# define ex_tcldo ex_ni
205# define ex_tclfile ex_ni
206#endif
207#ifndef FEAT_RUBY
208# define ex_ruby ex_script_ni
209# define ex_rubydo ex_ni
210# define ex_rubyfile ex_ni
211#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000212#ifndef FEAT_KEYMAP
213# define ex_loadkeymap ex_ni
214#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100215static void ex_swapname(exarg_T *eap);
216static void ex_syncbind(exarg_T *eap);
217static void ex_read(exarg_T *eap);
218static void ex_pwd(exarg_T *eap);
219static void ex_equal(exarg_T *eap);
220static void ex_sleep(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100221static void ex_winsize(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100222static void ex_wincmd(exarg_T *eap);
Bram Moolenaar843ee412004-06-30 16:16:41 +0000223#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100224static void ex_winpos(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000225#else
226# define ex_winpos ex_ni
227#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100228static void ex_operators(exarg_T *eap);
229static void ex_put(exarg_T *eap);
230static void ex_copymove(exarg_T *eap);
231static void ex_submagic(exarg_T *eap);
232static void ex_join(exarg_T *eap);
233static void ex_at(exarg_T *eap);
234static void ex_bang(exarg_T *eap);
235static void ex_undo(exarg_T *eap);
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200236#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100237static void ex_wundo(exarg_T *eap);
238static void ex_rundo(exarg_T *eap);
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200239#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100240static void ex_redo(exarg_T *eap);
241static void ex_later(exarg_T *eap);
242static void ex_redir(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100243static void ex_redrawstatus(exarg_T *eap);
Bram Moolenaare12bab32019-01-08 22:02:56 +0100244static void ex_redrawtabline(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100245static void close_redir(void);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100246static void ex_mark(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100247static void ex_startinsert(exarg_T *eap);
248static void ex_stopinsert(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000249#ifdef FEAT_FIND_ID
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100250static void ex_checkpath(exarg_T *eap);
251static void ex_findpat(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000252#else
253# define ex_findpat ex_ni
254# define ex_checkpath ex_ni
255#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +0200256#if defined(FEAT_FIND_ID) && defined(FEAT_QUICKFIX)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100257static void ex_psearch(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000258#else
259# define ex_psearch ex_ni
260#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100261static void ex_tag(exarg_T *eap);
262static void ex_tag_cmd(exarg_T *eap, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000263#ifndef FEAT_EVAL
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200264# define ex_break ex_ni
265# define ex_breakadd ex_ni
266# define ex_breakdel ex_ni
267# define ex_breaklist ex_ni
268# define ex_call ex_ni
269# define ex_catch ex_ni
270# define ex_compiler ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200271# define ex_continue ex_ni
272# define ex_debug ex_ni
273# define ex_debuggreedy ex_ni
Bram Moolenaar822ba242020-05-24 23:00:18 +0200274# define ex_def ex_ni
275# define ex_defcompile ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200276# define ex_delfunction ex_ni
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100277# define ex_disassemble ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000278# define ex_echo ex_ni
279# define ex_echohl ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000280# define ex_else ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200281# define ex_endfunction ex_ni
282# define ex_endif ex_ni
283# define ex_endtry ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000284# define ex_endwhile ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200285# define ex_eval ex_ni
286# define ex_execute ex_ni
287# define ex_finally ex_ni
288# define ex_finish ex_ni
289# define ex_function ex_ni
290# define ex_if ex_ni
291# define ex_let ex_ni
292# define ex_lockvar ex_ni
293# define ex_oldfiles ex_ni
294# define ex_options ex_ni
295# define ex_packadd ex_ni
296# define ex_packloadall ex_ni
297# define ex_return ex_ni
298# define ex_scriptnames ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000299# define ex_throw ex_ni
300# define ex_try ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000301# define ex_unlet ex_ni
Bram Moolenaar65c1b012005-01-31 19:02:28 +0000302# define ex_unlockvar ex_ni
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100303# define ex_vim9script ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200304# define ex_while ex_ni
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100305# define ex_import ex_ni
306# define ex_export ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000307#endif
Bram Moolenaar84538072019-07-28 14:15:42 +0200308#ifndef FEAT_SESSION
Bram Moolenaar071d4272004-06-13 20:20:40 +0000309# define ex_loadview ex_ni
310#endif
Bram Moolenaardefa0672019-07-21 19:25:37 +0200311#ifndef FEAT_VIMINFO
Bram Moolenaar071d4272004-06-13 20:20:40 +0000312# define ex_viminfo ex_ni
313#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100314static void ex_behave(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100315static void ex_filetype(exarg_T *eap);
316static void ex_setfiletype(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000317#ifndef FEAT_DIFF
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000318# define ex_diffoff ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000319# define ex_diffpatch ex_ni
320# define ex_diffgetput ex_ni
321# define ex_diffsplit ex_ni
322# define ex_diffthis ex_ni
323# define ex_diffupdate ex_ni
324#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100325static void ex_digraphs(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000326#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100327static void ex_nohlsearch(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000328#else
329# define ex_nohlsearch ex_ni
330# define ex_match ex_ni
331#endif
332#ifdef FEAT_CRYPT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100333static void ex_X(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000334#else
335# define ex_X ex_ni
336#endif
337#ifdef FEAT_FOLDING
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100338static void ex_fold(exarg_T *eap);
339static void ex_foldopen(exarg_T *eap);
340static void ex_folddo(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000341#else
342# define ex_fold ex_ni
343# define ex_foldopen ex_ni
344# define ex_folddo ex_ni
345#endif
Bram Moolenaar13505972019-01-24 15:04:48 +0100346#if !(defined(HAVE_LOCALE_H) || defined(X_LOCALE))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000347# define ex_language ex_ni
348#endif
349#ifndef FEAT_SIGNS
350# define ex_sign ex_ni
351#endif
Bram Moolenaar009b2592004-10-24 19:18:58 +0000352#ifndef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200353# define ex_nbclose ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000354# define ex_nbkey ex_ni
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200355# define ex_nbstart ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000356#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000357
Bram Moolenaar071d4272004-06-13 20:20:40 +0000358#ifndef FEAT_JUMPLIST
359# define ex_jumps ex_ni
Bram Moolenaar2d358992016-06-12 21:20:54 +0200360# define ex_clearjumps ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000361# define ex_changes ex_ni
362#endif
363
Bram Moolenaar05159a02005-02-26 23:04:13 +0000364#ifndef FEAT_PROFILE
365# define ex_profile ex_ni
366#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200367#ifndef FEAT_TERMINAL
368# define ex_terminal ex_ni
369#endif
Bram Moolenaard4aa83a2019-05-09 18:59:31 +0200370#if !defined(FEAT_X11) || !defined(FEAT_XCLIPBOARD)
371# define ex_xrestore ex_ni
372#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100373#if !defined(FEAT_PROP_POPUP)
Bram Moolenaar682725c2019-05-25 20:10:37 +0200374# define ex_popupclear ex_ni
375#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +0000376
Bram Moolenaar071d4272004-06-13 20:20:40 +0000377/*
378 * Declare cmdnames[].
379 */
380#define DO_DECLARE_EXCMD
381#include "ex_cmds.h"
Bram Moolenaar6de5e122017-04-20 21:55:44 +0200382#include "ex_cmdidxs.h"
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +0100383
Bram Moolenaar071d4272004-06-13 20:20:40 +0000384static char_u dollar_command[2] = {'$', 0};
385
386
387#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100388// Struct for storing a line inside a while/for loop
Bram Moolenaar071d4272004-06-13 20:20:40 +0000389typedef struct
390{
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100391 char_u *line; // command line
392 linenr_T lnum; // sourcing_lnum of the line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000393} wcmd_T;
394
395/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000396 * Structure used to store info for line position in a while or for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000397 * This is required, because do_one_cmd() may invoke ex_function(), which
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000398 * reads more lines that may come from the while/for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000399 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000400struct loop_cookie
Bram Moolenaar071d4272004-06-13 20:20:40 +0000401{
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100402 garray_T *lines_gap; // growarray with line info
403 int current_line; // last read line from growarray
404 int repeating; // TRUE when looping a second time
405 // When "repeating" is FALSE use "getline" and "cookie" to get lines
Bram Moolenaare96a2492019-06-25 04:12:16 +0200406 char_u *(*getline)(int, void *, int, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000407 void *cookie;
408};
409
Bram Moolenaare96a2492019-06-25 04:12:16 +0200410static char_u *get_loop_line(int c, void *cookie, int indent, int do_concat);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100411static int store_loop_line(garray_T *gap, char_u *line);
412static void free_cmdlines(garray_T *gap);
Bram Moolenaared203462004-06-16 11:19:22 +0000413
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100414// Struct to save a few things while debugging. Used in do_cmdline() only.
Bram Moolenaared203462004-06-16 11:19:22 +0000415struct dbg_stuff
416{
417 int trylevel;
418 int force_abort;
419 except_T *caught_stack;
420 char_u *vv_exception;
421 char_u *vv_throwpoint;
422 int did_emsg;
423 int got_int;
424 int did_throw;
425 int need_rethrow;
426 int check_cstack;
427 except_T *current_exception;
428};
429
Bram Moolenaared203462004-06-16 11:19:22 +0000430 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100431save_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000432{
433 dsp->trylevel = trylevel; trylevel = 0;
434 dsp->force_abort = force_abort; force_abort = FALSE;
435 dsp->caught_stack = caught_stack; caught_stack = NULL;
436 dsp->vv_exception = v_exception(NULL);
437 dsp->vv_throwpoint = v_throwpoint(NULL);
438
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100439 // Necessary for debugging an inactive ":catch", ":finally", ":endtry"
Bram Moolenaared203462004-06-16 11:19:22 +0000440 dsp->did_emsg = did_emsg; did_emsg = FALSE;
441 dsp->got_int = got_int; got_int = FALSE;
442 dsp->did_throw = did_throw; did_throw = FALSE;
443 dsp->need_rethrow = need_rethrow; need_rethrow = FALSE;
444 dsp->check_cstack = check_cstack; check_cstack = FALSE;
445 dsp->current_exception = current_exception; current_exception = NULL;
446}
447
448 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100449restore_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000450{
451 suppress_errthrow = FALSE;
452 trylevel = dsp->trylevel;
453 force_abort = dsp->force_abort;
454 caught_stack = dsp->caught_stack;
455 (void)v_exception(dsp->vv_exception);
456 (void)v_throwpoint(dsp->vv_throwpoint);
457 did_emsg = dsp->did_emsg;
458 got_int = dsp->got_int;
459 did_throw = dsp->did_throw;
460 need_rethrow = dsp->need_rethrow;
461 check_cstack = dsp->check_cstack;
462 current_exception = dsp->current_exception;
463}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000464#endif
465
Bram Moolenaar071d4272004-06-13 20:20:40 +0000466/*
467 * do_exmode(): Repeatedly get commands for the "Ex" mode, until the ":vi"
468 * command is given.
469 */
470 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100471do_exmode(
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100472 int improved) // TRUE for "improved Ex" mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000473{
474 int save_msg_scroll;
475 int prev_msg_row;
476 linenr_T prev_line;
Bram Moolenaar79518e22017-02-17 16:31:35 +0100477 varnumber_T changedtick;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000478
479 if (improved)
480 exmode_active = EXMODE_VIM;
481 else
482 exmode_active = EXMODE_NORMAL;
483 State = NORMAL;
484
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100485 // When using ":global /pat/ visual" and then "Q" we return to continue
486 // the :global command.
Bram Moolenaardf177f62005-02-22 08:39:57 +0000487 if (global_busy)
488 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000489
490 save_msg_scroll = msg_scroll;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100491 ++RedrawingDisabled; // don't redisplay the window
492 ++no_wait_return; // don't wait for return
Bram Moolenaar071d4272004-06-13 20:20:40 +0000493#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100494 // Ignore scrollbar and mouse events in Ex mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000495 ++hold_gui_events;
496#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000497
Bram Moolenaar32526b32019-01-19 17:43:09 +0100498 msg(_("Entering Ex mode. Type \"visual\" to go to Normal mode."));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000499 while (exmode_active)
500 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100501 // Check for a ":normal" command and no more characters left.
Bram Moolenaar7c626922005-02-07 22:01:03 +0000502 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
503 {
504 exmode_active = FALSE;
505 break;
506 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000507 msg_scroll = TRUE;
508 need_wait_return = FALSE;
509 ex_pressedreturn = FALSE;
510 ex_no_reprint = FALSE;
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100511 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000512 prev_msg_row = msg_row;
513 prev_line = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000514 if (improved)
515 {
516 cmdline_row = msg_row;
517 do_cmdline(NULL, getexline, NULL, 0);
518 }
519 else
520 do_cmdline(NULL, getexmodeline, NULL, DOCMD_NOWAIT);
521 lines_left = Rows - 1;
522
Bram Moolenaardf177f62005-02-22 08:39:57 +0000523 if ((prev_line != curwin->w_cursor.lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100524 || changedtick != CHANGEDTICK(curbuf)) && !ex_no_reprint)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000525 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000526 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100527 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000528 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000529 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000530 if (ex_pressedreturn)
531 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100532 // go up one line, to overwrite the ":<CR>" line, so the
533 // output doesn't contain empty lines.
Bram Moolenaardf177f62005-02-22 08:39:57 +0000534 msg_row = prev_msg_row;
535 if (prev_msg_row == Rows - 1)
536 msg_row--;
537 }
538 msg_col = 0;
539 print_line_no_prefix(curwin->w_cursor.lnum, FALSE, FALSE);
540 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000541 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000542 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100543 else if (ex_pressedreturn && !ex_no_reprint) // must be at EOF
Bram Moolenaardf177f62005-02-22 08:39:57 +0000544 {
545 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100546 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000547 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100548 emsg(_("E501: At end-of-file"));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000549 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000550 }
551
552#ifdef FEAT_GUI
553 --hold_gui_events;
554#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000555 --RedrawingDisabled;
556 --no_wait_return;
557 update_screen(CLEAR);
558 need_wait_return = FALSE;
559 msg_scroll = save_msg_scroll;
560}
561
562/*
Bram Moolenaar4facea32019-10-12 20:17:40 +0200563 * Print the executed command for when 'verbose' is set.
564 * When "lnum" is 0 only print the command.
565 */
566 static void
567msg_verbose_cmd(linenr_T lnum, char_u *cmd)
568{
569 ++no_wait_return;
570 verbose_enter_scroll();
571
572 if (lnum == 0)
573 smsg(_("Executing: %s"), cmd);
574 else
575 smsg(_("line %ld: %s"), (long)lnum, cmd);
576 if (msg_silent == 0)
577 msg_puts("\n"); // don't overwrite this
578
579 verbose_leave_scroll();
580 --no_wait_return;
581}
582
583/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000584 * Execute a simple command line. Used for translated commands like "*".
585 */
586 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100587do_cmdline_cmd(char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000588{
589 return do_cmdline(cmd, NULL, NULL,
590 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
591}
592
593/*
594 * do_cmdline(): execute one Ex command line
595 *
596 * 1. Execute "cmdline" when it is not NULL.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100597 * If "cmdline" is NULL, or more lines are needed, fgetline() is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000598 * 2. Split up in parts separated with '|'.
599 *
600 * This function can be called recursively!
601 *
602 * flags:
603 * DOCMD_VERBOSE - The command will be included in the error message.
604 * DOCMD_NOWAIT - Don't call wait_return() and friends.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100605 * DOCMD_REPEAT - Repeat execution until fgetline() returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000606 * DOCMD_KEYTYPED - Don't reset KeyTyped.
607 * DOCMD_EXCRESET - Reset the exception environment (used for debugging).
608 * DOCMD_KEEPLINE - Store first typed line (for repeating with ".").
609 *
610 * return FAIL if cmdline could not be executed, OK otherwise
611 */
612 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100613do_cmdline(
614 char_u *cmdline,
Bram Moolenaare96a2492019-06-25 04:12:16 +0200615 char_u *(*fgetline)(int, void *, int, int),
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100616 void *cookie, // argument for fgetline()
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100617 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000618{
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100619 char_u *next_cmdline; // next cmd to execute
620 char_u *cmdline_copy = NULL; // copy of cmd line
621 int used_getline = FALSE; // used "fgetline" to obtain command
622 static int recursive = 0; // recursive depth
Bram Moolenaar071d4272004-06-13 20:20:40 +0000623 int msg_didout_before_start = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100624 int count = 0; // line number count
625 int did_inc = FALSE; // incremented RedrawingDisabled
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626 int retval = OK;
627#ifdef FEAT_EVAL
Bram Moolenaarddef1292019-12-16 17:10:33 +0100628 cstack_T cstack; // conditional stack
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100629 garray_T lines_ga; // keep lines for ":while"/":for"
630 int current_line = 0; // active line in lines_ga
Bram Moolenaard5053d02020-06-28 15:51:16 +0200631 int current_line_before = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100632 char_u *fname = NULL; // function or script name
633 linenr_T *breakpoint = NULL; // ptr to breakpoint field in cookie
634 int *dbg_tick = NULL; // ptr to dbg_tick field in cookie
635 struct dbg_stuff debug_saved; // saved things for debug mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000636 int initial_trylevel;
Bram Moolenaar25e0f582020-05-25 22:36:50 +0200637 msglist_T **saved_msg_list = NULL;
638 msglist_T *private_msg_list;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000639
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100640 // "fgetline" and "cookie" passed to do_one_cmd()
Bram Moolenaare96a2492019-06-25 04:12:16 +0200641 char_u *(*cmd_getline)(int, void *, int, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642 void *cmd_cookie;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000643 struct loop_cookie cmd_loop_cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000644 void *real_cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000645 int getline_is_func;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000646#else
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100647# define cmd_getline fgetline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000648# define cmd_cookie cookie
649#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100650 static int call_depth = 0; // recursiveness
Bram Moolenaar2196bce2020-04-12 20:01:11 +0200651#ifdef FEAT_EVAL
Bram Moolenaare31ee862020-01-07 20:59:34 +0100652 ESTACK_CHECK_DECLARATION
Bram Moolenaar071d4272004-06-13 20:20:40 +0000653
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100654 // For every pair of do_cmdline()/do_one_cmd() calls, use an extra memory
655 // location for storing error messages to be converted to an exception.
656 // This ensures that the do_errthrow() call in do_one_cmd() does not
657 // combine the messages stored by an earlier invocation of do_one_cmd()
658 // with the command name of the later one. This would happen when
659 // BufWritePost autocommands are executed after a write error.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000660 saved_msg_list = msg_list;
661 msg_list = &private_msg_list;
662 private_msg_list = NULL;
663#endif
664
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100665 // It's possible to create an endless loop with ":execute", catch that
666 // here. The value of 200 allows nested function calls, ":source", etc.
667 // Allow 200 or 'maxfuncdepth', whatever is larger.
Bram Moolenaarb094ff42017-01-02 16:16:39 +0100668 if (call_depth >= 200
669#ifdef FEAT_EVAL
670 && call_depth >= p_mfd
671#endif
672 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000673 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100674 emsg(_("E169: Command too recursive"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100676 // When converting to an exception, we do not include the command name
677 // since this is not an error of the specific command.
Bram Moolenaarddef1292019-12-16 17:10:33 +0100678 do_errthrow((cstack_T *)NULL, (char_u *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679 msg_list = saved_msg_list;
680#endif
681 return FAIL;
682 }
683 ++call_depth;
684
685#ifdef FEAT_EVAL
686 cstack.cs_idx = -1;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000687 cstack.cs_looplevel = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688 cstack.cs_trylevel = 0;
689 cstack.cs_emsg_silent_list = NULL;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000690 cstack.cs_lflags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691 ga_init2(&lines_ga, (int)sizeof(wcmd_T), 10);
692
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100693 real_cookie = getline_cookie(fgetline, cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100695 // Inside a function use a higher nesting level.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100696 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000697 if (getline_is_func && ex_nesting_level == func_level(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698 ++ex_nesting_level;
699
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100700 // Get the function or script name and the address where the next breakpoint
701 // line and the debug tick for a function or script are stored.
Bram Moolenaar05159a02005-02-26 23:04:13 +0000702 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000703 {
704 fname = func_name(real_cookie);
705 breakpoint = func_breakpoint(real_cookie);
706 dbg_tick = func_dbg_tick(real_cookie);
707 }
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100708 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100710 fname = SOURCING_NAME;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000711 breakpoint = source_breakpoint(real_cookie);
712 dbg_tick = source_dbg_tick(real_cookie);
713 }
714
715 /*
716 * Initialize "force_abort" and "suppress_errthrow" at the top level.
717 */
718 if (!recursive)
719 {
720 force_abort = FALSE;
721 suppress_errthrow = FALSE;
722 }
723
724 /*
725 * If requested, store and reset the global values controlling the
Bram Moolenaar89d40322006-08-29 15:30:07 +0000726 * exception handling (used when debugging). Otherwise clear it to avoid
727 * a bogus compiler warning when the optimizer uses inline functions...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000728 */
Bram Moolenaarb4872942006-05-13 10:32:52 +0000729 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +0000730 save_dbg_stuff(&debug_saved);
Bram Moolenaar89d40322006-08-29 15:30:07 +0000731 else
Bram Moolenaara80faa82020-04-12 19:37:17 +0200732 CLEAR_FIELD(debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000733
734 initial_trylevel = trylevel;
735
736 /*
737 * "did_throw" will be set to TRUE when an exception is being thrown.
738 */
739 did_throw = FALSE;
740#endif
741 /*
742 * "did_emsg" will be set to TRUE when emsg() is used, in which case we
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000743 * cancel the whole command line, and any if/endif or loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000744 * If force_abort is set, we cancel everything.
745 */
746 did_emsg = FALSE;
747
748 /*
749 * KeyTyped is only set when calling vgetc(). Reset it here when not
750 * calling vgetc() (sourced command lines).
751 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100752 if (!(flags & DOCMD_KEYTYPED)
753 && !getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754 KeyTyped = FALSE;
755
756 /*
757 * Continue executing command lines:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000758 * - when inside an ":if", ":while" or ":for"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000759 * - for multiple commands on one line, separated with '|'
760 * - when repeating until there are no more lines (for ":source")
761 */
762 next_cmdline = cmdline;
763 do
764 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000765#ifdef FEAT_EVAL
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100766 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000767#endif
768
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100769 // stop skipping cmds for an error msg after all endif/while/for
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770 if (next_cmdline == NULL
771#ifdef FEAT_EVAL
772 && !force_abort
773 && cstack.cs_idx < 0
Bram Moolenaar05159a02005-02-26 23:04:13 +0000774 && !(getline_is_func && func_has_abort(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775#endif
776 )
777 did_emsg = FALSE;
778
779 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000780 * 1. If repeating a line in a loop, get a line from lines_ga.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100781 * 2. If no line given: Get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782 * 3. If a line is given: Make a copy, so we can mess with it.
783 */
784
785#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100786 // 1. If repeating, get a previous line from lines_ga.
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000787 if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100789 // Each '|' separated command is stored separately in lines_ga, to
790 // be able to jump to it. Don't use next_cmdline now.
Bram Moolenaard23a8232018-02-10 18:45:26 +0100791 VIM_CLEAR(cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100793 // Check if a function has returned or, unless it has an unclosed
794 // try conditional, aborted.
Bram Moolenaar05159a02005-02-26 23:04:13 +0000795 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000797# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000798 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000799 func_line_end(real_cookie);
800# endif
801 if (func_has_ended(real_cookie))
802 {
803 retval = FAIL;
804 break;
805 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000807#ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000808 else if (do_profiling == PROF_YES
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100809 && getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000810 script_line_end();
811#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000812
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100813 // Check if a sourced file hit a ":finish" command.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100814 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815 {
816 retval = FAIL;
817 break;
818 }
819
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100820 // If breakpoints have been added/deleted need to check for it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000821 if (breakpoint != NULL && dbg_tick != NULL
822 && *dbg_tick != debug_tick)
823 {
824 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100825 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100826 fname, SOURCING_LNUM);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827 *dbg_tick = debug_tick;
828 }
829
830 next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100831 SOURCING_LNUM = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100833 // Did we encounter a breakpoint?
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834 if (breakpoint != NULL && *breakpoint != 0
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100835 && *breakpoint <= SOURCING_LNUM)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000836 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100837 dbg_breakpoint(fname, SOURCING_LNUM);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100838 // Find next breakpoint.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100840 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100841 fname, SOURCING_LNUM);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842 *dbg_tick = debug_tick;
843 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000844# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000845 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000846 {
847 if (getline_is_func)
848 func_line_start(real_cookie);
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100849 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000850 script_line_start();
851 }
852# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000853 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854#endif
855
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100856 // 2. If no line given, get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000857 if (next_cmdline == NULL)
858 {
859 /*
860 * Need to set msg_didout for the first line after an ":if",
861 * otherwise the ":if" will be overwritten.
862 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100863 if (count == 1 && getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000864 msg_didout = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100865 if (fgetline == NULL || (next_cmdline = fgetline(':', cookie,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000866#ifdef FEAT_EVAL
867 cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2
868#else
869 0
870#endif
Bram Moolenaare96a2492019-06-25 04:12:16 +0200871 , TRUE)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000872 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100873 // Don't call wait_return for aborted command line. The NULL
874 // returned for the end of a sourced file or executed function
875 // doesn't do this.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876 if (KeyTyped && !(flags & DOCMD_REPEAT))
877 need_wait_return = FALSE;
878 retval = FAIL;
879 break;
880 }
881 used_getline = TRUE;
882
883 /*
884 * Keep the first typed line. Clear it when more lines are typed.
885 */
886 if (flags & DOCMD_KEEPLINE)
887 {
888 vim_free(repeat_cmdline);
889 if (count == 0)
890 repeat_cmdline = vim_strsave(next_cmdline);
891 else
892 repeat_cmdline = NULL;
893 }
894 }
895
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100896 // 3. Make a copy of the command so we can mess with it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000897 else if (cmdline_copy == NULL)
898 {
899 next_cmdline = vim_strsave(next_cmdline);
900 if (next_cmdline == NULL)
901 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100902 emsg(_(e_outofmem));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000903 retval = FAIL;
904 break;
905 }
906 }
907 cmdline_copy = next_cmdline;
908
909#ifdef FEAT_EVAL
910 /*
Bram Moolenaard5053d02020-06-28 15:51:16 +0200911 * Inside a while/for loop, and when the command looks like a ":while"
912 * or ":for", the line is stored, because we may need it later when
913 * looping.
914 *
915 * When there is a '|' and another command, it is stored separately,
916 * because we need to be able to jump back to it from an
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000917 * :endwhile/:endfor.
Bram Moolenaard5053d02020-06-28 15:51:16 +0200918 *
919 * Pass a different "fgetline" function to do_one_cmd() below,
920 * that it stores lines in or reads them from "lines_ga". Makes it
921 * possible to define a function inside a while/for loop and handles
922 * line continuation.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000923 */
Bram Moolenaard5053d02020-06-28 15:51:16 +0200924 if ((cstack.cs_looplevel > 0 || has_loop_cmd(next_cmdline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000925 {
Bram Moolenaard5053d02020-06-28 15:51:16 +0200926 cmd_getline = get_loop_line;
927 cmd_cookie = (void *)&cmd_loop_cookie;
928 cmd_loop_cookie.lines_gap = &lines_ga;
929 cmd_loop_cookie.current_line = current_line;
930 cmd_loop_cookie.getline = fgetline;
931 cmd_loop_cookie.cookie = cookie;
932 cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len);
933
934 // Save the current line when encountering it the first time.
935 if (current_line == lines_ga.ga_len
936 && store_loop_line(&lines_ga, next_cmdline) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937 {
938 retval = FAIL;
939 break;
940 }
Bram Moolenaard5053d02020-06-28 15:51:16 +0200941 current_line_before = current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000942 }
Bram Moolenaard5053d02020-06-28 15:51:16 +0200943 else
944 {
945 cmd_getline = fgetline;
946 cmd_cookie = cookie;
947 }
948
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949 did_endif = FALSE;
950#endif
951
952 if (count++ == 0)
953 {
954 /*
955 * All output from the commands is put below each other, without
956 * waiting for a return. Don't do this when executing commands
957 * from a script or when being called recursive (e.g. for ":e
958 * +command file").
959 */
960 if (!(flags & DOCMD_NOWAIT) && !recursive)
961 {
962 msg_didout_before_start = msg_didout;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100963 msg_didany = FALSE; // no output yet
Bram Moolenaar071d4272004-06-13 20:20:40 +0000964 msg_start();
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100965 msg_scroll = TRUE; // put messages below each other
966 ++no_wait_return; // don't wait for return until finished
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967 ++RedrawingDisabled;
968 did_inc = TRUE;
969 }
970 }
971
Bram Moolenaar823654b2020-05-29 23:03:09 +0200972 if ((p_verbose >= 15 && SOURCING_NAME != NULL) || p_verbose >= 16)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100973 msg_verbose_cmd(SOURCING_LNUM, cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000974
975 /*
976 * 2. Execute one '|' separated command.
977 * do_one_cmd() will return NULL if there is no trailing '|'.
978 * "cmdline_copy" can change, e.g. for '%' and '#' expansion.
979 */
980 ++recursive;
981 next_cmdline = do_one_cmd(&cmdline_copy, flags & DOCMD_VERBOSE,
982#ifdef FEAT_EVAL
983 &cstack,
984#endif
985 cmd_getline, cmd_cookie);
986 --recursive;
987
988#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000989 if (cmd_cookie == (void *)&cmd_loop_cookie)
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100990 // Use "current_line" from "cmd_loop_cookie", it may have been
991 // incremented when defining a function.
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000992 current_line = cmd_loop_cookie.current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993#endif
994
995 if (next_cmdline == NULL)
996 {
Bram Moolenaard23a8232018-02-10 18:45:26 +0100997 VIM_CLEAR(cmdline_copy);
Bram Moolenaard7663c22019-08-06 21:59:57 +0200998
Bram Moolenaar071d4272004-06-13 20:20:40 +0000999 /*
1000 * If the command was typed, remember it for the ':' register.
1001 * Do this AFTER executing the command to make :@: work.
1002 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001003 if (getline_equal(fgetline, cookie, getexline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001004 && new_last_cmdline != NULL)
1005 {
1006 vim_free(last_cmdline);
1007 last_cmdline = new_last_cmdline;
1008 new_last_cmdline = NULL;
1009 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010 }
1011 else
1012 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001013 // need to copy the command after the '|' to cmdline_copy, for the
1014 // next do_one_cmd()
Bram Moolenaara7241f52008-06-24 20:39:31 +00001015 STRMOVE(cmdline_copy, next_cmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016 next_cmdline = cmdline_copy;
1017 }
1018
1019
1020#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001021 // reset did_emsg for a function that is not aborted by an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022 if (did_emsg && !force_abort
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001023 && getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001024 && !func_has_abort(real_cookie))
1025 did_emsg = FALSE;
1026
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001027 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028 {
1029 ++current_line;
1030
1031 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001032 * An ":endwhile", ":endfor" and ":continue" is handled here.
1033 * If we were executing commands, jump back to the ":while" or
1034 * ":for".
1035 * If we were not executing commands, decrement cs_looplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001037 if (cstack.cs_lflags & (CSL_HAD_CONT | CSL_HAD_ENDLOOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001039 cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001041 // Jump back to the matching ":while" or ":for". Be careful
1042 // not to use a cs_line[] from an entry that isn't a ":while"
1043 // or ":for": It would make "current_line" invalid and can
1044 // cause a crash.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045 if (!did_emsg && !got_int && !did_throw
1046 && cstack.cs_idx >= 0
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001047 && (cstack.cs_flags[cstack.cs_idx]
1048 & (CSF_WHILE | CSF_FOR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049 && cstack.cs_line[cstack.cs_idx] >= 0
1050 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE))
1051 {
1052 current_line = cstack.cs_line[cstack.cs_idx];
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001053 // remember we jumped there
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001054 cstack.cs_lflags |= CSL_HAD_LOOP;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001055 line_breakcheck(); // check if CTRL-C typed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001057 // Check for the next breakpoint at or after the ":while"
1058 // or ":for".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059 if (breakpoint != NULL)
1060 {
1061 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001062 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063 fname,
1064 ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1);
1065 *dbg_tick = debug_tick;
1066 }
1067 }
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001068 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001070 // can only get here with ":endwhile" or ":endfor"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071 if (cstack.cs_idx >= 0)
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001072 rewind_conditionals(&cstack, cstack.cs_idx - 1,
1073 CSF_WHILE | CSF_FOR, &cstack.cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074 }
1075 }
1076
1077 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001078 * For a ":while" or ":for" we need to remember the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001080 else if (cstack.cs_lflags & CSL_HAD_LOOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001082 cstack.cs_lflags &= ~CSL_HAD_LOOP;
Bram Moolenaard5053d02020-06-28 15:51:16 +02001083 cstack.cs_line[cstack.cs_idx] = current_line_before;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084 }
1085 }
1086
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001087 // Check for the next breakpoint after a watchexpression
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01001088 if (breakpoint != NULL && has_watchexpr())
1089 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001090 *breakpoint = dbg_find_breakpoint(FALSE, fname, SOURCING_LNUM);
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01001091 *dbg_tick = debug_tick;
1092 }
1093
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094 /*
1095 * When not inside any ":while" loop, clear remembered lines.
1096 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001097 if (cstack.cs_looplevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098 {
1099 if (lines_ga.ga_len > 0)
1100 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001101 SOURCING_LNUM =
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102 ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum;
1103 free_cmdlines(&lines_ga);
1104 }
1105 current_line = 0;
1106 }
1107
1108 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001109 * A ":finally" makes did_emsg, got_int, and did_throw pending for
1110 * being restored at the ":endtry". Reset them here and set the
1111 * ACTIVE and FINALLY flags, so that the finally clause gets executed.
1112 * This includes the case where a missing ":endif", ":endwhile" or
1113 * ":endfor" was detected by the ":finally" itself.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001115 if (cstack.cs_lflags & CSL_HAD_FINA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001116 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001117 cstack.cs_lflags &= ~CSL_HAD_FINA;
1118 report_make_pending(cstack.cs_pending[cstack.cs_idx]
1119 & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001120 did_throw ? (void *)current_exception : NULL);
1121 did_emsg = got_int = did_throw = FALSE;
1122 cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY;
1123 }
1124
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001125 // Update global "trylevel" for recursive calls to do_cmdline() from
1126 // within this loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001127 trylevel = initial_trylevel + cstack.cs_trylevel;
1128
1129 /*
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001130 * If the outermost try conditional (across function calls and sourced
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131 * files) is aborted because of an error, an interrupt, or an uncaught
1132 * exception, cancel everything. If it is left normally, reset
1133 * force_abort to get the non-EH compatible abortion behavior for
1134 * the rest of the script.
1135 */
1136 if (trylevel == 0 && !did_emsg && !got_int && !did_throw)
1137 force_abort = FALSE;
1138
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001139 // Convert an interrupt to an exception if appropriate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001140 (void)do_intthrow(&cstack);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001141#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142
1143 }
1144 /*
1145 * Continue executing command lines when:
1146 * - no CTRL-C typed, no aborting error, no exception thrown or try
1147 * conditionals need to be checked for executing finally clauses or
1148 * catching an interrupt exception
1149 * - didn't get an error message or lines are not typed
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001150 * - there is a command after '|', inside a :if, :while, :for or :try, or
Bram Moolenaar071d4272004-06-13 20:20:40 +00001151 * looping for ":source" command or function call.
1152 */
1153 while (!((got_int
1154#ifdef FEAT_EVAL
1155 || (did_emsg && force_abort) || did_throw
1156#endif
1157 )
1158#ifdef FEAT_EVAL
1159 && cstack.cs_trylevel == 0
1160#endif
1161 )
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001162 && !(did_emsg
1163#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001164 // Keep going when inside try/catch, so that the error can be
1165 // deal with, except when it is a syntax error, it may cause
1166 // the :endtry to be missed.
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001167 && (cstack.cs_trylevel == 0 || did_emsg_syntax)
1168#endif
1169 && used_getline
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001170 && (getline_equal(fgetline, cookie, getexmodeline)
1171 || getline_equal(fgetline, cookie, getexline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172 && (next_cmdline != NULL
1173#ifdef FEAT_EVAL
1174 || cstack.cs_idx >= 0
1175#endif
1176 || (flags & DOCMD_REPEAT)));
1177
1178 vim_free(cmdline_copy);
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001179 did_emsg_syntax = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180#ifdef FEAT_EVAL
1181 free_cmdlines(&lines_ga);
1182 ga_clear(&lines_ga);
1183
1184 if (cstack.cs_idx >= 0)
1185 {
1186 /*
1187 * If a sourced file or executed function ran to its end, report the
1188 * unclosed conditional.
1189 */
1190 if (!got_int && !did_throw
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001191 && ((getline_equal(fgetline, cookie, getsourceline)
1192 && !source_finished(fgetline, cookie))
1193 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194 && !func_has_ended(real_cookie))))
1195 {
1196 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001197 emsg(_(e_endtry));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001199 emsg(_(e_endwhile));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001200 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001201 emsg(_(e_endfor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001203 emsg(_(e_endif));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204 }
1205
1206 /*
1207 * Reset "trylevel" in case of a ":finish" or ":return" or a missing
1208 * ":endtry" in a sourced file or executed function. If the try
1209 * conditional is in its finally clause, ignore anything pending.
1210 * If it is in a catch clause, finish the caught exception.
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001211 * Also cleanup any "cs_forinfo" structures.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212 */
1213 do
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001214 {
1215 int idx = cleanup_conditionals(&cstack, 0, TRUE);
1216
Bram Moolenaar89e5d682005-01-17 22:06:23 +00001217 if (idx >= 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001218 --idx; // remove try block not in its finally clause
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001219 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
1220 &cstack.cs_looplevel);
1221 }
1222 while (cstack.cs_idx >= 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223 trylevel = initial_trylevel;
1224 }
1225
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001226 // If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
1227 // lack was reported above and the error message is to be converted to an
1228 // exception, do this now after rewinding the cstack.
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001229 do_errthrow(&cstack, getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001230 ? (char_u *)"endfunction" : (char_u *)NULL);
1231
1232 if (trylevel == 0)
1233 {
1234 /*
1235 * When an exception is being thrown out of the outermost try
1236 * conditional, discard the uncaught exception, disable the conversion
1237 * of interrupts or errors to exceptions, and ensure that no more
1238 * commands are executed.
1239 */
1240 if (did_throw)
1241 {
1242 void *p = NULL;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02001243 msglist_T *messages = NULL, *next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244
1245 /*
1246 * If the uncaught exception is a user exception, report it as an
1247 * error. If it is an error exception, display the saved error
1248 * message now. For an interrupt exception, do nothing; the
1249 * interrupt message is given elsewhere.
1250 */
1251 switch (current_exception->type)
1252 {
1253 case ET_USER:
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001254 vim_snprintf((char *)IObuff, IOSIZE,
1255 _("E605: Exception not caught: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256 current_exception->value);
1257 p = vim_strsave(IObuff);
1258 break;
1259 case ET_ERROR:
1260 messages = current_exception->messages;
1261 current_exception->messages = NULL;
1262 break;
1263 case ET_INTERRUPT:
1264 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265 }
1266
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001267 estack_push(ETYPE_EXCEPT, current_exception->throw_name,
1268 current_exception->throw_lnum);
Bram Moolenaare31ee862020-01-07 20:59:34 +01001269 ESTACK_CHECK_SETUP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270 current_exception->throw_name = NULL;
1271
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001272 discard_current_exception(); // uses IObuff if 'verbose'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273 suppress_errthrow = TRUE;
1274 force_abort = TRUE;
1275
1276 if (messages != NULL)
1277 {
1278 do
1279 {
1280 next = messages->next;
1281 emsg(messages->msg);
1282 vim_free(messages->msg);
Bram Moolenaar5fbf3bc2020-06-01 21:13:11 +02001283 vim_free(messages->sfile);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284 vim_free(messages);
1285 messages = next;
1286 }
1287 while (messages != NULL);
1288 }
1289 else if (p != NULL)
1290 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01001291 emsg(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292 vim_free(p);
1293 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001294 vim_free(SOURCING_NAME);
Bram Moolenaare31ee862020-01-07 20:59:34 +01001295 ESTACK_CHECK_NOW
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001296 estack_pop();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297 }
1298
1299 /*
1300 * On an interrupt or an aborting error not converted to an exception,
1301 * disable the conversion of errors to exceptions. (Interrupts are not
Bram Moolenaar2196bce2020-04-12 20:01:11 +02001302 * converted anymore, here.) This enables also the interrupt message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303 * when force_abort is set and did_emsg unset in case of an interrupt
1304 * from a finally clause after an error.
1305 */
1306 else if (got_int || (did_emsg && force_abort))
1307 suppress_errthrow = TRUE;
1308 }
1309
1310 /*
1311 * The current cstack will be freed when do_cmdline() returns. An uncaught
1312 * exception will have to be rethrown in the previous cstack. If a function
1313 * has just returned or a script file was just finished and the previous
1314 * cstack belongs to the same function or, respectively, script file, it
1315 * will have to be checked for finally clauses to be executed due to the
1316 * ":return" or ":finish". This is done in do_one_cmd().
1317 */
1318 if (did_throw)
1319 need_rethrow = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001320 if ((getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321 && ex_nesting_level > source_level(real_cookie))
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001322 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323 && ex_nesting_level > func_level(real_cookie) + 1))
1324 {
1325 if (!did_throw)
1326 check_cstack = TRUE;
1327 }
1328 else
1329 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001330 // When leaving a function, reduce nesting level.
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001331 if (getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332 --ex_nesting_level;
1333 /*
1334 * Go to debug mode when returning from a function in which we are
1335 * single-stepping.
1336 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001337 if ((getline_equal(fgetline, cookie, getsourceline)
1338 || getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339 && ex_nesting_level + 1 <= debug_break_level)
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001340 do_debug(getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341 ? (char_u *)_("End of sourced file")
1342 : (char_u *)_("End of function"));
1343 }
1344
1345 /*
1346 * Restore the exception environment (done after returning from the
1347 * debugger).
1348 */
1349 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001350 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351
1352 msg_list = saved_msg_list;
Bram Moolenaar292b90d2020-03-18 15:23:16 +01001353
1354 // Cleanup if "cs_emsg_silent_list" remains.
1355 if (cstack.cs_emsg_silent_list != NULL)
1356 {
1357 eslist_T *elem, *temp;
1358
1359 for (elem = cstack.cs_emsg_silent_list; elem != NULL; elem = temp)
1360 {
1361 temp = elem->next;
1362 vim_free(elem);
1363 }
1364 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001365#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366
1367 /*
1368 * If there was too much output to fit on the command line, ask the user to
1369 * hit return before redrawing the screen. With the ":global" command we do
1370 * this only once after the command is finished.
1371 */
1372 if (did_inc)
1373 {
1374 --RedrawingDisabled;
1375 --no_wait_return;
1376 msg_scroll = FALSE;
1377
1378 /*
1379 * When just finished an ":if"-":else" which was typed, no need to
1380 * wait for hit-return. Also for an error situation.
1381 */
1382 if (retval == FAIL
1383#ifdef FEAT_EVAL
1384 || (did_endif && KeyTyped && !did_emsg)
1385#endif
1386 )
1387 {
1388 need_wait_return = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001389 msg_didany = FALSE; // don't wait when restarting edit
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390 }
1391 else if (need_wait_return)
1392 {
1393 /*
1394 * The msg_start() above clears msg_didout. The wait_return we do
1395 * here should not overwrite the command that may be shown before
1396 * doing that.
1397 */
1398 msg_didout |= msg_didout_before_start;
1399 wait_return(FALSE);
1400 }
1401 }
1402
Bram Moolenaar2a942252012-11-28 23:03:07 +01001403#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001404 did_endif = FALSE; // in case do_cmdline used recursively
Bram Moolenaar2a942252012-11-28 23:03:07 +01001405#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406 /*
1407 * Reset if_level, in case a sourced script file contains more ":if" than
1408 * ":endif" (could be ":if x | foo | endif").
1409 */
1410 if_level = 0;
Bram Moolenaar2e18a122012-11-28 19:10:54 +01001411#endif
Bram Moolenaard4ad0d42012-11-28 17:34:48 +01001412
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413 --call_depth;
1414 return retval;
1415}
1416
1417#ifdef FEAT_EVAL
1418/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001419 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420 */
1421 static char_u *
Bram Moolenaare96a2492019-06-25 04:12:16 +02001422get_loop_line(int c, void *cookie, int indent, int do_concat)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001424 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425 wcmd_T *wp;
1426 char_u *line;
1427
1428 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1429 {
1430 if (cp->repeating)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001431 return NULL; // trying to read past ":endwhile"/":endfor"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001433 // First time inside the ":while"/":for": get line normally.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001434 if (cp->getline == NULL)
Bram Moolenaare96a2492019-06-25 04:12:16 +02001435 line = getcmdline(c, 0L, indent, do_concat);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001436 else
Bram Moolenaare96a2492019-06-25 04:12:16 +02001437 line = cp->getline(c, cp->cookie, indent, do_concat);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001438 if (line != NULL && store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001439 ++cp->current_line;
1440
1441 return line;
1442 }
1443
1444 KeyTyped = FALSE;
1445 ++cp->current_line;
1446 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001447 SOURCING_LNUM = wp->lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001448 return vim_strsave(wp->line);
1449}
1450
1451/*
1452 * Store a line in "gap" so that a ":while" loop can execute it again.
1453 */
1454 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001455store_loop_line(garray_T *gap, char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001456{
1457 if (ga_grow(gap, 1) == FAIL)
1458 return FAIL;
1459 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001460 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = SOURCING_LNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001461 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001462 return OK;
1463}
1464
1465/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001466 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001467 */
1468 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001469free_cmdlines(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470{
1471 while (gap->ga_len > 0)
1472 {
1473 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1474 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001475 }
1476}
1477#endif
1478
1479/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001480 * If "fgetline" is get_loop_line(), return TRUE if the getline it uses equals
1481 * "func". * Otherwise return TRUE when "fgetline" equals "func".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001484getline_equal(
Bram Moolenaare96a2492019-06-25 04:12:16 +02001485 char_u *(*fgetline)(int, void *, int, int),
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001486 void *cookie UNUSED, // argument for fgetline()
Bram Moolenaare96a2492019-06-25 04:12:16 +02001487 char_u *(*func)(int, void *, int, int))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488{
1489#ifdef FEAT_EVAL
Bram Moolenaare96a2492019-06-25 04:12:16 +02001490 char_u *(*gp)(int, void *, int, int);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001491 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001492
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001493 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1494 // function that's originally used to obtain the lines. This may be
1495 // nested several levels.
Bram Moolenaar89d40322006-08-29 15:30:07 +00001496 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001497 cp = (struct loop_cookie *)cookie;
1498 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001499 {
1500 gp = cp->getline;
1501 cp = cp->cookie;
1502 }
1503 return gp == func;
1504#else
Bram Moolenaar89d40322006-08-29 15:30:07 +00001505 return fgetline == func;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001506#endif
1507}
1508
Bram Moolenaar071d4272004-06-13 20:20:40 +00001509/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001510 * If "fgetline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511 * getline function. Otherwise return "cookie".
1512 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513 void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001514getline_cookie(
Bram Moolenaare96a2492019-06-25 04:12:16 +02001515 char_u *(*fgetline)(int, void *, int, int) UNUSED,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001516 void *cookie) // argument for fgetline()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517{
Bram Moolenaar13505972019-01-24 15:04:48 +01001518#ifdef FEAT_EVAL
Bram Moolenaare96a2492019-06-25 04:12:16 +02001519 char_u *(*gp)(int, void *, int, int);
Bram Moolenaard5053d02020-06-28 15:51:16 +02001520 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001521
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001522 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1523 // cookie that's originally used to obtain the lines. This may be nested
1524 // several levels.
Bram Moolenaar89d40322006-08-29 15:30:07 +00001525 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001526 cp = (struct loop_cookie *)cookie;
1527 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001528 {
1529 gp = cp->getline;
1530 cp = cp->cookie;
1531 }
1532 return cp;
Bram Moolenaar13505972019-01-24 15:04:48 +01001533#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001534 return cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001535#endif
Bram Moolenaar13505972019-01-24 15:04:48 +01001536}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537
Bram Moolenaard5053d02020-06-28 15:51:16 +02001538#if defined(FEAT_EVAL) || defined(PROT)
1539/*
1540 * Get the next line source line without advancing.
1541 */
1542 char_u *
1543getline_peek(
1544 char_u *(*fgetline)(int, void *, int, int) UNUSED,
1545 void *cookie) // argument for fgetline()
1546{
1547 char_u *(*gp)(int, void *, int, int);
1548 struct loop_cookie *cp;
1549 wcmd_T *wp;
1550
1551 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1552 // cookie that's originally used to obtain the lines. This may be nested
1553 // several levels.
1554 gp = fgetline;
1555 cp = (struct loop_cookie *)cookie;
1556 while (gp == get_loop_line)
1557 {
1558 if (cp->current_line + 1 < cp->lines_gap->ga_len)
1559 {
1560 // executing lines a second time, use the stored copy
1561 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line + 1;
1562 return wp->line;
1563 }
1564 gp = cp->getline;
1565 cp = cp->cookie;
1566 }
1567 if (gp == getsourceline)
1568 return source_nextline(cp);
1569 return NULL;
1570}
1571#endif
1572
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001573
1574/*
1575 * Helper function to apply an offset for buffer commands, i.e. ":bdelete",
1576 * ":bwipeout", etc.
1577 * Returns the buffer number.
1578 */
1579 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001580compute_buffer_local_count(int addr_type, int lnum, int offset)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001581{
1582 buf_T *buf;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001583 buf_T *nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001584 int count = offset;
1585
1586 buf = firstbuf;
1587 while (buf->b_next != NULL && buf->b_fnum < lnum)
1588 buf = buf->b_next;
1589 while (count != 0)
1590 {
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001591 count += (offset < 0) ? 1 : -1;
1592 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1593 if (nextbuf == NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001594 break;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001595 buf = nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001596 if (addr_type == ADDR_LOADED_BUFFERS)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001597 // skip over unloaded buffers
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001598 while (buf->b_ml.ml_mfp == NULL)
1599 {
1600 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1601 if (nextbuf == NULL)
1602 break;
1603 buf = nextbuf;
1604 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001605 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001606 // we might have gone too far, last buffer is not loadedd
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001607 if (addr_type == ADDR_LOADED_BUFFERS)
1608 while (buf->b_ml.ml_mfp == NULL)
1609 {
1610 nextbuf = (offset >= 0) ? buf->b_prev : buf->b_next;
1611 if (nextbuf == NULL)
1612 break;
1613 buf = nextbuf;
1614 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001615 return buf->b_fnum;
1616}
1617
Bram Moolenaarb7316892019-05-01 18:08:42 +02001618/*
1619 * Return the window number of "win".
1620 * When "win" is NULL return the number of windows.
1621 */
Bram Moolenaarf240e182014-11-27 18:33:02 +01001622 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001623current_win_nr(win_T *win)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001624{
1625 win_T *wp;
1626 int nr = 0;
1627
Bram Moolenaar29323592016-07-24 22:04:11 +02001628 FOR_ALL_WINDOWS(wp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001629 {
1630 ++nr;
1631 if (wp == win)
1632 break;
1633 }
1634 return nr;
1635}
1636
1637 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001638current_tab_nr(tabpage_T *tab)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001639{
1640 tabpage_T *tp;
1641 int nr = 0;
1642
Bram Moolenaar29323592016-07-24 22:04:11 +02001643 FOR_ALL_TABPAGES(tp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001644 {
1645 ++nr;
1646 if (tp == tab)
1647 break;
1648 }
1649 return nr;
1650}
1651
1652# define CURRENT_WIN_NR current_win_nr(curwin)
1653# define LAST_WIN_NR current_win_nr(NULL)
1654# define CURRENT_TAB_NR current_tab_nr(curtab)
1655# define LAST_TAB_NR current_tab_nr(NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001656
Bram Moolenaar071d4272004-06-13 20:20:40 +00001657/*
1658 * Execute one Ex command.
1659 *
1660 * If 'sourcing' is TRUE, the command will be included in the error message.
1661 *
1662 * 1. skip comment lines and leading space
1663 * 2. handle command modifiers
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001664 * 3. find the command
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001665 * 4. parse range
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001666 * 5. Parse the command.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001667 * 6. parse arguments
1668 * 7. switch on command name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669 *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001670 * Note: "fgetline" can be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001671 *
1672 * This function may be called recursively!
1673 */
1674#if (_MSC_VER == 1200)
1675/*
Bram Moolenaared203462004-06-16 11:19:22 +00001676 * Avoid optimisation bug in VC++ version 6.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001677 */
Bram Moolenaar281bdce2005-01-25 21:53:18 +00001678 #pragma optimize( "g", off )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001679#endif
1680 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001681do_one_cmd(
Bram Moolenaarddef1292019-12-16 17:10:33 +01001682 char_u **cmdlinep,
1683 int sourcing,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684#ifdef FEAT_EVAL
Bram Moolenaarddef1292019-12-16 17:10:33 +01001685 cstack_T *cstack,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001686#endif
Bram Moolenaarddef1292019-12-16 17:10:33 +01001687 char_u *(*fgetline)(int, void *, int, int),
1688 void *cookie) // argument for fgetline()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689{
Bram Moolenaarddef1292019-12-16 17:10:33 +01001690 char_u *p;
1691 linenr_T lnum;
1692 long n;
1693 char *errormsg = NULL; // error message
1694 char_u *after_modifier = NULL;
1695 exarg_T ea; // Ex command arguments
1696 int save_msg_scroll = msg_scroll;
1697 cmdmod_T save_cmdmod;
1698 int save_reg_executing = reg_executing;
1699 int ni; // set when Not Implemented
1700 char_u *cmd;
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001701#ifdef FEAT_EVAL
1702 int starts_with_colon;
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001703 int starts_with_quote;
1704 int vim9script = in_vim9script();
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001705#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001706
Bram Moolenaara80faa82020-04-12 19:37:17 +02001707 CLEAR_FIELD(ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001708 ea.line1 = 1;
1709 ea.line2 = 1;
1710#ifdef FEAT_EVAL
1711 ++ex_nesting_level;
1712#endif
1713
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001714 // When the last file has not been edited :q has to be typed twice.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001715 if (quitmore
1716#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001717 // avoid that a function call in 'statusline' does this
Bram Moolenaar89d40322006-08-29 15:30:07 +00001718 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001719#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001720 // avoid that an autocommand, e.g. QuitPre, does this
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001721 && !getline_equal(fgetline, cookie, getnextac))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001722 --quitmore;
1723
1724 /*
1725 * Reset browse, confirm, etc.. They are restored when returning, for
1726 * recursive calls.
1727 */
1728 save_cmdmod = cmdmod;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001729
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001730 // "#!anything" is handled like a comment.
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001731 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1732 goto doend;
1733
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02001734/*
1735 * 1. Skip comment lines and leading white space and colons.
1736 * 2. Handle command modifiers.
1737 */
1738 // The "ea" structure holds the arguments that can be used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739 ea.cmd = *cmdlinep;
Bram Moolenaareffed932018-08-14 13:38:17 +02001740 ea.cmdlinep = cmdlinep;
1741 ea.getline = fgetline;
1742 ea.cookie = cookie;
1743#ifdef FEAT_EVAL
1744 ea.cstack = cstack;
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001745 starts_with_colon = *skipwhite(ea.cmd) == ':';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001746#endif
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02001747 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaareffed932018-08-14 13:38:17 +02001748 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001750 after_modifier = ea.cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751
1752#ifdef FEAT_EVAL
1753 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
1754 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
1755#else
1756 ea.skip = (if_level > 0);
1757#endif
1758
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001760 * 3. Skip over the range to find the command. Let "p" point to after it.
1761 *
1762 * We need the command to know what kind of range it uses.
1763 */
1764 cmd = ea.cmd;
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001765#ifdef FEAT_EVAL
1766 starts_with_quote = vim9script && *ea.cmd == '\'';
1767 if (!starts_with_quote)
1768#endif
1769 ea.cmd = skip_range(ea.cmd, NULL);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001770 if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
1771 ea.cmd = skipwhite(ea.cmd + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001772
1773#ifdef FEAT_EVAL
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001774 if (vim9script && !starts_with_colon)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02001775 {
1776 if (ea.cmd > cmd)
1777 {
1778 emsg(_(e_colon_required));
1779 goto doend;
1780 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001781 p = find_ex_command(&ea, NULL, lookup_scriptvar, NULL);
Bram Moolenaardf069ee2020-06-22 23:02:51 +02001782 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001783 else
1784#endif
1785 p = find_ex_command(&ea, NULL, NULL, NULL);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001786
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001787#ifdef FEAT_EVAL
1788# ifdef FEAT_PROFILE
1789 // Count this line for profiling if skip is TRUE.
1790 if (do_profiling == PROF_YES
1791 && (!ea.skip || cstack->cs_idx == 0 || (cstack->cs_idx > 0
1792 && (cstack->cs_flags[cstack->cs_idx - 1] & CSF_ACTIVE))))
1793 {
1794 int skip = did_emsg || got_int || did_throw;
1795
1796 if (ea.cmdidx == CMD_catch)
1797 skip = !skip && !(cstack->cs_idx >= 0
1798 && (cstack->cs_flags[cstack->cs_idx] & CSF_THROWN)
1799 && !(cstack->cs_flags[cstack->cs_idx] & CSF_CAUGHT));
1800 else if (ea.cmdidx == CMD_else || ea.cmdidx == CMD_elseif)
1801 skip = skip || !(cstack->cs_idx >= 0
1802 && !(cstack->cs_flags[cstack->cs_idx]
1803 & (CSF_ACTIVE | CSF_TRUE)));
1804 else if (ea.cmdidx == CMD_finally)
1805 skip = FALSE;
1806 else if (ea.cmdidx != CMD_endif
1807 && ea.cmdidx != CMD_endfor
1808 && ea.cmdidx != CMD_endtry
1809 && ea.cmdidx != CMD_endwhile)
1810 skip = ea.skip;
1811
1812 if (!skip)
1813 {
1814 if (getline_equal(fgetline, cookie, get_func_line))
1815 func_line_exec(getline_cookie(fgetline, cookie));
1816 else if (getline_equal(fgetline, cookie, getsourceline))
1817 script_line_exec();
1818 }
1819 }
1820# endif
1821
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001822 // May go to debug mode. If this happens and the ">quit" debug command is
1823 // used, throw an interrupt exception and skip the next command.
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001824 dbg_check_breakpoint(&ea);
1825 if (!ea.skip && got_int)
1826 {
1827 ea.skip = TRUE;
1828 (void)do_intthrow(cstack);
1829 }
1830#endif
1831
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001832/*
1833 * 4. parse a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834 *
1835 * where 'addr' is:
1836 *
1837 * % (entire file)
1838 * $ [+-NUM]
1839 * 'x [+-NUM] (where x denotes a currently defined mark)
1840 * . [+-NUM]
1841 * [+-NUM]..
1842 * NUM
1843 *
1844 * The ea.cmd pointer is updated to point to the first character following the
1845 * range spec. If an initial address is found, but no second, the upper bound
1846 * is equal to the lower.
1847 */
1848
Bram Moolenaar25190db2019-05-04 15:05:28 +02001849 // ea.addr_type for user commands is set by find_ucmd
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001850 if (!IS_USER_CMDIDX(ea.cmdidx))
1851 {
1852 if (ea.cmdidx != CMD_SIZE)
1853 ea.addr_type = cmdnames[(int)ea.cmdidx].cmd_addr_type;
1854 else
1855 ea.addr_type = ADDR_LINES;
1856
Bram Moolenaar25190db2019-05-04 15:05:28 +02001857 // :wincmd range depends on the argument.
Bram Moolenaar164f3262015-01-14 21:22:01 +01001858 if (ea.cmdidx == CMD_wincmd && p != NULL)
1859 get_wincmd_addr_type(skipwhite(p), &ea);
Bram Moolenaar25190db2019-05-04 15:05:28 +02001860#ifdef FEAT_QUICKFIX
1861 // :.cc in quickfix window uses line number
1862 if ((ea.cmdidx == CMD_cc || ea.cmdidx == CMD_ll) && bt_quickfix(curbuf))
1863 ea.addr_type = ADDR_OTHER;
1864#endif
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001865 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001866
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001867 ea.cmd = cmd;
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001868#ifdef FEAT_EVAL
1869 if (!starts_with_quote)
1870#endif
1871 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
1872 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001873
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001875 * 5. Parse the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001876 */
1877
1878 /*
1879 * Skip ':' and any white space
1880 */
1881 ea.cmd = skipwhite(ea.cmd);
1882 while (*ea.cmd == ':')
1883 ea.cmd = skipwhite(ea.cmd + 1);
1884
1885 /*
1886 * If we got a line, but no command, then go to the line.
1887 * If we find a '|' or '\n' we set ea.nextcmd.
1888 */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001889 if (*ea.cmd == NUL || *ea.cmd == '"'
Bram Moolenaar7a092242020-04-16 22:10:49 +02001890#ifdef FEAT_EVAL
Bram Moolenaara26b9702020-04-18 19:53:28 +02001891 || (*ea.cmd == '#' && ea.cmd[1] != '{'
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001892 && !starts_with_colon && vim9script)
Bram Moolenaar7a092242020-04-16 22:10:49 +02001893#endif
1894 || (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895 {
1896 /*
1897 * strange vi behaviour:
1898 * ":3" jumps to line 3
1899 * ":3|..." prints line 3
1900 * ":|" prints current line
1901 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001902 if (ea.skip) // skip this if inside :if
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001904 if (*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905 {
1906 ea.cmdidx = CMD_print;
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001907 ea.argt = EX_RANGE+EX_COUNT+EX_TRLBAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001908 if ((errormsg = invalid_range(&ea)) == NULL)
1909 {
1910 correct_range(&ea);
1911 ex_print(&ea);
1912 }
1913 }
1914 else if (ea.addr_count != 0)
1915 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001916 if (ea.line2 > curbuf->b_ml.ml_line_count)
1917 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001918 // With '-' in 'cpoptions' a line number past the file is an
1919 // error, otherwise put it at the end of the file.
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001920 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
1921 ea.line2 = -1;
1922 else
1923 ea.line2 = curbuf->b_ml.ml_line_count;
1924 }
1925
1926 if (ea.line2 < 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001927 errormsg = _(e_invrange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001928 else
1929 {
1930 if (ea.line2 == 0)
1931 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001932 else
1933 curwin->w_cursor.lnum = ea.line2;
1934 beginline(BL_SOL | BL_FIX);
1935 }
1936 }
1937 goto doend;
1938 }
1939
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001940 // If this looks like an undefined user command and there are CmdUndefined
1941 // autocommands defined, trigger the matching autocommands.
Bram Moolenaard5005162014-08-22 23:05:54 +02001942 if (p != NULL && ea.cmdidx == CMD_SIZE && !ea.skip
1943 && ASCII_ISUPPER(*ea.cmd)
1944 && has_cmdundefined())
1945 {
Bram Moolenaard5005162014-08-22 23:05:54 +02001946 int ret;
1947
Bram Moolenaar5a31b462014-08-23 14:16:20 +02001948 p = ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02001949 while (ASCII_ISALNUM(*p))
1950 ++p;
Bram Moolenaar158ea172020-06-18 17:28:39 +02001951 p = vim_strnsave(ea.cmd, p - ea.cmd);
Bram Moolenaard5005162014-08-22 23:05:54 +02001952 ret = apply_autocmds(EVENT_CMDUNDEFINED, p, p, TRUE, NULL);
1953 vim_free(p);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001954 // If the autocommands did something and didn't cause an error, try
1955 // finding the command again.
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001956 p = (ret
1957#ifdef FEAT_EVAL
1958 && !aborting()
Bram Moolenaard5005162014-08-22 23:05:54 +02001959#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001960 ) ? find_ex_command(&ea, NULL, NULL, NULL) : ea.cmd;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001961 }
Bram Moolenaard5005162014-08-22 23:05:54 +02001962
Bram Moolenaar071d4272004-06-13 20:20:40 +00001963 if (p == NULL)
1964 {
1965 if (!ea.skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001966 errormsg = _("E464: Ambiguous use of user-defined command");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001967 goto doend;
1968 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001969 // Check for wrong commands.
Bram Moolenaar6f9a4762017-06-22 20:39:17 +02001970 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78
1971 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972 {
1973 errormsg = uc_fun_cmd();
1974 goto doend;
1975 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001976
Bram Moolenaar071d4272004-06-13 20:20:40 +00001977 if (ea.cmdidx == CMD_SIZE)
1978 {
1979 if (!ea.skip)
1980 {
1981 STRCPY(IObuff, _("E492: Not an editor command"));
1982 if (!sourcing)
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001983 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001984 // If the modifier was parsed OK the error must be in the
1985 // following command
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001986 if (after_modifier != NULL)
1987 append_command(after_modifier);
1988 else
1989 append_command(*cmdlinep);
1990 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001991 errormsg = (char *)IObuff;
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001992 did_emsg_syntax = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001993 }
1994 goto doend;
1995 }
1996
Bram Moolenaar958636c2014-10-21 20:01:58 +02001997 ni = (!IS_USER_CMDIDX(ea.cmdidx)
1998 && (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00001999#ifdef HAVE_EX_SCRIPT_NI
2000 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
2001#endif
2002 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002003
2004#ifndef FEAT_EVAL
2005 /*
2006 * When the expression evaluation is disabled, recognize the ":if" and
2007 * ":endif" commands and ignore everything in between it.
2008 */
2009 if (ea.cmdidx == CMD_if)
2010 ++if_level;
2011 if (if_level)
2012 {
2013 if (ea.cmdidx == CMD_endif)
2014 --if_level;
2015 goto doend;
2016 }
2017
2018#endif
2019
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002020 // forced commands
Bram Moolenaar196b3b02008-06-20 16:51:41 +00002021 if (*p == '!' && ea.cmdidx != CMD_substitute
2022 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002023 {
2024 ++p;
2025 ea.forceit = TRUE;
2026 }
2027 else
2028 ea.forceit = FALSE;
2029
2030/*
Bram Moolenaarb7316892019-05-01 18:08:42 +02002031 * 6. Parse arguments. Then check for errors.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002032 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02002033 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002034 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002035
2036 if (!ea.skip)
2037 {
2038#ifdef HAVE_SANDBOX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002039 if (sandbox != 0 && !(ea.argt & EX_SBOXOK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002040 {
Bram Moolenaar8c62a082019-02-08 14:34:10 +01002041 // Command not allowed in sandbox.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002042 errormsg = _(e_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002043 goto doend;
2044 }
2045#endif
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002046 if (restricted != 0 && (ea.argt & EX_RESTRICT))
Bram Moolenaar8c62a082019-02-08 14:34:10 +01002047 {
2048 errormsg = _("E981: Command not allowed in rvim");
2049 goto doend;
2050 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002051 if (!curbuf->b_p_ma && (ea.argt & EX_MODIFY))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002052 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002053 // Command not allowed in non-'modifiable' buffer
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002054 errormsg = _(e_modifiable);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002055 goto doend;
2056 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002057
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002058 if (text_locked() && !(ea.argt & EX_CMDWIN)
Bram Moolenaar958636c2014-10-21 20:01:58 +02002059 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002060 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002061 // Command not allowed when editing the command line.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002062 errormsg = _(get_text_locked_msg());
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063 goto doend;
2064 }
Bram Moolenaar379fb762018-08-30 15:58:28 +02002065
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002066 // Disallow editing another buffer when "curbuf_lock" is set.
2067 // Do allow ":checktime" (it is postponed).
2068 // Do allow ":edit" (check for an argument later).
2069 // Do allow ":file" with no arguments (check for an argument later).
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002070 if (!(ea.argt & EX_CMDWIN)
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002071 && ea.cmdidx != CMD_checktime
Bram Moolenaar379fb762018-08-30 15:58:28 +02002072 && ea.cmdidx != CMD_edit
2073 && ea.cmdidx != CMD_file
Bram Moolenaar958636c2014-10-21 20:01:58 +02002074 && !IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002075 && curbuf_locked())
2076 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002077
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002078 if (!ni && !(ea.argt & EX_RANGE) && ea.addr_count > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002079 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002080 // no range allowed
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002081 errormsg = _(e_norange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002082 goto doend;
2083 }
2084 }
2085
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002086 if (!ni && !(ea.argt & EX_BANG) && ea.forceit) // no <!> allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002087 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002088 errormsg = _(e_nobang);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002089 goto doend;
2090 }
2091
2092 /*
2093 * Don't complain about the range if it is not used
2094 * (could happen if line_count is accidentally set to 0).
2095 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002096 if (!ea.skip && !ni && (ea.argt & EX_RANGE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002097 {
2098 /*
2099 * If the range is backwards, ask for confirmation and, if given, swap
2100 * ea.line1 & ea.line2 so it's forwards again.
2101 * When global command is busy, don't ask, will fail below.
2102 */
2103 if (!global_busy && ea.line1 > ea.line2)
2104 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002105 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002106 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002107 if (sourcing || exmode_active)
2108 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002109 errormsg = _("E493: Backwards range given");
Bram Moolenaara5792f52005-11-23 21:25:05 +00002110 goto doend;
2111 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002112 if (ask_yesno((char_u *)
2113 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002114 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002115 }
2116 lnum = ea.line1;
2117 ea.line1 = ea.line2;
2118 ea.line2 = lnum;
2119 }
2120 if ((errormsg = invalid_range(&ea)) != NULL)
2121 goto doend;
2122 }
2123
Bram Moolenaarb7316892019-05-01 18:08:42 +02002124 if ((ea.addr_type == ADDR_OTHER) && ea.addr_count == 0)
2125 // default is 1, not cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002126 ea.line2 = 1;
2127
2128 correct_range(&ea);
2129
2130#ifdef FEAT_FOLDING
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002131 if (((ea.argt & EX_WHOLEFOLD) || ea.addr_count >= 2) && !global_busy
Bram Moolenaara3306952016-01-02 21:41:06 +01002132 && ea.addr_type == ADDR_LINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002133 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002134 // Put the first line at the start of a closed fold, put the last line
2135 // at the end of a closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002136 (void)hasFolding(ea.line1, &ea.line1, NULL);
2137 (void)hasFolding(ea.line2, NULL, &ea.line2);
2138 }
2139#endif
2140
2141#ifdef FEAT_QUICKFIX
2142 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002143 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002144 * option here, so things like % get expanded.
2145 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002146 p = replace_makeprg(&ea, p, cmdlinep);
2147 if (p == NULL)
2148 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002149#endif
2150
2151 /*
2152 * Skip to start of argument.
2153 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2154 */
2155 if (ea.cmdidx == CMD_bang)
2156 ea.arg = p;
2157 else
2158 ea.arg = skipwhite(p);
2159
Bram Moolenaar379fb762018-08-30 15:58:28 +02002160 // ":file" cannot be run with an argument when "curbuf_lock" is set
2161 if (ea.cmdidx == CMD_file && *ea.arg != NUL && curbuf_locked())
2162 goto doend;
2163
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164 /*
2165 * Check for "++opt=val" argument.
2166 * Must be first, allow ":w ++enc=utf8 !cmd"
2167 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002168 if (ea.argt & EX_ARGOPT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002169 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2170 if (getargopt(&ea) == FAIL && !ni)
2171 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002172 errormsg = _(e_invarg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002173 goto doend;
2174 }
2175
2176 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2177 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002178 if (*ea.arg == '>') // append
Bram Moolenaar071d4272004-06-13 20:20:40 +00002179 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002180 if (*++ea.arg != '>') // typed wrong
Bram Moolenaar071d4272004-06-13 20:20:40 +00002181 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002182 errormsg = _("E494: Use w or w>>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002183 goto doend;
2184 }
2185 ea.arg = skipwhite(ea.arg + 1);
2186 ea.append = TRUE;
2187 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002188 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) // :w !filter
Bram Moolenaar071d4272004-06-13 20:20:40 +00002189 {
2190 ++ea.arg;
2191 ea.usefilter = TRUE;
2192 }
2193 }
2194
2195 if (ea.cmdidx == CMD_read)
2196 {
2197 if (ea.forceit)
2198 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002199 ea.usefilter = TRUE; // :r! filter if ea.forceit
Bram Moolenaar071d4272004-06-13 20:20:40 +00002200 ea.forceit = FALSE;
2201 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002202 else if (*ea.arg == '!') // :r !filter
Bram Moolenaar071d4272004-06-13 20:20:40 +00002203 {
2204 ++ea.arg;
2205 ea.usefilter = TRUE;
2206 }
2207 }
2208
2209 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2210 {
2211 ea.amount = 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002212 while (*ea.arg == *ea.cmd) // count number of '>' or '<'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002213 {
2214 ++ea.arg;
2215 ++ea.amount;
2216 }
2217 ea.arg = skipwhite(ea.arg);
2218 }
2219
2220 /*
2221 * Check for "+command" argument, before checking for next command.
2222 * Don't do this for ":read !cmd" and ":write !cmd".
2223 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002224 if ((ea.argt & EX_CMDARG) && !ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2226
2227 /*
2228 * Check for '|' to separate commands and '"' to start comments.
2229 * Don't do this for ":read !cmd" and ":write !cmd".
2230 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002231 if ((ea.argt & EX_TRLBAR) && !ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002232 separate_nextcmd(&ea);
2233
2234 /*
2235 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002236 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2237 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002238 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002239 else if (ea.cmdidx == CMD_bang
Bram Moolenaar67883b42017-07-27 22:57:00 +02002240 || ea.cmdidx == CMD_terminal
Bram Moolenaardf177f62005-02-22 08:39:57 +00002241 || ea.cmdidx == CMD_global
2242 || ea.cmdidx == CMD_vglobal
2243 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002244 {
2245 for (p = ea.arg; *p; ++p)
2246 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002247 // Remove one backslash before a newline, so that it's possible to
2248 // pass a newline to the shell and also a newline that is preceded
2249 // with a backslash. This makes it impossible to end a shell
2250 // command in a backslash, but that doesn't appear useful.
2251 // Halving the number of backslashes is incompatible with previous
2252 // versions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002253 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002254 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002255 else if (*p == '\n')
2256 {
2257 ea.nextcmd = p + 1;
2258 *p = NUL;
2259 break;
2260 }
2261 }
2262 }
2263
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002264 if ((ea.argt & EX_DFLALL) && ea.addr_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002265 {
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002266 buf_T *buf;
2267
Bram Moolenaar071d4272004-06-13 20:20:40 +00002268 ea.line1 = 1;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002269 switch (ea.addr_type)
2270 {
2271 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02002272 case ADDR_OTHER:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002273 ea.line2 = curbuf->b_ml.ml_line_count;
2274 break;
2275 case ADDR_LOADED_BUFFERS:
2276 buf = firstbuf;
2277 while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL)
2278 buf = buf->b_next;
2279 ea.line1 = buf->b_fnum;
2280 buf = lastbuf;
2281 while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL)
2282 buf = buf->b_prev;
2283 ea.line2 = buf->b_fnum;
2284 break;
2285 case ADDR_BUFFERS:
2286 ea.line1 = firstbuf->b_fnum;
2287 ea.line2 = lastbuf->b_fnum;
2288 break;
2289 case ADDR_WINDOWS:
2290 ea.line2 = LAST_WIN_NR;
2291 break;
2292 case ADDR_TABS:
2293 ea.line2 = LAST_TAB_NR;
2294 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01002295 case ADDR_TABS_RELATIVE:
2296 ea.line2 = 1;
2297 break;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002298 case ADDR_ARGUMENTS:
2299 if (ARGCOUNT == 0)
2300 ea.line1 = ea.line2 = 0;
2301 else
2302 ea.line2 = ARGCOUNT;
2303 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02002304 case ADDR_QUICKFIX_VALID:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002305#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02002306 ea.line2 = qf_get_valid_size(&ea);
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002307 if (ea.line2 == 0)
2308 ea.line2 = 1;
Bram Moolenaare906c502015-09-09 21:10:39 +02002309#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002310 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02002311 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02002312 case ADDR_UNSIGNED:
2313 case ADDR_QUICKFIX:
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002314 iemsg(_("INTERNAL: Cannot use EX_DFLALL with ADDR_NONE, ADDR_UNSIGNED or ADDR_QUICKFIX"));
Bram Moolenaarb7316892019-05-01 18:08:42 +02002315 break;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002316 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317 }
2318
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002319 // accept numbered register only when no count allowed (:put)
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002320 if ( (ea.argt & EX_REGSTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002321 && *ea.arg != NUL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002322 // Do not allow register = for user commands
Bram Moolenaar958636c2014-10-21 20:01:58 +02002323 && (!IS_USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002324 && !((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002326#ifndef FEAT_CLIPBOARD
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002327 // check these explicitly for a more specific error message
Bram Moolenaar85de2062011-05-05 14:26:41 +02002328 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002329 {
Bram Moolenaar99b12722019-01-14 20:16:40 +01002330 errormsg = _(e_invalidreg);
Bram Moolenaar85de2062011-05-05 14:26:41 +02002331 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002332 }
2333#endif
Bram Moolenaar958636c2014-10-21 20:01:58 +02002334 if (valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2335 && !IS_USER_CMDIDX(ea.cmdidx))))
Bram Moolenaar85de2062011-05-05 14:26:41 +02002336 {
2337 ea.regname = *ea.arg++;
2338#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002339 // for '=' register: accept the rest of the line as an expression
Bram Moolenaar85de2062011-05-05 14:26:41 +02002340 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2341 {
2342 set_expr_line(vim_strsave(ea.arg));
2343 ea.arg += STRLEN(ea.arg);
2344 }
2345#endif
2346 ea.arg = skipwhite(ea.arg);
2347 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348 }
2349
2350 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002351 * Check for a count. When accepting a EX_BUFNAME, don't use "123foo" as a
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352 * count, it's a buffer name.
2353 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002354 if ((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)
2355 && (!(ea.argt & EX_BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01002356 || VIM_ISWHITE(*p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357 {
2358 n = getdigits(&ea.arg);
2359 ea.arg = skipwhite(ea.arg);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002360 if (n <= 0 && !ni && (ea.argt & EX_ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002362 errormsg = _(e_zerocount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363 goto doend;
2364 }
Bram Moolenaarb7316892019-05-01 18:08:42 +02002365 if (ea.addr_type != ADDR_LINES) // e.g. :buffer 2, :sleep 3
Bram Moolenaar071d4272004-06-13 20:20:40 +00002366 {
2367 ea.line2 = n;
2368 if (ea.addr_count == 0)
2369 ea.addr_count = 1;
2370 }
2371 else
2372 {
2373 ea.line1 = ea.line2;
2374 ea.line2 += n - 1;
2375 ++ea.addr_count;
2376 /*
2377 * Be vi compatible: no error message for out of range.
2378 */
Bram Moolenaarb7316892019-05-01 18:08:42 +02002379 if (ea.line2 > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002380 ea.line2 = curbuf->b_ml.ml_line_count;
2381 }
2382 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002383
2384 /*
2385 * Check for flags: 'l', 'p' and '#'.
2386 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002387 if (ea.argt & EX_FLAGS)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002388 get_flags(&ea);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002389 if (!ni && !(ea.argt & EX_EXTRA) && *ea.arg != NUL
2390 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & EX_TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002392 // no arguments allowed but there is something
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002393 errormsg = _(e_trailing);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002394 goto doend;
2395 }
2396
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002397 if (!ni && (ea.argt & EX_NEEDARG) && *ea.arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002399 errormsg = _(e_argreq);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400 goto doend;
2401 }
2402
2403#ifdef FEAT_EVAL
2404 /*
2405 * Skip the command when it's not going to be executed.
2406 * The commands like :if, :endif, etc. always need to be executed.
2407 * Also make an exception for commands that handle a trailing command
2408 * themselves.
2409 */
2410 if (ea.skip)
2411 {
2412 switch (ea.cmdidx)
2413 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002414 // commands that need evaluation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415 case CMD_while:
2416 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002417 case CMD_for:
2418 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419 case CMD_if:
2420 case CMD_elseif:
2421 case CMD_else:
2422 case CMD_endif:
2423 case CMD_try:
2424 case CMD_catch:
2425 case CMD_finally:
2426 case CMD_endtry:
2427 case CMD_function:
Bram Moolenaarab55c682020-03-01 19:41:43 +01002428 case CMD_def:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429 break;
2430
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002431 // Commands that handle '|' themselves. Check: A command should
2432 // either have the EX_TRLBAR flag, appear in this list or appear in
2433 // the list at ":help :bar".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434 case CMD_aboveleft:
2435 case CMD_and:
2436 case CMD_belowright:
2437 case CMD_botright:
2438 case CMD_browse:
2439 case CMD_call:
2440 case CMD_confirm:
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01002441 case CMD_const:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002442 case CMD_delfunction:
2443 case CMD_djump:
2444 case CMD_dlist:
2445 case CMD_dsearch:
2446 case CMD_dsplit:
2447 case CMD_echo:
2448 case CMD_echoerr:
2449 case CMD_echomsg:
2450 case CMD_echon:
Bram Moolenaara76b3152020-02-16 16:20:21 +01002451 case CMD_eval:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002452 case CMD_execute:
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002453 case CMD_filter:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454 case CMD_help:
2455 case CMD_hide:
2456 case CMD_ijump:
2457 case CMD_ilist:
2458 case CMD_isearch:
2459 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002460 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461 case CMD_keepjumps:
2462 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01002463 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002464 case CMD_leftabove:
2465 case CMD_let:
2466 case CMD_lockmarks:
Bram Moolenaarcc4423a2019-11-26 17:05:00 +01002467 case CMD_lockvar:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002468 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002470 case CMD_mzscheme:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01002471 case CMD_noautocmd:
2472 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473 case CMD_perl:
2474 case CMD_psearch:
2475 case CMD_python:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002476 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002477 case CMD_python3:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002478 case CMD_return:
2479 case CMD_rightbelow:
2480 case CMD_ruby:
2481 case CMD_silent:
2482 case CMD_smagic:
2483 case CMD_snomagic:
2484 case CMD_substitute:
2485 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002486 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487 case CMD_tcl:
2488 case CMD_throw:
2489 case CMD_tilde:
2490 case CMD_topleft:
2491 case CMD_unlet:
Bram Moolenaarcc4423a2019-11-26 17:05:00 +01002492 case CMD_unlockvar:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493 case CMD_verbose:
2494 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002495 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002496 break;
2497
2498 default: goto doend;
2499 }
2500 }
2501#endif
2502
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002503 if (ea.argt & EX_XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002504 {
2505 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2506 goto doend;
2507 }
2508
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509 /*
2510 * Accept buffer name. Cannot be used at the same time with a buffer
2511 * number. Don't do this for a user command.
2512 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002513 if ((ea.argt & EX_BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
Bram Moolenaar958636c2014-10-21 20:01:58 +02002514 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002515 {
2516 /*
2517 * :bdelete, :bwipeout and :bunload take several arguments, separated
2518 * by spaces: find next space (skipping over escaped characters).
2519 * The others take one argument: ignore trailing spaces.
2520 */
2521 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2522 || ea.cmdidx == CMD_bunload)
2523 p = skiptowhite_esc(ea.arg);
2524 else
2525 {
2526 p = ea.arg + STRLEN(ea.arg);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002527 while (p > ea.arg && VIM_ISWHITE(p[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002528 --p;
2529 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002530 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & EX_BUFUNL) != 0,
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002531 FALSE, FALSE);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002532 if (ea.line2 < 0) // failed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002533 goto doend;
2534 ea.addr_count = 1;
2535 ea.arg = skipwhite(p);
2536 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002537
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002538 // The :try command saves the emsg_silent flag, reset it here when
2539 // ":silent! try" was used, it should only apply to :try itself.
Bram Moolenaareffed932018-08-14 13:38:17 +02002540 if (ea.cmdidx == CMD_try && ea.did_esilent > 0)
Bram Moolenaar2be57332018-02-13 18:05:18 +01002541 {
Bram Moolenaareffed932018-08-14 13:38:17 +02002542 emsg_silent -= ea.did_esilent;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002543 if (emsg_silent < 0)
2544 emsg_silent = 0;
Bram Moolenaareffed932018-08-14 13:38:17 +02002545 ea.did_esilent = 0;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002546 }
2547
Bram Moolenaar071d4272004-06-13 20:20:40 +00002548/*
Bram Moolenaar2be57332018-02-13 18:05:18 +01002549 * 7. Execute the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002550 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002551
Bram Moolenaar958636c2014-10-21 20:01:58 +02002552 if (IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553 {
2554 /*
2555 * Execute a user-defined command.
2556 */
2557 do_ucmd(&ea);
2558 }
2559 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002560 {
2561 /*
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002562 * Call the function to execute the builtin command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002563 */
2564 ea.errmsg = NULL;
2565 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2566 if (ea.errmsg != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002567 errormsg = _(ea.errmsg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002568 }
2569
2570#ifdef FEAT_EVAL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002571 // Set flag that any command was executed, used by ex_vim9script().
Bram Moolenaar16531552020-02-08 16:00:46 +01002572 if (getline_equal(ea.getline, ea.cookie, getsourceline)
2573 && current_sctx.sc_sid > 0)
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002574 SCRIPT_ITEM(current_sctx.sc_sid)->sn_had_command = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002575
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576 /*
2577 * If the command just executed called do_cmdline(), any throw or ":return"
2578 * or ":finish" encountered there must also check the cstack of the still
2579 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2580 * exception, or reanimate a returned function or finished script file and
2581 * return or finish it again.
2582 */
2583 if (need_rethrow)
2584 do_throw(cstack);
2585 else if (check_cstack)
2586 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002587 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002588 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002589 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002590 && current_func_returned())
2591 do_return(&ea, TRUE, FALSE, NULL);
2592 }
2593 need_rethrow = check_cstack = FALSE;
2594#endif
2595
2596doend:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002597 if (curwin->w_cursor.lnum == 0) // can happen with zero line number
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002598 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599 curwin->w_cursor.lnum = 1;
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002600 curwin->w_cursor.col = 0;
2601 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602
2603 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2604 {
2605 if (sourcing)
2606 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002607 if (errormsg != (char *)IObuff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002608 {
2609 STRCPY(IObuff, errormsg);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002610 errormsg = (char *)IObuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002611 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002612 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002613 }
2614 emsg(errormsg);
2615 }
2616#ifdef FEAT_EVAL
2617 do_errthrow(cstack,
Bram Moolenaar958636c2014-10-21 20:01:58 +02002618 (ea.cmdidx != CMD_SIZE && !IS_USER_CMDIDX(ea.cmdidx))
2619 ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002620#endif
2621
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002622 undo_cmdmod(&ea, save_msg_scroll);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002623 cmdmod = save_cmdmod;
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01002624 reg_executing = save_reg_executing;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002625
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002626#ifdef HAVE_SANDBOX
Bram Moolenaareffed932018-08-14 13:38:17 +02002627 if (ea.did_sandbox)
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002628 --sandbox;
2629#endif
2630
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002631 if (ea.nextcmd && *ea.nextcmd == NUL) // not really a next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00002632 ea.nextcmd = NULL;
2633
2634#ifdef FEAT_EVAL
2635 --ex_nesting_level;
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002636 vim_free(ea.cmdline_tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002637#endif
2638
2639 return ea.nextcmd;
2640}
2641#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00002642 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643#endif
2644
2645/*
Bram Moolenaareffed932018-08-14 13:38:17 +02002646 * Parse and skip over command modifiers:
2647 * - update eap->cmd
2648 * - store flags in "cmdmod".
2649 * - Set ex_pressedreturn for an empty command line.
2650 * - set msg_silent for ":silent"
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002651 * - set 'eventignore' to "all" for ":noautocmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002652 * - set p_verbose for ":verbose"
2653 * - Increment "sandbox" for ":sandbox"
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002654 * When "skip_only" is TRUE the global variables are not changed, except for
2655 * "cmdmod".
Bram Moolenaareffed932018-08-14 13:38:17 +02002656 * Return FAIL when the command is not to be executed.
2657 * May set "errormsg" to an error message.
2658 */
2659 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002660parse_command_modifiers(exarg_T *eap, char **errormsg, int skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002661{
2662 char_u *p;
2663
Bram Moolenaara80faa82020-04-12 19:37:17 +02002664 CLEAR_FIELD(cmdmod);
Bram Moolenaareffed932018-08-14 13:38:17 +02002665 eap->verbose_save = -1;
2666 eap->save_msg_silent = -1;
2667
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002668 // Repeat until no more command modifiers are found.
Bram Moolenaareffed932018-08-14 13:38:17 +02002669 for (;;)
2670 {
Bram Moolenaareffed932018-08-14 13:38:17 +02002671 while (*eap->cmd == ' ' || *eap->cmd == '\t' || *eap->cmd == ':')
2672 ++eap->cmd;
2673
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002674 // in ex mode, an empty line works like :+
Bram Moolenaareffed932018-08-14 13:38:17 +02002675 if (*eap->cmd == NUL && exmode_active
2676 && (getline_equal(eap->getline, eap->cookie, getexmodeline)
2677 || getline_equal(eap->getline, eap->cookie, getexline))
2678 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2679 {
2680 eap->cmd = (char_u *)"+";
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002681 if (!skip_only)
2682 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002683 }
2684
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002685 // ignore comment and empty lines
Bram Moolenaareffed932018-08-14 13:38:17 +02002686 if (*eap->cmd == '"')
2687 return FAIL;
2688 if (*eap->cmd == NUL)
2689 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002690 if (!skip_only)
2691 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002692 return FAIL;
2693 }
2694
Bram Moolenaareffed932018-08-14 13:38:17 +02002695 p = skip_range(eap->cmd, NULL);
2696 switch (*p)
2697 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002698 // When adding an entry, also modify cmd_exists().
Bram Moolenaareffed932018-08-14 13:38:17 +02002699 case 'a': if (!checkforcmd(&eap->cmd, "aboveleft", 3))
2700 break;
2701 cmdmod.split |= WSP_ABOVE;
2702 continue;
2703
2704 case 'b': if (checkforcmd(&eap->cmd, "belowright", 3))
2705 {
2706 cmdmod.split |= WSP_BELOW;
2707 continue;
2708 }
2709 if (checkforcmd(&eap->cmd, "browse", 3))
2710 {
2711#ifdef FEAT_BROWSE_CMD
2712 cmdmod.browse = TRUE;
2713#endif
2714 continue;
2715 }
2716 if (!checkforcmd(&eap->cmd, "botright", 2))
2717 break;
2718 cmdmod.split |= WSP_BOT;
2719 continue;
2720
2721 case 'c': if (!checkforcmd(&eap->cmd, "confirm", 4))
2722 break;
2723#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2724 cmdmod.confirm = TRUE;
2725#endif
2726 continue;
2727
2728 case 'k': if (checkforcmd(&eap->cmd, "keepmarks", 3))
2729 {
2730 cmdmod.keepmarks = TRUE;
2731 continue;
2732 }
2733 if (checkforcmd(&eap->cmd, "keepalt", 5))
2734 {
2735 cmdmod.keepalt = TRUE;
2736 continue;
2737 }
2738 if (checkforcmd(&eap->cmd, "keeppatterns", 5))
2739 {
2740 cmdmod.keeppatterns = TRUE;
2741 continue;
2742 }
2743 if (!checkforcmd(&eap->cmd, "keepjumps", 5))
2744 break;
2745 cmdmod.keepjumps = TRUE;
2746 continue;
2747
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002748 case 'f': // only accept ":filter {pat} cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002749 {
2750 char_u *reg_pat;
2751
2752 if (!checkforcmd(&p, "filter", 4)
2753 || *p == NUL || ends_excmd(*p))
2754 break;
2755 if (*p == '!')
2756 {
2757 cmdmod.filter_force = TRUE;
2758 p = skipwhite(p + 1);
2759 if (*p == NUL || ends_excmd(*p))
2760 break;
2761 }
Bram Moolenaar1e624c92020-07-11 14:08:04 +02002762#ifdef FEAT_EVAL
Bram Moolenaarb074e8b2020-07-11 13:40:45 +02002763 // Avoid that "filter(arg)" is recognized.
2764 if (in_vim9script() && !VIM_ISWHITE(*p))
2765 break;
Bram Moolenaar1e624c92020-07-11 14:08:04 +02002766#endif
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002767 if (skip_only)
2768 p = skip_vimgrep_pat(p, NULL, NULL);
2769 else
2770 // NOTE: This puts a NUL after the pattern.
2771 p = skip_vimgrep_pat(p, &reg_pat, NULL);
Bram Moolenaareffed932018-08-14 13:38:17 +02002772 if (p == NULL || *p == NUL)
2773 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002774 if (!skip_only)
2775 {
2776 cmdmod.filter_regmatch.regprog =
Bram Moolenaareffed932018-08-14 13:38:17 +02002777 vim_regcomp(reg_pat, RE_MAGIC);
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002778 if (cmdmod.filter_regmatch.regprog == NULL)
2779 break;
2780 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002781 eap->cmd = p;
2782 continue;
2783 }
2784
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002785 // ":hide" and ":hide | cmd" are not modifiers
Bram Moolenaareffed932018-08-14 13:38:17 +02002786 case 'h': if (p != eap->cmd || !checkforcmd(&p, "hide", 3)
2787 || *p == NUL || ends_excmd(*p))
2788 break;
2789 eap->cmd = p;
2790 cmdmod.hide = TRUE;
2791 continue;
2792
2793 case 'l': if (checkforcmd(&eap->cmd, "lockmarks", 3))
2794 {
2795 cmdmod.lockmarks = TRUE;
2796 continue;
2797 }
2798
2799 if (!checkforcmd(&eap->cmd, "leftabove", 5))
2800 break;
2801 cmdmod.split |= WSP_ABOVE;
2802 continue;
2803
2804 case 'n': if (checkforcmd(&eap->cmd, "noautocmd", 3))
2805 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002806 if (cmdmod.save_ei == NULL && !skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002807 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002808 // Set 'eventignore' to "all". Restore the
2809 // existing option value later.
Bram Moolenaareffed932018-08-14 13:38:17 +02002810 cmdmod.save_ei = vim_strsave(p_ei);
2811 set_string_option_direct((char_u *)"ei", -1,
2812 (char_u *)"all", OPT_FREE, SID_NONE);
2813 }
2814 continue;
2815 }
2816 if (!checkforcmd(&eap->cmd, "noswapfile", 3))
2817 break;
2818 cmdmod.noswapfile = TRUE;
2819 continue;
2820
2821 case 'r': if (!checkforcmd(&eap->cmd, "rightbelow", 6))
2822 break;
2823 cmdmod.split |= WSP_BELOW;
2824 continue;
2825
2826 case 's': if (checkforcmd(&eap->cmd, "sandbox", 3))
2827 {
2828#ifdef HAVE_SANDBOX
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002829 if (!skip_only)
2830 {
2831 if (!eap->did_sandbox)
2832 ++sandbox;
2833 eap->did_sandbox = TRUE;
2834 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002835#endif
2836 continue;
2837 }
2838 if (!checkforcmd(&eap->cmd, "silent", 3))
2839 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002840 if (!skip_only)
2841 {
2842 if (eap->save_msg_silent == -1)
2843 eap->save_msg_silent = msg_silent;
2844 ++msg_silent;
2845 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002846 if (*eap->cmd == '!' && !VIM_ISWHITE(eap->cmd[-1]))
2847 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002848 // ":silent!", but not "silent !cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002849 eap->cmd = skipwhite(eap->cmd + 1);
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002850 if (!skip_only)
2851 {
2852 ++emsg_silent;
2853 ++eap->did_esilent;
2854 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002855 }
2856 continue;
2857
2858 case 't': if (checkforcmd(&p, "tab", 3))
2859 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002860 if (!skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002861 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002862 long tabnr = get_address(eap, &eap->cmd,
2863 ADDR_TABS, eap->skip,
2864 skip_only, FALSE, 1);
2865 if (tabnr == MAXLNUM)
2866 cmdmod.tab = tabpage_index(curtab) + 1;
2867 else
Bram Moolenaareffed932018-08-14 13:38:17 +02002868 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002869 if (tabnr < 0 || tabnr > LAST_TAB_NR)
2870 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002871 *errormsg = _(e_invrange);
Bram Moolenaar548e5982018-12-26 21:45:00 +01002872 return FAIL;
2873 }
2874 cmdmod.tab = tabnr + 1;
Bram Moolenaareffed932018-08-14 13:38:17 +02002875 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002876 }
2877 eap->cmd = p;
2878 continue;
2879 }
2880 if (!checkforcmd(&eap->cmd, "topleft", 2))
2881 break;
2882 cmdmod.split |= WSP_TOP;
2883 continue;
2884
2885 case 'u': if (!checkforcmd(&eap->cmd, "unsilent", 3))
2886 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002887 if (!skip_only)
2888 {
2889 if (eap->save_msg_silent == -1)
2890 eap->save_msg_silent = msg_silent;
2891 msg_silent = 0;
2892 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002893 continue;
2894
2895 case 'v': if (checkforcmd(&eap->cmd, "vertical", 4))
2896 {
2897 cmdmod.split |= WSP_VERT;
2898 continue;
2899 }
2900 if (!checkforcmd(&p, "verbose", 4))
2901 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002902 if (!skip_only)
2903 {
2904 if (eap->verbose_save < 0)
2905 eap->verbose_save = p_verbose;
2906 if (vim_isdigit(*eap->cmd))
2907 p_verbose = atoi((char *)eap->cmd);
2908 else
2909 p_verbose = 1;
2910 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002911 eap->cmd = p;
2912 continue;
2913 }
2914 break;
2915 }
2916
2917 return OK;
2918}
2919
2920/*
Bram Moolenaarb074e8b2020-07-11 13:40:45 +02002921 * Undo and free contents of "cmdmod".
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002922 */
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002923 void
2924undo_cmdmod(exarg_T *eap, int save_msg_scroll)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002925{
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002926 if (eap->verbose_save >= 0)
2927 p_verbose = eap->verbose_save;
2928
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002929 if (cmdmod.save_ei != NULL)
2930 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002931 // Restore 'eventignore' to the value before ":noautocmd".
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002932 set_string_option_direct((char_u *)"ei", -1, cmdmod.save_ei,
2933 OPT_FREE, SID_NONE);
2934 free_string_option(cmdmod.save_ei);
2935 }
2936
2937 if (cmdmod.filter_regmatch.regprog != NULL)
2938 vim_regfree(cmdmod.filter_regmatch.regprog);
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002939
2940 if (eap->save_msg_silent != -1)
2941 {
2942 // messages could be enabled for a serious error, need to check if the
2943 // counters don't become negative
2944 if (!did_emsg || msg_silent > eap->save_msg_silent)
2945 msg_silent = eap->save_msg_silent;
2946 emsg_silent -= eap->did_esilent;
2947 if (emsg_silent < 0)
2948 emsg_silent = 0;
2949 // Restore msg_scroll, it's set by file I/O commands, even when no
2950 // message is actually displayed.
2951 msg_scroll = save_msg_scroll;
2952
2953 // "silent reg" or "silent echo x" inside "redir" leaves msg_col
2954 // somewhere in the line. Put it back in the first column.
2955 if (redirecting())
2956 msg_col = 0;
2957 }
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002958}
2959
2960/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002961 * Parse the address range, if any, in "eap".
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02002962 * May set the last search pattern, unless "silent" is TRUE.
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002963 * Return FAIL and set "errormsg" or return OK.
2964 */
2965 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002966parse_cmd_address(exarg_T *eap, char **errormsg, int silent)
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002967{
2968 int address_count = 1;
2969 linenr_T lnum;
2970
2971 // Repeat for all ',' or ';' separated addresses.
2972 for (;;)
2973 {
2974 eap->line1 = eap->line2;
2975 switch (eap->addr_type)
2976 {
2977 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02002978 case ADDR_OTHER:
Bram Moolenaarb5383b12020-05-18 19:46:48 +02002979 // Default is the cursor line number. Avoid using an invalid
2980 // line number though.
2981 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
2982 eap->line2 = curbuf->b_ml.ml_line_count;
2983 else
2984 eap->line2 = curwin->w_cursor.lnum;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002985 break;
2986 case ADDR_WINDOWS:
2987 eap->line2 = CURRENT_WIN_NR;
2988 break;
2989 case ADDR_ARGUMENTS:
2990 eap->line2 = curwin->w_arg_idx + 1;
2991 if (eap->line2 > ARGCOUNT)
2992 eap->line2 = ARGCOUNT;
2993 break;
2994 case ADDR_LOADED_BUFFERS:
2995 case ADDR_BUFFERS:
2996 eap->line2 = curbuf->b_fnum;
2997 break;
2998 case ADDR_TABS:
2999 eap->line2 = CURRENT_TAB_NR;
3000 break;
3001 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003002 case ADDR_UNSIGNED:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003003 eap->line2 = 1;
3004 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003005 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003006#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02003007 eap->line2 = qf_get_cur_idx(eap);
3008#endif
3009 break;
3010 case ADDR_QUICKFIX_VALID:
3011#ifdef FEAT_QUICKFIX
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003012 eap->line2 = qf_get_cur_valid_idx(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003013#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003014 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003015 case ADDR_NONE:
3016 // Will give an error later if a range is found.
3017 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003018 }
3019 eap->cmd = skipwhite(eap->cmd);
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003020 lnum = get_address(eap, &eap->cmd, eap->addr_type, eap->skip, silent,
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003021 eap->addr_count == 0, address_count++);
3022 if (eap->cmd == NULL) // error detected
3023 return FAIL;
3024 if (lnum == MAXLNUM)
3025 {
3026 if (*eap->cmd == '%') // '%' - all lines
3027 {
3028 ++eap->cmd;
3029 switch (eap->addr_type)
3030 {
3031 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003032 case ADDR_OTHER:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003033 eap->line1 = 1;
3034 eap->line2 = curbuf->b_ml.ml_line_count;
3035 break;
3036 case ADDR_LOADED_BUFFERS:
3037 {
3038 buf_T *buf = firstbuf;
3039
3040 while (buf->b_next != NULL
3041 && buf->b_ml.ml_mfp == NULL)
3042 buf = buf->b_next;
3043 eap->line1 = buf->b_fnum;
3044 buf = lastbuf;
3045 while (buf->b_prev != NULL
3046 && buf->b_ml.ml_mfp == NULL)
3047 buf = buf->b_prev;
3048 eap->line2 = buf->b_fnum;
3049 break;
3050 }
3051 case ADDR_BUFFERS:
3052 eap->line1 = firstbuf->b_fnum;
3053 eap->line2 = lastbuf->b_fnum;
3054 break;
3055 case ADDR_WINDOWS:
3056 case ADDR_TABS:
3057 if (IS_USER_CMDIDX(eap->cmdidx))
3058 {
3059 eap->line1 = 1;
3060 eap->line2 = eap->addr_type == ADDR_WINDOWS
3061 ? LAST_WIN_NR : LAST_TAB_NR;
3062 }
3063 else
3064 {
3065 // there is no Vim command which uses '%' and
3066 // ADDR_WINDOWS or ADDR_TABS
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003067 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003068 return FAIL;
3069 }
3070 break;
3071 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003072 case ADDR_UNSIGNED:
3073 case ADDR_QUICKFIX:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003074 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003075 return FAIL;
3076 case ADDR_ARGUMENTS:
3077 if (ARGCOUNT == 0)
3078 eap->line1 = eap->line2 = 0;
3079 else
3080 {
3081 eap->line1 = 1;
3082 eap->line2 = ARGCOUNT;
3083 }
3084 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003085 case ADDR_QUICKFIX_VALID:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003086#ifdef FEAT_QUICKFIX
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003087 eap->line1 = 1;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003088 eap->line2 = qf_get_valid_size(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003089 if (eap->line2 == 0)
3090 eap->line2 = 1;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003091#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003092 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003093 case ADDR_NONE:
3094 // Will give an error later if a range is found.
3095 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003096 }
3097 ++eap->addr_count;
3098 }
3099 else if (*eap->cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
3100 {
3101 pos_T *fp;
3102
3103 // '*' - visual area
3104 if (eap->addr_type != ADDR_LINES)
3105 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003106 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003107 return FAIL;
3108 }
3109
3110 ++eap->cmd;
3111 if (!eap->skip)
3112 {
3113 fp = getmark('<', FALSE);
3114 if (check_mark(fp) == FAIL)
3115 return FAIL;
3116 eap->line1 = fp->lnum;
3117 fp = getmark('>', FALSE);
3118 if (check_mark(fp) == FAIL)
3119 return FAIL;
3120 eap->line2 = fp->lnum;
3121 ++eap->addr_count;
3122 }
3123 }
3124 }
3125 else
3126 eap->line2 = lnum;
3127 eap->addr_count++;
3128
3129 if (*eap->cmd == ';')
3130 {
3131 if (!eap->skip)
3132 {
3133 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar0e717042020-04-27 19:29:01 +02003134 // Don't leave the cursor on an illegal line or column, but do
3135 // accept zero as address, so 0;/PATTERN/ works correctly.
3136 if (eap->line2 > 0)
3137 check_cursor();
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003138 }
3139 }
3140 else if (*eap->cmd != ',')
3141 break;
3142 ++eap->cmd;
3143 }
3144
3145 // One address given: set start and end lines.
3146 if (eap->addr_count == 1)
3147 {
3148 eap->line1 = eap->line2;
3149 // ... but only implicit: really no address given
3150 if (lnum == MAXLNUM)
3151 eap->addr_count = 0;
3152 }
3153 return OK;
3154}
3155
3156/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003157 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003158 * If there is a match advance "pp" to the argument and return TRUE.
3159 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003160 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003161checkforcmd(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003162 char_u **pp, // start of command
3163 char *cmd, // name of command
3164 int len) // required length
Bram Moolenaar071d4272004-06-13 20:20:40 +00003165{
3166 int i;
3167
3168 for (i = 0; cmd[i] != NUL; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003169 if (((char_u *)cmd)[i] != (*pp)[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003170 break;
3171 if (i >= len && !isalpha((*pp)[i]))
3172 {
3173 *pp = skipwhite(*pp + i);
3174 return TRUE;
3175 }
3176 return FALSE;
3177}
3178
3179/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003180 * Append "cmd" to the error message in IObuff.
3181 * Takes care of limiting the length and handling 0xa0, which would be
3182 * invisible otherwise.
3183 */
3184 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003185append_command(char_u *cmd)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003186{
3187 char_u *s = cmd;
3188 char_u *d;
3189
3190 STRCAT(IObuff, ": ");
3191 d = IObuff + STRLEN(IObuff);
3192 while (*s != NUL && d - IObuff < IOSIZE - 7)
3193 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003194 if (enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) : *s == 0xa0)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003195 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003196 s += enc_utf8 ? 2 : 1;
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003197 STRCPY(d, "<a0>");
3198 d += 4;
3199 }
3200 else
3201 MB_COPY_CHAR(s, d);
3202 }
3203 *d = NUL;
3204}
3205
3206/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003207 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003208 * Start of the name can be found at eap->cmd.
Bram Moolenaar25190db2019-05-04 15:05:28 +02003209 * Sets eap->cmdidx and returns a pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003210 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003211 *
3212 * If "lookup" is not NULL recognize expression without "eval" or "call" and
3213 * assignment without "let". Sets eap->cmdidx to the command while returning
3214 * "eap->cmd".
3215 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003216 * Returns NULL for an ambiguous user command.
3217 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003218 char_u *
3219find_ex_command(
3220 exarg_T *eap,
Bram Moolenaara494f562020-04-18 17:45:38 +02003221 int *full UNUSED,
Bram Moolenaarb84a3812020-05-01 15:44:29 +02003222 void *(*lookup)(char_u *, size_t, cctx_T *) UNUSED,
Bram Moolenaara494f562020-04-18 17:45:38 +02003223 cctx_T *cctx UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003224{
3225 int len;
3226 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003227 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003228
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003229#ifdef FEAT_EVAL
3230 /*
3231 * Recognize a Vim9 script function/method call and assignment:
3232 * "lvar = value", "lvar(arg)", "[1, 2 3]->Func()"
3233 */
Bram Moolenaar0c6ceaf2020-02-22 18:36:32 +01003234 p = eap->cmd;
Bram Moolenaar3d48e252020-07-15 14:15:52 +02003235 if (lookup != NULL && (vim_strchr((char_u *)"{('[", *p) != NULL
3236 || ((p = to_name_const_end(eap->cmd)) > eap->cmd
3237 && *p != NUL)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003238 {
3239 int oplen;
3240 int heredoc;
3241
Bram Moolenaar3d48e252020-07-15 14:15:52 +02003242 if (
3243 // "(..." is an expression.
3244 // "funcname(" is always a function call.
3245 *p == '('
3246 || (p == eap->cmd
3247 ? (
3248 // "{..." is an dict expression.
3249 *eap->cmd == '{'
3250 // "'string'->func()" is an expression.
3251 || *eap->cmd == '\''
3252 // "g:varname" is an expression.
3253 || eap->cmd[1] == ':'
3254 )
3255 : (
3256 // "varname[]" is an expression.
3257 *p == '['
3258 // "varname->func()" is an expression.
3259 || (*p == '-' && p[1] == '>')
3260 // "varname.expr" is an expression.
3261 || (*p == '.' && ASCII_ISALPHA(p[1]))
3262 )))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003263 {
3264 eap->cmdidx = CMD_eval;
3265 return eap->cmd;
3266 }
3267
Bram Moolenaard2ef6b32020-07-02 21:11:34 +02003268 // "[...]->Method()" is a list expression, but "[a, b] = Func()" is
3269 // an assignment.
3270 // If there is no line break inside the "[...]" then "p" is advanced to
3271 // after the "]" by to_name_const_end(): check if a "=" follows.
3272 // If "[...]" has a line break "p" still points at the "[" and it can't
3273 // be an assignment.
3274 if (*eap->cmd == '[' && (p == eap->cmd || *skipwhite(p) != '='))
3275 {
3276 eap->cmdidx = CMD_eval;
3277 return eap->cmd;
3278 }
3279
Bram Moolenaar5f195932020-07-01 20:07:14 +02003280 // Recognize an assignment if we recognize the variable name:
3281 // "g:var = expr"
3282 // "var = expr" where "var" is a local var name.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003283 oplen = assignment_len(skipwhite(p), &heredoc);
3284 if (oplen > 0)
3285 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003286 if (((p - eap->cmd) > 2 && eap->cmd[1] == ':')
Bram Moolenaarb84a3812020-05-01 15:44:29 +02003287 || lookup(eap->cmd, p - eap->cmd, cctx) != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003288 {
3289 eap->cmdidx = CMD_let;
3290 return eap->cmd;
3291 }
3292 }
3293 }
3294#endif
3295
Bram Moolenaar071d4272004-06-13 20:20:40 +00003296 /*
3297 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00003298 * Exceptions:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003299 * - the 'k' command can directly be followed by any character.
3300 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003301 * but :sre[wind] is another command, as are :scr[iptnames],
Bram Moolenaar071d4272004-06-13 20:20:40 +00003302 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00003303 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304 */
3305 p = eap->cmd;
3306 if (*p == 'k')
3307 {
3308 eap->cmdidx = CMD_k;
3309 ++p;
3310 }
3311 else if (p[0] == 's'
Bram Moolenaar204b93f2015-08-04 22:02:51 +02003312 && ((p[1] == 'c' && (p[2] == NUL || (p[2] != 's' && p[2] != 'r'
3313 && (p[3] == NUL || (p[3] != 'i' && p[4] != 'p')))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003314 || p[1] == 'g'
3315 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
3316 || p[1] == 'I'
3317 || (p[1] == 'r' && p[2] != 'e')))
3318 {
3319 eap->cmdidx = CMD_substitute;
3320 ++p;
3321 }
3322 else
3323 {
3324 while (ASCII_ISALPHA(*p))
3325 ++p;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003326 // for python 3.x support ":py3", ":python3", ":py3file", etc.
Bram Moolenaar55d5c032010-07-17 23:52:29 +02003327 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003328 {
Bram Moolenaarb6590522010-07-21 16:00:43 +02003329 while (ASCII_ISALNUM(*p))
3330 ++p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003331 }
3332 else if (*p == '9' && STRNCMP("vim9", eap->cmd, 4) == 0)
3333 {
3334 // include "9" for "vim9script"
3335 ++p;
3336 while (ASCII_ISALPHA(*p))
3337 ++p;
3338 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02003339
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003340 // check for non-alpha command
Bram Moolenaar071d4272004-06-13 20:20:40 +00003341 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3342 ++p;
3343 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003344 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
3345 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003346 // Check for ":dl", ":dell", etc. to ":deletel": that's
3347 // :delete with the 'l' flag. Same for 'p'.
Bram Moolenaardf177f62005-02-22 08:39:57 +00003348 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003349 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00003350 break;
3351 if (i == len - 1)
3352 {
3353 --len;
3354 if (p[-1] == 'l')
3355 eap->flags |= EXFLAG_LIST;
3356 else
3357 eap->flags |= EXFLAG_PRINT;
3358 }
3359 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003361 if (ASCII_ISLOWER(eap->cmd[0]))
3362 {
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003363 int c1 = eap->cmd[0];
Bram Moolenaar94f82cb2019-07-24 22:30:27 +02003364 int c2 = len == 1 ? NUL : eap->cmd[1];
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003365
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003366 if (command_count != (int)CMD_SIZE)
3367 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003368 iemsg(_("E943: Command table needs to be updated, run 'make cmdidxs'"));
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003369 getout(1);
3370 }
3371
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003372 // Use a precomputed index for fast look-up in cmdnames[]
3373 // taking into account the first 2 letters of eap->cmd.
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003374 eap->cmdidx = cmdidxs1[CharOrdLow(c1)];
3375 if (ASCII_ISLOWER(c2))
3376 eap->cmdidx += cmdidxs2[CharOrdLow(c1)][CharOrdLow(c2)];
3377 }
Bram Moolenaara494f562020-04-18 17:45:38 +02003378 else if (ASCII_ISUPPER(eap->cmd[0]))
3379 eap->cmdidx = CMD_Next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380 else
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003381 eap->cmdidx = CMD_bang;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382
3383 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
3384 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
3385 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
3386 (size_t)len) == 0)
3387 {
3388#ifdef FEAT_EVAL
3389 if (full != NULL
3390 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
3391 *full = TRUE;
3392#endif
3393 break;
3394 }
3395
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003396 // Look for a user defined command as a last resort. Let ":Print" be
3397 // overruled by a user defined command.
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01003398 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
3399 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400 {
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003401 // User defined commands may contain digits.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402 while (ASCII_ISALNUM(*p))
3403 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003404 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003406 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407 eap->cmdidx = CMD_SIZE;
3408 }
3409
3410 return p;
3411}
3412
3413#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003414static struct cmdmod
3415{
3416 char *name;
3417 int minlen;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003418 int has_count; // :123verbose :3tab
Bram Moolenaared53fb92007-11-24 20:50:24 +00003419} cmdmods[] = {
3420 {"aboveleft", 3, FALSE},
3421 {"belowright", 3, FALSE},
3422 {"botright", 2, FALSE},
3423 {"browse", 3, FALSE},
3424 {"confirm", 4, FALSE},
Bram Moolenaar7b668e82016-08-23 23:51:21 +02003425 {"filter", 4, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003426 {"hide", 3, FALSE},
3427 {"keepalt", 5, FALSE},
3428 {"keepjumps", 5, FALSE},
3429 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01003430 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003431 {"leftabove", 5, FALSE},
3432 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003433 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003434 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003435 {"rightbelow", 6, FALSE},
3436 {"sandbox", 3, FALSE},
3437 {"silent", 3, FALSE},
3438 {"tab", 3, TRUE},
3439 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003440 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003441 {"verbose", 4, TRUE},
3442 {"vertical", 4, FALSE},
3443};
3444
3445/*
3446 * Return length of a command modifier (including optional count).
3447 * Return zero when it's not a modifier.
3448 */
3449 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003450modifier_len(char_u *cmd)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003451{
3452 int i, j;
3453 char_u *p = cmd;
3454
3455 if (VIM_ISDIGIT(*cmd))
3456 p = skipwhite(skipdigits(cmd));
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003457 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003458 {
3459 for (j = 0; p[j] != NUL; ++j)
3460 if (p[j] != cmdmods[i].name[j])
3461 break;
Bram Moolenaar2d473ab2013-06-12 17:12:24 +02003462 if (!ASCII_ISALPHA(p[j]) && j >= cmdmods[i].minlen
Bram Moolenaared53fb92007-11-24 20:50:24 +00003463 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003464 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003465 }
3466 return 0;
3467}
3468
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469/*
3470 * Return > 0 if an Ex command "name" exists.
3471 * Return 2 if there is an exact match.
3472 * Return 3 if there is an ambiguous match.
3473 */
3474 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003475cmd_exists(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476{
3477 exarg_T ea;
3478 int full = FALSE;
3479 int i;
3480 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003481 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003483 // Check command modifiers.
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003484 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485 {
3486 for (j = 0; name[j] != NUL; ++j)
3487 if (name[j] != cmdmods[i].name[j])
3488 break;
3489 if (name[j] == NUL && j >= cmdmods[i].minlen)
3490 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3491 }
3492
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003493 // Check built-in commands and user defined commands.
3494 // For ":2match" and ":3match" we need to skip the number.
Bram Moolenaara9587612006-05-04 21:47:50 +00003495 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003497 p = find_ex_command(&ea, &full, NULL, NULL);
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003498 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003499 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003500 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3501 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003502 if (*skipwhite(p) != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003503 return 0; // trailing garbage
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3505}
3506#endif
3507
Bram Moolenaard0190392019-08-23 21:17:35 +02003508 cmdidx_T
3509excmd_get_cmdidx(char_u *cmd, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510{
Bram Moolenaard0190392019-08-23 21:17:35 +02003511 cmdidx_T idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512
Bram Moolenaard0190392019-08-23 21:17:35 +02003513 for (idx = (cmdidx_T)0; (int)idx < (int)CMD_SIZE;
3514 idx = (cmdidx_T)((int)idx + 1))
3515 if (STRNCMP(cmdnames[(int)idx].cmd_name, cmd, (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516 break;
3517
Bram Moolenaard0190392019-08-23 21:17:35 +02003518 return idx;
3519}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520
Bram Moolenaard0190392019-08-23 21:17:35 +02003521 long
3522excmd_get_argt(cmdidx_T idx)
3523{
3524 return (long)cmdnames[(int)idx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525}
3526
3527/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003528 * Skip a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00003529 *
3530 * Backslashed delimiters after / or ? will be skipped, and commands will
3531 * not be expanded between /'s and ?'s or after "'".
3532 *
Bram Moolenaardf069ee2020-06-22 23:02:51 +02003533 * Also skip white space and ":" characters after the range.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534 * Returns the "cmd" pointer advanced to beyond the range.
3535 */
3536 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003537skip_range(
3538 char_u *cmd,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003539 int *ctx) // pointer to xp_context or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003541 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01003543 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;\\", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544 {
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01003545 if (*cmd == '\\')
3546 {
3547 if (cmd[1] == '?' || cmd[1] == '/' || cmd[1] == '&')
3548 ++cmd;
3549 else
3550 break;
3551 }
3552 else if (*cmd == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553 {
3554 if (*++cmd == NUL && ctx != NULL)
3555 *ctx = EXPAND_NOTHING;
3556 }
3557 else if (*cmd == '/' || *cmd == '?')
3558 {
3559 delim = *cmd++;
3560 while (*cmd != NUL && *cmd != delim)
3561 if (*cmd++ == '\\' && *cmd != NUL)
3562 ++cmd;
3563 if (*cmd == NUL && ctx != NULL)
3564 *ctx = EXPAND_NOTHING;
3565 }
3566 if (*cmd != NUL)
3567 ++cmd;
3568 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00003569
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003570 // Skip ":" and white space.
Bram Moolenaardf177f62005-02-22 08:39:57 +00003571 while (*cmd == ':')
3572 cmd = skipwhite(cmd + 1);
3573
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574 return cmd;
3575}
3576
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003577 static void
3578addr_error(cmd_addr_T addr_type)
3579{
3580 if (addr_type == ADDR_NONE)
3581 emsg(_(e_norange));
3582 else
3583 emsg(_(e_invrange));
3584}
3585
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586/*
Bram Moolenaar198cb662018-09-06 21:44:17 +02003587 * Get a single EX address.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003588 *
3589 * Set ptr to the next character after the part that was interpreted.
3590 * Set ptr to NULL when an error is encountered.
Bram Moolenaar198cb662018-09-06 21:44:17 +02003591 * This may set the last used search pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003592 *
3593 * Return MAXLNUM when no Ex address was found.
3594 */
3595 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003596get_address(
3597 exarg_T *eap UNUSED,
3598 char_u **ptr,
Bram Moolenaar1b03a192019-12-08 17:08:29 +01003599 cmd_addr_T addr_type,
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003600 int skip, // only skip the address, don't use it
3601 int silent, // no errors or side effects
3602 int to_other_file, // flag: may jump to other file
3603 int address_count UNUSED) // 1 for first address, >1 after comma
Bram Moolenaar071d4272004-06-13 20:20:40 +00003604{
3605 int c;
3606 int i;
3607 long n;
3608 char_u *cmd;
3609 pos_T pos;
3610 pos_T *fp;
3611 linenr_T lnum;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003612 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613
3614 cmd = skipwhite(*ptr);
3615 lnum = MAXLNUM;
3616 do
3617 {
3618 switch (*cmd)
3619 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003620 case '.': // '.' - Cursor position
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003621 ++cmd;
3622 switch (addr_type)
3623 {
3624 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003625 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626 lnum = curwin->w_cursor.lnum;
3627 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003628 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003629 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003630 break;
3631 case ADDR_ARGUMENTS:
3632 lnum = curwin->w_arg_idx + 1;
3633 break;
3634 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003635 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003636 lnum = curbuf->b_fnum;
3637 break;
3638 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003639 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003640 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003641 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003642 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003643 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003644 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003645 cmd = NULL;
3646 goto error;
3647 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003648 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003649#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02003650 lnum = qf_get_cur_idx(eap);
3651#endif
3652 break;
3653 case ADDR_QUICKFIX_VALID:
3654#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003655 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02003656#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003657 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003658 }
3659 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003660
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003661 case '$': // '$' - last line
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003662 ++cmd;
3663 switch (addr_type)
3664 {
3665 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003666 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667 lnum = curbuf->b_ml.ml_line_count;
3668 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003669 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003670 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003671 break;
3672 case ADDR_ARGUMENTS:
3673 lnum = ARGCOUNT;
3674 break;
3675 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003676 buf = lastbuf;
3677 while (buf->b_ml.ml_mfp == NULL)
3678 {
3679 if (buf->b_prev == NULL)
3680 break;
3681 buf = buf->b_prev;
3682 }
3683 lnum = buf->b_fnum;
3684 break;
3685 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003686 lnum = lastbuf->b_fnum;
3687 break;
3688 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003689 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003690 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003691 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003692 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003693 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003694 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003695 cmd = NULL;
3696 goto error;
3697 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003698 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003699#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003700 lnum = qf_get_size(eap);
3701 if (lnum == 0)
3702 lnum = 1;
Bram Moolenaare906c502015-09-09 21:10:39 +02003703#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003704 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003705 case ADDR_QUICKFIX_VALID:
3706#ifdef FEAT_QUICKFIX
3707 lnum = qf_get_valid_size(eap);
3708 if (lnum == 0)
3709 lnum = 1;
3710#endif
3711 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003712 }
3713 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003714
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003715 case '\'': // ''' - mark
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003716 if (*++cmd == NUL)
3717 {
3718 cmd = NULL;
3719 goto error;
3720 }
3721 if (addr_type != ADDR_LINES)
3722 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003723 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003724 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003725 goto error;
3726 }
3727 if (skip)
3728 ++cmd;
3729 else
3730 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003731 // Only accept a mark in another file when it is
3732 // used by itself: ":'M".
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003733 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
3734 ++cmd;
3735 if (fp == (pos_T *)-1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003736 // Jumped to another file.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003737 lnum = curwin->w_cursor.lnum;
3738 else
3739 {
3740 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003741 {
3742 cmd = NULL;
3743 goto error;
3744 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003745 lnum = fp->lnum;
3746 }
3747 }
3748 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003749
3750 case '/':
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003751 case '?': // '/' or '?' - search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003752 c = *cmd++;
3753 if (addr_type != ADDR_LINES)
3754 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003755 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003756 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003757 goto error;
3758 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003759 if (skip) // skip "/pat/"
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003760 {
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02003761 cmd = skip_regexp(cmd, c, (int)p_magic);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003762 if (*cmd == c)
3763 ++cmd;
3764 }
3765 else
3766 {
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003767 int flags;
3768
3769 pos = curwin->w_cursor; // save curwin->w_cursor
3770
3771 // When '/' or '?' follows another address, start from
3772 // there.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003773 if (lnum != MAXLNUM)
3774 curwin->w_cursor.lnum = lnum;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003775
3776 // Start a forward search at the end of the line (unless
3777 // before the first line).
3778 // Start a backward search at the start of the line.
3779 // This makes sure we never match in the current
3780 // line, and can match anywhere in the
3781 // next/previous line.
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01003782 if (c == '/' && curwin->w_cursor.lnum > 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003783 curwin->w_cursor.col = MAXCOL;
3784 else
3785 curwin->w_cursor.col = 0;
3786 searchcmdlen = 0;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003787 flags = silent ? 0 : SEARCH_HIS | SEARCH_MSG;
Bram Moolenaarc036e872020-02-21 21:30:52 +01003788 if (!do_search(NULL, c, c, cmd, 1L, flags, NULL))
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003789 {
3790 curwin->w_cursor = pos;
3791 cmd = NULL;
3792 goto error;
3793 }
3794 lnum = curwin->w_cursor.lnum;
3795 curwin->w_cursor = pos;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003796 // adjust command string pointer
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003797 cmd += searchcmdlen;
3798 }
3799 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003800
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003801 case '\\': // "\?", "\/" or "\&", repeat search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003802 ++cmd;
3803 if (addr_type != ADDR_LINES)
3804 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003805 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003806 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003807 goto error;
3808 }
3809 if (*cmd == '&')
3810 i = RE_SUBST;
3811 else if (*cmd == '?' || *cmd == '/')
3812 i = RE_SEARCH;
3813 else
3814 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003815 emsg(_(e_backslash));
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003816 cmd = NULL;
3817 goto error;
3818 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003819
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003820 if (!skip)
3821 {
3822 /*
3823 * When search follows another address, start from
3824 * there.
3825 */
3826 if (lnum != MAXLNUM)
3827 pos.lnum = lnum;
3828 else
3829 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003831 /*
3832 * Start the search just like for the above
3833 * do_search().
3834 */
3835 if (*cmd != '?')
3836 pos.col = MAXCOL;
3837 else
3838 pos.col = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02003839 pos.coladd = 0;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01003840 if (searchit(curwin, curbuf, &pos, NULL,
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003841 *cmd == '?' ? BACKWARD : FORWARD,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02003842 (char_u *)"", 1L, SEARCH_MSG, i, NULL) != FAIL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003843 lnum = pos.lnum;
3844 else
3845 {
3846 cmd = NULL;
3847 goto error;
3848 }
3849 }
3850 ++cmd;
3851 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003852
3853 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003854 if (VIM_ISDIGIT(*cmd)) // absolute line number
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003855 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003856 }
3857
3858 for (;;)
3859 {
3860 cmd = skipwhite(cmd);
3861 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
3862 break;
3863
3864 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003865 {
3866 switch (addr_type)
3867 {
3868 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003869 case ADDR_OTHER:
3870 // "+1" is same as ".+1"
Bram Moolenaarf240e182014-11-27 18:33:02 +01003871 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003872 break;
3873 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003874 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003875 break;
3876 case ADDR_ARGUMENTS:
3877 lnum = curwin->w_arg_idx + 1;
3878 break;
3879 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003880 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003881 lnum = curbuf->b_fnum;
3882 break;
3883 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003884 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003885 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003886 case ADDR_TABS_RELATIVE:
3887 lnum = 1;
3888 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003889 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003890#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02003891 lnum = qf_get_cur_idx(eap);
3892#endif
3893 break;
3894 case ADDR_QUICKFIX_VALID:
3895#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003896 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02003897#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003898 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003899 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003900 case ADDR_UNSIGNED:
3901 lnum = 0;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003902 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003903 }
3904 }
3905
Bram Moolenaar071d4272004-06-13 20:20:40 +00003906 if (VIM_ISDIGIT(*cmd))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003907 i = '+'; // "number" is same as "+number"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003908 else
3909 i = *cmd++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003910 if (!VIM_ISDIGIT(*cmd)) // '+' is '+1', but '+0' is not '+1'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003911 n = 1;
3912 else
3913 n = getdigits(&cmd);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003914
3915 if (addr_type == ADDR_TABS_RELATIVE)
3916 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003917 emsg(_(e_invrange));
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003918 cmd = NULL;
3919 goto error;
3920 }
3921 else if (addr_type == ADDR_LOADED_BUFFERS
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003922 || addr_type == ADDR_BUFFERS)
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003923 lnum = compute_buffer_local_count(
3924 addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003925 else
Bram Moolenaarded27822017-01-02 14:27:34 +01003926 {
3927#ifdef FEAT_FOLDING
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003928 // Relative line addressing, need to adjust for folded lines
3929 // now, but only do it after the first address.
Bram Moolenaarded27822017-01-02 14:27:34 +01003930 if (addr_type == ADDR_LINES && (i == '-' || i == '+')
3931 && address_count >= 2)
3932 (void)hasFolding(lnum, NULL, &lnum);
3933#endif
3934 if (i == '-')
3935 lnum -= n;
3936 else
3937 lnum += n;
3938 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003939 }
3940 } while (*cmd == '/' || *cmd == '?');
3941
3942error:
3943 *ptr = cmd;
3944 return lnum;
3945}
3946
3947/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00003948 * Get flags from an Ex command argument.
3949 */
3950 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003951get_flags(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00003952{
3953 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
3954 {
3955 if (*eap->arg == 'l')
3956 eap->flags |= EXFLAG_LIST;
3957 else if (*eap->arg == 'p')
3958 eap->flags |= EXFLAG_PRINT;
3959 else
3960 eap->flags |= EXFLAG_NR;
3961 eap->arg = skipwhite(eap->arg + 1);
3962 }
3963}
3964
3965/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003966 * Function called for command which is Not Implemented. NI!
3967 */
3968 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003969ex_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003970{
3971 if (!eap->skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003972 eap->errmsg = N_("E319: Sorry, the command is not available in this version");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003973}
3974
Bram Moolenaar7bb75552007-07-16 18:39:49 +00003975#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003976/*
3977 * Function called for script command which is Not Implemented. NI!
3978 * Skips over ":perl <<EOF" constructs.
3979 */
3980 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003981ex_script_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003982{
3983 if (!eap->skip)
3984 ex_ni(eap);
3985 else
3986 vim_free(script_get(eap, eap->arg));
3987}
3988#endif
3989
3990/*
3991 * Check range in Ex command for validity.
3992 * Return NULL when valid, error message when invalid.
3993 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003994 static char *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003995invalid_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003996{
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003997 buf_T *buf;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003998
Bram Moolenaar071d4272004-06-13 20:20:40 +00003999 if ( eap->line1 < 0
4000 || eap->line2 < 0
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004001 || eap->line1 > eap->line2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004002 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004003
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004004 if (eap->argt & EX_RANGE)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004005 {
Bram Moolenaarb7316892019-05-01 18:08:42 +02004006 switch (eap->addr_type)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004007 {
4008 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004009 if (eap->line2 > curbuf->b_ml.ml_line_count
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004010#ifdef FEAT_DIFF
4011 + (eap->cmdidx == CMD_diffget)
4012#endif
4013 )
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004014 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004015 break;
4016 case ADDR_ARGUMENTS:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004017 // add 1 if ARGCOUNT is 0
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004018 if (eap->line2 > ARGCOUNT + (!ARGCOUNT))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004019 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004020 break;
4021 case ADDR_BUFFERS:
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02004022 // Only a boundary check, not whether the buffers actually
4023 // exist.
4024 if (eap->line1 < 1 || eap->line2 > get_highest_fnum())
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004025 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004026 break;
4027 case ADDR_LOADED_BUFFERS:
4028 buf = firstbuf;
4029 while (buf->b_ml.ml_mfp == NULL)
4030 {
4031 if (buf->b_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004032 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004033 buf = buf->b_next;
4034 }
4035 if (eap->line1 < buf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004036 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004037 buf = lastbuf;
4038 while (buf->b_ml.ml_mfp == NULL)
4039 {
4040 if (buf->b_prev == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004041 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004042 buf = buf->b_prev;
4043 }
4044 if (eap->line2 > buf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004045 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004046 break;
4047 case ADDR_WINDOWS:
Bram Moolenaar8be63882015-01-14 11:25:05 +01004048 if (eap->line2 > LAST_WIN_NR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004049 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004050 break;
4051 case ADDR_TABS:
4052 if (eap->line2 > LAST_TAB_NR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004053 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004054 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004055 case ADDR_TABS_RELATIVE:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004056 case ADDR_OTHER:
4057 // Any range is OK.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004058 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004059 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004060#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004061 // No error for value that is too big, will use the last entry.
4062 if (eap->line2 <= 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004063 return _(e_invrange);
Bram Moolenaare906c502015-09-09 21:10:39 +02004064#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004065 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004066 case ADDR_QUICKFIX_VALID:
4067#ifdef FEAT_QUICKFIX
4068 if ((eap->line2 != 1 && eap->line2 > qf_get_valid_size(eap))
4069 || eap->line2 < 0)
4070 return _(e_invrange);
4071#endif
4072 break;
4073 case ADDR_UNSIGNED:
4074 if (eap->line2 < 0)
4075 return _(e_invrange);
4076 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004077 case ADDR_NONE:
4078 // Will give an error elsewhere.
4079 break;
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004080 }
4081 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082 return NULL;
4083}
4084
4085/*
4086 * Correct the range for zero line number, if required.
4087 */
4088 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004089correct_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004090{
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004091 if (!(eap->argt & EX_ZEROR)) // zero in range not allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092 {
4093 if (eap->line1 == 0)
4094 eap->line1 = 1;
4095 if (eap->line2 == 0)
4096 eap->line2 = 1;
4097 }
4098}
4099
Bram Moolenaar748bf032005-02-02 23:04:36 +00004100#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004101/*
4102 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4103 * pattern. Otherwise return eap->arg.
4104 */
4105 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004106skip_grep_pat(exarg_T *eap)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004107{
4108 char_u *p = eap->arg;
4109
Bram Moolenaara37420f2006-02-04 22:37:47 +00004110 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4111 || eap->cmdidx == CMD_vimgrepadd
4112 || eap->cmdidx == CMD_lvimgrepadd
4113 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004114 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004115 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004116 if (p == NULL)
4117 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004118 }
4119 return p;
4120}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004121
4122/*
4123 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4124 * in the command line, so that things like % get expanded.
4125 */
4126 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004127replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004128{
4129 char_u *new_cmdline;
4130 char_u *program;
4131 char_u *pos;
4132 char_u *ptr;
4133 int len;
4134 int i;
4135
4136 /*
4137 * Don't do it when ":vimgrep" is used for ":grep".
4138 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004139 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4140 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4141 || eap->cmdidx == CMD_grepadd
4142 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004143 && !grep_internal(eap->cmdidx))
4144 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004145 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4146 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004147 {
4148 if (*curbuf->b_p_gp == NUL)
4149 program = p_gp;
4150 else
4151 program = curbuf->b_p_gp;
4152 }
4153 else
4154 {
4155 if (*curbuf->b_p_mp == NUL)
4156 program = p_mp;
4157 else
4158 program = curbuf->b_p_mp;
4159 }
4160
4161 p = skipwhite(p);
4162
4163 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4164 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004165 // replace $* by given arguments
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004166 i = 1;
4167 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4168 ++i;
4169 len = (int)STRLEN(p);
Bram Moolenaar51e14382019-05-25 20:21:28 +02004170 new_cmdline = alloc(STRLEN(program) + i * (len - 2) + 1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004171 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004172 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004173 ptr = new_cmdline;
4174 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4175 {
4176 i = (int)(pos - program);
4177 STRNCPY(ptr, program, i);
4178 STRCPY(ptr += i, p);
4179 ptr += len;
4180 program = pos + 2;
4181 }
4182 STRCPY(ptr, program);
4183 }
4184 else
4185 {
Bram Moolenaar51e14382019-05-25 20:21:28 +02004186 new_cmdline = alloc(STRLEN(program) + STRLEN(p) + 2);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004187 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004188 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004189 STRCPY(new_cmdline, program);
4190 STRCAT(new_cmdline, " ");
4191 STRCAT(new_cmdline, p);
4192 }
4193 msg_make(p);
4194
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004195 // 'eap->cmd' is not set here, because it is not used at CMD_make
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004196 vim_free(*cmdlinep);
4197 *cmdlinep = new_cmdline;
4198 p = new_cmdline;
4199 }
4200 return p;
4201}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004202#endif
4203
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204/*
4205 * Expand file name in Ex command argument.
Bram Moolenaar0abb4272019-06-15 16:06:00 +02004206 * When an error is detected, "errormsgp" is set to a non-NULL pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004207 * Return FAIL for failure, OK otherwise.
4208 */
4209 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004210expand_filename(
4211 exarg_T *eap,
4212 char_u **cmdlinep,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004213 char **errormsgp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004215 int has_wildcards; // need to expand wildcards
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216 char_u *repl;
4217 int srclen;
4218 char_u *p;
4219 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004220 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221
Bram Moolenaar748bf032005-02-02 23:04:36 +00004222#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004223 // Skip a regexp pattern for ":vimgrep[add] pat file..."
Bram Moolenaar748bf032005-02-02 23:04:36 +00004224 p = skip_grep_pat(eap);
4225#else
4226 p = eap->arg;
4227#endif
4228
Bram Moolenaar071d4272004-06-13 20:20:40 +00004229 /*
4230 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4231 * the file name contains a wildcard it should not cause expanding.
4232 * (it will be expanded anyway if there is a wildcard before replacing).
4233 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004234 has_wildcards = mch_has_wildcard(p);
4235 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004236 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004237#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004238 // Skip over `=expr`, wildcards in it are not expanded.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004239 if (p[0] == '`' && p[1] == '=')
4240 {
4241 p += 2;
4242 (void)skip_expr(&p);
4243 if (*p == '`')
4244 ++p;
4245 continue;
4246 }
4247#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248 /*
4249 * Quick check if this cannot be the start of a special string.
4250 * Also removes backslash before '%', '#' and '<'.
4251 */
4252 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4253 {
4254 ++p;
4255 continue;
4256 }
4257
4258 /*
4259 * Try to find a match at this position.
4260 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00004261 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
4262 errormsgp, &escaped);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004263 if (*errormsgp != NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00004264 return FAIL;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004265 if (repl == NULL) // no match found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004266 {
4267 p += srclen;
4268 continue;
4269 }
4270
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004271 // Wildcards won't be expanded below, the replacement is taken
4272 // literally. But do expand "~/file", "~user/file" and "$HOME/file".
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004273 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4274 {
4275 char_u *l = repl;
4276
4277 repl = expand_env_save(repl);
4278 vim_free(l);
4279 }
4280
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004281 // Need to escape white space et al. with a backslash.
4282 // Don't do this for:
4283 // - replacement that already has been escaped: "##"
4284 // - shell commands (may have to use quotes instead).
4285 // - non-unix systems when there is a single argument (spaces don't
4286 // separate arguments then).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004287 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00004288 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00004289 && eap->cmdidx != CMD_bang
Bram Moolenaar071d4272004-06-13 20:20:40 +00004290 && eap->cmdidx != CMD_grep
4291 && eap->cmdidx != CMD_grepadd
Bram Moolenaarbf15b8d2017-06-04 20:43:48 +02004292 && eap->cmdidx != CMD_hardcopy
Bram Moolenaar67883b42017-07-27 22:57:00 +02004293 && eap->cmdidx != CMD_lgrep
4294 && eap->cmdidx != CMD_lgrepadd
4295 && eap->cmdidx != CMD_lmake
4296 && eap->cmdidx != CMD_make
4297 && eap->cmdidx != CMD_terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00004298#ifndef UNIX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004299 && !(eap->argt & EX_NOSPC)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004300#endif
4301 )
4302 {
4303 char_u *l;
4304#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004305 // Don't escape a backslash here, because rem_backslash() doesn't
4306 // remove it later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307 static char_u *nobslash = (char_u *)" \t\"|";
4308# define ESCAPE_CHARS nobslash
4309#else
4310# define ESCAPE_CHARS escape_chars
4311#endif
4312
4313 for (l = repl; *l; ++l)
4314 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
4315 {
4316 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
4317 if (l != NULL)
4318 {
4319 vim_free(repl);
4320 repl = l;
4321 }
4322 break;
4323 }
4324 }
4325
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004326 // For a shell command a '!' must be escaped.
Bram Moolenaar67883b42017-07-27 22:57:00 +02004327 if ((eap->usefilter || eap->cmdidx == CMD_bang
4328 || eap->cmdidx == CMD_terminal)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004329 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004330 {
4331 char_u *l;
4332
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004333 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004334 if (l != NULL)
4335 {
4336 vim_free(repl);
4337 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004338 }
4339 }
4340
4341 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
4342 vim_free(repl);
4343 if (p == NULL)
4344 return FAIL;
4345 }
4346
4347 /*
4348 * One file argument: Expand wildcards.
4349 * Don't do this with ":r !command" or ":w !command".
4350 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004351 if ((eap->argt & EX_NOSPC) && !eap->usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004352 {
4353 /*
4354 * May do this twice:
4355 * 1. Replace environment variables.
4356 * 2. Replace any other wildcards, remove backslashes.
4357 */
4358 for (n = 1; n <= 2; ++n)
4359 {
4360 if (n == 2)
4361 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362 /*
4363 * Halve the number of backslashes (this is Vi compatible).
4364 * For Unix and OS/2, when wildcards are expanded, this is
4365 * done by ExpandOne() below.
4366 */
Bram Moolenaare7fedb62015-12-31 19:07:19 +01004367#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004368 if (!has_wildcards)
4369#endif
4370 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004371 }
4372
4373 if (has_wildcards)
4374 {
4375 if (n == 1)
4376 {
4377 /*
4378 * First loop: May expand environment variables. This
4379 * can be done much faster with expand_env() than with
4380 * something else (e.g., calling a shell).
4381 * After expanding environment variables, check again
4382 * if there are still wildcards present.
4383 */
4384 if (vim_strchr(eap->arg, '$') != NULL
4385 || vim_strchr(eap->arg, '~') != NULL)
4386 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00004387 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004388 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004389 has_wildcards = mch_has_wildcard(NameBuff);
4390 p = NameBuff;
4391 }
4392 else
4393 p = NULL;
4394 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004395 else // n == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004396 {
4397 expand_T xpc;
Bram Moolenaarb40c2572019-10-19 21:01:05 +02004398 int options = WILD_LIST_NOTFOUND
4399 | WILD_NOERROR | WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004400
4401 ExpandInit(&xpc);
4402 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004403 if (p_wic)
4404 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01004406 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004407 if (p == NULL)
4408 return FAIL;
4409 }
4410 if (p != NULL)
4411 {
4412 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
4413 p, cmdlinep);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004414 if (n == 2) // p came from ExpandOne()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004415 vim_free(p);
4416 }
4417 }
4418 }
4419 }
4420 return OK;
4421}
4422
4423/*
4424 * Replace part of the command line, keeping eap->cmd, eap->arg and
4425 * eap->nextcmd correct.
4426 * "src" points to the part that is to be replaced, of length "srclen".
4427 * "repl" is the replacement string.
4428 * Returns a pointer to the character after the replaced string.
4429 * Returns NULL for failure.
4430 */
4431 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004432repl_cmdline(
4433 exarg_T *eap,
4434 char_u *src,
4435 int srclen,
4436 char_u *repl,
4437 char_u **cmdlinep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004438{
4439 int len;
4440 int i;
4441 char_u *new_cmdline;
4442
4443 /*
4444 * The new command line is build in new_cmdline[].
4445 * First allocate it.
4446 * Careful: a "+cmd" argument may have been NUL terminated.
4447 */
4448 len = (int)STRLEN(repl);
4449 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004450 if (eap->nextcmd != NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004451 i += (int)STRLEN(eap->nextcmd);// add space for next command
Bram Moolenaar964b3742019-05-24 18:54:09 +02004452 if ((new_cmdline = alloc(i)) == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004453 return NULL; // out of memory!
Bram Moolenaar071d4272004-06-13 20:20:40 +00004454
4455 /*
4456 * Copy the stuff before the expanded part.
4457 * Copy the expanded stuff.
4458 * Copy what came after the expanded part.
4459 * Copy the next commands, if there are any.
4460 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004461 i = (int)(src - *cmdlinep); // length of part before match
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00004463
Bram Moolenaar071d4272004-06-13 20:20:40 +00004464 mch_memmove(new_cmdline + i, repl, (size_t)len);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004465 i += len; // remember the end of the string
Bram Moolenaar071d4272004-06-13 20:20:40 +00004466 STRCPY(new_cmdline + i, src + srclen);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004467 src = new_cmdline + i; // remember where to continue
Bram Moolenaar071d4272004-06-13 20:20:40 +00004468
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004469 if (eap->nextcmd != NULL) // append next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470 {
4471 i = (int)STRLEN(new_cmdline) + 1;
4472 STRCPY(new_cmdline + i, eap->nextcmd);
4473 eap->nextcmd = new_cmdline + i;
4474 }
4475 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
4476 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
4477 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
4478 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
4479 vim_free(*cmdlinep);
4480 *cmdlinep = new_cmdline;
4481
4482 return src;
4483}
4484
4485/*
4486 * Check for '|' to separate commands and '"' to start comments.
4487 */
4488 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004489separate_nextcmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004490{
4491 char_u *p;
4492
Bram Moolenaar86b68352004-12-27 21:59:20 +00004493#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004494 p = skip_grep_pat(eap);
4495#else
4496 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004497#endif
4498
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004499 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004500 {
4501 if (*p == Ctrl_V)
4502 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004503 if (eap->argt & (EX_CTRLV | EX_XFILE))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004504 ++p; // skip CTRL-V and next char
Bram Moolenaar071d4272004-06-13 20:20:40 +00004505 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004506 // remove CTRL-V and skip next char
Bram Moolenaara7241f52008-06-24 20:39:31 +00004507 STRMOVE(p, p + 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004508 if (*p == NUL) // stop at NUL after CTRL-V
Bram Moolenaar071d4272004-06-13 20:20:40 +00004509 break;
4510 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004511
4512#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004513 // Skip over `=expr` when wildcards are expanded.
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004514 else if (p[0] == '`' && p[1] == '=' && (eap->argt & EX_XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004515 {
4516 p += 2;
4517 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004518 }
4519#endif
4520
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004521 // Check for '"': start of comment or '|': next command
4522 // :@" and :*" do not start a comment!
4523 // :redir @" doesn't either.
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004524 else if ((*p == '"' && !(eap->argt & EX_NOTRLCOM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
4526 || p != eap->arg)
4527 && (eap->cmdidx != CMD_redir
4528 || p != eap->arg + 1 || p[-1] != '@'))
Bram Moolenaara26b9702020-04-18 19:53:28 +02004529#ifdef FEAT_EVAL
4530 || (*p == '#' && in_vim9script()
4531 && p[1] != '{' && p > eap->cmd && VIM_ISWHITE(p[-1]))
4532#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004533 || *p == '|' || *p == '\n')
4534 {
4535 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004536 * We remove the '\' before the '|', unless EX_CTRLV is used
Bram Moolenaar071d4272004-06-13 20:20:40 +00004537 * AND 'b' is present in 'cpoptions'.
4538 */
4539 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004540 || !(eap->argt & EX_CTRLV)) && *(p - 1) == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004541 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004542 STRMOVE(p - 1, p); // remove the '\'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004543 --p;
4544 }
4545 else
4546 {
4547 eap->nextcmd = check_nextcmd(p);
4548 *p = NUL;
4549 break;
4550 }
4551 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004552 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004553
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004554 if (!(eap->argt & EX_NOTRLCOM)) // remove trailing spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00004555 del_trailing_spaces(eap->arg);
4556}
4557
4558/*
4559 * get + command from ex argument
4560 */
4561 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004562getargcmd(char_u **argp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004563{
4564 char_u *arg = *argp;
4565 char_u *command = NULL;
4566
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004567 if (*arg == '+') // +[command]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004568 {
4569 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02004570 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004571 command = dollar_command;
4572 else
4573 {
4574 command = arg;
4575 arg = skip_cmd_arg(command, TRUE);
4576 if (*arg != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004577 *arg++ = NUL; // terminate command with NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004578 }
4579
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004580 arg = skipwhite(arg); // skip over spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00004581 *argp = arg;
4582 }
4583 return command;
4584}
4585
4586/*
4587 * Find end of "+command" argument. Skip over "\ " and "\\".
4588 */
Bram Moolenaard0190392019-08-23 21:17:35 +02004589 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004590skip_cmd_arg(
4591 char_u *p,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004592 int rembs) // TRUE to halve the number of backslashes
Bram Moolenaar071d4272004-06-13 20:20:40 +00004593{
4594 while (*p && !vim_isspace(*p))
4595 {
4596 if (*p == '\\' && p[1] != NUL)
4597 {
4598 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00004599 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004600 else
4601 ++p;
4602 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004603 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004604 }
4605 return p;
4606}
4607
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004608 int
4609get_bad_opt(char_u *p, exarg_T *eap)
4610{
4611 if (STRICMP(p, "keep") == 0)
4612 eap->bad_char = BAD_KEEP;
4613 else if (STRICMP(p, "drop") == 0)
4614 eap->bad_char = BAD_DROP;
4615 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
4616 eap->bad_char = *p;
Bram Moolenaar75808492018-06-12 12:39:41 +02004617 else
4618 return FAIL;
4619 return OK;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004620}
4621
Bram Moolenaar071d4272004-06-13 20:20:40 +00004622/*
4623 * Get "++opt=arg" argument.
4624 * Return FAIL or OK.
4625 */
4626 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004627getargopt(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004628{
4629 char_u *arg = eap->arg + 2;
4630 int *pp = NULL;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004631 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004632 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004633
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004634 // ":edit ++[no]bin[ary] file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004635 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
4636 {
4637 if (*arg == 'n')
4638 {
4639 arg += 2;
4640 eap->force_bin = FORCE_NOBIN;
4641 }
4642 else
4643 eap->force_bin = FORCE_BIN;
4644 if (!checkforcmd(&arg, "binary", 3))
4645 return FAIL;
4646 eap->arg = skipwhite(arg);
4647 return OK;
4648 }
4649
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004650 // ":read ++edit file"
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004651 if (STRNCMP(arg, "edit", 4) == 0)
4652 {
4653 eap->read_edit = TRUE;
4654 eap->arg = skipwhite(arg + 4);
4655 return OK;
4656 }
4657
Bram Moolenaar071d4272004-06-13 20:20:40 +00004658 if (STRNCMP(arg, "ff", 2) == 0)
4659 {
4660 arg += 2;
4661 pp = &eap->force_ff;
4662 }
4663 else if (STRNCMP(arg, "fileformat", 10) == 0)
4664 {
4665 arg += 10;
4666 pp = &eap->force_ff;
4667 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004668 else if (STRNCMP(arg, "enc", 3) == 0)
4669 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01004670 if (STRNCMP(arg, "encoding", 8) == 0)
4671 arg += 8;
4672 else
4673 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004674 pp = &eap->force_enc;
4675 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004676 else if (STRNCMP(arg, "bad", 3) == 0)
4677 {
4678 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004679 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004680 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004681
4682 if (pp == NULL || *arg != '=')
4683 return FAIL;
4684
4685 ++arg;
4686 *pp = (int)(arg - eap->cmd);
4687 arg = skip_cmd_arg(arg, FALSE);
4688 eap->arg = skipwhite(arg);
4689 *arg = NUL;
4690
Bram Moolenaar071d4272004-06-13 20:20:40 +00004691 if (pp == &eap->force_ff)
4692 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004693 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
4694 return FAIL;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004695 eap->force_ff = eap->cmd[eap->force_ff];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004696 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004697 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004698 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004699 // Make 'fileencoding' lower case.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004700 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
4701 *p = TOLOWER_ASC(*p);
4702 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004703 else
4704 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004705 // Check ++bad= argument. Must be a single-byte character, "keep" or
4706 // "drop".
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004707 if (get_bad_opt(eap->cmd + bad_char_idx, eap) == FAIL)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004708 return FAIL;
4709 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004710
4711 return OK;
4712}
4713
Bram Moolenaar071d4272004-06-13 20:20:40 +00004714 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004715ex_autocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004716{
4717 /*
Bram Moolenaar26d4b892018-07-04 22:26:28 +02004718 * Disallow autocommands from .exrc and .vimrc in current
Bram Moolenaar071d4272004-06-13 20:20:40 +00004719 * directory for security reasons.
4720 */
4721 if (secure)
4722 {
4723 secure = 2;
4724 eap->errmsg = e_curdir;
4725 }
4726 else if (eap->cmdidx == CMD_autocmd)
4727 do_autocmd(eap->arg, eap->forceit);
4728 else
4729 do_augroup(eap->arg, eap->forceit);
4730}
4731
4732/*
4733 * ":doautocmd": Apply the automatic commands to the current buffer.
4734 */
4735 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004736ex_doautocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004737{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01004738 char_u *arg = eap->arg;
4739 int call_do_modelines = check_nomodeline(&arg);
Bram Moolenaar1610d052016-06-09 22:53:01 +02004740 int did_aucmd;
Bram Moolenaar60542ac2012-02-12 20:14:01 +01004741
Bram Moolenaar1610d052016-06-09 22:53:01 +02004742 (void)do_doautocmd(arg, TRUE, &did_aucmd);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004743 // Only when there is no <nomodeline>.
Bram Moolenaar1610d052016-06-09 22:53:01 +02004744 if (call_do_modelines && did_aucmd)
Bram Moolenaar60542ac2012-02-12 20:14:01 +01004745 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004746}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004747
Bram Moolenaar071d4272004-06-13 20:20:40 +00004748/*
4749 * :[N]bunload[!] [N] [bufname] unload buffer
4750 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
4751 * :[N]bwipeout[!] [N] [bufname] delete buffer really
4752 */
4753 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004754ex_bunload(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004756 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02004757 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004758 eap->errmsg = do_bufdel(
4759 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
4760 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
4761 : DOBUF_UNLOAD, eap->arg,
4762 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
4763}
4764
4765/*
4766 * :[N]buffer [N] to buffer N
4767 * :[N]sbuffer [N] to buffer N
4768 */
4769 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004770ex_buffer(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004771{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004772 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02004773 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004774 if (*eap->arg)
4775 eap->errmsg = e_trailing;
4776 else
4777 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004778 if (eap->addr_count == 0) // default is current buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00004779 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
4780 else
4781 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004782 if (eap->do_ecmd_cmd != NULL)
4783 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004784 }
4785}
4786
4787/*
4788 * :[N]bmodified [N] to next mod. buffer
4789 * :[N]sbmodified [N] to next mod. buffer
4790 */
4791 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004792ex_bmodified(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004793{
4794 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004795 if (eap->do_ecmd_cmd != NULL)
4796 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004797}
4798
4799/*
4800 * :[N]bnext [N] to next buffer
4801 * :[N]sbnext [N] split and to next buffer
4802 */
4803 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004804ex_bnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004805{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004806 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01004807 return;
4808
Bram Moolenaar071d4272004-06-13 20:20:40 +00004809 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004810 if (eap->do_ecmd_cmd != NULL)
4811 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004812}
4813
4814/*
4815 * :[N]bNext [N] to previous buffer
4816 * :[N]bprevious [N] to previous buffer
4817 * :[N]sbNext [N] split and to previous buffer
4818 * :[N]sbprevious [N] split and to previous buffer
4819 */
4820 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004821ex_bprevious(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004822{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004823 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01004824 return;
4825
Bram Moolenaar071d4272004-06-13 20:20:40 +00004826 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004827 if (eap->do_ecmd_cmd != NULL)
4828 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004829}
4830
4831/*
4832 * :brewind to first buffer
4833 * :bfirst to first buffer
4834 * :sbrewind split and to first buffer
4835 * :sbfirst split and to first buffer
4836 */
4837 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004838ex_brewind(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004839{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004840 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01004841 return;
4842
Bram Moolenaar071d4272004-06-13 20:20:40 +00004843 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004844 if (eap->do_ecmd_cmd != NULL)
4845 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004846}
4847
4848/*
4849 * :blast to last buffer
4850 * :sblast split and to last buffer
4851 */
4852 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004853ex_blast(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004854{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004855 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01004856 return;
4857
Bram Moolenaar071d4272004-06-13 20:20:40 +00004858 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004859 if (eap->do_ecmd_cmd != NULL)
4860 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004862
Bram Moolenaar7a092242020-04-16 22:10:49 +02004863/*
4864 * Check if "c" ends an Ex command.
Bram Moolenaara494f562020-04-18 17:45:38 +02004865 * In Vim9 script does not check for white space before # or #{.
Bram Moolenaar7a092242020-04-16 22:10:49 +02004866 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004867 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004868ends_excmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004869{
Bram Moolenaarfbda69b2020-04-13 15:06:53 +02004870#ifdef FEAT_EVAL
Bram Moolenaar2c330432020-04-13 14:41:35 +02004871 if (c == '#')
Bram Moolenaar7a092242020-04-16 22:10:49 +02004872 return in_vim9script();
4873#endif
4874 return (c == NUL || c == '|' || c == '"' || c == '\n');
4875}
4876
4877/*
4878 * Like ends_excmd() but checks that a # in Vim9 script either has "cmd" equal
4879 * to "cmd_start" or has a white space character before it.
4880 */
4881 int
Bram Moolenaar21cfe502020-04-16 23:01:50 +02004882ends_excmd2(char_u *cmd_start UNUSED, char_u *cmd)
Bram Moolenaar7a092242020-04-16 22:10:49 +02004883{
4884 int c = *cmd;
4885
4886#ifdef FEAT_EVAL
Bram Moolenaara26b9702020-04-18 19:53:28 +02004887 if (c == '#' && cmd[1] != '{' && (cmd == cmd_start || VIM_ISWHITE(cmd[-1])))
Bram Moolenaar2c330432020-04-13 14:41:35 +02004888 return in_vim9script();
Bram Moolenaarfbda69b2020-04-13 15:06:53 +02004889#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004890 return (c == NUL || c == '|' || c == '"' || c == '\n');
4891}
4892
4893#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
4894 || defined(PROTO)
4895/*
4896 * Return the next command, after the first '|' or '\n'.
4897 * Return NULL if not found.
4898 */
4899 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004900find_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004901{
4902 while (*p != '|' && *p != '\n')
4903 {
4904 if (*p == NUL)
4905 return NULL;
4906 ++p;
4907 }
4908 return (p + 1);
4909}
4910#endif
4911
4912/*
Bram Moolenaar2256c992016-11-15 21:17:07 +01004913 * Check if *p is a separator between Ex commands, skipping over white space.
4914 * Return NULL if it isn't, the following character if it is.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004915 */
4916 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004917check_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004918{
Bram Moolenaar2256c992016-11-15 21:17:07 +01004919 char_u *s = skipwhite(p);
4920
4921 if (*s == '|' || *s == '\n')
4922 return (s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004923 else
4924 return NULL;
4925}
4926
4927/*
4928 * - if there are more files to edit
4929 * - and this is the last window
4930 * - and forceit not used
4931 * - and not repeated twice on a row
4932 * return FAIL and give error message if 'message' TRUE
4933 * return OK otherwise
4934 */
4935 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004936check_more(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004937 int message, // when FALSE check only, no messages
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004938 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004939{
4940 int n = ARGCOUNT - curwin->w_arg_idx - 1;
4941
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00004942 if (!forceit && only_one_window()
4943 && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944 {
4945 if (message)
4946 {
4947#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
4948 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
4949 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02004950 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004951
Bram Moolenaarda6e8912018-08-21 15:12:14 +02004952 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
4953 NGETTEXT("%d more file to edit. Quit anyway?",
4954 "%d more files to edit. Quit anyway?", n), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004955 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
4956 return OK;
4957 return FAIL;
4958 }
4959#endif
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01004960 semsg(NGETTEXT("E173: %d more file to edit",
4961 "E173: %d more files to edit", n), n);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004962 quitmore = 2; // next try to quit is allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004963 }
4964 return FAIL;
4965 }
4966 return OK;
4967}
4968
Bram Moolenaar071d4272004-06-13 20:20:40 +00004969/*
4970 * Function given to ExpandGeneric() to obtain the list of command names.
4971 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004972 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004973get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004974{
4975 if (idx >= (int)CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004976 return get_user_command_name(idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004977 return cmdnames[idx].cmd_name;
4978}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004979
Bram Moolenaar071d4272004-06-13 20:20:40 +00004980 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004981ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004982{
Bram Moolenaare6850792010-05-14 15:28:44 +02004983 if (*eap->arg == NUL)
4984 {
4985#ifdef FEAT_EVAL
4986 char_u *expr = vim_strsave((char_u *)"g:colors_name");
4987 char_u *p = NULL;
4988
4989 if (expr != NULL)
4990 {
4991 ++emsg_off;
Bram Moolenaarb171fb12020-06-24 20:34:03 +02004992 p = eval_to_string(expr, FALSE);
Bram Moolenaare6850792010-05-14 15:28:44 +02004993 --emsg_off;
4994 vim_free(expr);
4995 }
4996 if (p != NULL)
4997 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004998 msg((char *)p);
Bram Moolenaare6850792010-05-14 15:28:44 +02004999 vim_free(p);
5000 }
5001 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005002 msg("default");
Bram Moolenaare6850792010-05-14 15:28:44 +02005003#else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005004 msg(_("unknown"));
Bram Moolenaare6850792010-05-14 15:28:44 +02005005#endif
5006 }
5007 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005008 semsg(_("E185: Cannot find color scheme '%s'"), eap->arg);
Bram Moolenaarf58d81a2019-01-28 20:19:05 +01005009
5010#ifdef FEAT_VTP
5011 else if (has_vtp_working())
5012 {
5013 // background color change requires clear + redraw
5014 update_screen(CLEAR);
5015 redrawcmd();
5016 }
5017#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005018}
5019
5020 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005021ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005022{
5023 if (*eap->arg == NUL && eap->cmd[2] == '!')
Bram Moolenaar32526b32019-01-19 17:43:09 +01005024 msg(_("Greetings, Vim user!"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005025 do_highlight(eap->arg, eap->forceit, FALSE);
5026}
5027
5028
5029/*
5030 * Call this function if we thought we were going to exit, but we won't
5031 * (because of an error). May need to restore the terminal mode.
5032 */
5033 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005034not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005035{
5036 exiting = FALSE;
5037 settmode(TMODE_RAW);
5038}
5039
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005040 static int
5041before_quit_autocmds(win_T *wp, int quit_all, int forceit)
5042{
5043 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, wp->w_buffer);
5044
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02005045 // Bail out when autocommands closed the window.
5046 // Refuse to quit when the buffer in the last window is being closed (can
5047 // only happen in autocommands).
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005048 if (!win_valid(wp)
5049 || curbuf_locked()
5050 || (wp->w_buffer->b_nwindows == 1 && wp->w_buffer->b_locked > 0))
5051 return TRUE;
5052
5053 if (quit_all || (check_more(FALSE, forceit) == OK && only_one_window()))
5054 {
5055 apply_autocmds(EVENT_EXITPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02005056 // Refuse to quit when locked or when the window was closed or the
5057 // buffer in the last window is being closed (can only happen in
5058 // autocommands).
5059 if (!win_valid(wp) || curbuf_locked()
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005060 || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
5061 return TRUE;
5062 }
5063
5064 return FALSE;
5065}
5066
Bram Moolenaar071d4272004-06-13 20:20:40 +00005067/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01005068 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005069 * ":{nr}quit": quit window {nr}
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02005070 * Also used when closing a terminal window that's the last one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005071 */
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02005072 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005073ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005074{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005075 win_T *wp;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005076
Bram Moolenaar071d4272004-06-13 20:20:40 +00005077#ifdef FEAT_CMDWIN
5078 if (cmdwin_type != 0)
5079 {
5080 cmdwin_result = Ctrl_C;
5081 return;
5082 }
5083#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005084 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005085 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005086 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005087 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005088 return;
5089 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005090 if (eap->addr_count > 0)
5091 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01005092 int wnr = eap->line2;
5093
5094 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
5095 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005096 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005097 }
5098 else
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005099 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01005100
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005101 // Refuse to quit when locked.
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02005102 if (curbuf_locked())
5103 return;
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005104
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005105 // Trigger QuitPre and maybe ExitPre
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005106 if (before_quit_autocmds(wp, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005107 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005108
5109#ifdef FEAT_NETBEANS_INTG
5110 netbeansForcedQuit = eap->forceit;
5111#endif
5112
5113 /*
5114 * If there are more files or windows we won't exit.
5115 */
5116 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
5117 exiting = TRUE;
Bram Moolenaarff930ca2017-10-19 17:12:10 +02005118 if ((!buf_hide(wp->w_buffer)
5119 && check_changed(wp->w_buffer, (p_awa ? CCGD_AW : 0)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01005120 | (eap->forceit ? CCGD_FORCEIT : 0)
5121 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005122 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01005123 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005124 {
5125 not_exiting();
5126 }
5127 else
5128 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005129 // quit last window
5130 // Note: only_one_window() returns true, even so a help window is
5131 // still open. In that case only quit, if no address has been
5132 // specified. Example:
5133 // :h|wincmd w|1q - don't quit
5134 // :h|wincmd w|q - quit
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01005135 if (only_one_window() && (ONE_WINDOW || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005136 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02005137 not_exiting();
Bram Moolenaar4033c552017-09-16 20:54:51 +02005138#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005139 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005140#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005141 // close window; may free buffer
Bram Moolenaareb44a682017-08-03 22:44:55 +02005142 win_close(wp, !buf_hide(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005143 }
5144}
5145
5146/*
5147 * ":cquit".
5148 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005149 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005150ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005151{
Bram Moolenaar1860bde2020-01-06 21:47:21 +01005152 // this does not always pass on the exit code to the Manx compiler. why?
5153 getout(eap->addr_count > 0 ? (int)eap->line2 : EXIT_FAILURE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005154}
5155
5156/*
5157 * ":qall": try to quit all windows
5158 */
5159 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005160ex_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005161{
5162# ifdef FEAT_CMDWIN
5163 if (cmdwin_type != 0)
5164 {
5165 if (eap->forceit)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005166 cmdwin_result = K_XF1; // ex_window() takes care of this
Bram Moolenaar071d4272004-06-13 20:20:40 +00005167 else
5168 cmdwin_result = K_XF2;
5169 return;
5170 }
5171# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005172
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005173 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005174 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005175 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005176 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005177 return;
5178 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005179
5180 if (before_quit_autocmds(curwin, TRUE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005181 return;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005182
Bram Moolenaar071d4272004-06-13 20:20:40 +00005183 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01005184 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185 getout(0);
5186 not_exiting();
5187}
5188
Bram Moolenaar071d4272004-06-13 20:20:40 +00005189/*
5190 * ":close": close current window, unless it is the last one
5191 */
5192 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005193ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005195 win_T *win;
5196 int winnr = 0;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005197#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00005198 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02005199 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005200 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005201#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005202 if (!text_locked() && !curbuf_locked())
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005203 {
5204 if (eap->addr_count == 0)
5205 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar6d41c782018-06-06 09:11:12 +02005206 else
5207 {
Bram Moolenaar29323592016-07-24 22:04:11 +02005208 FOR_ALL_WINDOWS(win)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005209 {
5210 winnr++;
5211 if (winnr == eap->line2)
5212 break;
5213 }
5214 if (win == NULL)
5215 win = lastwin;
5216 ex_win_close(eap->forceit, win, NULL);
5217 }
5218 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005219}
5220
Bram Moolenaar4033c552017-09-16 20:54:51 +02005221#ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005222/*
5223 * ":pclose": Close any preview window.
5224 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005225 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005226ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005227{
5228 win_T *win;
5229
Bram Moolenaar79648732019-07-18 21:43:07 +02005230 // First close any normal window.
Bram Moolenaar29323592016-07-24 22:04:11 +02005231 FOR_ALL_WINDOWS(win)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005232 if (win->w_p_pvw)
5233 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005234 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar79648732019-07-18 21:43:07 +02005235 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005236 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005237# ifdef FEAT_PROP_POPUP
Bram Moolenaar79648732019-07-18 21:43:07 +02005238 // Also when 'previewpopup' is empty, it might have been cleared.
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02005239 popup_close_preview();
Bram Moolenaar79648732019-07-18 21:43:07 +02005240# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005241}
Bram Moolenaar4033c552017-09-16 20:54:51 +02005242#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005243
Bram Moolenaarf740b292006-02-16 22:11:02 +00005244/*
5245 * Close window "win" and take care of handling closing the last window for a
5246 * modified buffer.
5247 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005248 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005249ex_win_close(
5250 int forceit,
5251 win_T *win,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005252 tabpage_T *tp) // NULL or the tab page "win" is in
Bram Moolenaar071d4272004-06-13 20:20:40 +00005253{
5254 int need_hide;
5255 buf_T *buf = win->w_buffer;
5256
Bram Moolenaarcf844172020-06-26 19:44:06 +02005257 // Never close the autocommand window.
5258 if (win == aucmd_win)
5259 {
5260 emsg(_(e_autocmd_close));
5261 return;
5262 }
5263
Bram Moolenaar071d4272004-06-13 20:20:40 +00005264 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02005265 if (need_hide && !buf_hide(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005266 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02005267#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005268 if ((p_confirm || cmdmod.confirm) && p_write)
5269 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005270 bufref_T bufref;
5271
5272 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005273 dialog_changed(buf, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005274 if (bufref_valid(&bufref) && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005275 return;
5276 need_hide = FALSE;
5277 }
5278 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005279#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005280 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02005281 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005282 return;
5283 }
5284 }
5285
Bram Moolenaar4033c552017-09-16 20:54:51 +02005286#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005287 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005288#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005289
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005290 // free buffer when not hiding it or when it's a scratch buffer
Bram Moolenaarf740b292006-02-16 22:11:02 +00005291 if (tp == NULL)
Bram Moolenaareb44a682017-08-03 22:44:55 +02005292 win_close(win, !need_hide && !buf_hide(buf));
Bram Moolenaarf740b292006-02-16 22:11:02 +00005293 else
Bram Moolenaareb44a682017-08-03 22:44:55 +02005294 win_close_othertab(win, !need_hide && !buf_hide(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005295}
5296
Bram Moolenaar071d4272004-06-13 20:20:40 +00005297/*
Bram Moolenaardea25702017-01-29 15:18:10 +01005298 * Handle the argument for a tabpage related ex command.
5299 * Returns a tabpage number.
5300 * When an error is encountered then eap->errmsg is set.
5301 */
5302 static int
5303get_tabpage_arg(exarg_T *eap)
5304{
5305 int tab_number;
5306 int unaccept_arg0 = (eap->cmdidx == CMD_tabmove) ? 0 : 1;
5307
5308 if (eap->arg && *eap->arg != NUL)
5309 {
5310 char_u *p = eap->arg;
5311 char_u *p_save;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005312 int relative = 0; // argument +N/-N means: go to N places to the
5313 // right/left relative to the current position.
Bram Moolenaardea25702017-01-29 15:18:10 +01005314
5315 if (*p == '-')
5316 {
5317 relative = -1;
5318 p++;
5319 }
5320 else if (*p == '+')
5321 {
5322 relative = 1;
5323 p++;
5324 }
5325
5326 p_save = p;
5327 tab_number = getdigits(&p);
5328
5329 if (relative == 0)
5330 {
5331 if (STRCMP(p, "$") == 0)
5332 tab_number = LAST_TAB_NR;
5333 else if (p == p_save || *p_save == '-' || *p != NUL
5334 || tab_number > LAST_TAB_NR)
5335 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005336 // No numbers as argument.
Bram Moolenaardea25702017-01-29 15:18:10 +01005337 eap->errmsg = e_invarg;
5338 goto theend;
5339 }
5340 }
5341 else
5342 {
5343 if (*p_save == NUL)
5344 tab_number = 1;
5345 else if (p == p_save || *p_save == '-' || *p != NUL
5346 || tab_number == 0)
5347 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005348 // No numbers as argument.
Bram Moolenaardea25702017-01-29 15:18:10 +01005349 eap->errmsg = e_invarg;
5350 goto theend;
5351 }
5352 tab_number = tab_number * relative + tabpage_index(curtab);
5353 if (!unaccept_arg0 && relative == -1)
5354 --tab_number;
5355 }
5356 if (tab_number < unaccept_arg0 || tab_number > LAST_TAB_NR)
5357 eap->errmsg = e_invarg;
5358 }
5359 else if (eap->addr_count > 0)
5360 {
5361 if (unaccept_arg0 && eap->line2 == 0)
Bram Moolenaarc6251552017-01-29 21:42:20 +01005362 {
Bram Moolenaardea25702017-01-29 15:18:10 +01005363 eap->errmsg = e_invrange;
Bram Moolenaarc6251552017-01-29 21:42:20 +01005364 tab_number = 0;
5365 }
Bram Moolenaardea25702017-01-29 15:18:10 +01005366 else
5367 {
5368 tab_number = eap->line2;
Bram Moolenaar82a12462019-01-22 22:41:42 +01005369 if (!unaccept_arg0 && *skipwhite(*eap->cmdlinep) == '-')
Bram Moolenaardea25702017-01-29 15:18:10 +01005370 {
5371 --tab_number;
5372 if (tab_number < unaccept_arg0)
5373 eap->errmsg = e_invarg;
5374 }
5375 }
5376 }
5377 else
5378 {
5379 switch (eap->cmdidx)
5380 {
5381 case CMD_tabnext:
5382 tab_number = tabpage_index(curtab) + 1;
5383 if (tab_number > LAST_TAB_NR)
5384 tab_number = 1;
5385 break;
5386 case CMD_tabmove:
5387 tab_number = LAST_TAB_NR;
5388 break;
5389 default:
5390 tab_number = tabpage_index(curtab);
5391 }
5392 }
5393
5394theend:
5395 return tab_number;
5396}
5397
5398/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00005399 * ":tabclose": close current tab page, unless it is the last one.
5400 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005401 */
5402 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005403ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005404{
Bram Moolenaarf740b292006-02-16 22:11:02 +00005405 tabpage_T *tp;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005406 int tab_number;
Bram Moolenaarf740b292006-02-16 22:11:02 +00005407
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005408# ifdef FEAT_CMDWIN
5409 if (cmdwin_type != 0)
5410 cmdwin_result = K_IGNORE;
5411 else
5412# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005413 if (first_tabpage->tp_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005414 emsg(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00005415 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005416 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005417 tab_number = get_tabpage_arg(eap);
5418 if (eap->errmsg == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005419 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005420 tp = find_tabpage(tab_number);
Bram Moolenaarf740b292006-02-16 22:11:02 +00005421 if (tp == NULL)
5422 {
5423 beep_flush();
5424 return;
5425 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005426 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005427 {
5428 tabpage_close_other(tp, eap->forceit);
5429 return;
5430 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005431 else if (!text_locked() && !curbuf_locked())
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005432 tabpage_close(eap->forceit);
5433 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005434 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005435}
5436
5437/*
5438 * ":tabonly": close all tab pages except the current one
5439 */
5440 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005441ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005442{
5443 tabpage_T *tp;
5444 int done;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005445 int tab_number;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005446
5447# ifdef FEAT_CMDWIN
5448 if (cmdwin_type != 0)
5449 cmdwin_result = K_IGNORE;
5450 else
5451# endif
5452 if (first_tabpage->tp_next == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005453 msg(_("Already only one tab page"));
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005454 else
5455 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005456 tab_number = get_tabpage_arg(eap);
5457 if (eap->errmsg == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005458 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005459 goto_tabpage(tab_number);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005460 // Repeat this up to a 1000 times, because autocommands may
5461 // mess up the lists.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005462 for (done = 0; done < 1000; ++done)
5463 {
5464 FOR_ALL_TABPAGES(tp)
5465 if (tp->tp_topframe != topframe)
5466 {
5467 tabpage_close_other(tp, eap->forceit);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005468 // if we failed to close it quit
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005469 if (valid_tabpage(tp))
5470 done = 1000;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005471 // start over, "tp" is now invalid
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005472 break;
5473 }
5474 if (first_tabpage->tp_next == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005475 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005476 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005477 }
5478 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005479}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005480
5481/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00005482 * Close the current tab page.
5483 */
5484 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005485tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005486{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005487 // First close all the windows but the current one. If that worked then
5488 // close the last window in this tab, that will close it.
Bram Moolenaar459ca562016-11-10 18:16:33 +01005489 if (!ONE_WINDOW)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005490 close_others(TRUE, forceit);
Bram Moolenaar459ca562016-11-10 18:16:33 +01005491 if (ONE_WINDOW)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005492 ex_win_close(forceit, curwin, NULL);
5493# ifdef FEAT_GUI
5494 need_mouse_correct = TRUE;
5495# endif
5496}
5497
5498/*
5499 * Close tab page "tp", which is not the current tab page.
5500 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005501 * Also takes care of the tab pages line disappearing when closing the
5502 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00005503 */
5504 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005505tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005506{
5507 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005508 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005509 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00005510
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005511 // Limit to 1000 windows, autocommands may add a window while we close
5512 // one. OK, so I'm paranoid...
Bram Moolenaarf740b292006-02-16 22:11:02 +00005513 while (++done < 1000)
5514 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005515 wp = tp->tp_firstwin;
5516 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00005517
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005518 // Autocommands may delete the tab page under our fingers and we may
5519 // fail to close a window with a modified buffer.
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005520 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005521 break;
5522 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005523
Bram Moolenaar29323592016-07-24 22:04:11 +02005524 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar29323592016-07-24 22:04:11 +02005525
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005526 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005527 if (h != tabline_height())
5528 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00005529}
5530
5531/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005532 * ":only".
5533 */
5534 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005535ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005536{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005537 win_T *wp;
5538 int wnr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005539# ifdef FEAT_GUI
5540 need_mouse_correct = TRUE;
5541# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005542 if (eap->addr_count > 0)
5543 {
5544 wnr = eap->line2;
5545 for (wp = firstwin; --wnr > 0; )
5546 {
5547 if (wp->w_next == NULL)
5548 break;
5549 else
5550 wp = wp->w_next;
5551 }
5552 win_goto(wp);
5553 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005554 close_others(TRUE, eap->forceit);
5555}
5556
Bram Moolenaar071d4272004-06-13 20:20:40 +00005557 static void
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01005558ex_hide(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005559{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005560 // ":hide" or ":hide | cmd": hide current window
Bram Moolenaar2256c992016-11-15 21:17:07 +01005561 if (!eap->skip)
5562 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02005563#ifdef FEAT_GUI
Bram Moolenaar2256c992016-11-15 21:17:07 +01005564 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005565#endif
Bram Moolenaar2256c992016-11-15 21:17:07 +01005566 if (eap->addr_count == 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005567 win_close(curwin, FALSE); // don't free buffer
Bram Moolenaar2256c992016-11-15 21:17:07 +01005568 else
5569 {
5570 int winnr = 0;
5571 win_T *win;
Bram Moolenaarf240e182014-11-27 18:33:02 +01005572
Bram Moolenaar2256c992016-11-15 21:17:07 +01005573 FOR_ALL_WINDOWS(win)
5574 {
5575 winnr++;
5576 if (winnr == eap->line2)
5577 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005578 }
Bram Moolenaar2256c992016-11-15 21:17:07 +01005579 if (win == NULL)
5580 win = lastwin;
5581 win_close(win, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005582 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005583 }
5584}
5585
5586/*
5587 * ":stop" and ":suspend": Suspend Vim.
5588 */
5589 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005590ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005591{
5592 /*
5593 * Disallow suspending for "rvim".
5594 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005595 if (!check_restricted())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005596 {
5597 if (!eap->forceit)
5598 autowrite_all();
5599 windgoto((int)Rows - 1, 0);
5600 out_char('\n');
5601 out_flush();
5602 stoptermcap();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005603 out_flush(); // needed for SUN to restore xterm buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00005604#ifdef FEAT_TITLE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005605 mch_restore_title(SAVE_RESTORE_BOTH); // restore window titles
Bram Moolenaar071d4272004-06-13 20:20:40 +00005606#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005607 ui_suspend(); // call machine specific function
Bram Moolenaar071d4272004-06-13 20:20:40 +00005608#ifdef FEAT_TITLE
5609 maketitle();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005610 resettitle(); // force updating the title
Bram Moolenaar071d4272004-06-13 20:20:40 +00005611#endif
5612 starttermcap();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005613 scroll_start(); // scroll screen before redrawing
Bram Moolenaar071d4272004-06-13 20:20:40 +00005614 redraw_later_clear();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005615 shell_resized(); // may have resized window
Bram Moolenaar071d4272004-06-13 20:20:40 +00005616 }
5617}
5618
5619/*
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005620 * ":exit", ":xit" and ":wq": Write file and quite the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005621 */
5622 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005623ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005624{
5625#ifdef FEAT_CMDWIN
5626 if (cmdwin_type != 0)
5627 {
5628 cmdwin_result = Ctrl_C;
5629 return;
5630 }
5631#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005632 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005633 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005634 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005635 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005636 return;
5637 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005638
5639 if (before_quit_autocmds(curwin, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005640 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005641
5642 /*
5643 * if more files or windows we won't exit
5644 */
5645 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
5646 exiting = TRUE;
5647 if ( ((eap->cmdidx == CMD_wq
5648 || curbufIsChanged())
5649 && do_write(eap) == FAIL)
5650 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01005651 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005652 {
5653 not_exiting();
5654 }
5655 else
5656 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005657 if (only_one_window()) // quit last window, exit Vim
Bram Moolenaar071d4272004-06-13 20:20:40 +00005658 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02005659 not_exiting();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005660# ifdef FEAT_GUI
5661 need_mouse_correct = TRUE;
5662# endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005663 // Quit current window, may free the buffer.
Bram Moolenaareb44a682017-08-03 22:44:55 +02005664 win_close(curwin, !buf_hide(curwin->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005665 }
5666}
5667
5668/*
5669 * ":print", ":list", ":number".
5670 */
5671 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005672ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005673{
Bram Moolenaardf177f62005-02-22 08:39:57 +00005674 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005675 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +00005676 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005677 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00005678 for ( ;!got_int; ui_breakcheck())
5679 {
5680 print_line(eap->line1,
5681 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
5682 || (eap->flags & EXFLAG_NR)),
5683 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
5684 if (++eap->line1 > eap->line2)
5685 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005686 out_flush(); // show one line at a time
Bram Moolenaardf177f62005-02-22 08:39:57 +00005687 }
5688 setpcmark();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005689 // put cursor at last line
Bram Moolenaardf177f62005-02-22 08:39:57 +00005690 curwin->w_cursor.lnum = eap->line2;
5691 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005692 }
5693
Bram Moolenaar071d4272004-06-13 20:20:40 +00005694 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005695}
5696
5697#ifdef FEAT_BYTEOFF
5698 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005699ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005700{
5701 goto_byte(eap->line2);
5702}
5703#endif
5704
5705/*
5706 * ":shell".
5707 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005708 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005709ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005710{
5711 do_shell(NULL, 0);
5712}
5713
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005714#if defined(HAVE_DROP_FILE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005715
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005716static int drop_busy = FALSE;
5717static int drop_filec;
5718static char_u **drop_filev = NULL;
5719static int drop_split;
5720static void (*drop_callback)(void *);
5721static void *drop_cookie;
5722
5723 static void
5724handle_drop_internal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005725{
5726 exarg_T ea;
5727 int save_msg_scroll = msg_scroll;
5728
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005729 // Setting the argument list may cause screen updates and being called
5730 // recursively. Avoid that by setting drop_busy.
5731 drop_busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005732
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005733 // Check whether the current buffer is changed. If so, we will need
5734 // to split the current window or data could be lost.
5735 // We don't need to check if the 'hidden' option is set, as in this
5736 // case the buffer won't be lost.
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005737 if (!buf_hide(curbuf) && !drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005738 {
5739 ++emsg_off;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005740 drop_split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005741 --emsg_off;
5742 }
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005743 if (drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005744 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005745 if (win_split(0, 0) == FAIL)
5746 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02005747 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005748
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005749 // When splitting the window, create a new alist. Otherwise the
5750 // existing one is overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005751 alist_unlink(curwin->w_alist);
5752 alist_new();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005753 }
5754
5755 /*
5756 * Set up the new argument list.
5757 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005758 alist_set(ALIST(curwin), drop_filec, drop_filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005759
5760 /*
5761 * Move to the first file.
5762 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005763 // Fake up a minimal "next" command for do_argfile()
Bram Moolenaara80faa82020-04-12 19:37:17 +02005764 CLEAR_FIELD(ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005765 ea.cmd = (char_u *)"next";
5766 do_argfile(&ea, 0);
5767
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005768 // do_ecmd() may set need_start_insertmode, but since we never left Insert
5769 // mode that is not needed here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005770 need_start_insertmode = FALSE;
5771
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005772 // Restore msg_scroll, otherwise a following command may cause scrolling
5773 // unexpectedly. The screen will be redrawn by the caller, thus
5774 // msg_scroll being set by displaying a message is irrelevant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005775 msg_scroll = save_msg_scroll;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005776
5777 if (drop_callback != NULL)
5778 drop_callback(drop_cookie);
5779
5780 drop_filev = NULL;
5781 drop_busy = FALSE;
5782}
5783
5784/*
5785 * Handle a file drop. The code is here because a drop is *nearly* like an
5786 * :args command, but not quite (we have a list of exact filenames, so we
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01005787 * don't want to (a) parse a command line, or (b) expand wildcards). So the
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005788 * code is very similar to :args and hence needs access to a lot of the static
5789 * functions in this file.
5790 *
5791 * The "filev" list must have been allocated using alloc(), as should each item
5792 * in the list. This function takes over responsibility for freeing the "filev"
5793 * list.
5794 */
5795 void
5796handle_drop(
5797 int filec, // the number of files dropped
5798 char_u **filev, // the list of files dropped
5799 int split, // force splitting the window
5800 void (*callback)(void *), // to be called after setting the argument
5801 // list
5802 void *cookie) // argument for "callback" (allocated)
5803{
5804 // Cannot handle recursive drops, finish the pending one.
5805 if (drop_busy)
5806 {
5807 FreeWild(filec, filev);
5808 vim_free(cookie);
5809 return;
5810 }
5811
5812 // When calling handle_drop() more than once in a row we only use the last
5813 // one.
5814 if (drop_filev != NULL)
5815 {
5816 FreeWild(drop_filec, drop_filev);
5817 vim_free(drop_cookie);
5818 }
5819
5820 drop_filec = filec;
5821 drop_filev = filev;
5822 drop_split = split;
5823 drop_callback = callback;
5824 drop_cookie = cookie;
5825
5826 // Postpone this when:
5827 // - editing the command line
5828 // - not possible to change the current buffer
5829 // - updating the screen
5830 // As it may change buffers and window structures that are in use and cause
5831 // freed memory to be used.
5832 if (text_locked() || curbuf_locked() || updating_screen)
5833 return;
5834
5835 handle_drop_internal();
5836}
5837
5838/*
5839 * To be called when text is unlocked, curbuf is unlocked or updating_screen is
5840 * reset: Handle a postponed drop.
5841 */
5842 void
5843handle_any_postponed_drop(void)
5844{
5845 if (!drop_busy && drop_filev != NULL
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02005846 && !text_locked() && !curbuf_locked() && !updating_screen)
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005847 handle_drop_internal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005848}
5849#endif
5850
Bram Moolenaar071d4272004-06-13 20:20:40 +00005851/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005852 * ":preserve".
5853 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005854 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005855ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005856{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005857 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005858 ml_preserve(curbuf, TRUE);
5859}
5860
5861/*
5862 * ":recover".
5863 */
5864 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005865ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005866{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005867 // Set recoverymode right away to avoid the ATTENTION prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005868 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01005869 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
5870 | CCGD_MULTWIN
5871 | (eap->forceit ? CCGD_FORCEIT : 0)
5872 | CCGD_EXCMD)
5873
Bram Moolenaar071d4272004-06-13 20:20:40 +00005874 && (*eap->arg == NUL
5875 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
Bram Moolenaar99499b12019-05-23 21:35:48 +02005876 ml_recover(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005877 recoverymode = FALSE;
5878}
5879
5880/*
5881 * Command modifier used in a wrong way.
5882 */
5883 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005884ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005885{
5886 eap->errmsg = e_invcmd;
5887}
5888
Bram Moolenaar071d4272004-06-13 20:20:40 +00005889/*
5890 * :sview [+command] file split window with new file, read-only
5891 * :split [[+command] file] split window with current or new file
5892 * :vsplit [[+command] file] split window vertically with current or new file
5893 * :new [[+command] file] split window with no or new file
5894 * :vnew [[+command] file] split vertically window with no or new file
5895 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005896 *
5897 * :tabedit open new Tab page with empty window
5898 * :tabedit [+command] file open new Tab page and edit "file"
5899 * :tabnew [[+command] file] just like :tabedit
5900 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005901 */
5902 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005903ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005904{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005905 win_T *old_curwin = curwin;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005906#if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005907 char_u *fname = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005908#endif
5909#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005910 int browse_flag = cmdmod.browse;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005911#endif
Bram Moolenaar39902a02018-06-21 22:10:08 +02005912 int use_tab = eap->cmdidx == CMD_tabedit
5913 || eap->cmdidx == CMD_tabfind
5914 || eap->cmdidx == CMD_tabnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005915
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005916 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005917 return;
5918
Bram Moolenaar4033c552017-09-16 20:54:51 +02005919#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005920 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005921#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005922
Bram Moolenaar4033c552017-09-16 20:54:51 +02005923#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005924 // A ":split" in the quickfix window works like ":new". Don't want two
5925 // quickfix windows. But it's OK when doing ":tab split".
Bram Moolenaar05bb9532008-07-04 09:44:11 +00005926 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005927 {
5928 if (eap->cmdidx == CMD_split)
5929 eap->cmdidx = CMD_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005930 if (eap->cmdidx == CMD_vsplit)
5931 eap->cmdidx = CMD_vnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005932 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02005933#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005934
Bram Moolenaar4033c552017-09-16 20:54:51 +02005935#ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005936 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005937 {
5938 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
5939 FNAME_MESS, TRUE, curbuf->b_ffname);
5940 if (fname == NULL)
5941 goto theend;
5942 eap->arg = fname;
5943 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005944# ifdef FEAT_BROWSE
Bram Moolenaar4033c552017-09-16 20:54:51 +02005945 else
5946# endif
5947#endif
5948#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005949 if (cmdmod.browse
Bram Moolenaar071d4272004-06-13 20:20:40 +00005950 && eap->cmdidx != CMD_vnew
Bram Moolenaar071d4272004-06-13 20:20:40 +00005951 && eap->cmdidx != CMD_new)
5952 {
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005953 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005954# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005955 !gui.in_use &&
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005956# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005957 au_has_group((char_u *)"FileExplorer"))
5958 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005959 // No browsing supported but we do have the file explorer:
5960 // Edit the directory.
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005961 if (*eap->arg == NUL || !mch_isdir(eap->arg))
5962 eap->arg = (char_u *)".";
5963 }
5964 else
5965 {
Bram Moolenaar39902a02018-06-21 22:10:08 +02005966 fname = do_browse(0, (char_u *)(use_tab
5967 ? _("Edit File in new tab page")
5968 : _("Edit File in new window")),
Bram Moolenaar071d4272004-06-13 20:20:40 +00005969 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005970 if (fname == NULL)
5971 goto theend;
5972 eap->arg = fname;
5973 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005974 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005975 cmdmod.browse = FALSE; // Don't browse again in do_ecmd().
Bram Moolenaar4033c552017-09-16 20:54:51 +02005976#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005977
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005978 /*
5979 * Either open new tab page or split the window.
5980 */
Bram Moolenaar39902a02018-06-21 22:10:08 +02005981 if (use_tab)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005982 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005983 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
5984 : eap->addr_count == 0 ? 0
5985 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005986 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00005987 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005988
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005989 // set the alternate buffer for the window we came from
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005990 if (curwin != old_curwin
5991 && win_valid(old_curwin)
5992 && old_curwin->w_buffer != curbuf
5993 && !cmdmod.keepalt)
5994 old_curwin->w_alt_fnum = curbuf->b_fnum;
5995 }
5996 }
5997 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005998 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
5999 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006000 // Reset 'scrollbind' when editing another file, but keep it when
6001 // doing ":split" without arguments.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006002 if (*eap->arg != NUL
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01006003# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006004 || cmdmod.browse
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01006005# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006006 )
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006007 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006008 else
6009 do_check_scrollbind(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006010 do_exedit(eap, old_curwin);
6011 }
6012
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006013# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006014 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006015# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006016
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006017# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006018theend:
6019 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006020# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006021}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006022
6023/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006024 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006025 */
6026 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006027tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006028{
6029 exarg_T ea;
6030
Bram Moolenaara80faa82020-04-12 19:37:17 +02006031 CLEAR_FIELD(ea);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006032 ea.cmdidx = CMD_tabnew;
6033 ea.cmd = (char_u *)"tabn";
6034 ea.arg = (char_u *)"";
6035 ex_splitview(&ea);
6036}
6037
6038/*
6039 * :tabnext command
6040 */
6041 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006042ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006043{
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006044 int tab_number;
6045
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02006046 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006047 return;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006048 switch (eap->cmdidx)
6049 {
6050 case CMD_tabfirst:
6051 case CMD_tabrewind:
6052 goto_tabpage(1);
6053 break;
6054 case CMD_tablast:
6055 goto_tabpage(9999);
6056 break;
6057 case CMD_tabprevious:
6058 case CMD_tabNext:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006059 if (eap->arg && *eap->arg != NUL)
6060 {
6061 char_u *p = eap->arg;
6062 char_u *p_save = p;
6063
6064 tab_number = getdigits(&p);
6065 if (p == p_save || *p_save == '-' || *p != NUL
6066 || tab_number == 0)
6067 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006068 // No numbers as argument.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006069 eap->errmsg = e_invarg;
6070 return;
6071 }
6072 }
6073 else
6074 {
6075 if (eap->addr_count == 0)
6076 tab_number = 1;
6077 else
6078 {
6079 tab_number = eap->line2;
6080 if (tab_number < 1)
6081 {
6082 eap->errmsg = e_invrange;
6083 return;
6084 }
6085 }
6086 }
6087 goto_tabpage(-tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006088 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006089 default: // CMD_tabnext
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006090 tab_number = get_tabpage_arg(eap);
6091 if (eap->errmsg == NULL)
6092 goto_tabpage(tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006093 break;
6094 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006095}
6096
6097/*
6098 * :tabmove command
6099 */
6100 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006101ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006102{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02006103 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02006104
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006105 tab_number = get_tabpage_arg(eap);
6106 if (eap->errmsg == NULL)
6107 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006108}
6109
6110/*
6111 * :tabs command: List tabs and their contents.
6112 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006113 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006114ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006115{
6116 tabpage_T *tp;
6117 win_T *wp;
6118 int tabcount = 1;
6119
6120 msg_start();
6121 msg_scroll = TRUE;
6122 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
6123 {
6124 msg_putchar('\n');
6125 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
Bram Moolenaar8820b482017-03-16 17:23:31 +01006126 msg_outtrans_attr(IObuff, HL_ATTR(HLF_T));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006127 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006128 ui_breakcheck();
6129
Bram Moolenaar030f0df2006-02-21 22:02:53 +00006130 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006131 wp = firstwin;
6132 else
6133 wp = tp->tp_firstwin;
6134 for ( ; wp != NULL && !got_int; wp = wp->w_next)
6135 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006136 msg_putchar('\n');
6137 msg_putchar(wp == curwin ? '>' : ' ');
6138 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006139 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
6140 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006141 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02006142 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006143 else
6144 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
6145 IObuff, IOSIZE, TRUE);
6146 msg_outtrans(IObuff);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006147 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006148 ui_breakcheck();
6149 }
6150 }
6151}
6152
Bram Moolenaar071d4272004-06-13 20:20:40 +00006153/*
6154 * ":mode": Set screen mode.
6155 * If no argument given, just get the screen size and redraw.
6156 */
6157 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006158ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006159{
6160 if (*eap->arg == NUL)
6161 shell_resized();
6162 else
Bram Moolenaar3c71aec2020-01-17 19:32:20 +01006163 emsg(_(e_screenmode));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006164}
6165
Bram Moolenaar071d4272004-06-13 20:20:40 +00006166/*
6167 * ":resize".
6168 * set, increment or decrement current window height
6169 */
6170 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006171ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006172{
6173 int n;
6174 win_T *wp = curwin;
6175
6176 if (eap->addr_count > 0)
6177 {
6178 n = eap->line2;
6179 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
6180 ;
6181 }
6182
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006183# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006184 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006185# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006186 n = atol((char *)eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006187 if (cmdmod.split & WSP_VERT)
6188 {
6189 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar02631462017-09-22 15:20:32 +02006190 n += curwin->w_width;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006191 else if (n == 0 && eap->arg[0] == NUL) // default is very wide
Bram Moolenaar071d4272004-06-13 20:20:40 +00006192 n = 9999;
6193 win_setwidth_win((int)n, wp);
6194 }
6195 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006196 {
6197 if (*eap->arg == '-' || *eap->arg == '+')
6198 n += curwin->w_height;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006199 else if (n == 0 && eap->arg[0] == NUL) // default is very high
Bram Moolenaar071d4272004-06-13 20:20:40 +00006200 n = 9999;
6201 win_setheight_win((int)n, wp);
6202 }
6203}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006204
6205/*
6206 * ":find [+command] <file>" command.
6207 */
6208 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006209ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006210{
6211#ifdef FEAT_SEARCHPATH
6212 char_u *fname;
6213 int count;
6214
6215 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
6216 TRUE, curbuf->b_ffname);
6217 if (eap->addr_count > 0)
6218 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006219 // Repeat finding the file "count" times. This matters when it
6220 // appears several times in the path.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006221 count = eap->line2;
6222 while (fname != NULL && --count > 0)
6223 {
6224 vim_free(fname);
6225 fname = find_file_in_path(NULL, 0, FNAME_MESS,
6226 FALSE, curbuf->b_ffname);
6227 }
6228 }
6229
6230 if (fname != NULL)
6231 {
6232 eap->arg = fname;
6233#endif
6234 do_exedit(eap, NULL);
6235#ifdef FEAT_SEARCHPATH
6236 vim_free(fname);
6237 }
6238#endif
6239}
6240
6241/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00006242 * ":open" simulation: for now just work like ":visual".
6243 */
6244 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006245ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006246{
6247 regmatch_T regmatch;
6248 char_u *p;
6249
6250 curwin->w_cursor.lnum = eap->line2;
6251 beginline(BL_SOL | BL_FIX);
6252 if (*eap->arg == '/')
6253 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006254 // ":open /pattern/": put cursor in column found with pattern
Bram Moolenaardf177f62005-02-22 08:39:57 +00006255 ++eap->arg;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006256 p = skip_regexp(eap->arg, '/', p_magic);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006257 *p = NUL;
6258 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
6259 if (regmatch.regprog != NULL)
6260 {
6261 regmatch.rm_ic = p_ic;
6262 p = ml_get_curline();
6263 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006264 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006265 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006266 emsg(_(e_nomatch));
Bram Moolenaar473de612013-06-08 18:19:48 +02006267 vim_regfree(regmatch.regprog);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006268 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006269 // Move to the NUL, ignore any other arguments.
Bram Moolenaardf177f62005-02-22 08:39:57 +00006270 eap->arg += STRLEN(eap->arg);
6271 }
6272 check_cursor();
6273
6274 eap->cmdidx = CMD_visual;
6275 do_exedit(eap, NULL);
6276}
6277
6278/*
6279 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006280 */
6281 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006282ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006283{
6284 do_exedit(eap, NULL);
6285}
6286
6287/*
Bram Moolenaar4b96df52020-01-26 22:00:26 +01006288 * ":edit <file>" command and alike.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006289 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006290 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006291do_exedit(
6292 exarg_T *eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006293 win_T *old_curwin) // curwin before doing a split or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006294{
6295 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006296 int need_hide;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006297 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006298
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01006299 if ((eap->cmdidx != CMD_pedit && ERROR_IF_POPUP_WINDOW)
6300 || ERROR_IF_TERM_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006301 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006302 /*
6303 * ":vi" command ends Ex mode.
6304 */
6305 if (exmode_active && (eap->cmdidx == CMD_visual
6306 || eap->cmdidx == CMD_view))
6307 {
6308 exmode_active = FALSE;
Bram Moolenaar158ea172020-06-18 17:28:39 +02006309 ex_pressedreturn = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006310 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006311 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006312 // Special case: ":global/pat/visual\NLvi-commands"
Bram Moolenaardf177f62005-02-22 08:39:57 +00006313 if (global_busy)
6314 {
6315 int rd = RedrawingDisabled;
6316 int nwr = no_wait_return;
6317 int ms = msg_scroll;
6318#ifdef FEAT_GUI
6319 int he = hold_gui_events;
6320#endif
6321
6322 if (eap->nextcmd != NULL)
6323 {
6324 stuffReadbuff(eap->nextcmd);
6325 eap->nextcmd = NULL;
6326 }
6327
6328 if (exmode_was != EXMODE_VIM)
6329 settmode(TMODE_RAW);
6330 RedrawingDisabled = 0;
6331 no_wait_return = 0;
6332 need_wait_return = FALSE;
6333 msg_scroll = 0;
6334#ifdef FEAT_GUI
6335 hold_gui_events = 0;
6336#endif
6337 must_redraw = CLEAR;
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01006338 pending_exmode_active = TRUE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006339
6340 main_loop(FALSE, TRUE);
6341
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01006342 pending_exmode_active = FALSE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006343 RedrawingDisabled = rd;
6344 no_wait_return = nwr;
6345 msg_scroll = ms;
6346#ifdef FEAT_GUI
6347 hold_gui_events = he;
6348#endif
6349 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006350 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006351 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006352 }
6353
6354 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006355 || eap->cmdidx == CMD_tabnew
6356 || eap->cmdidx == CMD_tabedit
Bram Moolenaar4033c552017-09-16 20:54:51 +02006357 || eap->cmdidx == CMD_vnew) && *eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006358 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006359 // ":new" or ":tabnew" without argument: edit an new empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006360 setpcmark();
6361 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006362 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
6363 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006364 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006365 else if ((eap->cmdidx != CMD_split && eap->cmdidx != CMD_vsplit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006366 || *eap->arg != NUL
6367#ifdef FEAT_BROWSE
6368 || cmdmod.browse
6369#endif
6370 )
6371 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006372 // Can't edit another file when "curbuf_lock" is set. Only ":edit"
6373 // can bring us here, others are stopped earlier.
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006374 if (*eap->arg != NUL && curbuf_locked())
6375 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006376
Bram Moolenaar071d4272004-06-13 20:20:40 +00006377 n = readonlymode;
6378 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
6379 readonlymode = TRUE;
6380 else if (eap->cmdidx == CMD_enew)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006381 readonlymode = FALSE; // 'readonly' doesn't make sense in an
6382 // empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006383 setpcmark();
6384 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
6385 NULL, eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006386 // ":edit" goes to first line if Vi compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00006387 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
6388 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
6389 ? ECMD_ONE : eap->do_ecmd_lnum,
Bram Moolenaareb44a682017-08-03 22:44:55 +02006390 (buf_hide(curbuf) ? ECMD_HIDE : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006391 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006392 // after a split we can use an existing buffer
Bram Moolenaar7b449342014-03-25 13:03:48 +01006393 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006394 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006395 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006396 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006397 // Editing the file failed. If the window was split, close it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006398 if (old_curwin != NULL)
6399 {
6400 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02006401 if (!need_hide || buf_hide(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006402 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006403#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006404 cleanup_T cs;
6405
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006406 // Reset the error/interrupt/exception state here so that
6407 // aborting() returns FALSE when closing a window.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006408 enter_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02006409#endif
6410#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006411 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006412#endif
Bram Moolenaareb44a682017-08-03 22:44:55 +02006413 win_close(curwin, !need_hide && !buf_hide(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006414
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006415#if defined(FEAT_EVAL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006416 // Restore the error/interrupt/exception state if not
6417 // discarded by a new aborting error, interrupt, or
6418 // uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006419 leave_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02006420#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006421 }
6422 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006423 }
6424 else if (readonlymode && curbuf->b_nwindows == 1)
6425 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006426 // When editing an already visited buffer, 'readonly' won't be set
6427 // but the previous value is kept. With ":view" and ":sview" we
6428 // want the file to be readonly, except when another window is
6429 // editing the same buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006430 curbuf->b_p_ro = TRUE;
6431 }
6432 readonlymode = n;
6433 }
6434 else
6435 {
6436 if (eap->do_ecmd_cmd != NULL)
6437 do_cmdline_cmd(eap->do_ecmd_cmd);
6438#ifdef FEAT_TITLE
6439 n = curwin->w_arg_idx_invalid;
6440#endif
6441 check_arg_idx(curwin);
6442#ifdef FEAT_TITLE
6443 if (n != curwin->w_arg_idx_invalid)
6444 maketitle();
6445#endif
6446 }
6447
Bram Moolenaar071d4272004-06-13 20:20:40 +00006448 /*
6449 * if ":split file" worked, set alternate file name in old window to new
6450 * file
6451 */
6452 if (old_curwin != NULL
6453 && *eap->arg != NUL
6454 && curwin != old_curwin
6455 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00006456 && old_curwin->w_buffer != curbuf
6457 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006458 old_curwin->w_alt_fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006459
6460 ex_no_reprint = TRUE;
6461}
6462
6463#ifndef FEAT_GUI
6464/*
6465 * ":gui" and ":gvim" when there is no GUI.
6466 */
6467 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006468ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006469{
6470 eap->errmsg = e_nogvim;
6471}
6472#endif
6473
Bram Moolenaar4f974752019-02-17 17:44:42 +01006474#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006475 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006476ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006477{
6478 gui_make_tearoff(eap->arg);
6479}
6480#endif
6481
Bram Moolenaar29a2c082018-03-05 21:06:23 +01006482#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
6483 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006484 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006485ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006486{
Bram Moolenaar29a2c082018-03-05 21:06:23 +01006487# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
6488 if (gui.in_use)
6489 gui_make_popup(eap->arg, eap->forceit);
6490# ifdef FEAT_TERM_POPUP_MENU
6491 else
6492# endif
6493# endif
6494# ifdef FEAT_TERM_POPUP_MENU
6495 pum_make_popup(eap->arg, eap->forceit);
6496# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006497}
6498#endif
6499
Bram Moolenaar071d4272004-06-13 20:20:40 +00006500 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006501ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006502{
6503 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006504 msg(_("No swap file"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006505 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01006506 msg((char *)curbuf->b_ml.ml_mfp->mf_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006507}
6508
6509/*
6510 * ":syncbind" forces all 'scrollbind' windows to have the same relative
6511 * offset.
6512 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
6513 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006514 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006515ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006516{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006517 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006518 win_T *save_curwin = curwin;
6519 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006520 long topline;
6521 long y;
6522 linenr_T old_linenr = curwin->w_cursor.lnum;
6523
6524 setpcmark();
6525
6526 /*
6527 * determine max topline
6528 */
6529 if (curwin->w_p_scb)
6530 {
6531 topline = curwin->w_topline;
Bram Moolenaar29323592016-07-24 22:04:11 +02006532 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006533 {
6534 if (wp->w_p_scb && wp->w_buffer)
6535 {
Bram Moolenaar375e3392019-01-31 18:26:10 +01006536 y = wp->w_buffer->b_ml.ml_line_count - get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006537 if (topline > y)
6538 topline = y;
6539 }
6540 }
6541 if (topline < 1)
6542 topline = 1;
6543 }
6544 else
6545 {
6546 topline = 1;
6547 }
6548
6549
6550 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006551 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006552 */
Bram Moolenaar29323592016-07-24 22:04:11 +02006553 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006554 {
6555 if (curwin->w_p_scb)
6556 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006557 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006558 y = topline - curwin->w_topline;
6559 if (y > 0)
6560 scrollup(y, TRUE);
6561 else
6562 scrolldown(-y, TRUE);
6563 curwin->w_scbind_pos = topline;
6564 redraw_later(VALID);
6565 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006566 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006567 }
6568 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006569 curwin = save_curwin;
6570 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006571 if (curwin->w_p_scb)
6572 {
6573 did_syncbind = TRUE;
6574 checkpcmark();
6575 if (old_linenr != curwin->w_cursor.lnum)
6576 {
6577 char_u ctrl_o[2];
6578
6579 ctrl_o[0] = Ctrl_O;
6580 ctrl_o[1] = 0;
6581 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
6582 }
6583 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006584}
6585
6586
6587 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006588ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006589{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006590 int i;
6591 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
6592 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006593
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006594 if (eap->usefilter) // :r!cmd
Bram Moolenaar071d4272004-06-13 20:20:40 +00006595 do_bang(1, eap, FALSE, FALSE, TRUE);
6596 else
6597 {
6598 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
6599 return;
6600
6601#ifdef FEAT_BROWSE
6602 if (cmdmod.browse)
6603 {
6604 char_u *browseFile;
6605
Bram Moolenaar7171abe2004-10-11 10:06:20 +00006606 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006607 NULL, NULL, NULL, curbuf);
6608 if (browseFile != NULL)
6609 {
6610 i = readfile(browseFile, NULL,
6611 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
6612 vim_free(browseFile);
6613 }
6614 else
6615 i = OK;
6616 }
6617 else
6618#endif
6619 if (*eap->arg == NUL)
6620 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006621 if (check_fname() == FAIL) // check for no file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00006622 return;
6623 i = readfile(curbuf->b_ffname, curbuf->b_fname,
6624 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
6625 }
6626 else
6627 {
6628 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
6629 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
6630 i = readfile(eap->arg, NULL,
6631 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
6632
6633 }
Bram Moolenaare13b9af2017-01-13 22:01:02 +01006634 if (i != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006635 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006636#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006637 if (!aborting())
6638#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006639 semsg(_(e_notopen), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006640 }
6641 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00006642 {
6643 if (empty && exmode_active)
6644 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006645 // Delete the empty line that remains. Historically ex does
6646 // this but vi doesn't.
Bram Moolenaardf177f62005-02-22 08:39:57 +00006647 if (eap->line2 == 0)
6648 lnum = curbuf->b_ml.ml_line_count;
6649 else
6650 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00006651 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006652 {
Bram Moolenaarca70c072020-05-30 20:30:46 +02006653 ml_delete(lnum);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00006654 if (curwin->w_cursor.lnum > 1
6655 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006656 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00006657 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006658 }
6659 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006660 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006661 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006662 }
6663}
6664
Bram Moolenaarea408852005-06-25 22:49:46 +00006665static char_u *prev_dir = NULL;
6666
6667#if defined(EXITFREE) || defined(PROTO)
6668 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006669free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00006670{
Bram Moolenaard23a8232018-02-10 18:45:26 +01006671 VIM_CLEAR(prev_dir);
6672 VIM_CLEAR(globaldir);
Bram Moolenaarea408852005-06-25 22:49:46 +00006673}
6674#endif
6675
Bram Moolenaarf4258302013-06-02 18:20:17 +02006676/*
Bram Moolenaar002bc792020-06-05 22:33:42 +02006677 * Get the previous directory for the given chdir scope.
6678 */
6679 static char_u *
6680get_prevdir(cdscope_T scope)
6681{
6682 if (scope == CDSCOPE_WINDOW)
6683 return curwin->w_prevdir;
6684 else if (scope == CDSCOPE_TABPAGE)
6685 return curtab->tp_prevdir;
6686 return prev_dir;
6687}
6688
6689/*
Bram Moolenaarf4258302013-06-02 18:20:17 +02006690 * Deal with the side effects of changing the current directory.
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006691 * When 'scope' is CDSCOPE_TABPAGE then this was after an ":tcd" command.
6692 * When 'scope' is CDSCOPE_WINDOW then this was after an ":lcd" command.
Bram Moolenaarf4258302013-06-02 18:20:17 +02006693 */
6694 void
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006695post_chdir(cdscope_T scope)
Bram Moolenaarf4258302013-06-02 18:20:17 +02006696{
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006697 if (scope != CDSCOPE_WINDOW)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006698 // Clear tab local directory for both :cd and :tcd
6699 VIM_CLEAR(curtab->tp_localdir);
Bram Moolenaard23a8232018-02-10 18:45:26 +01006700 VIM_CLEAR(curwin->w_localdir);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006701 if (scope != CDSCOPE_GLOBAL)
Bram Moolenaarf4258302013-06-02 18:20:17 +02006702 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02006703 char_u *pdir = get_prevdir(scope);
6704
6705 // If still in the global directory, need to remember current
6706 // directory as the global directory.
6707 if (globaldir == NULL && pdir != NULL)
6708 globaldir = vim_strsave(pdir);
6709
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006710 // Remember this local directory for the window.
Bram Moolenaarf4258302013-06-02 18:20:17 +02006711 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006712 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006713 if (scope == CDSCOPE_TABPAGE)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006714 curtab->tp_localdir = vim_strsave(NameBuff);
6715 else
6716 curwin->w_localdir = vim_strsave(NameBuff);
6717 }
Bram Moolenaarf4258302013-06-02 18:20:17 +02006718 }
6719 else
6720 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02006721 // We are now in the global directory, no need to remember its name.
Bram Moolenaard23a8232018-02-10 18:45:26 +01006722 VIM_CLEAR(globaldir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006723 }
6724
6725 shorten_fnames(TRUE);
6726}
6727
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006728/*
6729 * Change directory function used by :cd/:tcd/:lcd Ex commands and the
Bram Moolenaar95058722020-06-01 16:26:19 +02006730 * chdir() function.
6731 * scope == CDSCOPE_WINDOW: changes the window-local directory
6732 * scope == CDSCOPE_TABPAGE: changes the tab-local directory
6733 * Otherwise: changes the global directory
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006734 * Returns TRUE if the directory is successfully changed.
6735 */
6736 int
6737changedir_func(
6738 char_u *new_dir,
6739 int forceit,
6740 cdscope_T scope)
6741{
6742 char_u *tofree;
Bram Moolenaar002bc792020-06-05 22:33:42 +02006743 char_u *pdir = NULL;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006744 int dir_differs;
6745 int retval = FALSE;
6746
Bram Moolenaar7cc96922020-01-31 22:41:38 +01006747 if (new_dir == NULL || allbuf_locked())
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006748 return FALSE;
6749
6750 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged() && !forceit)
6751 {
6752 emsg(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
6753 return FALSE;
6754 }
6755
6756 // ":cd -": Change to previous directory
6757 if (STRCMP(new_dir, "-") == 0)
6758 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02006759 pdir = get_prevdir(scope);
6760 if (pdir == NULL)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006761 {
6762 emsg(_("E186: No previous directory"));
6763 return FALSE;
6764 }
Bram Moolenaar002bc792020-06-05 22:33:42 +02006765 new_dir = pdir;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006766 }
6767
Bram Moolenaar002bc792020-06-05 22:33:42 +02006768 // Free the previous directory
6769 tofree = get_prevdir(scope);
6770
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006771 // Save current directory for next ":cd -"
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006772 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar002bc792020-06-05 22:33:42 +02006773 pdir = vim_strsave(NameBuff);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006774 else
Bram Moolenaar002bc792020-06-05 22:33:42 +02006775 pdir = NULL;
6776 if (scope == CDSCOPE_WINDOW)
6777 curwin->w_prevdir = pdir;
6778 else if (scope == CDSCOPE_TABPAGE)
6779 curtab->tp_prevdir = pdir;
6780 else
6781 prev_dir = pdir;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006782
6783#if defined(UNIX) || defined(VMS)
6784 // for UNIX ":cd" means: go to home directory
6785 if (*new_dir == NUL)
6786 {
6787 // use NameBuff for home directory name
6788# ifdef VMS
6789 char_u *p;
6790
6791 p = mch_getenv((char_u *)"SYS$LOGIN");
6792 if (p == NULL || *p == NUL) // empty is the same as not set
6793 NameBuff[0] = NUL;
6794 else
6795 vim_strncpy(NameBuff, p, MAXPATHL - 1);
6796# else
6797 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
6798# endif
6799 new_dir = NameBuff;
6800 }
6801#endif
Bram Moolenaar002bc792020-06-05 22:33:42 +02006802 dir_differs = new_dir == NULL || pdir == NULL
6803 || pathcmp((char *)pdir, (char *)new_dir, -1) != 0;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006804 if (new_dir == NULL || (dir_differs && vim_chdir(new_dir)))
6805 emsg(_(e_failed));
6806 else
6807 {
6808 char_u *acmd_fname;
6809
6810 post_chdir(scope);
6811
6812 if (dir_differs)
6813 {
6814 if (scope == CDSCOPE_WINDOW)
6815 acmd_fname = (char_u *)"window";
6816 else if (scope == CDSCOPE_TABPAGE)
6817 acmd_fname = (char_u *)"tabpage";
6818 else
6819 acmd_fname = (char_u *)"global";
6820 apply_autocmds(EVENT_DIRCHANGED, acmd_fname, new_dir, FALSE,
6821 curbuf);
6822 }
6823 retval = TRUE;
6824 }
6825 vim_free(tofree);
6826
6827 return retval;
6828}
Bram Moolenaarea408852005-06-25 22:49:46 +00006829
Bram Moolenaar071d4272004-06-13 20:20:40 +00006830/*
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006831 * ":cd", ":tcd", ":lcd", ":chdir" ":tchdir" and ":lchdir".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006832 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00006833 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006834ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006835{
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01006836 char_u *new_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006837
6838 new_dir = eap->arg;
6839#if !defined(UNIX) && !defined(VMS)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006840 // for non-UNIX ":cd" means: print current directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006841 if (*new_dir == NUL)
6842 ex_pwd(NULL);
6843 else
6844#endif
6845 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006846 cdscope_T scope = CDSCOPE_GLOBAL;
6847
6848 if (eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir)
6849 scope = CDSCOPE_WINDOW;
6850 else if (eap->cmdidx == CMD_tcd || eap->cmdidx == CMD_tchdir)
6851 scope = CDSCOPE_TABPAGE;
6852
6853 if (changedir_func(new_dir, eap->forceit, scope))
Bram Moolenaardf177f62005-02-22 08:39:57 +00006854 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006855 // Echo the new current directory if the command was typed.
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00006856 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006857 ex_pwd(eap);
6858 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006859 }
6860}
6861
6862/*
6863 * ":pwd".
6864 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006865 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006866ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006867{
6868 if (mch_dirname(NameBuff, MAXPATHL) == OK)
6869 {
6870#ifdef BACKSLASH_IN_FILENAME
6871 slash_adjust(NameBuff);
6872#endif
Bram Moolenaar95058722020-06-01 16:26:19 +02006873 if (p_verbose > 0)
6874 {
6875 char *context = "global";
6876
6877 if (curwin->w_localdir != NULL)
6878 context = "window";
6879 else if (curtab->tp_localdir != NULL)
6880 context = "tabpage";
6881 smsg("[%s] %s", context, (char *)NameBuff);
6882 }
6883 else
6884 msg((char *)NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006885 }
6886 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006887 emsg(_("E187: Unknown"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006888}
6889
6890/*
6891 * ":=".
6892 */
6893 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006894ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006895{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006896 smsg("%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006897 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006898}
6899
6900 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006901ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006902{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006903 int n;
6904 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006905
6906 if (cursor_valid())
6907 {
6908 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
6909 if (n >= 0)
Bram Moolenaar53f81742017-09-22 14:35:51 +02006910 windgoto((int)n, curwin->w_wincol + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006911 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006912
6913 len = eap->line2;
6914 switch (*eap->arg)
6915 {
6916 case 'm': break;
6917 case NUL: len *= 1000L; break;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006918 default: semsg(_(e_invarg2), eap->arg); return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006919 }
6920 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006921}
6922
6923/*
6924 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
6925 */
6926 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006927do_sleep(long msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006928{
Bram Moolenaar52953192019-08-16 10:27:13 +02006929 long done = 0;
Bram Moolenaar975b5272016-03-15 23:10:59 +01006930 long wait_now;
Bram Moolenaar52953192019-08-16 10:27:13 +02006931# ifdef ELAPSED_FUNC
6932 elapsed_T start_tv;
6933
6934 // Remember at what time we started, so that we know how much longer we
6935 // should wait after waiting for a bit.
6936 ELAPSED_INIT(start_tv);
6937# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006938
6939 cursor_on();
Bram Moolenaarf45d7472018-09-30 18:22:26 +02006940 out_flush_cursor(FALSE, FALSE);
Bram Moolenaar52953192019-08-16 10:27:13 +02006941 while (!got_int && done < msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006942 {
Bram Moolenaar975b5272016-03-15 23:10:59 +01006943 wait_now = msec - done > 1000L ? 1000L : msec - done;
6944#ifdef FEAT_TIMERS
6945 {
6946 long due_time = check_due_timer();
6947
6948 if (due_time > 0 && due_time < wait_now)
6949 wait_now = due_time;
6950 }
6951#endif
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02006952#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar28e67e02019-08-15 23:05:49 +02006953 if (has_any_channel() && wait_now > 20L)
6954 wait_now = 20L;
6955#endif
6956#ifdef FEAT_SOUND
6957 if (has_any_sound_callback() && wait_now > 20L)
6958 wait_now = 20L;
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02006959#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +01006960 ui_delay(wait_now, TRUE);
Bram Moolenaar52953192019-08-16 10:27:13 +02006961
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02006962#ifdef FEAT_JOB_CHANNEL
6963 if (has_any_channel())
6964 ui_breakcheck_force(TRUE);
6965 else
6966#endif
6967 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006968#ifdef MESSAGE_QUEUE
Bram Moolenaar52953192019-08-16 10:27:13 +02006969 // Process the netbeans and clientserver messages that may have been
6970 // received in the call to ui_breakcheck() when the GUI is in use. This
6971 // may occur when running a test case.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006972 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02006973#endif
Bram Moolenaar52953192019-08-16 10:27:13 +02006974
6975# ifdef ELAPSED_FUNC
6976 // actual time passed
6977 done = ELAPSED_FUNC(start_tv);
6978# else
6979 // guestimate time passed (will actually be more)
6980 done += wait_now;
6981# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006982 }
Bram Moolenaar1ebff3d2018-07-07 16:18:13 +02006983
6984 // If CTRL-C was typed to interrupt the sleep, drop the CTRL-C from the
6985 // input buffer, otherwise a following call to input() fails.
6986 if (got_int)
6987 (void)vpeekc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006988}
6989
Bram Moolenaar071d4272004-06-13 20:20:40 +00006990/*
6991 * ":winsize" command (obsolete).
6992 */
6993 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006994ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006995{
6996 int w, h;
6997 char_u *arg = eap->arg;
6998 char_u *p;
6999
7000 w = getdigits(&arg);
7001 arg = skipwhite(arg);
7002 p = arg;
7003 h = getdigits(&arg);
7004 if (*p != NUL && *arg == NUL)
7005 set_shellsize(w, h, TRUE);
7006 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007007 emsg(_("E465: :winsize requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007008}
7009
Bram Moolenaar071d4272004-06-13 20:20:40 +00007010 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007011ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007012{
7013 int xchar = NUL;
7014 char_u *p;
7015
7016 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
7017 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007018 // CTRL-W g and CTRL-W CTRL-G have an extra command character
Bram Moolenaar071d4272004-06-13 20:20:40 +00007019 if (eap->arg[1] == NUL)
7020 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007021 emsg(_(e_invarg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007022 return;
7023 }
7024 xchar = eap->arg[1];
7025 p = eap->arg + 2;
7026 }
7027 else
7028 p = eap->arg + 1;
7029
7030 eap->nextcmd = check_nextcmd(p);
7031 p = skipwhite(p);
7032 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007033 emsg(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02007034 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007035 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007036 // Pass flags on for ":vertical wincmd ]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007037 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00007038 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007039 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
7040 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00007041 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007042 }
7043}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007044
Bram Moolenaar843ee412004-06-30 16:16:41 +00007045#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007046/*
7047 * ":winpos".
7048 */
7049 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007050ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007051{
7052 int x, y;
7053 char_u *arg = eap->arg;
7054 char_u *p;
7055
7056 if (*arg == NUL)
7057 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00007058# if defined(FEAT_GUI) || defined(MSWIN)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007059# ifdef VIMDLL
7060 if (gui.in_use ? gui_mch_get_winpos(&x, &y) != FAIL :
7061 mch_get_winpos(&x, &y) != FAIL)
7062# elif defined(FEAT_GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007063 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00007064# else
7065 if (mch_get_winpos(&x, &y) != FAIL)
7066# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007067 {
7068 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
Bram Moolenaar32526b32019-01-19 17:43:09 +01007069 msg((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007070 }
7071 else
7072# endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007073 emsg(_("E188: Obtaining window position not implemented for this platform"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007074 }
7075 else
7076 {
7077 x = getdigits(&arg);
7078 arg = skipwhite(arg);
7079 p = arg;
7080 y = getdigits(&arg);
7081 if (*p == NUL || *arg != NUL)
7082 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007083 emsg(_("E466: :winpos requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007084 return;
7085 }
7086# ifdef FEAT_GUI
7087 if (gui.in_use)
7088 gui_mch_set_winpos(x, y);
7089 else if (gui.starting)
7090 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007091 // Remember the coordinates for when the window is opened.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007092 gui_win_x = x;
7093 gui_win_y = y;
7094 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007095# if defined(HAVE_TGETENT) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007096 else
7097# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007098# endif
7099# if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar843ee412004-06-30 16:16:41 +00007100 mch_set_winpos(x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007101# endif
7102# ifdef HAVE_TGETENT
7103 if (*T_CWP)
7104 term_set_winpos(x, y);
7105# endif
7106 }
7107}
7108#endif
7109
7110/*
7111 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
7112 */
7113 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007114ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007115{
7116 oparg_T oa;
7117
7118 clear_oparg(&oa);
7119 oa.regname = eap->regname;
7120 oa.start.lnum = eap->line1;
7121 oa.end.lnum = eap->line2;
7122 oa.line_count = eap->line2 - eap->line1 + 1;
7123 oa.motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007124 virtual_op = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007125 if (eap->cmdidx != CMD_yank) // position cursor for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00007126 {
7127 setpcmark();
7128 curwin->w_cursor.lnum = eap->line1;
7129 beginline(BL_SOL | BL_FIX);
7130 }
7131
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007132 if (VIsual_active)
7133 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007134
Bram Moolenaar071d4272004-06-13 20:20:40 +00007135 switch (eap->cmdidx)
7136 {
7137 case CMD_delete:
7138 oa.op_type = OP_DELETE;
7139 op_delete(&oa);
7140 break;
7141
7142 case CMD_yank:
7143 oa.op_type = OP_YANK;
7144 (void)op_yank(&oa, FALSE, TRUE);
7145 break;
7146
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007147 default: // CMD_rshift or CMD_lshift
Bram Moolenaar6e707362013-06-14 19:15:58 +02007148 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007149#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02007150 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
7151#else
7152 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00007153#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02007154 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007155 oa.op_type = OP_RSHIFT;
7156 else
7157 oa.op_type = OP_LSHIFT;
7158 op_shift(&oa, FALSE, eap->amount);
7159 break;
7160 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007161 virtual_op = MAYBE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007162 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007163}
7164
7165/*
7166 * ":put".
7167 */
7168 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007169ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007170{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007171 // ":0put" works like ":1put!".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007172 if (eap->line2 == 0)
7173 {
7174 eap->line2 = 1;
7175 eap->forceit = TRUE;
7176 }
7177 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007178 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
7179 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007180}
7181
7182/*
7183 * Handle ":copy" and ":move".
7184 */
7185 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007186ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007187{
7188 long n;
7189
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02007190 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE, FALSE, 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007191 if (eap->arg == NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00007192 {
7193 eap->nextcmd = NULL;
7194 return;
7195 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00007196 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007197
7198 /*
7199 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
7200 */
7201 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
7202 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02007203 emsg(_(e_invrange));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007204 return;
7205 }
7206
7207 if (eap->cmdidx == CMD_move)
7208 {
7209 if (do_move(eap->line1, eap->line2, n) == FAIL)
7210 return;
7211 }
7212 else
7213 ex_copy(eap->line1, eap->line2, n);
7214 u_clearline();
7215 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007216 ex_may_print(eap);
7217}
7218
7219/*
7220 * Print the current line if flags were given to the Ex command.
7221 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02007222 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007223ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007224{
7225 if (eap->flags != 0)
7226 {
7227 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
7228 (eap->flags & EXFLAG_LIST));
7229 ex_no_reprint = TRUE;
7230 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007231}
7232
7233/*
7234 * ":smagic" and ":snomagic".
7235 */
7236 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007237ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007238{
7239 int magic_save = p_magic;
7240
7241 p_magic = (eap->cmdidx == CMD_smagic);
7242 do_sub(eap);
7243 p_magic = magic_save;
7244}
7245
7246/*
7247 * ":join".
7248 */
7249 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007250ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007251{
7252 curwin->w_cursor.lnum = eap->line1;
7253 if (eap->line1 == eap->line2)
7254 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007255 if (eap->addr_count >= 2) // :2,2join does nothing
Bram Moolenaar071d4272004-06-13 20:20:40 +00007256 return;
7257 if (eap->line2 == curbuf->b_ml.ml_line_count)
7258 {
7259 beep_flush();
7260 return;
7261 }
7262 ++eap->line2;
7263 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02007264 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007265 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007266 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007267}
7268
7269/*
7270 * ":[addr]@r" or ":[addr]*r": execute register
7271 */
7272 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007273ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007274{
7275 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00007276 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007277
7278 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4930a762016-09-11 14:39:53 +02007279 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007280
7281#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007282 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00007283#endif
7284
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007285 // get the register name. No name means to use the previous one
Bram Moolenaar071d4272004-06-13 20:20:40 +00007286 c = *eap->arg;
7287 if (c == NUL || (c == '*' && *eap->cmd == '*'))
7288 c = '@';
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007289 // Put the register in the typeahead buffer with the "silent" flag.
Bram Moolenaard333d1e2006-11-07 17:43:47 +00007290 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
7291 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007292 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007293 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00007294 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007295 else
7296 {
7297 int save_efr = exec_from_reg;
7298
7299 exec_from_reg = TRUE;
7300
7301 /*
7302 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00007303 * Continue until the stuff buffer is empty and all added characters
7304 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007305 */
Bram Moolenaar60462872009-11-03 11:40:19 +00007306 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007307 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
7308
7309 exec_from_reg = save_efr;
7310 }
7311}
7312
7313/*
7314 * ":!".
7315 */
7316 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007317ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007318{
7319 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
7320}
7321
7322/*
7323 * ":undo".
7324 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007325 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01007326ex_undo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007327{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007328 if (eap->addr_count == 1) // :undo 123
Bram Moolenaar730cde92010-06-27 05:18:54 +02007329 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00007330 else
7331 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007332}
7333
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007334#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02007335 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007336ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007337{
7338 char_u hash[UNDO_HASH_SIZE];
7339
7340 u_compute_hash(hash);
7341 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
7342}
7343
Bram Moolenaar6df6f472010-07-18 18:04:50 +02007344 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007345ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007346{
7347 char_u hash[UNDO_HASH_SIZE];
7348
7349 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02007350 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007351}
7352#endif
7353
Bram Moolenaar071d4272004-06-13 20:20:40 +00007354/*
7355 * ":redo".
7356 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007357 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007358ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007359{
7360 u_redo(1);
7361}
7362
7363/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007364 * ":earlier" and ":later".
7365 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007366 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007367ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007368{
7369 long count = 0;
7370 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02007371 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007372 char_u *p = eap->arg;
7373
7374 if (*p == NUL)
7375 count = 1;
7376 else if (isdigit(*p))
7377 {
7378 count = getdigits(&p);
7379 switch (*p)
7380 {
7381 case 's': ++p; sec = TRUE; break;
7382 case 'm': ++p; sec = TRUE; count *= 60; break;
7383 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02007384 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
7385 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007386 }
7387 }
7388
7389 if (*p != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007390 semsg(_(e_invarg2), eap->arg);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007391 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02007392 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
7393 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007394}
7395
7396/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007397 * ":redir": start/stop redirection.
7398 */
7399 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007400ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007401{
7402 char *mode;
7403 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00007404 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007405
Bram Moolenaarba768492016-07-08 15:32:54 +02007406#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02007407 if (redir_execute)
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007408 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007409 emsg(_("E930: Cannot use :redir inside execute()"));
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007410 return;
7411 }
Bram Moolenaarba768492016-07-08 15:32:54 +02007412#endif
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007413
Bram Moolenaar071d4272004-06-13 20:20:40 +00007414 if (STRICMP(eap->arg, "END") == 0)
7415 close_redir();
7416 else
7417 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007418 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007419 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007420 ++arg;
7421 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007422 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007423 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007424 mode = "a";
7425 }
7426 else
7427 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00007428 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007429
7430 close_redir();
7431
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007432 // Expand environment variables and "~/".
Bram Moolenaarca472992005-01-21 11:46:23 +00007433 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007434 if (fname == NULL)
7435 return;
7436#ifdef FEAT_BROWSE
7437 if (cmdmod.browse)
7438 {
7439 char_u *browseFile;
7440
Bram Moolenaar7171abe2004-10-11 10:06:20 +00007441 browseFile = do_browse(BROWSE_SAVE,
7442 (char_u *)_("Save Redirection"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +02007443 fname, NULL, NULL,
7444 (char_u *)_(BROWSE_FILTER_ALL_FILES), curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007445 if (browseFile == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007446 return; // operation cancelled
Bram Moolenaar071d4272004-06-13 20:20:40 +00007447 vim_free(fname);
7448 fname = browseFile;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007449 eap->forceit = TRUE; // since dialog already asked
Bram Moolenaar071d4272004-06-13 20:20:40 +00007450 }
7451#endif
7452
7453 redir_fd = open_exfile(fname, eap->forceit, mode);
7454 vim_free(fname);
7455 }
7456#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00007457 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007458 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007459 // redirect to a register a-z (resp. A-Z for appending)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007460 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00007461 ++arg;
7462 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007463# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00007464 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00007465 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00007466# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00007467 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007468 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007469 redir_reg = *arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007470 if (*arg == '>' && arg[1] == '>') // append
Bram Moolenaard9d30582005-05-18 22:10:28 +00007471 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00007472 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007473 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007474 // Can use both "@a" and "@a>".
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00007475 if (*arg == '>')
7476 arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007477 // Make register empty when not using @A-@Z and the
7478 // command is valid.
Bram Moolenaarc188b882007-10-19 14:20:54 +00007479 if (*arg == NUL && !isupper(redir_reg))
7480 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007481 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00007482 }
7483 if (*arg != NUL)
7484 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007485 redir_reg = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007486 semsg(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007487 }
7488 }
7489 else if (*arg == '=' && arg[1] == '>')
7490 {
7491 int append;
7492
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007493 // redirect to a variable
Bram Moolenaardf177f62005-02-22 08:39:57 +00007494 close_redir();
7495 arg += 2;
7496
7497 if (*arg == '>')
7498 {
7499 ++arg;
7500 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007501 }
7502 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00007503 append = FALSE;
7504
7505 if (var_redir_start(skipwhite(arg), append) == OK)
7506 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007507 }
7508#endif
7509
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007510 // TODO: redirect to a buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00007511
Bram Moolenaar071d4272004-06-13 20:20:40 +00007512 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007513 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007514 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00007515
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007516 // Make sure redirection is not off. Can happen for cmdline completion
7517 // that indirectly invokes a command to catch its output.
Bram Moolenaar29b2d262006-09-10 19:07:28 +00007518 if (redir_fd != NULL
7519#ifdef FEAT_EVAL
7520 || redir_reg || redir_vname
7521#endif
7522 )
7523 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007524}
7525
7526/*
7527 * ":redraw": force redraw
7528 */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01007529 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007530ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007531{
7532 int r = RedrawingDisabled;
7533 int p = p_lz;
7534
7535 RedrawingDisabled = 0;
7536 p_lz = FALSE;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01007537 validate_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007538 update_topline();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007539 update_screen(eap->forceit ? CLEAR : VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007540#ifdef FEAT_TITLE
7541 if (need_maketitle)
7542 maketitle();
7543#endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007544#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
7545# ifdef VIMDLL
7546 if (!gui.in_use)
7547# endif
7548 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01007549#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007550 RedrawingDisabled = r;
7551 p_lz = p;
7552
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007553 // Reset msg_didout, so that a message that's there is overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007554 msg_didout = FALSE;
7555 msg_col = 0;
7556
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007557 // No need to wait after an intentional redraw.
Bram Moolenaar56667a52013-07-17 11:54:28 +02007558 need_wait_return = FALSE;
7559
Bram Moolenaar071d4272004-06-13 20:20:40 +00007560 out_flush();
7561}
7562
7563/*
7564 * ":redrawstatus": force redraw of status line(s)
7565 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007566 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007567ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007568{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007569 int r = RedrawingDisabled;
7570 int p = p_lz;
7571
7572 RedrawingDisabled = 0;
7573 p_lz = FALSE;
7574 if (eap->forceit)
7575 status_redraw_all();
7576 else
7577 status_redraw_curbuf();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007578 update_screen(VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007579 RedrawingDisabled = r;
7580 p_lz = p;
7581 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007582}
7583
Bram Moolenaare12bab32019-01-08 22:02:56 +01007584/*
7585 * ":redrawtabline": force redraw of the tabline
7586 */
7587 static void
7588ex_redrawtabline(exarg_T *eap UNUSED)
7589{
7590 int r = RedrawingDisabled;
7591 int p = p_lz;
7592
7593 RedrawingDisabled = 0;
7594 p_lz = FALSE;
7595
7596 draw_tabline();
7597
7598 RedrawingDisabled = r;
7599 p_lz = p;
7600 out_flush();
7601}
7602
Bram Moolenaar071d4272004-06-13 20:20:40 +00007603 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007604close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007605{
7606 if (redir_fd != NULL)
7607 {
7608 fclose(redir_fd);
7609 redir_fd = NULL;
7610 }
7611#ifdef FEAT_EVAL
7612 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007613 if (redir_vname)
7614 {
7615 var_redir_stop();
7616 redir_vname = 0;
7617 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007618#endif
7619}
7620
Bram Moolenaarf96ae0b2019-07-28 15:21:55 +02007621#if (defined(FEAT_SESSION) || defined(FEAT_EVAL)) || defined(PROTO)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007622 int
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02007623vim_mkdir_emsg(char_u *name, int prot UNUSED)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007624{
7625 if (vim_mkdir(name, prot) != 0)
7626 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007627 semsg(_("E739: Cannot create directory: %s"), name);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007628 return FAIL;
7629 }
7630 return OK;
7631}
7632#endif
7633
Bram Moolenaar071d4272004-06-13 20:20:40 +00007634/*
7635 * Open a file for writing for an Ex command, with some checks.
7636 * Return file descriptor, or NULL on failure.
7637 */
7638 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007639open_exfile(
7640 char_u *fname,
7641 int forceit,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007642 char *mode) // "w" for create new file or "a" for append
Bram Moolenaar071d4272004-06-13 20:20:40 +00007643{
7644 FILE *fd;
7645
7646#ifdef UNIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007647 // with Unix it is possible to open a directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007648 if (mch_isdir(fname))
7649 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007650 semsg(_(e_isadir2), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007651 return NULL;
7652 }
7653#endif
7654 if (!forceit && *mode != 'a' && vim_fexists(fname))
7655 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007656 semsg(_("E189: \"%s\" exists (add ! to override)"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007657 return NULL;
7658 }
7659
7660 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007661 semsg(_("E190: Cannot open \"%s\" for writing"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007662
7663 return fd;
7664}
7665
7666/*
7667 * ":mark" and ":k".
7668 */
7669 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007670ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007671{
7672 pos_T pos;
7673
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007674 if (*eap->arg == NUL) // No argument?
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007675 emsg(_(e_argreq));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007676 else if (eap->arg[1] != NUL) // more than one character?
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007677 emsg(_(e_trailing));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007678 else
7679 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007680 pos = curwin->w_cursor; // save curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00007681 curwin->w_cursor.lnum = eap->line2;
7682 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007683 if (setmark(*eap->arg) == FAIL) // set mark
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007684 emsg(_("E191: Argument must be a letter or forward/backward quote"));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007685 curwin->w_cursor = pos; // restore curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00007686 }
7687}
7688
7689/*
7690 * Update w_topline, w_leftcol and the cursor position.
7691 */
7692 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007693update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007694{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007695 check_cursor(); // put cursor on valid line
Bram Moolenaar071d4272004-06-13 20:20:40 +00007696 update_topline();
7697 if (!curwin->w_p_wrap)
7698 validate_cursor();
7699 update_curswant();
7700}
7701
Bram Moolenaar071d4272004-06-13 20:20:40 +00007702/*
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007703 * Save the current State and go to Normal mode.
7704 * Return TRUE if the typeahead could be saved.
7705 */
7706 int
7707save_current_state(save_state_T *sst)
7708{
7709 sst->save_msg_scroll = msg_scroll;
7710 sst->save_restart_edit = restart_edit;
7711 sst->save_msg_didout = msg_didout;
7712 sst->save_State = State;
7713 sst->save_insertmode = p_im;
7714 sst->save_finish_op = finish_op;
7715 sst->save_opcount = opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01007716 sst->save_reg_executing = reg_executing;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007717
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007718 msg_scroll = FALSE; // no msg scrolling in Normal mode
7719 restart_edit = 0; // don't go to Insert mode
7720 p_im = FALSE; // don't use 'insertmode'
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007721
7722 /*
7723 * Save the current typeahead. This is required to allow using ":normal"
7724 * from an event handler and makes sure we don't hang when the argument
7725 * ends with half a command.
7726 */
7727 save_typeahead(&sst->tabuf);
7728 return sst->tabuf.typebuf_valid;
7729}
7730
7731 void
7732restore_current_state(save_state_T *sst)
7733{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007734 // Restore the previous typeahead.
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007735 restore_typeahead(&sst->tabuf);
7736
7737 msg_scroll = sst->save_msg_scroll;
7738 restart_edit = sst->save_restart_edit;
7739 p_im = sst->save_insertmode;
7740 finish_op = sst->save_finish_op;
7741 opcount = sst->save_opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01007742 reg_executing = sst->save_reg_executing;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007743 msg_didout |= sst->save_msg_didout; // don't reset msg_didout now
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007744
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007745 // Restore the state (needed when called from a function executed for
7746 // 'indentexpr'). Update the mouse and cursor, they may have changed.
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007747 State = sst->save_State;
7748#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007749 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007750#endif
7751}
7752
7753/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007754 * ":normal[!] {commands}": Execute normal mode commands.
7755 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01007756 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007757ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007758{
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007759 save_state_T save_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007760 char_u *arg = NULL;
7761 int l;
7762 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007763
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00007764 if (ex_normal_lock > 0)
7765 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007766 emsg(_(e_secure));
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00007767 return;
7768 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007769 if (ex_normal_busy >= p_mmd)
7770 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007771 emsg(_("E192: Recursive use of :normal too deep"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007772 return;
7773 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007774
Bram Moolenaar071d4272004-06-13 20:20:40 +00007775 /*
7776 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
7777 * this for the K_SPECIAL leading byte, otherwise special keys will not
7778 * work.
7779 */
7780 if (has_mbyte)
7781 {
7782 int len = 0;
7783
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007784 // Count the number of characters to be escaped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007785 for (p = eap->arg; *p != NUL; ++p)
7786 {
Bram Moolenaar13505972019-01-24 15:04:48 +01007787#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007788 if (*p == CSI) // leadbyte CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007789 len += 2;
Bram Moolenaar13505972019-01-24 15:04:48 +01007790#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007791 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007792 if (*++p == K_SPECIAL // trailbyte K_SPECIAL or CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01007793#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007794 || *p == CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01007795#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007796 )
7797 len += 2;
7798 }
7799 if (len > 0)
7800 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02007801 arg = alloc(STRLEN(eap->arg) + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007802 if (arg != NULL)
7803 {
7804 len = 0;
7805 for (p = eap->arg; *p != NUL; ++p)
7806 {
7807 arg[len++] = *p;
Bram Moolenaar13505972019-01-24 15:04:48 +01007808#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007809 if (*p == CSI)
7810 {
7811 arg[len++] = KS_EXTRA;
7812 arg[len++] = (int)KE_CSI;
7813 }
Bram Moolenaar13505972019-01-24 15:04:48 +01007814#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007815 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007816 {
7817 arg[len++] = *++p;
7818 if (*p == K_SPECIAL)
7819 {
7820 arg[len++] = KS_SPECIAL;
7821 arg[len++] = KE_FILLER;
7822 }
Bram Moolenaar13505972019-01-24 15:04:48 +01007823#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007824 else if (*p == CSI)
7825 {
7826 arg[len++] = KS_EXTRA;
7827 arg[len++] = (int)KE_CSI;
7828 }
Bram Moolenaar13505972019-01-24 15:04:48 +01007829#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007830 }
7831 arg[len] = NUL;
7832 }
7833 }
7834 }
7835 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007836
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007837 ++ex_normal_busy;
7838 if (save_current_state(&save_state))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007839 {
7840 /*
7841 * Repeat the :normal command for each line in the range. When no
7842 * range given, execute it just once, without positioning the cursor
7843 * first.
7844 */
7845 do
7846 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007847 if (eap->addr_count != 0)
7848 {
7849 curwin->w_cursor.lnum = eap->line1++;
7850 curwin->w_cursor.col = 0;
Bram Moolenaard5d37532017-03-27 23:02:07 +02007851 check_cursor_moved(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007852 }
7853
Bram Moolenaar13505972019-01-24 15:04:48 +01007854 exec_normal_cmd(arg != NULL
7855 ? arg
7856 : eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007857 }
7858 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
7859 }
7860
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007861 // Might not return to the main loop when in an event handler.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007862 update_topline_cursor();
7863
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007864 restore_current_state(&save_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007865 --ex_normal_busy;
Bram Moolenaareda73602014-11-05 09:53:23 +01007866 setmouse();
Bram Moolenaareda73602014-11-05 09:53:23 +01007867#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007868 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaareda73602014-11-05 09:53:23 +01007869#endif
7870
Bram Moolenaar071d4272004-06-13 20:20:40 +00007871 vim_free(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007872}
7873
7874/*
Bram Moolenaar64969662005-12-14 21:59:55 +00007875 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007876 */
7877 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007878ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007879{
Bram Moolenaarfd371682005-01-14 21:42:54 +00007880 if (eap->forceit)
7881 {
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007882 // cursor line can be zero on startup
Bram Moolenaar09ca9322017-09-26 17:40:45 +02007883 if (!curwin->w_cursor.lnum)
7884 curwin->w_cursor.lnum = 1;
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007885 set_cursor_for_append_to_line();
Bram Moolenaarfd371682005-01-14 21:42:54 +00007886 }
Bram Moolenaareb58a242020-04-19 18:13:19 +02007887#ifdef FEAT_TERMINAL
7888 // Ignore this when running in an active terminal.
7889 if (term_job_running(curbuf->b_term))
7890 return;
7891#endif
Bram Moolenaarfd371682005-01-14 21:42:54 +00007892
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007893 // Ignore the command when already in Insert mode. Inserting an
7894 // expression register that invokes a function can do this.
Bram Moolenaara40c5002005-01-09 21:16:21 +00007895 if (State & INSERT)
7896 return;
7897
Bram Moolenaar64969662005-12-14 21:59:55 +00007898 if (eap->cmdidx == CMD_startinsert)
7899 restart_edit = 'a';
7900 else if (eap->cmdidx == CMD_startreplace)
7901 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00007902 else
Bram Moolenaar64969662005-12-14 21:59:55 +00007903 restart_edit = 'V';
7904
7905 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007906 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00007907 if (eap->cmdidx == CMD_startinsert)
7908 restart_edit = 'i';
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007909 curwin->w_curswant = 0; // avoid MAXCOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007910 }
7911}
7912
7913/*
7914 * ":stopinsert"
7915 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007916 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007917ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007918{
7919 restart_edit = 0;
7920 stop_insert_mode = TRUE;
Bram Moolenaarfd773e92016-04-02 19:39:16 +02007921 clearmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007922}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007923
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007924/*
7925 * Execute normal mode command "cmd".
7926 * "remap" can be REMAP_NONE or REMAP_YES.
7927 */
7928 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007929exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007930{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007931 // Stuff the argument into the typeahead buffer.
Bram Moolenaar25281632016-01-21 23:32:32 +01007932 ins_typebuf(cmd, remap, 0, TRUE, silent);
Bram Moolenaar586c70c2018-10-02 16:23:58 +02007933 exec_normal(FALSE, FALSE, FALSE);
Bram Moolenaar25281632016-01-21 23:32:32 +01007934}
Bram Moolenaar25281632016-01-21 23:32:32 +01007935
Bram Moolenaar25281632016-01-21 23:32:32 +01007936/*
7937 * Execute normal_cmd() until there is no typeahead left.
Bram Moolenaar586c70c2018-10-02 16:23:58 +02007938 * When "use_vpeekc" is TRUE use vpeekc() to check for available chars.
Bram Moolenaar25281632016-01-21 23:32:32 +01007939 */
7940 void
Bram Moolenaar586c70c2018-10-02 16:23:58 +02007941exec_normal(int was_typed, int use_vpeekc, int may_use_terminal_loop UNUSED)
Bram Moolenaar25281632016-01-21 23:32:32 +01007942{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007943 oparg_T oa;
Bram Moolenaar905dd902019-04-07 14:21:47 +02007944 int c;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007945
Bram Moolenaar905dd902019-04-07 14:21:47 +02007946 // When calling vpeekc() from feedkeys() it will return Ctrl_C when there
7947 // is nothing to get, so also check for Ctrl_C.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007948 clear_oparg(&oa);
7949 finish_op = FALSE;
Bram Moolenaar586c70c2018-10-02 16:23:58 +02007950 while ((!stuff_empty()
7951 || ((was_typed || !typebuf_typed()) && typebuf.tb_len > 0)
Bram Moolenaar905dd902019-04-07 14:21:47 +02007952 || (use_vpeekc && (c = vpeekc()) != NUL && c != Ctrl_C))
Bram Moolenaar586c70c2018-10-02 16:23:58 +02007953 && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007954 {
7955 update_topline_cursor();
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02007956#ifdef FEAT_TERMINAL
Bram Moolenaar655a82a2018-05-08 22:01:07 +02007957 if (may_use_terminal_loop && term_use_loop()
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02007958 && oa.op_type == OP_NOP && oa.regname == NUL
7959 && !VIsual_active)
7960 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007961 // If terminal_loop() returns OK we got a key that is handled
7962 // in Normal model. With FAIL we first need to position the
7963 // cursor and the screen needs to be redrawn.
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02007964 if (terminal_loop(TRUE) == OK)
7965 normal_cmd(&oa, TRUE);
7966 }
7967 else
7968#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007969 // execute a Normal mode cmd
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02007970 normal_cmd(&oa, TRUE);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007971 }
7972}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007973
Bram Moolenaar071d4272004-06-13 20:20:40 +00007974#ifdef FEAT_FIND_ID
7975 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007976ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007977{
7978 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
7979 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
7980 (linenr_T)1, (linenr_T)MAXLNUM);
7981}
7982
Bram Moolenaar4033c552017-09-16 20:54:51 +02007983#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007984/*
7985 * ":psearch"
7986 */
7987 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007988ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007989{
7990 g_do_tagpreview = p_pvh;
7991 ex_findpat(eap);
7992 g_do_tagpreview = 0;
7993}
7994#endif
7995
7996 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007997ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007998{
7999 int whole = TRUE;
8000 long n;
8001 char_u *p;
8002 int action;
8003
8004 switch (cmdnames[eap->cmdidx].cmd_name[2])
8005 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008006 case 'e': // ":psearch", ":isearch" and ":dsearch"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008007 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
8008 action = ACTION_GOTO;
8009 else
8010 action = ACTION_SHOW;
8011 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008012 case 'i': // ":ilist" and ":dlist"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008013 action = ACTION_SHOW_ALL;
8014 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008015 case 'u': // ":ijump" and ":djump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008016 action = ACTION_GOTO;
8017 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008018 default: // ":isplit" and ":dsplit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008019 action = ACTION_SPLIT;
8020 break;
8021 }
8022
8023 n = 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008024 if (vim_isdigit(*eap->arg)) // get count
Bram Moolenaar071d4272004-06-13 20:20:40 +00008025 {
8026 n = getdigits(&eap->arg);
8027 eap->arg = skipwhite(eap->arg);
8028 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008029 if (*eap->arg == '/') // Match regexp, not just whole words
Bram Moolenaar071d4272004-06-13 20:20:40 +00008030 {
8031 whole = FALSE;
8032 ++eap->arg;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02008033 p = skip_regexp(eap->arg, '/', p_magic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008034 if (*p)
8035 {
8036 *p++ = NUL;
8037 p = skipwhite(p);
8038
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008039 // Check for trailing illegal characters
Bram Moolenaarfaac4102020-04-20 17:46:14 +02008040 if (!ends_excmd2(eap->arg, p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008041 eap->errmsg = e_trailing;
8042 else
8043 eap->nextcmd = check_nextcmd(p);
8044 }
8045 }
8046 if (!eap->skip)
8047 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
8048 whole, !eap->forceit,
8049 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
8050 n, action, eap->line1, eap->line2);
8051}
8052#endif
8053
Bram Moolenaar071d4272004-06-13 20:20:40 +00008054
Bram Moolenaar4033c552017-09-16 20:54:51 +02008055#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00008056/*
8057 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
8058 */
8059 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008060ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008061{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008062 g_do_tagpreview = p_pvh; // will be reset to 0 in ex_tag_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00008063 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
8064}
8065
8066/*
8067 * ":pedit"
8068 */
8069 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008070ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008071{
8072 win_T *curwin_save = curwin;
8073
Bram Moolenaar3a2505c2020-03-09 16:40:41 +01008074 if (ERROR_IF_ANY_POPUP_WINDOW)
8075 return;
8076
Bram Moolenaar026587b2019-08-17 15:08:00 +02008077 // Open the preview window or popup and make it the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008078 g_do_tagpreview = p_pvh;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02008079 prepare_tagpreview(TRUE, TRUE, FALSE);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008080
Bram Moolenaar026587b2019-08-17 15:08:00 +02008081 // Edit the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008082 do_exedit(eap, NULL);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008083
Bram Moolenaar071d4272004-06-13 20:20:40 +00008084 if (curwin != curwin_save && win_valid(curwin_save))
8085 {
Bram Moolenaar026587b2019-08-17 15:08:00 +02008086 // Return cursor to where we were
Bram Moolenaar071d4272004-06-13 20:20:40 +00008087 validate_cursor();
8088 redraw_later(VALID);
8089 win_enter(curwin_save, TRUE);
8090 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01008091# ifdef FEAT_PROP_POPUP
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008092 else if (WIN_IS_POPUP(curwin))
8093 {
8094 // can't keep focus in popup window
8095 win_enter(firstwin, TRUE);
8096 }
8097# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008098 g_do_tagpreview = 0;
8099}
Bram Moolenaar4033c552017-09-16 20:54:51 +02008100#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008101
8102/*
8103 * ":stag", ":stselect" and ":stjump".
8104 */
8105 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008106ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008107{
8108 postponed_split = -1;
8109 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008110 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008111 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
8112 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008113 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008114}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008115
8116/*
8117 * ":tag", ":tselect", ":tjump", ":tnext", etc.
8118 */
8119 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008120ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008121{
8122 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
8123}
8124
8125 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008126ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008127{
8128 int cmd;
8129
8130 switch (name[1])
8131 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008132 case 'j': cmd = DT_JUMP; // ":tjump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008133 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008134 case 's': cmd = DT_SELECT; // ":tselect"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008135 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008136 case 'p': cmd = DT_PREV; // ":tprevious"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008137 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008138 case 'N': cmd = DT_PREV; // ":tNext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008139 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008140 case 'n': cmd = DT_NEXT; // ":tnext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008141 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008142 case 'o': cmd = DT_POP; // ":pop"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008143 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008144 case 'f': // ":tfirst"
8145 case 'r': cmd = DT_FIRST; // ":trewind"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008146 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008147 case 'l': cmd = DT_LAST; // ":tlast"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008148 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008149 default: // ":tag"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008150#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +00008151 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008152 {
Bram Moolenaard4db7712016-11-12 19:16:46 +01008153 ex_cstag(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008154 return;
8155 }
8156#endif
8157 cmd = DT_TAG;
8158 break;
8159 }
8160
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00008161 if (name[0] == 'l')
8162 {
8163#ifndef FEAT_QUICKFIX
8164 ex_ni(eap);
8165 return;
8166#else
8167 cmd = DT_LTAG;
8168#endif
8169 }
8170
Bram Moolenaar071d4272004-06-13 20:20:40 +00008171 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
8172 eap->forceit, TRUE);
8173}
8174
8175/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008176 * Check "str" for starting with a special cmdline variable.
8177 * If found return one of the SPEC_ values and set "*usedlen" to the length of
8178 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
8179 */
8180 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008181find_cmdline_var(char_u *src, int *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008182{
8183 int len;
8184 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +00008185 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008186 "%",
8187#define SPEC_PERC 0
8188 "#",
Bram Moolenaar65f08472017-09-10 18:16:20 +02008189#define SPEC_HASH (SPEC_PERC + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008190 "<cword>", // cursor word
Bram Moolenaar65f08472017-09-10 18:16:20 +02008191#define SPEC_CWORD (SPEC_HASH + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008192 "<cWORD>", // cursor WORD
Bram Moolenaar65f08472017-09-10 18:16:20 +02008193#define SPEC_CCWORD (SPEC_CWORD + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008194 "<cexpr>", // expr under cursor
Bram Moolenaar65f08472017-09-10 18:16:20 +02008195#define SPEC_CEXPR (SPEC_CCWORD + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008196 "<cfile>", // cursor path name
Bram Moolenaar65f08472017-09-10 18:16:20 +02008197#define SPEC_CFILE (SPEC_CEXPR + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008198 "<sfile>", // ":so" file name
Bram Moolenaar65f08472017-09-10 18:16:20 +02008199#define SPEC_SFILE (SPEC_CFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008200 "<slnum>", // ":so" file line number
Bram Moolenaar65f08472017-09-10 18:16:20 +02008201#define SPEC_SLNUM (SPEC_SFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008202 "<afile>", // autocommand file name
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008203#define SPEC_AFILE (SPEC_SLNUM + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008204 "<abuf>", // autocommand buffer number
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008205#define SPEC_ABUF (SPEC_AFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008206 "<amatch>", // autocommand match name
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008207#define SPEC_AMATCH (SPEC_ABUF + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008208 "<sflnum>", // script file line number
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008209#define SPEC_SFLNUM (SPEC_AMATCH + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008210#ifdef FEAT_CLIENTSERVER
8211 "<client>"
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008212# define SPEC_CLIENT (SPEC_SFLNUM + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008213#endif
8214 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008215
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008216 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008217 {
8218 len = (int)STRLEN(spec_str[i]);
8219 if (STRNCMP(src, spec_str[i], len) == 0)
8220 {
8221 *usedlen = len;
8222 return i;
8223 }
8224 }
8225 return -1;
8226}
8227
8228/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008229 * Evaluate cmdline variables.
8230 *
8231 * change '%' to curbuf->b_ffname
8232 * '#' to curwin->w_altfile
8233 * '<cword>' to word under the cursor
8234 * '<cWORD>' to WORD under the cursor
Bram Moolenaar8071cb22019-07-12 17:58:01 +02008235 * '<cexpr>' to C-expression under the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00008236 * '<cfile>' to path name under the cursor
8237 * '<sfile>' to sourced file name
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008238 * '<slnum>' to sourced file line number
Bram Moolenaar071d4272004-06-13 20:20:40 +00008239 * '<afile>' to file name for autocommand
8240 * '<abuf>' to buffer number for autocommand
8241 * '<amatch>' to matching name for autocommand
8242 *
8243 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
8244 * "" for error without a message) and NULL is returned.
8245 * Returns an allocated string if a valid match was found.
8246 * Returns NULL if no match was found. "usedlen" then still contains the
8247 * number of characters to skip.
8248 */
8249 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008250eval_vars(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008251 char_u *src, // pointer into commandline
8252 char_u *srcstart, // beginning of valid memory for src
8253 int *usedlen, // characters after src that are used
8254 linenr_T *lnump, // line number for :e command, or NULL
8255 char **errormsg, // pointer to error message
8256 int *escaped) // return value has escaped white space (can
8257 // be NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008258{
8259 int i;
8260 char_u *s;
8261 char_u *result;
8262 char_u *resultbuf = NULL;
8263 int resultlen;
8264 buf_T *buf;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008265 int valid = VALID_HEAD + VALID_PATH; // assume valid result
Bram Moolenaar071d4272004-06-13 20:20:40 +00008266 int spec_idx;
Bram Moolenaarfd249462018-07-28 16:14:30 +02008267 int tilde_file = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008268 int skip_mod = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008269 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008270
8271 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +00008272 if (escaped != NULL)
8273 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008274
8275 /*
8276 * Check if there is something to do.
8277 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008278 spec_idx = find_cmdline_var(src, usedlen);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008279 if (spec_idx < 0) // no match
Bram Moolenaar071d4272004-06-13 20:20:40 +00008280 {
8281 *usedlen = 1;
8282 return NULL;
8283 }
8284
8285 /*
8286 * Skip when preceded with a backslash "\%" and "\#".
8287 * Note: In "\\%" the % is also not recognized!
8288 */
8289 if (src > srcstart && src[-1] == '\\')
8290 {
8291 *usedlen = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008292 STRMOVE(src - 1, src); // remove backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00008293 return NULL;
8294 }
8295
8296 /*
8297 * word or WORD under cursor
8298 */
Bram Moolenaar65f08472017-09-10 18:16:20 +02008299 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD
8300 || spec_idx == SPEC_CEXPR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008301 {
Bram Moolenaar65f08472017-09-10 18:16:20 +02008302 resultlen = find_ident_under_cursor(&result,
8303 spec_idx == SPEC_CWORD ? (FIND_IDENT | FIND_STRING)
8304 : spec_idx == SPEC_CEXPR ? (FIND_IDENT | FIND_STRING | FIND_EVAL)
8305 : FIND_STRING);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008306 if (resultlen == 0)
8307 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008308 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008309 return NULL;
8310 }
8311 }
8312
8313 /*
8314 * '#': Alternate file name
8315 * '%': Current file name
8316 * File name under the cursor
8317 * File name for autocommand
8318 * and following modifiers
8319 */
8320 else
8321 {
8322 switch (spec_idx)
8323 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008324 case SPEC_PERC: // '%': current file
Bram Moolenaar071d4272004-06-13 20:20:40 +00008325 if (curbuf->b_fname == NULL)
8326 {
8327 result = (char_u *)"";
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008328 valid = 0; // Must have ":p:h" to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00008329 }
8330 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008331 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008332 result = curbuf->b_fname;
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008333 tilde_file = STRCMP(result, "~") == 0;
8334 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008335 break;
8336
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008337 case SPEC_HASH: // '#' or "#99": alternate file
8338 if (src[1] == '#') // "##": the argument list
Bram Moolenaar071d4272004-06-13 20:20:40 +00008339 {
8340 result = arg_all();
8341 resultbuf = result;
8342 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +00008343 if (escaped != NULL)
8344 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008345 skip_mod = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008346 break;
8347 }
8348 s = src + 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008349 if (*s == '<') // "#<99" uses v:oldfiles
Bram Moolenaard812df62008-11-09 12:46:09 +00008350 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008351 i = (int)getdigits(&s);
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008352 if (s == src + 2 && src[1] == '-')
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008353 // just a minus sign, don't skip over it
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008354 s--;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008355 *usedlen = (int)(s - src); // length of what we expand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008356
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008357 if (src[1] == '<' && i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008358 {
Bram Moolenaard812df62008-11-09 12:46:09 +00008359 if (*usedlen < 2)
8360 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008361 // Should we give an error message for #<text?
Bram Moolenaard812df62008-11-09 12:46:09 +00008362 *usedlen = 1;
8363 return NULL;
8364 }
8365#ifdef FEAT_EVAL
8366 result = list_find_str(get_vim_var_list(VV_OLDFILES),
8367 (long)i);
8368 if (result == NULL)
8369 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008370 *errormsg = "";
Bram Moolenaard812df62008-11-09 12:46:09 +00008371 return NULL;
8372 }
8373#else
Bram Moolenaar8e481e82019-01-15 20:07:48 +01008374 *errormsg = _("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008375 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00008376#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008377 }
8378 else
Bram Moolenaard812df62008-11-09 12:46:09 +00008379 {
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008380 if (i == 0 && src[1] == '<' && *usedlen > 1)
8381 *usedlen = 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00008382 buf = buflist_findnr(i);
8383 if (buf == NULL)
8384 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008385 *errormsg = _("E194: No alternate file name to substitute for '#'");
Bram Moolenaard812df62008-11-09 12:46:09 +00008386 return NULL;
8387 }
8388 if (lnump != NULL)
8389 *lnump = ECMD_LAST;
8390 if (buf->b_fname == NULL)
8391 {
8392 result = (char_u *)"";
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008393 valid = 0; // Must have ":p:h" to be valid
Bram Moolenaard812df62008-11-09 12:46:09 +00008394 }
8395 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008396 {
Bram Moolenaard812df62008-11-09 12:46:09 +00008397 result = buf->b_fname;
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008398 tilde_file = STRCMP(result, "~") == 0;
8399 }
Bram Moolenaard812df62008-11-09 12:46:09 +00008400 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008401 break;
8402
8403#ifdef FEAT_SEARCHPATH
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008404 case SPEC_CFILE: // file name under cursor
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008405 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008406 if (result == NULL)
8407 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008408 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008409 return NULL;
8410 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008411 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00008412 break;
8413#endif
8414
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008415 case SPEC_AFILE: // file name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008416 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +00008417 if (result != NULL && !autocmd_fname_full)
8418 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008419 // Still need to turn the fname into a full path. It is
8420 // postponed to avoid a delay when <afile> is not used.
Bram Moolenaarf6dad432008-09-18 19:29:58 +00008421 autocmd_fname_full = TRUE;
8422 result = FullName_save(autocmd_fname, FALSE);
8423 vim_free(autocmd_fname);
8424 autocmd_fname = result;
8425 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008426 if (result == NULL)
8427 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008428 *errormsg = _("E495: no autocommand file name to substitute for \"<afile>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008429 return NULL;
8430 }
Bram Moolenaara0174af2008-01-02 20:08:25 +00008431 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008432 break;
8433
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008434 case SPEC_ABUF: // buffer number for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008435 if (autocmd_bufnr <= 0)
8436 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008437 *errormsg = _("E496: no autocommand buffer number to substitute for \"<abuf>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008438 return NULL;
8439 }
8440 sprintf((char *)strbuf, "%d", autocmd_bufnr);
8441 result = strbuf;
8442 break;
8443
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008444 case SPEC_AMATCH: // match name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008445 result = autocmd_match;
8446 if (result == NULL)
8447 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008448 *errormsg = _("E497: no autocommand match name to substitute for \"<amatch>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008449 return NULL;
8450 }
8451 break;
8452
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008453 case SPEC_SFILE: // file name for ":so" command
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008454 result = estack_sfile();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008455 if (result == NULL)
8456 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008457 *errormsg = _("E498: no :source file name to substitute for \"<sfile>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008458 return NULL;
8459 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008460 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00008461 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008462
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008463 case SPEC_SLNUM: // line in file for ":so" command
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008464 if (SOURCING_NAME == NULL || SOURCING_LNUM == 0)
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008465 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008466 *errormsg = _("E842: no line number to use for \"<slnum>\"");
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008467 return NULL;
8468 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008469 sprintf((char *)strbuf, "%ld", SOURCING_LNUM);
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008470 result = strbuf;
8471 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008472
8473#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008474 case SPEC_SFLNUM: // line in script file
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008475 if (current_sctx.sc_lnum + SOURCING_LNUM == 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008476 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008477 *errormsg = _("E961: no line number to use for \"<sflnum>\"");
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008478 return NULL;
8479 }
8480 sprintf((char *)strbuf, "%ld",
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008481 (long)(current_sctx.sc_lnum + SOURCING_LNUM));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008482 result = strbuf;
8483 break;
8484#endif
8485
8486#ifdef FEAT_CLIENTSERVER
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008487 case SPEC_CLIENT: // Source of last submitted input
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008488 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
8489 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008490 result = strbuf;
8491 break;
8492#endif
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008493
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02008494 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008495 result = (char_u *)""; // avoid gcc warning
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02008496 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008497 }
8498
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008499 resultlen = (int)STRLEN(result); // length of new string
8500 if (src[*usedlen] == '<') // remove the file name extension
Bram Moolenaar071d4272004-06-13 20:20:40 +00008501 {
8502 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008503 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008504 resultlen = (int)(s - result);
8505 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008506 else if (!skip_mod)
8507 {
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008508 valid |= modify_fname(src, tilde_file, usedlen, &result, &resultbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008509 &resultlen);
8510 if (result == NULL)
8511 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008512 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008513 return NULL;
8514 }
8515 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008516 }
8517
8518 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
8519 {
8520 if (valid != VALID_HEAD + VALID_PATH)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008521 // xgettext:no-c-format
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008522 *errormsg = _("E499: Empty file name for '%' or '#', only works with \":p:h\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008523 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008524 *errormsg = _("E500: Evaluates to an empty string");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008525 result = NULL;
8526 }
8527 else
8528 result = vim_strnsave(result, resultlen);
8529 vim_free(resultbuf);
8530 return result;
8531}
8532
8533/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008534 * Expand the <sfile> string in "arg".
8535 *
8536 * Returns an allocated string, or NULL for any error.
8537 */
8538 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008539expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008540{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008541 char *errormsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008542 int len;
8543 char_u *result;
8544 char_u *newres;
8545 char_u *repl;
8546 int srclen;
8547 char_u *p;
8548
8549 result = vim_strsave(arg);
8550 if (result == NULL)
8551 return NULL;
8552
8553 for (p = result; *p; )
8554 {
8555 if (STRNCMP(p, "<sfile>", 7) != 0)
8556 ++p;
8557 else
8558 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008559 // replace "<sfile>" with the sourced file name, and do ":" stuff
Bram Moolenaar63b92542007-03-27 14:57:09 +00008560 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008561 if (errormsg != NULL)
8562 {
8563 if (*errormsg)
8564 emsg(errormsg);
8565 vim_free(result);
8566 return NULL;
8567 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008568 if (repl == NULL) // no match (cannot happen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008569 {
8570 p += srclen;
8571 continue;
8572 }
8573 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
8574 newres = alloc(len);
8575 if (newres == NULL)
8576 {
8577 vim_free(repl);
8578 vim_free(result);
8579 return NULL;
8580 }
8581 mch_memmove(newres, result, (size_t)(p - result));
8582 STRCPY(newres + (p - result), repl);
8583 len = (int)STRLEN(newres);
8584 STRCAT(newres, p + srclen);
8585 vim_free(repl);
8586 vim_free(result);
8587 result = newres;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008588 p = newres + len; // continue after the match
Bram Moolenaar071d4272004-06-13 20:20:40 +00008589 }
8590 }
8591
8592 return result;
8593}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008594
Bram Moolenaar071d4272004-06-13 20:20:40 +00008595#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00008596/*
Bram Moolenaard9462e32011-04-11 21:35:11 +02008597 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +00008598 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +00008599 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008600 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008601dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008602{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008603 if (fname == NULL)
8604 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +02008605 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008606}
8607#endif
8608
8609/*
8610 * ":behave {mswin,xterm}"
8611 */
8612 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008613ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008614{
8615 if (STRCMP(eap->arg, "mswin") == 0)
8616 {
8617 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
8618 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
8619 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
8620 set_option_value((char_u *)"keymodel", 0L,
8621 (char_u *)"startsel,stopsel", 0);
8622 }
8623 else if (STRCMP(eap->arg, "xterm") == 0)
8624 {
8625 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
8626 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
8627 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
8628 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
8629 }
8630 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008631 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008632}
8633
Bram Moolenaar071d4272004-06-13 20:20:40 +00008634static int filetype_detect = FALSE;
8635static int filetype_plugin = FALSE;
8636static int filetype_indent = FALSE;
8637
8638/*
8639 * ":filetype [plugin] [indent] {on,off,detect}"
8640 * on: Load the filetype.vim file to install autocommands for file types.
8641 * off: Load the ftoff.vim file to remove all autocommands for file types.
8642 * plugin on: load filetype.vim and ftplugin.vim
8643 * plugin off: load ftplugof.vim
8644 * indent on: load filetype.vim and indent.vim
8645 * indent off: load indoff.vim
8646 */
8647 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008648ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008649{
8650 char_u *arg = eap->arg;
8651 int plugin = FALSE;
8652 int indent = FALSE;
8653
8654 if (*eap->arg == NUL)
8655 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008656 // Print current status.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008657 smsg("filetype detection:%s plugin:%s indent:%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00008658 filetype_detect ? "ON" : "OFF",
8659 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
8660 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
8661 return;
8662 }
8663
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008664 // Accept "plugin" and "indent" in any order.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008665 for (;;)
8666 {
8667 if (STRNCMP(arg, "plugin", 6) == 0)
8668 {
8669 plugin = TRUE;
8670 arg = skipwhite(arg + 6);
8671 continue;
8672 }
8673 if (STRNCMP(arg, "indent", 6) == 0)
8674 {
8675 indent = TRUE;
8676 arg = skipwhite(arg + 6);
8677 continue;
8678 }
8679 break;
8680 }
8681 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
8682 {
8683 if (*arg == 'o' || !filetype_detect)
8684 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008685 source_runtime((char_u *)FILETYPE_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008686 filetype_detect = TRUE;
8687 if (plugin)
8688 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008689 source_runtime((char_u *)FTPLUGIN_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008690 filetype_plugin = TRUE;
8691 }
8692 if (indent)
8693 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008694 source_runtime((char_u *)INDENT_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008695 filetype_indent = TRUE;
8696 }
8697 }
8698 if (*arg == 'd')
8699 {
Bram Moolenaar1610d052016-06-09 22:53:01 +02008700 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +00008701 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008702 }
8703 }
8704 else if (STRCMP(arg, "off") == 0)
8705 {
8706 if (plugin || indent)
8707 {
8708 if (plugin)
8709 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008710 source_runtime((char_u *)FTPLUGOF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008711 filetype_plugin = FALSE;
8712 }
8713 if (indent)
8714 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008715 source_runtime((char_u *)INDOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008716 filetype_indent = FALSE;
8717 }
8718 }
8719 else
8720 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008721 source_runtime((char_u *)FTOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008722 filetype_detect = FALSE;
8723 }
8724 }
8725 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008726 semsg(_(e_invarg2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008727}
8728
8729/*
Bram Moolenaar3e545692017-06-04 19:00:32 +02008730 * ":setfiletype [FALLBACK] {name}"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008731 */
8732 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008733ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008734{
8735 if (!did_filetype)
Bram Moolenaar3e545692017-06-04 19:00:32 +02008736 {
8737 char_u *arg = eap->arg;
8738
8739 if (STRNCMP(arg, "FALLBACK ", 9) == 0)
8740 arg += 9;
8741
8742 set_option_value((char_u *)"filetype", 0L, arg, OPT_LOCAL);
8743 if (arg != eap->arg)
8744 did_filetype = FALSE;
8745 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008746}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008747
Bram Moolenaar071d4272004-06-13 20:20:40 +00008748 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008749ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008750{
8751#ifdef FEAT_DIGRAPHS
8752 if (*eap->arg != NUL)
8753 putdigraph(eap->arg);
8754 else
Bram Moolenaareae8ae12018-12-14 18:53:02 +01008755 listdigraphs(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008756#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008757 emsg(_("E196: No digraphs in this version"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008758#endif
8759}
8760
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02008761#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
8762 void
8763set_no_hlsearch(int flag)
8764{
8765 no_hlsearch = flag;
8766# ifdef FEAT_EVAL
8767 set_vim_var_nr(VV_HLSEARCH, !no_hlsearch && p_hls);
8768# endif
8769}
8770
Bram Moolenaar071d4272004-06-13 20:20:40 +00008771/*
8772 * ":nohlsearch"
8773 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008774 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008775ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008776{
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02008777 set_no_hlsearch(TRUE);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00008778 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008779}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008780#endif
8781
8782#ifdef FEAT_CRYPT
8783/*
8784 * ":X": Get crypt key
8785 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008786 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008787ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008788{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +01008789 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02008790 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008791}
8792#endif
8793
8794#ifdef FEAT_FOLDING
8795 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008796ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008797{
8798 if (foldManualAllowed(TRUE))
8799 foldCreate(eap->line1, eap->line2);
8800}
8801
8802 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008803ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008804{
8805 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
8806 eap->forceit, FALSE);
8807}
8808
8809 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008810ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008811{
8812 linenr_T lnum;
8813
Bram Moolenaar4facea32019-10-12 20:17:40 +02008814# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02008815 start_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +02008816# endif
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02008817
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008818 // First set the marks for all lines closed/open.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008819 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
8820 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
8821 ml_setmarked(lnum);
8822
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008823 // Execute the command on the marked lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008824 global_exe(eap->arg);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008825 ml_clearmarked(); // clear rest of the marks
Bram Moolenaar4facea32019-10-12 20:17:40 +02008826# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02008827 end_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +02008828# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008829}
8830#endif
Bram Moolenaarf5291f32017-09-14 22:55:37 +02008831
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008832#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar39665952018-08-15 20:59:48 +02008833/*
8834 * Returns TRUE if the supplied Ex cmdidx is for a location list command
8835 * instead of a quickfix command.
8836 */
8837 int
8838is_loclist_cmd(int cmdidx)
8839{
Bram Moolenaar74c8be22018-08-23 22:51:40 +02008840 if (cmdidx < 0 || cmdidx >= CMD_SIZE)
Bram Moolenaar39665952018-08-15 20:59:48 +02008841 return FALSE;
8842 return cmdnames[cmdidx].cmd_name[0] == 'l';
8843}
8844#endif
8845
Bram Moolenaar4facea32019-10-12 20:17:40 +02008846#if defined(FEAT_TIMERS) || defined(PROTO)
Bram Moolenaarf5291f32017-09-14 22:55:37 +02008847 int
8848get_pressedreturn(void)
8849{
8850 return ex_pressedreturn;
8851}
8852
8853 void
8854set_pressedreturn(int val)
8855{
8856 ex_pressedreturn = val;
8857}
8858#endif