blob: 5436373e53ebc1e719f72e79c2150b939a8b0414 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * ex_docmd.c: functions for executing an Ex command line.
12 */
13
14#include "vim.h"
15
Bram Moolenaar071d4272004-06-13 20:20:40 +000016static int quitmore = 0;
17static int ex_pressedreturn = FALSE;
18#ifndef FEAT_PRINTER
19# define ex_hardcopy ex_ni
20#endif
21
Bram Moolenaar071d4272004-06-13 20:20:40 +000022#ifdef FEAT_EVAL
Bram Moolenaarddef1292019-12-16 17:10:33 +010023static char_u *do_one_cmd(char_u **, int, cstack_T *, char_u *(*fgetline)(int, void *, int, int), void *cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024#else
Bram Moolenaare96a2492019-06-25 04:12:16 +020025static char_u *do_one_cmd(char_u **, int, char_u *(*fgetline)(int, void *, int, int), void *cookie);
Bram Moolenaar217e1b82019-12-01 21:41:28 +010026static int if_level = 0; // depth in :if
Bram Moolenaar071d4272004-06-13 20:20:40 +000027#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010028static void append_command(char_u *cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000029
Bram Moolenaar071d4272004-06-13 20:20:40 +000030#ifndef FEAT_MENU
31# define ex_emenu ex_ni
32# define ex_menu ex_ni
33# define ex_menutranslate ex_ni
34#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010035static void ex_autocmd(exarg_T *eap);
36static void ex_doautocmd(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010037static void ex_bunload(exarg_T *eap);
38static void ex_buffer(exarg_T *eap);
39static void ex_bmodified(exarg_T *eap);
40static void ex_bnext(exarg_T *eap);
41static void ex_bprevious(exarg_T *eap);
42static void ex_brewind(exarg_T *eap);
43static void ex_blast(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010044static char_u *getargcmd(char_u **);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010045static int getargopt(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000046#ifndef FEAT_QUICKFIX
47# define ex_make ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +000048# define ex_cbuffer ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000049# define ex_cc ex_ni
50# define ex_cnext ex_ni
Bram Moolenaar3ff33112019-05-03 21:56:35 +020051# define ex_cbelow ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000052# define ex_cfile ex_ni
53# define qf_list ex_ni
54# define qf_age ex_ni
Bram Moolenaarf6acffb2016-07-16 16:54:24 +020055# define qf_history ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000056# define ex_helpgrep ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +000057# define ex_vimgrep ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000058#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +020059#if !defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +000060# define ex_cclose ex_ni
61# define ex_copen ex_ni
62# define ex_cwindow ex_ni
Bram Moolenaardcb17002016-07-07 18:58:59 +020063# define ex_cbottom ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000064#endif
Bram Moolenaar1e015462005-09-25 22:16:38 +000065#if !defined(FEAT_QUICKFIX) || !defined(FEAT_EVAL)
66# define ex_cexpr ex_ni
67#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000068
Bram Moolenaarb7316892019-05-01 18:08:42 +020069static linenr_T get_address(exarg_T *, char_u **, cmd_addr_T addr_type, int skip, int silent, int to_other_file, int address_count);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010070static void get_flags(exarg_T *eap);
Bram Moolenaar85363ab2010-07-18 13:58:26 +020071#if !defined(FEAT_PERL) \
72 || !defined(FEAT_PYTHON) || !defined(FEAT_PYTHON3) \
73 || !defined(FEAT_TCL) \
74 || !defined(FEAT_RUBY) \
75 || !defined(FEAT_LUA) \
76 || !defined(FEAT_MZSCHEME)
Bram Moolenaar7bb75552007-07-16 18:39:49 +000077# define HAVE_EX_SCRIPT_NI
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010078static void ex_script_ni(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000079#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010080static char *invalid_range(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010081static void correct_range(exarg_T *eap);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000082#ifdef FEAT_QUICKFIX
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010083static char_u *replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000084#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010085static char_u *repl_cmdline(exarg_T *eap, char_u *src, int srclen, char_u *repl, char_u **cmdlinep);
86static void ex_highlight(exarg_T *eap);
87static void ex_colorscheme(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010088static void ex_cquit(exarg_T *eap);
89static void ex_quit_all(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010090static void ex_close(exarg_T *eap);
91static void ex_win_close(int forceit, win_T *win, tabpage_T *tp);
92static void ex_only(exarg_T *eap);
93static void ex_resize(exarg_T *eap);
94static void ex_stag(exarg_T *eap);
95static void ex_tabclose(exarg_T *eap);
96static void ex_tabonly(exarg_T *eap);
97static void ex_tabnext(exarg_T *eap);
98static void ex_tabmove(exarg_T *eap);
99static void ex_tabs(exarg_T *eap);
Bram Moolenaar4033c552017-09-16 20:54:51 +0200100#if defined(FEAT_QUICKFIX)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100101static void ex_pclose(exarg_T *eap);
102static void ex_ptag(exarg_T *eap);
103static void ex_pedit(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000104#else
105# define ex_pclose ex_ni
106# define ex_ptag ex_ni
107# define ex_pedit ex_ni
108#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100109static void ex_hide(exarg_T *eap);
110static void ex_stop(exarg_T *eap);
111static void ex_exit(exarg_T *eap);
112static void ex_print(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000113#ifdef FEAT_BYTEOFF
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100114static void ex_goto(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000115#else
116# define ex_goto ex_ni
117#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100118static void ex_shell(exarg_T *eap);
119static void ex_preserve(exarg_T *eap);
120static void ex_recover(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100121static void ex_mode(exarg_T *eap);
122static void ex_wrongmodifier(exarg_T *eap);
123static void ex_find(exarg_T *eap);
124static void ex_open(exarg_T *eap);
125static void ex_edit(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000126#ifndef FEAT_GUI
127# define ex_gui ex_nogui
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100128static void ex_nogui(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000129#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +0100130#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100131static void ex_tearoff(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000132#else
133# define ex_tearoff ex_ni
134#endif
Bram Moolenaar29a2c082018-03-05 21:06:23 +0100135#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
136 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100137static void ex_popup(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000138#else
139# define ex_popup ex_ni
140#endif
141#ifndef FEAT_GUI_MSWIN
142# define ex_simalt ex_ni
143#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000144#if !defined(FEAT_GUI_MSWIN) && !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000145# define gui_mch_find_dialog ex_ni
146# define gui_mch_replace_dialog ex_ni
147#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000148#if !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000149# define ex_helpfind ex_ni
150#endif
151#ifndef FEAT_CSCOPE
Bram Moolenaard4db7712016-11-12 19:16:46 +0100152# define ex_cscope ex_ni
153# define ex_scscope ex_ni
154# define ex_cstag ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000155#endif
156#ifndef FEAT_SYN_HL
157# define ex_syntax ex_ni
Bram Moolenaar860cae12010-06-05 23:22:07 +0200158# define ex_ownsyntax ex_ni
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000159#endif
Bram Moolenaarf7512552013-06-06 14:55:19 +0200160#if !defined(FEAT_SYN_HL) || !defined(FEAT_PROFILE)
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +0200161# define ex_syntime ex_ni
162#endif
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000163#ifndef FEAT_SPELL
Bram Moolenaarb765d632005-06-07 21:00:02 +0000164# define ex_spell ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000165# define ex_mkspell ex_ni
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000166# define ex_spelldump ex_ni
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000167# define ex_spellinfo ex_ni
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000168# define ex_spellrepall ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000169#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200170#ifndef FEAT_PERSISTENT_UNDO
171# define ex_rundo ex_ni
172# define ex_wundo ex_ni
173#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200174#ifndef FEAT_LUA
175# define ex_lua ex_script_ni
176# define ex_luado ex_ni
177# define ex_luafile ex_ni
178#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000179#ifndef FEAT_MZSCHEME
180# define ex_mzscheme ex_script_ni
181# define ex_mzfile ex_ni
182#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000183#ifndef FEAT_PERL
184# define ex_perl ex_script_ni
185# define ex_perldo ex_ni
186#endif
187#ifndef FEAT_PYTHON
188# define ex_python ex_script_ni
Bram Moolenaard620aa92013-05-17 16:40:06 +0200189# define ex_pydo ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000190# define ex_pyfile ex_ni
191#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200192#ifndef FEAT_PYTHON3
Bram Moolenaar368373e2010-07-19 20:46:22 +0200193# define ex_py3 ex_script_ni
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200194# define ex_py3do ex_ni
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200195# define ex_py3file ex_ni
196#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100197#if !defined(FEAT_PYTHON) && !defined(FEAT_PYTHON3)
198# define ex_pyx ex_script_ni
199# define ex_pyxdo ex_ni
200# define ex_pyxfile ex_ni
201#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000202#ifndef FEAT_TCL
203# define ex_tcl ex_script_ni
204# define ex_tcldo ex_ni
205# define ex_tclfile ex_ni
206#endif
207#ifndef FEAT_RUBY
208# define ex_ruby ex_script_ni
209# define ex_rubydo ex_ni
210# define ex_rubyfile ex_ni
211#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000212#ifndef FEAT_KEYMAP
213# define ex_loadkeymap ex_ni
214#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100215static void ex_swapname(exarg_T *eap);
216static void ex_syncbind(exarg_T *eap);
217static void ex_read(exarg_T *eap);
218static void ex_pwd(exarg_T *eap);
219static void ex_equal(exarg_T *eap);
220static void ex_sleep(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100221static void ex_winsize(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100222static void ex_wincmd(exarg_T *eap);
Bram Moolenaar843ee412004-06-30 16:16:41 +0000223#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100224static void ex_winpos(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000225#else
226# define ex_winpos ex_ni
227#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100228static void ex_operators(exarg_T *eap);
229static void ex_put(exarg_T *eap);
230static void ex_copymove(exarg_T *eap);
231static void ex_submagic(exarg_T *eap);
232static void ex_join(exarg_T *eap);
233static void ex_at(exarg_T *eap);
234static void ex_bang(exarg_T *eap);
235static void ex_undo(exarg_T *eap);
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200236#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100237static void ex_wundo(exarg_T *eap);
238static void ex_rundo(exarg_T *eap);
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200239#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100240static void ex_redo(exarg_T *eap);
241static void ex_later(exarg_T *eap);
242static void ex_redir(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100243static void ex_redrawstatus(exarg_T *eap);
Bram Moolenaare12bab32019-01-08 22:02:56 +0100244static void ex_redrawtabline(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100245static void close_redir(void);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100246static void ex_mark(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100247static void ex_startinsert(exarg_T *eap);
248static void ex_stopinsert(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000249#ifdef FEAT_FIND_ID
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100250static void ex_checkpath(exarg_T *eap);
251static void ex_findpat(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000252#else
253# define ex_findpat ex_ni
254# define ex_checkpath ex_ni
255#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +0200256#if defined(FEAT_FIND_ID) && defined(FEAT_QUICKFIX)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100257static void ex_psearch(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000258#else
259# define ex_psearch ex_ni
260#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100261static void ex_tag(exarg_T *eap);
262static void ex_tag_cmd(exarg_T *eap, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000263#ifndef FEAT_EVAL
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200264# define ex_break ex_ni
265# define ex_breakadd ex_ni
266# define ex_breakdel ex_ni
267# define ex_breaklist ex_ni
268# define ex_call ex_ni
269# define ex_catch ex_ni
270# define ex_compiler ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200271# define ex_continue ex_ni
272# define ex_debug ex_ni
273# define ex_debuggreedy ex_ni
Bram Moolenaar822ba242020-05-24 23:00:18 +0200274# define ex_def ex_ni
275# define ex_defcompile ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200276# define ex_delfunction ex_ni
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100277# define ex_disassemble ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000278# define ex_echo ex_ni
279# define ex_echohl ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000280# define ex_else ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200281# define ex_endfunction ex_ni
282# define ex_endif ex_ni
283# define ex_endtry ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000284# define ex_endwhile ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200285# define ex_eval ex_ni
286# define ex_execute ex_ni
287# define ex_finally ex_ni
288# define ex_finish ex_ni
289# define ex_function ex_ni
290# define ex_if ex_ni
291# define ex_let ex_ni
292# define ex_lockvar ex_ni
293# define ex_oldfiles ex_ni
294# define ex_options ex_ni
295# define ex_packadd ex_ni
296# define ex_packloadall ex_ni
297# define ex_return ex_ni
298# define ex_scriptnames ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000299# define ex_throw ex_ni
300# define ex_try ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000301# define ex_unlet ex_ni
Bram Moolenaar65c1b012005-01-31 19:02:28 +0000302# define ex_unlockvar ex_ni
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100303# define ex_vim9script ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200304# define ex_while ex_ni
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100305# define ex_import ex_ni
306# define ex_export ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000307#endif
Bram Moolenaar84538072019-07-28 14:15:42 +0200308#ifndef FEAT_SESSION
Bram Moolenaar071d4272004-06-13 20:20:40 +0000309# define ex_loadview ex_ni
310#endif
Bram Moolenaardefa0672019-07-21 19:25:37 +0200311#ifndef FEAT_VIMINFO
Bram Moolenaar071d4272004-06-13 20:20:40 +0000312# define ex_viminfo ex_ni
313#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100314static void ex_behave(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100315static void ex_filetype(exarg_T *eap);
316static void ex_setfiletype(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000317#ifndef FEAT_DIFF
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000318# define ex_diffoff ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000319# define ex_diffpatch ex_ni
320# define ex_diffgetput ex_ni
321# define ex_diffsplit ex_ni
322# define ex_diffthis ex_ni
323# define ex_diffupdate ex_ni
324#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100325static void ex_digraphs(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000326#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100327static void ex_nohlsearch(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000328#else
329# define ex_nohlsearch ex_ni
330# define ex_match ex_ni
331#endif
332#ifdef FEAT_CRYPT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100333static void ex_X(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000334#else
335# define ex_X ex_ni
336#endif
337#ifdef FEAT_FOLDING
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100338static void ex_fold(exarg_T *eap);
339static void ex_foldopen(exarg_T *eap);
340static void ex_folddo(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000341#else
342# define ex_fold ex_ni
343# define ex_foldopen ex_ni
344# define ex_folddo ex_ni
345#endif
Bram Moolenaar13505972019-01-24 15:04:48 +0100346#if !(defined(HAVE_LOCALE_H) || defined(X_LOCALE))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000347# define ex_language ex_ni
348#endif
349#ifndef FEAT_SIGNS
350# define ex_sign ex_ni
351#endif
Bram Moolenaar009b2592004-10-24 19:18:58 +0000352#ifndef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200353# define ex_nbclose ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000354# define ex_nbkey ex_ni
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200355# define ex_nbstart ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000356#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000357
Bram Moolenaar071d4272004-06-13 20:20:40 +0000358#ifndef FEAT_JUMPLIST
359# define ex_jumps ex_ni
Bram Moolenaar2d358992016-06-12 21:20:54 +0200360# define ex_clearjumps ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000361# define ex_changes ex_ni
362#endif
363
Bram Moolenaar05159a02005-02-26 23:04:13 +0000364#ifndef FEAT_PROFILE
365# define ex_profile ex_ni
366#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200367#ifndef FEAT_TERMINAL
368# define ex_terminal ex_ni
369#endif
Bram Moolenaard4aa83a2019-05-09 18:59:31 +0200370#if !defined(FEAT_X11) || !defined(FEAT_XCLIPBOARD)
371# define ex_xrestore ex_ni
372#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100373#if !defined(FEAT_PROP_POPUP)
Bram Moolenaar682725c2019-05-25 20:10:37 +0200374# define ex_popupclear ex_ni
375#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +0000376
Bram Moolenaar071d4272004-06-13 20:20:40 +0000377/*
378 * Declare cmdnames[].
379 */
380#define DO_DECLARE_EXCMD
381#include "ex_cmds.h"
Bram Moolenaar6de5e122017-04-20 21:55:44 +0200382#include "ex_cmdidxs.h"
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +0100383
Bram Moolenaar071d4272004-06-13 20:20:40 +0000384static char_u dollar_command[2] = {'$', 0};
385
386
387#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100388// Struct for storing a line inside a while/for loop
Bram Moolenaar071d4272004-06-13 20:20:40 +0000389typedef struct
390{
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100391 char_u *line; // command line
392 linenr_T lnum; // sourcing_lnum of the line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000393} wcmd_T;
394
395/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000396 * Structure used to store info for line position in a while or for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000397 * This is required, because do_one_cmd() may invoke ex_function(), which
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000398 * reads more lines that may come from the while/for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000399 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000400struct loop_cookie
Bram Moolenaar071d4272004-06-13 20:20:40 +0000401{
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100402 garray_T *lines_gap; // growarray with line info
403 int current_line; // last read line from growarray
404 int repeating; // TRUE when looping a second time
405 // When "repeating" is FALSE use "getline" and "cookie" to get lines
Bram Moolenaare96a2492019-06-25 04:12:16 +0200406 char_u *(*getline)(int, void *, int, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000407 void *cookie;
408};
409
Bram Moolenaare96a2492019-06-25 04:12:16 +0200410static char_u *get_loop_line(int c, void *cookie, int indent, int do_concat);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100411static int store_loop_line(garray_T *gap, char_u *line);
412static void free_cmdlines(garray_T *gap);
Bram Moolenaared203462004-06-16 11:19:22 +0000413
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100414// Struct to save a few things while debugging. Used in do_cmdline() only.
Bram Moolenaared203462004-06-16 11:19:22 +0000415struct dbg_stuff
416{
417 int trylevel;
418 int force_abort;
419 except_T *caught_stack;
420 char_u *vv_exception;
421 char_u *vv_throwpoint;
422 int did_emsg;
423 int got_int;
424 int did_throw;
425 int need_rethrow;
426 int check_cstack;
427 except_T *current_exception;
428};
429
Bram Moolenaared203462004-06-16 11:19:22 +0000430 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100431save_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000432{
433 dsp->trylevel = trylevel; trylevel = 0;
434 dsp->force_abort = force_abort; force_abort = FALSE;
435 dsp->caught_stack = caught_stack; caught_stack = NULL;
436 dsp->vv_exception = v_exception(NULL);
437 dsp->vv_throwpoint = v_throwpoint(NULL);
438
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100439 // Necessary for debugging an inactive ":catch", ":finally", ":endtry"
Bram Moolenaared203462004-06-16 11:19:22 +0000440 dsp->did_emsg = did_emsg; did_emsg = FALSE;
441 dsp->got_int = got_int; got_int = FALSE;
442 dsp->did_throw = did_throw; did_throw = FALSE;
443 dsp->need_rethrow = need_rethrow; need_rethrow = FALSE;
444 dsp->check_cstack = check_cstack; check_cstack = FALSE;
445 dsp->current_exception = current_exception; current_exception = NULL;
446}
447
448 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100449restore_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000450{
451 suppress_errthrow = FALSE;
452 trylevel = dsp->trylevel;
453 force_abort = dsp->force_abort;
454 caught_stack = dsp->caught_stack;
455 (void)v_exception(dsp->vv_exception);
456 (void)v_throwpoint(dsp->vv_throwpoint);
457 did_emsg = dsp->did_emsg;
458 got_int = dsp->got_int;
459 did_throw = dsp->did_throw;
460 need_rethrow = dsp->need_rethrow;
461 check_cstack = dsp->check_cstack;
462 current_exception = dsp->current_exception;
463}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000464#endif
465
Bram Moolenaar071d4272004-06-13 20:20:40 +0000466/*
467 * do_exmode(): Repeatedly get commands for the "Ex" mode, until the ":vi"
468 * command is given.
469 */
470 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100471do_exmode(
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100472 int improved) // TRUE for "improved Ex" mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000473{
474 int save_msg_scroll;
475 int prev_msg_row;
476 linenr_T prev_line;
Bram Moolenaar79518e22017-02-17 16:31:35 +0100477 varnumber_T changedtick;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000478
479 if (improved)
480 exmode_active = EXMODE_VIM;
481 else
482 exmode_active = EXMODE_NORMAL;
483 State = NORMAL;
484
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100485 // When using ":global /pat/ visual" and then "Q" we return to continue
486 // the :global command.
Bram Moolenaardf177f62005-02-22 08:39:57 +0000487 if (global_busy)
488 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000489
490 save_msg_scroll = msg_scroll;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100491 ++RedrawingDisabled; // don't redisplay the window
492 ++no_wait_return; // don't wait for return
Bram Moolenaar071d4272004-06-13 20:20:40 +0000493#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100494 // Ignore scrollbar and mouse events in Ex mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000495 ++hold_gui_events;
496#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000497
Bram Moolenaar32526b32019-01-19 17:43:09 +0100498 msg(_("Entering Ex mode. Type \"visual\" to go to Normal mode."));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000499 while (exmode_active)
500 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100501 // Check for a ":normal" command and no more characters left.
Bram Moolenaar7c626922005-02-07 22:01:03 +0000502 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
503 {
504 exmode_active = FALSE;
505 break;
506 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000507 msg_scroll = TRUE;
508 need_wait_return = FALSE;
509 ex_pressedreturn = FALSE;
510 ex_no_reprint = FALSE;
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100511 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000512 prev_msg_row = msg_row;
513 prev_line = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000514 if (improved)
515 {
516 cmdline_row = msg_row;
517 do_cmdline(NULL, getexline, NULL, 0);
518 }
519 else
520 do_cmdline(NULL, getexmodeline, NULL, DOCMD_NOWAIT);
521 lines_left = Rows - 1;
522
Bram Moolenaardf177f62005-02-22 08:39:57 +0000523 if ((prev_line != curwin->w_cursor.lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100524 || changedtick != CHANGEDTICK(curbuf)) && !ex_no_reprint)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000525 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000526 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100527 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000528 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000529 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000530 if (ex_pressedreturn)
531 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100532 // go up one line, to overwrite the ":<CR>" line, so the
533 // output doesn't contain empty lines.
Bram Moolenaardf177f62005-02-22 08:39:57 +0000534 msg_row = prev_msg_row;
535 if (prev_msg_row == Rows - 1)
536 msg_row--;
537 }
538 msg_col = 0;
539 print_line_no_prefix(curwin->w_cursor.lnum, FALSE, FALSE);
540 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000541 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000542 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100543 else if (ex_pressedreturn && !ex_no_reprint) // must be at EOF
Bram Moolenaardf177f62005-02-22 08:39:57 +0000544 {
545 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100546 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000547 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100548 emsg(_("E501: At end-of-file"));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000549 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000550 }
551
552#ifdef FEAT_GUI
553 --hold_gui_events;
554#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000555 --RedrawingDisabled;
556 --no_wait_return;
557 update_screen(CLEAR);
558 need_wait_return = FALSE;
559 msg_scroll = save_msg_scroll;
560}
561
562/*
Bram Moolenaar4facea32019-10-12 20:17:40 +0200563 * Print the executed command for when 'verbose' is set.
564 * When "lnum" is 0 only print the command.
565 */
566 static void
567msg_verbose_cmd(linenr_T lnum, char_u *cmd)
568{
569 ++no_wait_return;
570 verbose_enter_scroll();
571
572 if (lnum == 0)
573 smsg(_("Executing: %s"), cmd);
574 else
575 smsg(_("line %ld: %s"), (long)lnum, cmd);
576 if (msg_silent == 0)
577 msg_puts("\n"); // don't overwrite this
578
579 verbose_leave_scroll();
580 --no_wait_return;
581}
582
583/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000584 * Execute a simple command line. Used for translated commands like "*".
585 */
586 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100587do_cmdline_cmd(char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000588{
589 return do_cmdline(cmd, NULL, NULL,
590 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
591}
592
593/*
594 * do_cmdline(): execute one Ex command line
595 *
596 * 1. Execute "cmdline" when it is not NULL.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100597 * If "cmdline" is NULL, or more lines are needed, fgetline() is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000598 * 2. Split up in parts separated with '|'.
599 *
600 * This function can be called recursively!
601 *
602 * flags:
603 * DOCMD_VERBOSE - The command will be included in the error message.
604 * DOCMD_NOWAIT - Don't call wait_return() and friends.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100605 * DOCMD_REPEAT - Repeat execution until fgetline() returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000606 * DOCMD_KEYTYPED - Don't reset KeyTyped.
607 * DOCMD_EXCRESET - Reset the exception environment (used for debugging).
608 * DOCMD_KEEPLINE - Store first typed line (for repeating with ".").
609 *
610 * return FAIL if cmdline could not be executed, OK otherwise
611 */
612 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100613do_cmdline(
614 char_u *cmdline,
Bram Moolenaare96a2492019-06-25 04:12:16 +0200615 char_u *(*fgetline)(int, void *, int, int),
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100616 void *cookie, // argument for fgetline()
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100617 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000618{
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100619 char_u *next_cmdline; // next cmd to execute
620 char_u *cmdline_copy = NULL; // copy of cmd line
621 int used_getline = FALSE; // used "fgetline" to obtain command
622 static int recursive = 0; // recursive depth
Bram Moolenaar071d4272004-06-13 20:20:40 +0000623 int msg_didout_before_start = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100624 int count = 0; // line number count
625 int did_inc = FALSE; // incremented RedrawingDisabled
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626 int retval = OK;
627#ifdef FEAT_EVAL
Bram Moolenaarddef1292019-12-16 17:10:33 +0100628 cstack_T cstack; // conditional stack
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100629 garray_T lines_ga; // keep lines for ":while"/":for"
630 int current_line = 0; // active line in lines_ga
Bram Moolenaard5053d02020-06-28 15:51:16 +0200631 int current_line_before = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100632 char_u *fname = NULL; // function or script name
633 linenr_T *breakpoint = NULL; // ptr to breakpoint field in cookie
634 int *dbg_tick = NULL; // ptr to dbg_tick field in cookie
635 struct dbg_stuff debug_saved; // saved things for debug mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000636 int initial_trylevel;
Bram Moolenaar25e0f582020-05-25 22:36:50 +0200637 msglist_T **saved_msg_list = NULL;
638 msglist_T *private_msg_list;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000639
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100640 // "fgetline" and "cookie" passed to do_one_cmd()
Bram Moolenaare96a2492019-06-25 04:12:16 +0200641 char_u *(*cmd_getline)(int, void *, int, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642 void *cmd_cookie;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000643 struct loop_cookie cmd_loop_cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000644 void *real_cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000645 int getline_is_func;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000646#else
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100647# define cmd_getline fgetline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000648# define cmd_cookie cookie
649#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100650 static int call_depth = 0; // recursiveness
Bram Moolenaar2196bce2020-04-12 20:01:11 +0200651#ifdef FEAT_EVAL
Bram Moolenaare31ee862020-01-07 20:59:34 +0100652 ESTACK_CHECK_DECLARATION
Bram Moolenaar071d4272004-06-13 20:20:40 +0000653
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100654 // For every pair of do_cmdline()/do_one_cmd() calls, use an extra memory
655 // location for storing error messages to be converted to an exception.
656 // This ensures that the do_errthrow() call in do_one_cmd() does not
657 // combine the messages stored by an earlier invocation of do_one_cmd()
658 // with the command name of the later one. This would happen when
659 // BufWritePost autocommands are executed after a write error.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000660 saved_msg_list = msg_list;
661 msg_list = &private_msg_list;
662 private_msg_list = NULL;
663#endif
664
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100665 // It's possible to create an endless loop with ":execute", catch that
666 // here. The value of 200 allows nested function calls, ":source", etc.
667 // Allow 200 or 'maxfuncdepth', whatever is larger.
Bram Moolenaarb094ff42017-01-02 16:16:39 +0100668 if (call_depth >= 200
669#ifdef FEAT_EVAL
670 && call_depth >= p_mfd
671#endif
672 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000673 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100674 emsg(_("E169: Command too recursive"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100676 // When converting to an exception, we do not include the command name
677 // since this is not an error of the specific command.
Bram Moolenaarddef1292019-12-16 17:10:33 +0100678 do_errthrow((cstack_T *)NULL, (char_u *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679 msg_list = saved_msg_list;
680#endif
681 return FAIL;
682 }
683 ++call_depth;
684
685#ifdef FEAT_EVAL
686 cstack.cs_idx = -1;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000687 cstack.cs_looplevel = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688 cstack.cs_trylevel = 0;
689 cstack.cs_emsg_silent_list = NULL;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000690 cstack.cs_lflags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691 ga_init2(&lines_ga, (int)sizeof(wcmd_T), 10);
692
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100693 real_cookie = getline_cookie(fgetline, cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100695 // Inside a function use a higher nesting level.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100696 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000697 if (getline_is_func && ex_nesting_level == func_level(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698 ++ex_nesting_level;
699
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100700 // Get the function or script name and the address where the next breakpoint
701 // line and the debug tick for a function or script are stored.
Bram Moolenaar05159a02005-02-26 23:04:13 +0000702 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000703 {
704 fname = func_name(real_cookie);
705 breakpoint = func_breakpoint(real_cookie);
706 dbg_tick = func_dbg_tick(real_cookie);
707 }
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100708 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100710 fname = SOURCING_NAME;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000711 breakpoint = source_breakpoint(real_cookie);
712 dbg_tick = source_dbg_tick(real_cookie);
713 }
714
715 /*
716 * Initialize "force_abort" and "suppress_errthrow" at the top level.
717 */
718 if (!recursive)
719 {
720 force_abort = FALSE;
721 suppress_errthrow = FALSE;
722 }
723
724 /*
725 * If requested, store and reset the global values controlling the
Bram Moolenaar89d40322006-08-29 15:30:07 +0000726 * exception handling (used when debugging). Otherwise clear it to avoid
727 * a bogus compiler warning when the optimizer uses inline functions...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000728 */
Bram Moolenaarb4872942006-05-13 10:32:52 +0000729 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +0000730 save_dbg_stuff(&debug_saved);
Bram Moolenaar89d40322006-08-29 15:30:07 +0000731 else
Bram Moolenaara80faa82020-04-12 19:37:17 +0200732 CLEAR_FIELD(debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000733
734 initial_trylevel = trylevel;
735
736 /*
737 * "did_throw" will be set to TRUE when an exception is being thrown.
738 */
739 did_throw = FALSE;
740#endif
741 /*
742 * "did_emsg" will be set to TRUE when emsg() is used, in which case we
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000743 * cancel the whole command line, and any if/endif or loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000744 * If force_abort is set, we cancel everything.
745 */
746 did_emsg = FALSE;
747
748 /*
749 * KeyTyped is only set when calling vgetc(). Reset it here when not
750 * calling vgetc() (sourced command lines).
751 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100752 if (!(flags & DOCMD_KEYTYPED)
753 && !getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754 KeyTyped = FALSE;
755
756 /*
757 * Continue executing command lines:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000758 * - when inside an ":if", ":while" or ":for"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000759 * - for multiple commands on one line, separated with '|'
760 * - when repeating until there are no more lines (for ":source")
761 */
762 next_cmdline = cmdline;
763 do
764 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000765#ifdef FEAT_EVAL
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100766 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000767#endif
768
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100769 // stop skipping cmds for an error msg after all endif/while/for
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770 if (next_cmdline == NULL
771#ifdef FEAT_EVAL
772 && !force_abort
773 && cstack.cs_idx < 0
Bram Moolenaar05159a02005-02-26 23:04:13 +0000774 && !(getline_is_func && func_has_abort(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775#endif
776 )
777 did_emsg = FALSE;
778
779 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000780 * 1. If repeating a line in a loop, get a line from lines_ga.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100781 * 2. If no line given: Get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782 * 3. If a line is given: Make a copy, so we can mess with it.
783 */
784
785#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100786 // 1. If repeating, get a previous line from lines_ga.
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000787 if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100789 // Each '|' separated command is stored separately in lines_ga, to
790 // be able to jump to it. Don't use next_cmdline now.
Bram Moolenaard23a8232018-02-10 18:45:26 +0100791 VIM_CLEAR(cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100793 // Check if a function has returned or, unless it has an unclosed
794 // try conditional, aborted.
Bram Moolenaar05159a02005-02-26 23:04:13 +0000795 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000797# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000798 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000799 func_line_end(real_cookie);
800# endif
801 if (func_has_ended(real_cookie))
802 {
803 retval = FAIL;
804 break;
805 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000807#ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000808 else if (do_profiling == PROF_YES
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100809 && getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000810 script_line_end();
811#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000812
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100813 // Check if a sourced file hit a ":finish" command.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100814 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815 {
816 retval = FAIL;
817 break;
818 }
819
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100820 // If breakpoints have been added/deleted need to check for it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000821 if (breakpoint != NULL && dbg_tick != NULL
822 && *dbg_tick != debug_tick)
823 {
824 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100825 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100826 fname, SOURCING_LNUM);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827 *dbg_tick = debug_tick;
828 }
829
830 next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100831 SOURCING_LNUM = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100833 // Did we encounter a breakpoint?
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834 if (breakpoint != NULL && *breakpoint != 0
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100835 && *breakpoint <= SOURCING_LNUM)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000836 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100837 dbg_breakpoint(fname, SOURCING_LNUM);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100838 // Find next breakpoint.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100840 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100841 fname, SOURCING_LNUM);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842 *dbg_tick = debug_tick;
843 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000844# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000845 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000846 {
847 if (getline_is_func)
848 func_line_start(real_cookie);
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100849 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000850 script_line_start();
851 }
852# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000853 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854#endif
855
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100856 // 2. If no line given, get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000857 if (next_cmdline == NULL)
858 {
859 /*
860 * Need to set msg_didout for the first line after an ":if",
861 * otherwise the ":if" will be overwritten.
862 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100863 if (count == 1 && getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000864 msg_didout = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100865 if (fgetline == NULL || (next_cmdline = fgetline(':', cookie,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000866#ifdef FEAT_EVAL
867 cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2
868#else
869 0
870#endif
Bram Moolenaare96a2492019-06-25 04:12:16 +0200871 , TRUE)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000872 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100873 // Don't call wait_return for aborted command line. The NULL
874 // returned for the end of a sourced file or executed function
875 // doesn't do this.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876 if (KeyTyped && !(flags & DOCMD_REPEAT))
877 need_wait_return = FALSE;
878 retval = FAIL;
879 break;
880 }
881 used_getline = TRUE;
882
883 /*
884 * Keep the first typed line. Clear it when more lines are typed.
885 */
886 if (flags & DOCMD_KEEPLINE)
887 {
888 vim_free(repeat_cmdline);
889 if (count == 0)
890 repeat_cmdline = vim_strsave(next_cmdline);
891 else
892 repeat_cmdline = NULL;
893 }
894 }
895
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100896 // 3. Make a copy of the command so we can mess with it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000897 else if (cmdline_copy == NULL)
898 {
899 next_cmdline = vim_strsave(next_cmdline);
900 if (next_cmdline == NULL)
901 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100902 emsg(_(e_outofmem));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000903 retval = FAIL;
904 break;
905 }
906 }
907 cmdline_copy = next_cmdline;
908
909#ifdef FEAT_EVAL
910 /*
Bram Moolenaard5053d02020-06-28 15:51:16 +0200911 * Inside a while/for loop, and when the command looks like a ":while"
912 * or ":for", the line is stored, because we may need it later when
913 * looping.
914 *
915 * When there is a '|' and another command, it is stored separately,
916 * because we need to be able to jump back to it from an
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000917 * :endwhile/:endfor.
Bram Moolenaard5053d02020-06-28 15:51:16 +0200918 *
919 * Pass a different "fgetline" function to do_one_cmd() below,
920 * that it stores lines in or reads them from "lines_ga". Makes it
921 * possible to define a function inside a while/for loop and handles
922 * line continuation.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000923 */
Bram Moolenaard5053d02020-06-28 15:51:16 +0200924 if ((cstack.cs_looplevel > 0 || has_loop_cmd(next_cmdline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000925 {
Bram Moolenaard5053d02020-06-28 15:51:16 +0200926 cmd_getline = get_loop_line;
927 cmd_cookie = (void *)&cmd_loop_cookie;
928 cmd_loop_cookie.lines_gap = &lines_ga;
929 cmd_loop_cookie.current_line = current_line;
930 cmd_loop_cookie.getline = fgetline;
931 cmd_loop_cookie.cookie = cookie;
932 cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len);
933
934 // Save the current line when encountering it the first time.
935 if (current_line == lines_ga.ga_len
936 && store_loop_line(&lines_ga, next_cmdline) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937 {
938 retval = FAIL;
939 break;
940 }
Bram Moolenaard5053d02020-06-28 15:51:16 +0200941 current_line_before = current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000942 }
Bram Moolenaard5053d02020-06-28 15:51:16 +0200943 else
944 {
945 cmd_getline = fgetline;
946 cmd_cookie = cookie;
947 }
948
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949 did_endif = FALSE;
950#endif
951
952 if (count++ == 0)
953 {
954 /*
955 * All output from the commands is put below each other, without
956 * waiting for a return. Don't do this when executing commands
957 * from a script or when being called recursive (e.g. for ":e
958 * +command file").
959 */
960 if (!(flags & DOCMD_NOWAIT) && !recursive)
961 {
962 msg_didout_before_start = msg_didout;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100963 msg_didany = FALSE; // no output yet
Bram Moolenaar071d4272004-06-13 20:20:40 +0000964 msg_start();
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100965 msg_scroll = TRUE; // put messages below each other
966 ++no_wait_return; // don't wait for return until finished
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967 ++RedrawingDisabled;
968 did_inc = TRUE;
969 }
970 }
971
Bram Moolenaar823654b2020-05-29 23:03:09 +0200972 if ((p_verbose >= 15 && SOURCING_NAME != NULL) || p_verbose >= 16)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100973 msg_verbose_cmd(SOURCING_LNUM, cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000974
975 /*
976 * 2. Execute one '|' separated command.
977 * do_one_cmd() will return NULL if there is no trailing '|'.
978 * "cmdline_copy" can change, e.g. for '%' and '#' expansion.
979 */
980 ++recursive;
981 next_cmdline = do_one_cmd(&cmdline_copy, flags & DOCMD_VERBOSE,
982#ifdef FEAT_EVAL
983 &cstack,
984#endif
985 cmd_getline, cmd_cookie);
986 --recursive;
987
988#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000989 if (cmd_cookie == (void *)&cmd_loop_cookie)
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100990 // Use "current_line" from "cmd_loop_cookie", it may have been
991 // incremented when defining a function.
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000992 current_line = cmd_loop_cookie.current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993#endif
994
995 if (next_cmdline == NULL)
996 {
Bram Moolenaard23a8232018-02-10 18:45:26 +0100997 VIM_CLEAR(cmdline_copy);
Bram Moolenaard7663c22019-08-06 21:59:57 +0200998
Bram Moolenaar071d4272004-06-13 20:20:40 +0000999 /*
1000 * If the command was typed, remember it for the ':' register.
1001 * Do this AFTER executing the command to make :@: work.
1002 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001003 if (getline_equal(fgetline, cookie, getexline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001004 && new_last_cmdline != NULL)
1005 {
1006 vim_free(last_cmdline);
1007 last_cmdline = new_last_cmdline;
1008 new_last_cmdline = NULL;
1009 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010 }
1011 else
1012 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001013 // need to copy the command after the '|' to cmdline_copy, for the
1014 // next do_one_cmd()
Bram Moolenaara7241f52008-06-24 20:39:31 +00001015 STRMOVE(cmdline_copy, next_cmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016 next_cmdline = cmdline_copy;
1017 }
1018
1019
1020#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001021 // reset did_emsg for a function that is not aborted by an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022 if (did_emsg && !force_abort
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001023 && getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001024 && !func_has_abort(real_cookie))
1025 did_emsg = FALSE;
1026
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001027 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028 {
1029 ++current_line;
1030
1031 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001032 * An ":endwhile", ":endfor" and ":continue" is handled here.
1033 * If we were executing commands, jump back to the ":while" or
1034 * ":for".
1035 * If we were not executing commands, decrement cs_looplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001037 if (cstack.cs_lflags & (CSL_HAD_CONT | CSL_HAD_ENDLOOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001039 cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001041 // Jump back to the matching ":while" or ":for". Be careful
1042 // not to use a cs_line[] from an entry that isn't a ":while"
1043 // or ":for": It would make "current_line" invalid and can
1044 // cause a crash.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045 if (!did_emsg && !got_int && !did_throw
1046 && cstack.cs_idx >= 0
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001047 && (cstack.cs_flags[cstack.cs_idx]
1048 & (CSF_WHILE | CSF_FOR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049 && cstack.cs_line[cstack.cs_idx] >= 0
1050 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE))
1051 {
1052 current_line = cstack.cs_line[cstack.cs_idx];
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001053 // remember we jumped there
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001054 cstack.cs_lflags |= CSL_HAD_LOOP;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001055 line_breakcheck(); // check if CTRL-C typed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001057 // Check for the next breakpoint at or after the ":while"
1058 // or ":for".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059 if (breakpoint != NULL)
1060 {
1061 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001062 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063 fname,
1064 ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1);
1065 *dbg_tick = debug_tick;
1066 }
1067 }
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001068 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001070 // can only get here with ":endwhile" or ":endfor"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071 if (cstack.cs_idx >= 0)
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001072 rewind_conditionals(&cstack, cstack.cs_idx - 1,
1073 CSF_WHILE | CSF_FOR, &cstack.cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074 }
1075 }
1076
1077 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001078 * For a ":while" or ":for" we need to remember the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001080 else if (cstack.cs_lflags & CSL_HAD_LOOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001082 cstack.cs_lflags &= ~CSL_HAD_LOOP;
Bram Moolenaard5053d02020-06-28 15:51:16 +02001083 cstack.cs_line[cstack.cs_idx] = current_line_before;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084 }
1085 }
1086
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001087 // Check for the next breakpoint after a watchexpression
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01001088 if (breakpoint != NULL && has_watchexpr())
1089 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001090 *breakpoint = dbg_find_breakpoint(FALSE, fname, SOURCING_LNUM);
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01001091 *dbg_tick = debug_tick;
1092 }
1093
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094 /*
1095 * When not inside any ":while" loop, clear remembered lines.
1096 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001097 if (cstack.cs_looplevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098 {
1099 if (lines_ga.ga_len > 0)
1100 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001101 SOURCING_LNUM =
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102 ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum;
1103 free_cmdlines(&lines_ga);
1104 }
1105 current_line = 0;
1106 }
1107
1108 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001109 * A ":finally" makes did_emsg, got_int, and did_throw pending for
1110 * being restored at the ":endtry". Reset them here and set the
1111 * ACTIVE and FINALLY flags, so that the finally clause gets executed.
1112 * This includes the case where a missing ":endif", ":endwhile" or
1113 * ":endfor" was detected by the ":finally" itself.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001115 if (cstack.cs_lflags & CSL_HAD_FINA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001116 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001117 cstack.cs_lflags &= ~CSL_HAD_FINA;
1118 report_make_pending(cstack.cs_pending[cstack.cs_idx]
1119 & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001120 did_throw ? (void *)current_exception : NULL);
1121 did_emsg = got_int = did_throw = FALSE;
1122 cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY;
1123 }
1124
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001125 // Update global "trylevel" for recursive calls to do_cmdline() from
1126 // within this loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001127 trylevel = initial_trylevel + cstack.cs_trylevel;
1128
1129 /*
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001130 * If the outermost try conditional (across function calls and sourced
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131 * files) is aborted because of an error, an interrupt, or an uncaught
1132 * exception, cancel everything. If it is left normally, reset
1133 * force_abort to get the non-EH compatible abortion behavior for
1134 * the rest of the script.
1135 */
1136 if (trylevel == 0 && !did_emsg && !got_int && !did_throw)
1137 force_abort = FALSE;
1138
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001139 // Convert an interrupt to an exception if appropriate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001140 (void)do_intthrow(&cstack);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001141#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142
1143 }
1144 /*
1145 * Continue executing command lines when:
1146 * - no CTRL-C typed, no aborting error, no exception thrown or try
1147 * conditionals need to be checked for executing finally clauses or
1148 * catching an interrupt exception
1149 * - didn't get an error message or lines are not typed
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001150 * - there is a command after '|', inside a :if, :while, :for or :try, or
Bram Moolenaar071d4272004-06-13 20:20:40 +00001151 * looping for ":source" command or function call.
1152 */
1153 while (!((got_int
1154#ifdef FEAT_EVAL
1155 || (did_emsg && force_abort) || did_throw
1156#endif
1157 )
1158#ifdef FEAT_EVAL
1159 && cstack.cs_trylevel == 0
1160#endif
1161 )
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001162 && !(did_emsg
1163#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001164 // Keep going when inside try/catch, so that the error can be
1165 // deal with, except when it is a syntax error, it may cause
1166 // the :endtry to be missed.
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001167 && (cstack.cs_trylevel == 0 || did_emsg_syntax)
1168#endif
1169 && used_getline
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001170 && (getline_equal(fgetline, cookie, getexmodeline)
1171 || getline_equal(fgetline, cookie, getexline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172 && (next_cmdline != NULL
1173#ifdef FEAT_EVAL
1174 || cstack.cs_idx >= 0
1175#endif
1176 || (flags & DOCMD_REPEAT)));
1177
1178 vim_free(cmdline_copy);
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001179 did_emsg_syntax = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180#ifdef FEAT_EVAL
1181 free_cmdlines(&lines_ga);
1182 ga_clear(&lines_ga);
1183
1184 if (cstack.cs_idx >= 0)
1185 {
1186 /*
1187 * If a sourced file or executed function ran to its end, report the
1188 * unclosed conditional.
1189 */
1190 if (!got_int && !did_throw
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001191 && ((getline_equal(fgetline, cookie, getsourceline)
1192 && !source_finished(fgetline, cookie))
1193 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194 && !func_has_ended(real_cookie))))
1195 {
1196 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001197 emsg(_(e_endtry));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001199 emsg(_(e_endwhile));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001200 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001201 emsg(_(e_endfor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001203 emsg(_(e_endif));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204 }
1205
1206 /*
1207 * Reset "trylevel" in case of a ":finish" or ":return" or a missing
1208 * ":endtry" in a sourced file or executed function. If the try
1209 * conditional is in its finally clause, ignore anything pending.
1210 * If it is in a catch clause, finish the caught exception.
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001211 * Also cleanup any "cs_forinfo" structures.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212 */
1213 do
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001214 {
1215 int idx = cleanup_conditionals(&cstack, 0, TRUE);
1216
Bram Moolenaar89e5d682005-01-17 22:06:23 +00001217 if (idx >= 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001218 --idx; // remove try block not in its finally clause
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001219 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
1220 &cstack.cs_looplevel);
1221 }
1222 while (cstack.cs_idx >= 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223 trylevel = initial_trylevel;
1224 }
1225
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001226 // If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
1227 // lack was reported above and the error message is to be converted to an
1228 // exception, do this now after rewinding the cstack.
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001229 do_errthrow(&cstack, getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001230 ? (char_u *)"endfunction" : (char_u *)NULL);
1231
1232 if (trylevel == 0)
1233 {
1234 /*
1235 * When an exception is being thrown out of the outermost try
1236 * conditional, discard the uncaught exception, disable the conversion
1237 * of interrupts or errors to exceptions, and ensure that no more
1238 * commands are executed.
1239 */
1240 if (did_throw)
1241 {
1242 void *p = NULL;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02001243 msglist_T *messages = NULL, *next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244
1245 /*
1246 * If the uncaught exception is a user exception, report it as an
1247 * error. If it is an error exception, display the saved error
1248 * message now. For an interrupt exception, do nothing; the
1249 * interrupt message is given elsewhere.
1250 */
1251 switch (current_exception->type)
1252 {
1253 case ET_USER:
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001254 vim_snprintf((char *)IObuff, IOSIZE,
1255 _("E605: Exception not caught: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256 current_exception->value);
1257 p = vim_strsave(IObuff);
1258 break;
1259 case ET_ERROR:
1260 messages = current_exception->messages;
1261 current_exception->messages = NULL;
1262 break;
1263 case ET_INTERRUPT:
1264 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265 }
1266
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001267 estack_push(ETYPE_EXCEPT, current_exception->throw_name,
1268 current_exception->throw_lnum);
Bram Moolenaare31ee862020-01-07 20:59:34 +01001269 ESTACK_CHECK_SETUP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270 current_exception->throw_name = NULL;
1271
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001272 discard_current_exception(); // uses IObuff if 'verbose'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273 suppress_errthrow = TRUE;
1274 force_abort = TRUE;
1275
1276 if (messages != NULL)
1277 {
1278 do
1279 {
1280 next = messages->next;
1281 emsg(messages->msg);
1282 vim_free(messages->msg);
Bram Moolenaar5fbf3bc2020-06-01 21:13:11 +02001283 vim_free(messages->sfile);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284 vim_free(messages);
1285 messages = next;
1286 }
1287 while (messages != NULL);
1288 }
1289 else if (p != NULL)
1290 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01001291 emsg(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292 vim_free(p);
1293 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001294 vim_free(SOURCING_NAME);
Bram Moolenaare31ee862020-01-07 20:59:34 +01001295 ESTACK_CHECK_NOW
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001296 estack_pop();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297 }
1298
1299 /*
1300 * On an interrupt or an aborting error not converted to an exception,
1301 * disable the conversion of errors to exceptions. (Interrupts are not
Bram Moolenaar2196bce2020-04-12 20:01:11 +02001302 * converted anymore, here.) This enables also the interrupt message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303 * when force_abort is set and did_emsg unset in case of an interrupt
1304 * from a finally clause after an error.
1305 */
1306 else if (got_int || (did_emsg && force_abort))
1307 suppress_errthrow = TRUE;
1308 }
1309
1310 /*
1311 * The current cstack will be freed when do_cmdline() returns. An uncaught
1312 * exception will have to be rethrown in the previous cstack. If a function
1313 * has just returned or a script file was just finished and the previous
1314 * cstack belongs to the same function or, respectively, script file, it
1315 * will have to be checked for finally clauses to be executed due to the
1316 * ":return" or ":finish". This is done in do_one_cmd().
1317 */
1318 if (did_throw)
1319 need_rethrow = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001320 if ((getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321 && ex_nesting_level > source_level(real_cookie))
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001322 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323 && ex_nesting_level > func_level(real_cookie) + 1))
1324 {
1325 if (!did_throw)
1326 check_cstack = TRUE;
1327 }
1328 else
1329 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001330 // When leaving a function, reduce nesting level.
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001331 if (getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332 --ex_nesting_level;
1333 /*
1334 * Go to debug mode when returning from a function in which we are
1335 * single-stepping.
1336 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001337 if ((getline_equal(fgetline, cookie, getsourceline)
1338 || getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339 && ex_nesting_level + 1 <= debug_break_level)
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001340 do_debug(getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341 ? (char_u *)_("End of sourced file")
1342 : (char_u *)_("End of function"));
1343 }
1344
1345 /*
1346 * Restore the exception environment (done after returning from the
1347 * debugger).
1348 */
1349 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001350 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351
1352 msg_list = saved_msg_list;
Bram Moolenaar292b90d2020-03-18 15:23:16 +01001353
1354 // Cleanup if "cs_emsg_silent_list" remains.
1355 if (cstack.cs_emsg_silent_list != NULL)
1356 {
1357 eslist_T *elem, *temp;
1358
1359 for (elem = cstack.cs_emsg_silent_list; elem != NULL; elem = temp)
1360 {
1361 temp = elem->next;
1362 vim_free(elem);
1363 }
1364 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001365#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366
1367 /*
1368 * If there was too much output to fit on the command line, ask the user to
1369 * hit return before redrawing the screen. With the ":global" command we do
1370 * this only once after the command is finished.
1371 */
1372 if (did_inc)
1373 {
1374 --RedrawingDisabled;
1375 --no_wait_return;
1376 msg_scroll = FALSE;
1377
1378 /*
1379 * When just finished an ":if"-":else" which was typed, no need to
1380 * wait for hit-return. Also for an error situation.
1381 */
1382 if (retval == FAIL
1383#ifdef FEAT_EVAL
1384 || (did_endif && KeyTyped && !did_emsg)
1385#endif
1386 )
1387 {
1388 need_wait_return = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001389 msg_didany = FALSE; // don't wait when restarting edit
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390 }
1391 else if (need_wait_return)
1392 {
1393 /*
1394 * The msg_start() above clears msg_didout. The wait_return we do
1395 * here should not overwrite the command that may be shown before
1396 * doing that.
1397 */
1398 msg_didout |= msg_didout_before_start;
1399 wait_return(FALSE);
1400 }
1401 }
1402
Bram Moolenaar2a942252012-11-28 23:03:07 +01001403#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001404 did_endif = FALSE; // in case do_cmdline used recursively
Bram Moolenaar2a942252012-11-28 23:03:07 +01001405#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406 /*
1407 * Reset if_level, in case a sourced script file contains more ":if" than
1408 * ":endif" (could be ":if x | foo | endif").
1409 */
1410 if_level = 0;
Bram Moolenaar2e18a122012-11-28 19:10:54 +01001411#endif
Bram Moolenaard4ad0d42012-11-28 17:34:48 +01001412
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413 --call_depth;
1414 return retval;
1415}
1416
1417#ifdef FEAT_EVAL
1418/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001419 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420 */
1421 static char_u *
Bram Moolenaare96a2492019-06-25 04:12:16 +02001422get_loop_line(int c, void *cookie, int indent, int do_concat)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001424 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425 wcmd_T *wp;
1426 char_u *line;
1427
1428 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1429 {
1430 if (cp->repeating)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001431 return NULL; // trying to read past ":endwhile"/":endfor"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001433 // First time inside the ":while"/":for": get line normally.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001434 if (cp->getline == NULL)
Bram Moolenaare96a2492019-06-25 04:12:16 +02001435 line = getcmdline(c, 0L, indent, do_concat);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001436 else
Bram Moolenaare96a2492019-06-25 04:12:16 +02001437 line = cp->getline(c, cp->cookie, indent, do_concat);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001438 if (line != NULL && store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001439 ++cp->current_line;
1440
1441 return line;
1442 }
1443
1444 KeyTyped = FALSE;
1445 ++cp->current_line;
1446 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001447 SOURCING_LNUM = wp->lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001448 return vim_strsave(wp->line);
1449}
1450
1451/*
1452 * Store a line in "gap" so that a ":while" loop can execute it again.
1453 */
1454 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001455store_loop_line(garray_T *gap, char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001456{
1457 if (ga_grow(gap, 1) == FAIL)
1458 return FAIL;
1459 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001460 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = SOURCING_LNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001461 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001462 return OK;
1463}
1464
1465/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001466 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001467 */
1468 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001469free_cmdlines(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470{
1471 while (gap->ga_len > 0)
1472 {
1473 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1474 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001475 }
1476}
1477#endif
1478
1479/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001480 * If "fgetline" is get_loop_line(), return TRUE if the getline it uses equals
1481 * "func". * Otherwise return TRUE when "fgetline" equals "func".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001484getline_equal(
Bram Moolenaare96a2492019-06-25 04:12:16 +02001485 char_u *(*fgetline)(int, void *, int, int),
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001486 void *cookie UNUSED, // argument for fgetline()
Bram Moolenaare96a2492019-06-25 04:12:16 +02001487 char_u *(*func)(int, void *, int, int))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488{
1489#ifdef FEAT_EVAL
Bram Moolenaare96a2492019-06-25 04:12:16 +02001490 char_u *(*gp)(int, void *, int, int);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001491 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001492
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001493 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1494 // function that's originally used to obtain the lines. This may be
1495 // nested several levels.
Bram Moolenaar89d40322006-08-29 15:30:07 +00001496 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001497 cp = (struct loop_cookie *)cookie;
1498 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001499 {
1500 gp = cp->getline;
1501 cp = cp->cookie;
1502 }
1503 return gp == func;
1504#else
Bram Moolenaar89d40322006-08-29 15:30:07 +00001505 return fgetline == func;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001506#endif
1507}
1508
Bram Moolenaar071d4272004-06-13 20:20:40 +00001509/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001510 * If "fgetline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511 * getline function. Otherwise return "cookie".
1512 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513 void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001514getline_cookie(
Bram Moolenaare96a2492019-06-25 04:12:16 +02001515 char_u *(*fgetline)(int, void *, int, int) UNUSED,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001516 void *cookie) // argument for fgetline()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517{
Bram Moolenaar13505972019-01-24 15:04:48 +01001518#ifdef FEAT_EVAL
Bram Moolenaare96a2492019-06-25 04:12:16 +02001519 char_u *(*gp)(int, void *, int, int);
Bram Moolenaard5053d02020-06-28 15:51:16 +02001520 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001521
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001522 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1523 // cookie that's originally used to obtain the lines. This may be nested
1524 // several levels.
Bram Moolenaar89d40322006-08-29 15:30:07 +00001525 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001526 cp = (struct loop_cookie *)cookie;
1527 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001528 {
1529 gp = cp->getline;
1530 cp = cp->cookie;
1531 }
1532 return cp;
Bram Moolenaar13505972019-01-24 15:04:48 +01001533#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001534 return cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001535#endif
Bram Moolenaar13505972019-01-24 15:04:48 +01001536}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537
Bram Moolenaard5053d02020-06-28 15:51:16 +02001538#if defined(FEAT_EVAL) || defined(PROT)
1539/*
1540 * Get the next line source line without advancing.
1541 */
1542 char_u *
1543getline_peek(
1544 char_u *(*fgetline)(int, void *, int, int) UNUSED,
1545 void *cookie) // argument for fgetline()
1546{
1547 char_u *(*gp)(int, void *, int, int);
1548 struct loop_cookie *cp;
1549 wcmd_T *wp;
1550
1551 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1552 // cookie that's originally used to obtain the lines. This may be nested
1553 // several levels.
1554 gp = fgetline;
1555 cp = (struct loop_cookie *)cookie;
1556 while (gp == get_loop_line)
1557 {
1558 if (cp->current_line + 1 < cp->lines_gap->ga_len)
1559 {
1560 // executing lines a second time, use the stored copy
1561 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line + 1;
1562 return wp->line;
1563 }
1564 gp = cp->getline;
1565 cp = cp->cookie;
1566 }
1567 if (gp == getsourceline)
1568 return source_nextline(cp);
1569 return NULL;
1570}
1571#endif
1572
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001573
1574/*
1575 * Helper function to apply an offset for buffer commands, i.e. ":bdelete",
1576 * ":bwipeout", etc.
1577 * Returns the buffer number.
1578 */
1579 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001580compute_buffer_local_count(int addr_type, int lnum, int offset)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001581{
1582 buf_T *buf;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001583 buf_T *nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001584 int count = offset;
1585
1586 buf = firstbuf;
1587 while (buf->b_next != NULL && buf->b_fnum < lnum)
1588 buf = buf->b_next;
1589 while (count != 0)
1590 {
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001591 count += (offset < 0) ? 1 : -1;
1592 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1593 if (nextbuf == NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001594 break;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001595 buf = nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001596 if (addr_type == ADDR_LOADED_BUFFERS)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001597 // skip over unloaded buffers
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001598 while (buf->b_ml.ml_mfp == NULL)
1599 {
1600 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1601 if (nextbuf == NULL)
1602 break;
1603 buf = nextbuf;
1604 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001605 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001606 // we might have gone too far, last buffer is not loadedd
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001607 if (addr_type == ADDR_LOADED_BUFFERS)
1608 while (buf->b_ml.ml_mfp == NULL)
1609 {
1610 nextbuf = (offset >= 0) ? buf->b_prev : buf->b_next;
1611 if (nextbuf == NULL)
1612 break;
1613 buf = nextbuf;
1614 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001615 return buf->b_fnum;
1616}
1617
Bram Moolenaarb7316892019-05-01 18:08:42 +02001618/*
1619 * Return the window number of "win".
1620 * When "win" is NULL return the number of windows.
1621 */
Bram Moolenaarf240e182014-11-27 18:33:02 +01001622 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001623current_win_nr(win_T *win)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001624{
1625 win_T *wp;
1626 int nr = 0;
1627
Bram Moolenaar29323592016-07-24 22:04:11 +02001628 FOR_ALL_WINDOWS(wp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001629 {
1630 ++nr;
1631 if (wp == win)
1632 break;
1633 }
1634 return nr;
1635}
1636
1637 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001638current_tab_nr(tabpage_T *tab)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001639{
1640 tabpage_T *tp;
1641 int nr = 0;
1642
Bram Moolenaar29323592016-07-24 22:04:11 +02001643 FOR_ALL_TABPAGES(tp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001644 {
1645 ++nr;
1646 if (tp == tab)
1647 break;
1648 }
1649 return nr;
1650}
1651
1652# define CURRENT_WIN_NR current_win_nr(curwin)
1653# define LAST_WIN_NR current_win_nr(NULL)
1654# define CURRENT_TAB_NR current_tab_nr(curtab)
1655# define LAST_TAB_NR current_tab_nr(NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001656
Bram Moolenaar071d4272004-06-13 20:20:40 +00001657/*
1658 * Execute one Ex command.
1659 *
1660 * If 'sourcing' is TRUE, the command will be included in the error message.
1661 *
1662 * 1. skip comment lines and leading space
1663 * 2. handle command modifiers
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001664 * 3. find the command
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001665 * 4. parse range
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001666 * 5. Parse the command.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001667 * 6. parse arguments
1668 * 7. switch on command name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669 *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001670 * Note: "fgetline" can be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001671 *
1672 * This function may be called recursively!
1673 */
1674#if (_MSC_VER == 1200)
1675/*
Bram Moolenaared203462004-06-16 11:19:22 +00001676 * Avoid optimisation bug in VC++ version 6.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001677 */
Bram Moolenaar281bdce2005-01-25 21:53:18 +00001678 #pragma optimize( "g", off )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001679#endif
1680 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001681do_one_cmd(
Bram Moolenaarddef1292019-12-16 17:10:33 +01001682 char_u **cmdlinep,
1683 int sourcing,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684#ifdef FEAT_EVAL
Bram Moolenaarddef1292019-12-16 17:10:33 +01001685 cstack_T *cstack,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001686#endif
Bram Moolenaarddef1292019-12-16 17:10:33 +01001687 char_u *(*fgetline)(int, void *, int, int),
1688 void *cookie) // argument for fgetline()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689{
Bram Moolenaarddef1292019-12-16 17:10:33 +01001690 char_u *p;
1691 linenr_T lnum;
1692 long n;
1693 char *errormsg = NULL; // error message
1694 char_u *after_modifier = NULL;
1695 exarg_T ea; // Ex command arguments
1696 int save_msg_scroll = msg_scroll;
1697 cmdmod_T save_cmdmod;
1698 int save_reg_executing = reg_executing;
1699 int ni; // set when Not Implemented
1700 char_u *cmd;
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001701#ifdef FEAT_EVAL
1702 int starts_with_colon;
1703#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001704
Bram Moolenaara80faa82020-04-12 19:37:17 +02001705 CLEAR_FIELD(ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001706 ea.line1 = 1;
1707 ea.line2 = 1;
1708#ifdef FEAT_EVAL
1709 ++ex_nesting_level;
1710#endif
1711
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001712 // When the last file has not been edited :q has to be typed twice.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713 if (quitmore
1714#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001715 // avoid that a function call in 'statusline' does this
Bram Moolenaar89d40322006-08-29 15:30:07 +00001716 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001717#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001718 // avoid that an autocommand, e.g. QuitPre, does this
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001719 && !getline_equal(fgetline, cookie, getnextac))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001720 --quitmore;
1721
1722 /*
1723 * Reset browse, confirm, etc.. They are restored when returning, for
1724 * recursive calls.
1725 */
1726 save_cmdmod = cmdmod;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001728 // "#!anything" is handled like a comment.
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001729 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1730 goto doend;
1731
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02001732/*
1733 * 1. Skip comment lines and leading white space and colons.
1734 * 2. Handle command modifiers.
1735 */
1736 // The "ea" structure holds the arguments that can be used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001737 ea.cmd = *cmdlinep;
Bram Moolenaareffed932018-08-14 13:38:17 +02001738 ea.cmdlinep = cmdlinep;
1739 ea.getline = fgetline;
1740 ea.cookie = cookie;
1741#ifdef FEAT_EVAL
1742 ea.cstack = cstack;
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001743 starts_with_colon = *skipwhite(ea.cmd) == ':';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001744#endif
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02001745 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaareffed932018-08-14 13:38:17 +02001746 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001747
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001748 after_modifier = ea.cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749
1750#ifdef FEAT_EVAL
1751 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
1752 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
1753#else
1754 ea.skip = (if_level > 0);
1755#endif
1756
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001758 * 3. Skip over the range to find the command. Let "p" point to after it.
1759 *
1760 * We need the command to know what kind of range it uses.
1761 */
1762 cmd = ea.cmd;
1763 ea.cmd = skip_range(ea.cmd, NULL);
1764 if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
1765 ea.cmd = skipwhite(ea.cmd + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001766
1767#ifdef FEAT_EVAL
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001768 if (current_sctx.sc_version == SCRIPT_VERSION_VIM9 && !starts_with_colon)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02001769 {
1770 if (ea.cmd > cmd)
1771 {
1772 emsg(_(e_colon_required));
1773 goto doend;
1774 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001775 p = find_ex_command(&ea, NULL, lookup_scriptvar, NULL);
Bram Moolenaardf069ee2020-06-22 23:02:51 +02001776 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001777 else
1778#endif
1779 p = find_ex_command(&ea, NULL, NULL, NULL);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001780
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001781#ifdef FEAT_EVAL
1782# ifdef FEAT_PROFILE
1783 // Count this line for profiling if skip is TRUE.
1784 if (do_profiling == PROF_YES
1785 && (!ea.skip || cstack->cs_idx == 0 || (cstack->cs_idx > 0
1786 && (cstack->cs_flags[cstack->cs_idx - 1] & CSF_ACTIVE))))
1787 {
1788 int skip = did_emsg || got_int || did_throw;
1789
1790 if (ea.cmdidx == CMD_catch)
1791 skip = !skip && !(cstack->cs_idx >= 0
1792 && (cstack->cs_flags[cstack->cs_idx] & CSF_THROWN)
1793 && !(cstack->cs_flags[cstack->cs_idx] & CSF_CAUGHT));
1794 else if (ea.cmdidx == CMD_else || ea.cmdidx == CMD_elseif)
1795 skip = skip || !(cstack->cs_idx >= 0
1796 && !(cstack->cs_flags[cstack->cs_idx]
1797 & (CSF_ACTIVE | CSF_TRUE)));
1798 else if (ea.cmdidx == CMD_finally)
1799 skip = FALSE;
1800 else if (ea.cmdidx != CMD_endif
1801 && ea.cmdidx != CMD_endfor
1802 && ea.cmdidx != CMD_endtry
1803 && ea.cmdidx != CMD_endwhile)
1804 skip = ea.skip;
1805
1806 if (!skip)
1807 {
1808 if (getline_equal(fgetline, cookie, get_func_line))
1809 func_line_exec(getline_cookie(fgetline, cookie));
1810 else if (getline_equal(fgetline, cookie, getsourceline))
1811 script_line_exec();
1812 }
1813 }
1814# endif
1815
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001816 // May go to debug mode. If this happens and the ">quit" debug command is
1817 // used, throw an interrupt exception and skip the next command.
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001818 dbg_check_breakpoint(&ea);
1819 if (!ea.skip && got_int)
1820 {
1821 ea.skip = TRUE;
1822 (void)do_intthrow(cstack);
1823 }
1824#endif
1825
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001826/*
1827 * 4. parse a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828 *
1829 * where 'addr' is:
1830 *
1831 * % (entire file)
1832 * $ [+-NUM]
1833 * 'x [+-NUM] (where x denotes a currently defined mark)
1834 * . [+-NUM]
1835 * [+-NUM]..
1836 * NUM
1837 *
1838 * The ea.cmd pointer is updated to point to the first character following the
1839 * range spec. If an initial address is found, but no second, the upper bound
1840 * is equal to the lower.
1841 */
1842
Bram Moolenaar25190db2019-05-04 15:05:28 +02001843 // ea.addr_type for user commands is set by find_ucmd
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001844 if (!IS_USER_CMDIDX(ea.cmdidx))
1845 {
1846 if (ea.cmdidx != CMD_SIZE)
1847 ea.addr_type = cmdnames[(int)ea.cmdidx].cmd_addr_type;
1848 else
1849 ea.addr_type = ADDR_LINES;
1850
Bram Moolenaar25190db2019-05-04 15:05:28 +02001851 // :wincmd range depends on the argument.
Bram Moolenaar164f3262015-01-14 21:22:01 +01001852 if (ea.cmdidx == CMD_wincmd && p != NULL)
1853 get_wincmd_addr_type(skipwhite(p), &ea);
Bram Moolenaar25190db2019-05-04 15:05:28 +02001854#ifdef FEAT_QUICKFIX
1855 // :.cc in quickfix window uses line number
1856 if ((ea.cmdidx == CMD_cc || ea.cmdidx == CMD_ll) && bt_quickfix(curbuf))
1857 ea.addr_type = ADDR_OTHER;
1858#endif
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001859 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001860
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001861 ea.cmd = cmd;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02001862 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaaree8415b2018-08-10 23:13:12 +02001863 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001864
Bram Moolenaar071d4272004-06-13 20:20:40 +00001865/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001866 * 5. Parse the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867 */
1868
1869 /*
1870 * Skip ':' and any white space
1871 */
1872 ea.cmd = skipwhite(ea.cmd);
1873 while (*ea.cmd == ':')
1874 ea.cmd = skipwhite(ea.cmd + 1);
1875
1876 /*
1877 * If we got a line, but no command, then go to the line.
1878 * If we find a '|' or '\n' we set ea.nextcmd.
1879 */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001880 if (*ea.cmd == NUL || *ea.cmd == '"'
Bram Moolenaar7a092242020-04-16 22:10:49 +02001881#ifdef FEAT_EVAL
Bram Moolenaara26b9702020-04-18 19:53:28 +02001882 || (*ea.cmd == '#' && ea.cmd[1] != '{'
1883 && !starts_with_colon && in_vim9script())
Bram Moolenaar7a092242020-04-16 22:10:49 +02001884#endif
1885 || (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886 {
1887 /*
1888 * strange vi behaviour:
1889 * ":3" jumps to line 3
1890 * ":3|..." prints line 3
1891 * ":|" prints current line
1892 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001893 if (ea.skip) // skip this if inside :if
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001895 if (*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896 {
1897 ea.cmdidx = CMD_print;
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001898 ea.argt = EX_RANGE+EX_COUNT+EX_TRLBAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001899 if ((errormsg = invalid_range(&ea)) == NULL)
1900 {
1901 correct_range(&ea);
1902 ex_print(&ea);
1903 }
1904 }
1905 else if (ea.addr_count != 0)
1906 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001907 if (ea.line2 > curbuf->b_ml.ml_line_count)
1908 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001909 // With '-' in 'cpoptions' a line number past the file is an
1910 // error, otherwise put it at the end of the file.
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001911 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
1912 ea.line2 = -1;
1913 else
1914 ea.line2 = curbuf->b_ml.ml_line_count;
1915 }
1916
1917 if (ea.line2 < 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001918 errormsg = _(e_invrange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001919 else
1920 {
1921 if (ea.line2 == 0)
1922 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923 else
1924 curwin->w_cursor.lnum = ea.line2;
1925 beginline(BL_SOL | BL_FIX);
1926 }
1927 }
1928 goto doend;
1929 }
1930
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001931 // If this looks like an undefined user command and there are CmdUndefined
1932 // autocommands defined, trigger the matching autocommands.
Bram Moolenaard5005162014-08-22 23:05:54 +02001933 if (p != NULL && ea.cmdidx == CMD_SIZE && !ea.skip
1934 && ASCII_ISUPPER(*ea.cmd)
1935 && has_cmdundefined())
1936 {
Bram Moolenaard5005162014-08-22 23:05:54 +02001937 int ret;
1938
Bram Moolenaar5a31b462014-08-23 14:16:20 +02001939 p = ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02001940 while (ASCII_ISALNUM(*p))
1941 ++p;
Bram Moolenaar158ea172020-06-18 17:28:39 +02001942 p = vim_strnsave(ea.cmd, p - ea.cmd);
Bram Moolenaard5005162014-08-22 23:05:54 +02001943 ret = apply_autocmds(EVENT_CMDUNDEFINED, p, p, TRUE, NULL);
1944 vim_free(p);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001945 // If the autocommands did something and didn't cause an error, try
1946 // finding the command again.
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001947 p = (ret
1948#ifdef FEAT_EVAL
1949 && !aborting()
Bram Moolenaard5005162014-08-22 23:05:54 +02001950#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001951 ) ? find_ex_command(&ea, NULL, NULL, NULL) : ea.cmd;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001952 }
Bram Moolenaard5005162014-08-22 23:05:54 +02001953
Bram Moolenaar071d4272004-06-13 20:20:40 +00001954 if (p == NULL)
1955 {
1956 if (!ea.skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001957 errormsg = _("E464: Ambiguous use of user-defined command");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001958 goto doend;
1959 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001960 // Check for wrong commands.
Bram Moolenaar6f9a4762017-06-22 20:39:17 +02001961 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78
1962 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001963 {
1964 errormsg = uc_fun_cmd();
1965 goto doend;
1966 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001967
Bram Moolenaar071d4272004-06-13 20:20:40 +00001968 if (ea.cmdidx == CMD_SIZE)
1969 {
1970 if (!ea.skip)
1971 {
1972 STRCPY(IObuff, _("E492: Not an editor command"));
1973 if (!sourcing)
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001974 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001975 // If the modifier was parsed OK the error must be in the
1976 // following command
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001977 if (after_modifier != NULL)
1978 append_command(after_modifier);
1979 else
1980 append_command(*cmdlinep);
1981 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001982 errormsg = (char *)IObuff;
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001983 did_emsg_syntax = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001984 }
1985 goto doend;
1986 }
1987
Bram Moolenaar958636c2014-10-21 20:01:58 +02001988 ni = (!IS_USER_CMDIDX(ea.cmdidx)
1989 && (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00001990#ifdef HAVE_EX_SCRIPT_NI
1991 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
1992#endif
1993 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001994
1995#ifndef FEAT_EVAL
1996 /*
1997 * When the expression evaluation is disabled, recognize the ":if" and
1998 * ":endif" commands and ignore everything in between it.
1999 */
2000 if (ea.cmdidx == CMD_if)
2001 ++if_level;
2002 if (if_level)
2003 {
2004 if (ea.cmdidx == CMD_endif)
2005 --if_level;
2006 goto doend;
2007 }
2008
2009#endif
2010
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002011 // forced commands
Bram Moolenaar196b3b02008-06-20 16:51:41 +00002012 if (*p == '!' && ea.cmdidx != CMD_substitute
2013 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002014 {
2015 ++p;
2016 ea.forceit = TRUE;
2017 }
2018 else
2019 ea.forceit = FALSE;
2020
2021/*
Bram Moolenaarb7316892019-05-01 18:08:42 +02002022 * 6. Parse arguments. Then check for errors.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002023 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02002024 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002025 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002026
2027 if (!ea.skip)
2028 {
2029#ifdef HAVE_SANDBOX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002030 if (sandbox != 0 && !(ea.argt & EX_SBOXOK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002031 {
Bram Moolenaar8c62a082019-02-08 14:34:10 +01002032 // Command not allowed in sandbox.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002033 errormsg = _(e_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002034 goto doend;
2035 }
2036#endif
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002037 if (restricted != 0 && (ea.argt & EX_RESTRICT))
Bram Moolenaar8c62a082019-02-08 14:34:10 +01002038 {
2039 errormsg = _("E981: Command not allowed in rvim");
2040 goto doend;
2041 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002042 if (!curbuf->b_p_ma && (ea.argt & EX_MODIFY))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002043 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002044 // Command not allowed in non-'modifiable' buffer
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002045 errormsg = _(e_modifiable);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046 goto doend;
2047 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002048
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002049 if (text_locked() && !(ea.argt & EX_CMDWIN)
Bram Moolenaar958636c2014-10-21 20:01:58 +02002050 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002051 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002052 // Command not allowed when editing the command line.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002053 errormsg = _(get_text_locked_msg());
Bram Moolenaar071d4272004-06-13 20:20:40 +00002054 goto doend;
2055 }
Bram Moolenaar379fb762018-08-30 15:58:28 +02002056
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002057 // Disallow editing another buffer when "curbuf_lock" is set.
2058 // Do allow ":checktime" (it is postponed).
2059 // Do allow ":edit" (check for an argument later).
2060 // Do allow ":file" with no arguments (check for an argument later).
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002061 if (!(ea.argt & EX_CMDWIN)
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002062 && ea.cmdidx != CMD_checktime
Bram Moolenaar379fb762018-08-30 15:58:28 +02002063 && ea.cmdidx != CMD_edit
2064 && ea.cmdidx != CMD_file
Bram Moolenaar958636c2014-10-21 20:01:58 +02002065 && !IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002066 && curbuf_locked())
2067 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002068
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002069 if (!ni && !(ea.argt & EX_RANGE) && ea.addr_count > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002070 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002071 // no range allowed
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002072 errormsg = _(e_norange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002073 goto doend;
2074 }
2075 }
2076
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002077 if (!ni && !(ea.argt & EX_BANG) && ea.forceit) // no <!> allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002078 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002079 errormsg = _(e_nobang);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002080 goto doend;
2081 }
2082
2083 /*
2084 * Don't complain about the range if it is not used
2085 * (could happen if line_count is accidentally set to 0).
2086 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002087 if (!ea.skip && !ni && (ea.argt & EX_RANGE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002088 {
2089 /*
2090 * If the range is backwards, ask for confirmation and, if given, swap
2091 * ea.line1 & ea.line2 so it's forwards again.
2092 * When global command is busy, don't ask, will fail below.
2093 */
2094 if (!global_busy && ea.line1 > ea.line2)
2095 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002096 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002097 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002098 if (sourcing || exmode_active)
2099 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002100 errormsg = _("E493: Backwards range given");
Bram Moolenaara5792f52005-11-23 21:25:05 +00002101 goto doend;
2102 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002103 if (ask_yesno((char_u *)
2104 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002105 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002106 }
2107 lnum = ea.line1;
2108 ea.line1 = ea.line2;
2109 ea.line2 = lnum;
2110 }
2111 if ((errormsg = invalid_range(&ea)) != NULL)
2112 goto doend;
2113 }
2114
Bram Moolenaarb7316892019-05-01 18:08:42 +02002115 if ((ea.addr_type == ADDR_OTHER) && ea.addr_count == 0)
2116 // default is 1, not cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002117 ea.line2 = 1;
2118
2119 correct_range(&ea);
2120
2121#ifdef FEAT_FOLDING
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002122 if (((ea.argt & EX_WHOLEFOLD) || ea.addr_count >= 2) && !global_busy
Bram Moolenaara3306952016-01-02 21:41:06 +01002123 && ea.addr_type == ADDR_LINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002125 // Put the first line at the start of a closed fold, put the last line
2126 // at the end of a closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002127 (void)hasFolding(ea.line1, &ea.line1, NULL);
2128 (void)hasFolding(ea.line2, NULL, &ea.line2);
2129 }
2130#endif
2131
2132#ifdef FEAT_QUICKFIX
2133 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002134 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135 * option here, so things like % get expanded.
2136 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002137 p = replace_makeprg(&ea, p, cmdlinep);
2138 if (p == NULL)
2139 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002140#endif
2141
2142 /*
2143 * Skip to start of argument.
2144 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2145 */
2146 if (ea.cmdidx == CMD_bang)
2147 ea.arg = p;
2148 else
2149 ea.arg = skipwhite(p);
2150
Bram Moolenaar379fb762018-08-30 15:58:28 +02002151 // ":file" cannot be run with an argument when "curbuf_lock" is set
2152 if (ea.cmdidx == CMD_file && *ea.arg != NUL && curbuf_locked())
2153 goto doend;
2154
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155 /*
2156 * Check for "++opt=val" argument.
2157 * Must be first, allow ":w ++enc=utf8 !cmd"
2158 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002159 if (ea.argt & EX_ARGOPT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2161 if (getargopt(&ea) == FAIL && !ni)
2162 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002163 errormsg = _(e_invarg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164 goto doend;
2165 }
2166
2167 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2168 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002169 if (*ea.arg == '>') // append
Bram Moolenaar071d4272004-06-13 20:20:40 +00002170 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002171 if (*++ea.arg != '>') // typed wrong
Bram Moolenaar071d4272004-06-13 20:20:40 +00002172 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002173 errormsg = _("E494: Use w or w>>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002174 goto doend;
2175 }
2176 ea.arg = skipwhite(ea.arg + 1);
2177 ea.append = TRUE;
2178 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002179 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) // :w !filter
Bram Moolenaar071d4272004-06-13 20:20:40 +00002180 {
2181 ++ea.arg;
2182 ea.usefilter = TRUE;
2183 }
2184 }
2185
2186 if (ea.cmdidx == CMD_read)
2187 {
2188 if (ea.forceit)
2189 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002190 ea.usefilter = TRUE; // :r! filter if ea.forceit
Bram Moolenaar071d4272004-06-13 20:20:40 +00002191 ea.forceit = FALSE;
2192 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002193 else if (*ea.arg == '!') // :r !filter
Bram Moolenaar071d4272004-06-13 20:20:40 +00002194 {
2195 ++ea.arg;
2196 ea.usefilter = TRUE;
2197 }
2198 }
2199
2200 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2201 {
2202 ea.amount = 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002203 while (*ea.arg == *ea.cmd) // count number of '>' or '<'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002204 {
2205 ++ea.arg;
2206 ++ea.amount;
2207 }
2208 ea.arg = skipwhite(ea.arg);
2209 }
2210
2211 /*
2212 * Check for "+command" argument, before checking for next command.
2213 * Don't do this for ":read !cmd" and ":write !cmd".
2214 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002215 if ((ea.argt & EX_CMDARG) && !ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002216 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2217
2218 /*
2219 * Check for '|' to separate commands and '"' to start comments.
2220 * Don't do this for ":read !cmd" and ":write !cmd".
2221 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002222 if ((ea.argt & EX_TRLBAR) && !ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223 separate_nextcmd(&ea);
2224
2225 /*
2226 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002227 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2228 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002229 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002230 else if (ea.cmdidx == CMD_bang
Bram Moolenaar67883b42017-07-27 22:57:00 +02002231 || ea.cmdidx == CMD_terminal
Bram Moolenaardf177f62005-02-22 08:39:57 +00002232 || ea.cmdidx == CMD_global
2233 || ea.cmdidx == CMD_vglobal
2234 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002235 {
2236 for (p = ea.arg; *p; ++p)
2237 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002238 // Remove one backslash before a newline, so that it's possible to
2239 // pass a newline to the shell and also a newline that is preceded
2240 // with a backslash. This makes it impossible to end a shell
2241 // command in a backslash, but that doesn't appear useful.
2242 // Halving the number of backslashes is incompatible with previous
2243 // versions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002244 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002245 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002246 else if (*p == '\n')
2247 {
2248 ea.nextcmd = p + 1;
2249 *p = NUL;
2250 break;
2251 }
2252 }
2253 }
2254
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002255 if ((ea.argt & EX_DFLALL) && ea.addr_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002256 {
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002257 buf_T *buf;
2258
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259 ea.line1 = 1;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002260 switch (ea.addr_type)
2261 {
2262 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02002263 case ADDR_OTHER:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002264 ea.line2 = curbuf->b_ml.ml_line_count;
2265 break;
2266 case ADDR_LOADED_BUFFERS:
2267 buf = firstbuf;
2268 while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL)
2269 buf = buf->b_next;
2270 ea.line1 = buf->b_fnum;
2271 buf = lastbuf;
2272 while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL)
2273 buf = buf->b_prev;
2274 ea.line2 = buf->b_fnum;
2275 break;
2276 case ADDR_BUFFERS:
2277 ea.line1 = firstbuf->b_fnum;
2278 ea.line2 = lastbuf->b_fnum;
2279 break;
2280 case ADDR_WINDOWS:
2281 ea.line2 = LAST_WIN_NR;
2282 break;
2283 case ADDR_TABS:
2284 ea.line2 = LAST_TAB_NR;
2285 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01002286 case ADDR_TABS_RELATIVE:
2287 ea.line2 = 1;
2288 break;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002289 case ADDR_ARGUMENTS:
2290 if (ARGCOUNT == 0)
2291 ea.line1 = ea.line2 = 0;
2292 else
2293 ea.line2 = ARGCOUNT;
2294 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02002295 case ADDR_QUICKFIX_VALID:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002296#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02002297 ea.line2 = qf_get_valid_size(&ea);
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002298 if (ea.line2 == 0)
2299 ea.line2 = 1;
Bram Moolenaare906c502015-09-09 21:10:39 +02002300#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002301 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02002302 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02002303 case ADDR_UNSIGNED:
2304 case ADDR_QUICKFIX:
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002305 iemsg(_("INTERNAL: Cannot use EX_DFLALL with ADDR_NONE, ADDR_UNSIGNED or ADDR_QUICKFIX"));
Bram Moolenaarb7316892019-05-01 18:08:42 +02002306 break;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002307 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308 }
2309
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002310 // accept numbered register only when no count allowed (:put)
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002311 if ( (ea.argt & EX_REGSTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312 && *ea.arg != NUL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002313 // Do not allow register = for user commands
Bram Moolenaar958636c2014-10-21 20:01:58 +02002314 && (!IS_USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002315 && !((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002316 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002317#ifndef FEAT_CLIPBOARD
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002318 // check these explicitly for a more specific error message
Bram Moolenaar85de2062011-05-05 14:26:41 +02002319 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002320 {
Bram Moolenaar99b12722019-01-14 20:16:40 +01002321 errormsg = _(e_invalidreg);
Bram Moolenaar85de2062011-05-05 14:26:41 +02002322 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323 }
2324#endif
Bram Moolenaar958636c2014-10-21 20:01:58 +02002325 if (valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2326 && !IS_USER_CMDIDX(ea.cmdidx))))
Bram Moolenaar85de2062011-05-05 14:26:41 +02002327 {
2328 ea.regname = *ea.arg++;
2329#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002330 // for '=' register: accept the rest of the line as an expression
Bram Moolenaar85de2062011-05-05 14:26:41 +02002331 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2332 {
2333 set_expr_line(vim_strsave(ea.arg));
2334 ea.arg += STRLEN(ea.arg);
2335 }
2336#endif
2337 ea.arg = skipwhite(ea.arg);
2338 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002339 }
2340
2341 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002342 * Check for a count. When accepting a EX_BUFNAME, don't use "123foo" as a
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343 * count, it's a buffer name.
2344 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002345 if ((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)
2346 && (!(ea.argt & EX_BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01002347 || VIM_ISWHITE(*p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348 {
2349 n = getdigits(&ea.arg);
2350 ea.arg = skipwhite(ea.arg);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002351 if (n <= 0 && !ni && (ea.argt & EX_ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002353 errormsg = _(e_zerocount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002354 goto doend;
2355 }
Bram Moolenaarb7316892019-05-01 18:08:42 +02002356 if (ea.addr_type != ADDR_LINES) // e.g. :buffer 2, :sleep 3
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357 {
2358 ea.line2 = n;
2359 if (ea.addr_count == 0)
2360 ea.addr_count = 1;
2361 }
2362 else
2363 {
2364 ea.line1 = ea.line2;
2365 ea.line2 += n - 1;
2366 ++ea.addr_count;
2367 /*
2368 * Be vi compatible: no error message for out of range.
2369 */
Bram Moolenaarb7316892019-05-01 18:08:42 +02002370 if (ea.line2 > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371 ea.line2 = curbuf->b_ml.ml_line_count;
2372 }
2373 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002374
2375 /*
2376 * Check for flags: 'l', 'p' and '#'.
2377 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002378 if (ea.argt & EX_FLAGS)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002379 get_flags(&ea);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002380 if (!ni && !(ea.argt & EX_EXTRA) && *ea.arg != NUL
2381 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & EX_TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002382 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002383 // no arguments allowed but there is something
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002384 errormsg = _(e_trailing);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385 goto doend;
2386 }
2387
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002388 if (!ni && (ea.argt & EX_NEEDARG) && *ea.arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002390 errormsg = _(e_argreq);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391 goto doend;
2392 }
2393
2394#ifdef FEAT_EVAL
2395 /*
2396 * Skip the command when it's not going to be executed.
2397 * The commands like :if, :endif, etc. always need to be executed.
2398 * Also make an exception for commands that handle a trailing command
2399 * themselves.
2400 */
2401 if (ea.skip)
2402 {
2403 switch (ea.cmdidx)
2404 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002405 // commands that need evaluation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406 case CMD_while:
2407 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002408 case CMD_for:
2409 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410 case CMD_if:
2411 case CMD_elseif:
2412 case CMD_else:
2413 case CMD_endif:
2414 case CMD_try:
2415 case CMD_catch:
2416 case CMD_finally:
2417 case CMD_endtry:
2418 case CMD_function:
Bram Moolenaarab55c682020-03-01 19:41:43 +01002419 case CMD_def:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420 break;
2421
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002422 // Commands that handle '|' themselves. Check: A command should
2423 // either have the EX_TRLBAR flag, appear in this list or appear in
2424 // the list at ":help :bar".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425 case CMD_aboveleft:
2426 case CMD_and:
2427 case CMD_belowright:
2428 case CMD_botright:
2429 case CMD_browse:
2430 case CMD_call:
2431 case CMD_confirm:
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01002432 case CMD_const:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433 case CMD_delfunction:
2434 case CMD_djump:
2435 case CMD_dlist:
2436 case CMD_dsearch:
2437 case CMD_dsplit:
2438 case CMD_echo:
2439 case CMD_echoerr:
2440 case CMD_echomsg:
2441 case CMD_echon:
Bram Moolenaara76b3152020-02-16 16:20:21 +01002442 case CMD_eval:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443 case CMD_execute:
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002444 case CMD_filter:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002445 case CMD_help:
2446 case CMD_hide:
2447 case CMD_ijump:
2448 case CMD_ilist:
2449 case CMD_isearch:
2450 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002451 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002452 case CMD_keepjumps:
2453 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01002454 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002455 case CMD_leftabove:
2456 case CMD_let:
2457 case CMD_lockmarks:
Bram Moolenaarcc4423a2019-11-26 17:05:00 +01002458 case CMD_lockvar:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002459 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002460 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002461 case CMD_mzscheme:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01002462 case CMD_noautocmd:
2463 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002464 case CMD_perl:
2465 case CMD_psearch:
2466 case CMD_python:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002467 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002468 case CMD_python3:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469 case CMD_return:
2470 case CMD_rightbelow:
2471 case CMD_ruby:
2472 case CMD_silent:
2473 case CMD_smagic:
2474 case CMD_snomagic:
2475 case CMD_substitute:
2476 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002477 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002478 case CMD_tcl:
2479 case CMD_throw:
2480 case CMD_tilde:
2481 case CMD_topleft:
2482 case CMD_unlet:
Bram Moolenaarcc4423a2019-11-26 17:05:00 +01002483 case CMD_unlockvar:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484 case CMD_verbose:
2485 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002486 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487 break;
2488
2489 default: goto doend;
2490 }
2491 }
2492#endif
2493
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002494 if (ea.argt & EX_XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495 {
2496 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2497 goto doend;
2498 }
2499
Bram Moolenaar071d4272004-06-13 20:20:40 +00002500 /*
2501 * Accept buffer name. Cannot be used at the same time with a buffer
2502 * number. Don't do this for a user command.
2503 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002504 if ((ea.argt & EX_BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
Bram Moolenaar958636c2014-10-21 20:01:58 +02002505 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002506 {
2507 /*
2508 * :bdelete, :bwipeout and :bunload take several arguments, separated
2509 * by spaces: find next space (skipping over escaped characters).
2510 * The others take one argument: ignore trailing spaces.
2511 */
2512 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2513 || ea.cmdidx == CMD_bunload)
2514 p = skiptowhite_esc(ea.arg);
2515 else
2516 {
2517 p = ea.arg + STRLEN(ea.arg);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002518 while (p > ea.arg && VIM_ISWHITE(p[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002519 --p;
2520 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002521 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & EX_BUFUNL) != 0,
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002522 FALSE, FALSE);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002523 if (ea.line2 < 0) // failed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524 goto doend;
2525 ea.addr_count = 1;
2526 ea.arg = skipwhite(p);
2527 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002528
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002529 // The :try command saves the emsg_silent flag, reset it here when
2530 // ":silent! try" was used, it should only apply to :try itself.
Bram Moolenaareffed932018-08-14 13:38:17 +02002531 if (ea.cmdidx == CMD_try && ea.did_esilent > 0)
Bram Moolenaar2be57332018-02-13 18:05:18 +01002532 {
Bram Moolenaareffed932018-08-14 13:38:17 +02002533 emsg_silent -= ea.did_esilent;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002534 if (emsg_silent < 0)
2535 emsg_silent = 0;
Bram Moolenaareffed932018-08-14 13:38:17 +02002536 ea.did_esilent = 0;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002537 }
2538
Bram Moolenaar071d4272004-06-13 20:20:40 +00002539/*
Bram Moolenaar2be57332018-02-13 18:05:18 +01002540 * 7. Execute the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002541 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542
Bram Moolenaar958636c2014-10-21 20:01:58 +02002543 if (IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544 {
2545 /*
2546 * Execute a user-defined command.
2547 */
2548 do_ucmd(&ea);
2549 }
2550 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002551 {
2552 /*
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002553 * Call the function to execute the builtin command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554 */
2555 ea.errmsg = NULL;
2556 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2557 if (ea.errmsg != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002558 errormsg = _(ea.errmsg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002559 }
2560
2561#ifdef FEAT_EVAL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002562 // Set flag that any command was executed, used by ex_vim9script().
Bram Moolenaar16531552020-02-08 16:00:46 +01002563 if (getline_equal(ea.getline, ea.cookie, getsourceline)
2564 && current_sctx.sc_sid > 0)
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002565 SCRIPT_ITEM(current_sctx.sc_sid)->sn_had_command = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002566
Bram Moolenaar071d4272004-06-13 20:20:40 +00002567 /*
2568 * If the command just executed called do_cmdline(), any throw or ":return"
2569 * or ":finish" encountered there must also check the cstack of the still
2570 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2571 * exception, or reanimate a returned function or finished script file and
2572 * return or finish it again.
2573 */
2574 if (need_rethrow)
2575 do_throw(cstack);
2576 else if (check_cstack)
2577 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002578 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002579 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002580 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002581 && current_func_returned())
2582 do_return(&ea, TRUE, FALSE, NULL);
2583 }
2584 need_rethrow = check_cstack = FALSE;
2585#endif
2586
2587doend:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002588 if (curwin->w_cursor.lnum == 0) // can happen with zero line number
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002589 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002590 curwin->w_cursor.lnum = 1;
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002591 curwin->w_cursor.col = 0;
2592 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593
2594 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2595 {
2596 if (sourcing)
2597 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002598 if (errormsg != (char *)IObuff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599 {
2600 STRCPY(IObuff, errormsg);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002601 errormsg = (char *)IObuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002603 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604 }
2605 emsg(errormsg);
2606 }
2607#ifdef FEAT_EVAL
2608 do_errthrow(cstack,
Bram Moolenaar958636c2014-10-21 20:01:58 +02002609 (ea.cmdidx != CMD_SIZE && !IS_USER_CMDIDX(ea.cmdidx))
2610 ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002611#endif
2612
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002613 undo_cmdmod(&ea, save_msg_scroll);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002614 cmdmod = save_cmdmod;
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01002615 reg_executing = save_reg_executing;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002616
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002617#ifdef HAVE_SANDBOX
Bram Moolenaareffed932018-08-14 13:38:17 +02002618 if (ea.did_sandbox)
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002619 --sandbox;
2620#endif
2621
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002622 if (ea.nextcmd && *ea.nextcmd == NUL) // not really a next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00002623 ea.nextcmd = NULL;
2624
2625#ifdef FEAT_EVAL
2626 --ex_nesting_level;
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002627 vim_free(ea.cmdline_tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002628#endif
2629
2630 return ea.nextcmd;
2631}
2632#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00002633 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002634#endif
2635
2636/*
Bram Moolenaareffed932018-08-14 13:38:17 +02002637 * Parse and skip over command modifiers:
2638 * - update eap->cmd
2639 * - store flags in "cmdmod".
2640 * - Set ex_pressedreturn for an empty command line.
2641 * - set msg_silent for ":silent"
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002642 * - set 'eventignore' to "all" for ":noautocmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002643 * - set p_verbose for ":verbose"
2644 * - Increment "sandbox" for ":sandbox"
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002645 * When "skip_only" is TRUE the global variables are not changed, except for
2646 * "cmdmod".
Bram Moolenaareffed932018-08-14 13:38:17 +02002647 * Return FAIL when the command is not to be executed.
2648 * May set "errormsg" to an error message.
2649 */
2650 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002651parse_command_modifiers(exarg_T *eap, char **errormsg, int skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002652{
2653 char_u *p;
2654
Bram Moolenaara80faa82020-04-12 19:37:17 +02002655 CLEAR_FIELD(cmdmod);
Bram Moolenaareffed932018-08-14 13:38:17 +02002656 eap->verbose_save = -1;
2657 eap->save_msg_silent = -1;
2658
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002659 // Repeat until no more command modifiers are found.
Bram Moolenaareffed932018-08-14 13:38:17 +02002660 for (;;)
2661 {
Bram Moolenaareffed932018-08-14 13:38:17 +02002662 while (*eap->cmd == ' ' || *eap->cmd == '\t' || *eap->cmd == ':')
2663 ++eap->cmd;
2664
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002665 // in ex mode, an empty line works like :+
Bram Moolenaareffed932018-08-14 13:38:17 +02002666 if (*eap->cmd == NUL && exmode_active
2667 && (getline_equal(eap->getline, eap->cookie, getexmodeline)
2668 || getline_equal(eap->getline, eap->cookie, getexline))
2669 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2670 {
2671 eap->cmd = (char_u *)"+";
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002672 if (!skip_only)
2673 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002674 }
2675
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002676 // ignore comment and empty lines
Bram Moolenaareffed932018-08-14 13:38:17 +02002677 if (*eap->cmd == '"')
2678 return FAIL;
2679 if (*eap->cmd == NUL)
2680 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002681 if (!skip_only)
2682 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002683 return FAIL;
2684 }
2685
Bram Moolenaareffed932018-08-14 13:38:17 +02002686 p = skip_range(eap->cmd, NULL);
2687 switch (*p)
2688 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002689 // When adding an entry, also modify cmd_exists().
Bram Moolenaareffed932018-08-14 13:38:17 +02002690 case 'a': if (!checkforcmd(&eap->cmd, "aboveleft", 3))
2691 break;
2692 cmdmod.split |= WSP_ABOVE;
2693 continue;
2694
2695 case 'b': if (checkforcmd(&eap->cmd, "belowright", 3))
2696 {
2697 cmdmod.split |= WSP_BELOW;
2698 continue;
2699 }
2700 if (checkforcmd(&eap->cmd, "browse", 3))
2701 {
2702#ifdef FEAT_BROWSE_CMD
2703 cmdmod.browse = TRUE;
2704#endif
2705 continue;
2706 }
2707 if (!checkforcmd(&eap->cmd, "botright", 2))
2708 break;
2709 cmdmod.split |= WSP_BOT;
2710 continue;
2711
2712 case 'c': if (!checkforcmd(&eap->cmd, "confirm", 4))
2713 break;
2714#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2715 cmdmod.confirm = TRUE;
2716#endif
2717 continue;
2718
2719 case 'k': if (checkforcmd(&eap->cmd, "keepmarks", 3))
2720 {
2721 cmdmod.keepmarks = TRUE;
2722 continue;
2723 }
2724 if (checkforcmd(&eap->cmd, "keepalt", 5))
2725 {
2726 cmdmod.keepalt = TRUE;
2727 continue;
2728 }
2729 if (checkforcmd(&eap->cmd, "keeppatterns", 5))
2730 {
2731 cmdmod.keeppatterns = TRUE;
2732 continue;
2733 }
2734 if (!checkforcmd(&eap->cmd, "keepjumps", 5))
2735 break;
2736 cmdmod.keepjumps = TRUE;
2737 continue;
2738
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002739 case 'f': // only accept ":filter {pat} cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002740 {
2741 char_u *reg_pat;
2742
2743 if (!checkforcmd(&p, "filter", 4)
2744 || *p == NUL || ends_excmd(*p))
2745 break;
2746 if (*p == '!')
2747 {
2748 cmdmod.filter_force = TRUE;
2749 p = skipwhite(p + 1);
2750 if (*p == NUL || ends_excmd(*p))
2751 break;
2752 }
Bram Moolenaar1e624c92020-07-11 14:08:04 +02002753#ifdef FEAT_EVAL
Bram Moolenaarb074e8b2020-07-11 13:40:45 +02002754 // Avoid that "filter(arg)" is recognized.
2755 if (in_vim9script() && !VIM_ISWHITE(*p))
2756 break;
Bram Moolenaar1e624c92020-07-11 14:08:04 +02002757#endif
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002758 if (skip_only)
2759 p = skip_vimgrep_pat(p, NULL, NULL);
2760 else
2761 // NOTE: This puts a NUL after the pattern.
2762 p = skip_vimgrep_pat(p, &reg_pat, NULL);
Bram Moolenaareffed932018-08-14 13:38:17 +02002763 if (p == NULL || *p == NUL)
2764 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002765 if (!skip_only)
2766 {
2767 cmdmod.filter_regmatch.regprog =
Bram Moolenaareffed932018-08-14 13:38:17 +02002768 vim_regcomp(reg_pat, RE_MAGIC);
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002769 if (cmdmod.filter_regmatch.regprog == NULL)
2770 break;
2771 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002772 eap->cmd = p;
2773 continue;
2774 }
2775
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002776 // ":hide" and ":hide | cmd" are not modifiers
Bram Moolenaareffed932018-08-14 13:38:17 +02002777 case 'h': if (p != eap->cmd || !checkforcmd(&p, "hide", 3)
2778 || *p == NUL || ends_excmd(*p))
2779 break;
2780 eap->cmd = p;
2781 cmdmod.hide = TRUE;
2782 continue;
2783
2784 case 'l': if (checkforcmd(&eap->cmd, "lockmarks", 3))
2785 {
2786 cmdmod.lockmarks = TRUE;
2787 continue;
2788 }
2789
2790 if (!checkforcmd(&eap->cmd, "leftabove", 5))
2791 break;
2792 cmdmod.split |= WSP_ABOVE;
2793 continue;
2794
2795 case 'n': if (checkforcmd(&eap->cmd, "noautocmd", 3))
2796 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002797 if (cmdmod.save_ei == NULL && !skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002798 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002799 // Set 'eventignore' to "all". Restore the
2800 // existing option value later.
Bram Moolenaareffed932018-08-14 13:38:17 +02002801 cmdmod.save_ei = vim_strsave(p_ei);
2802 set_string_option_direct((char_u *)"ei", -1,
2803 (char_u *)"all", OPT_FREE, SID_NONE);
2804 }
2805 continue;
2806 }
2807 if (!checkforcmd(&eap->cmd, "noswapfile", 3))
2808 break;
2809 cmdmod.noswapfile = TRUE;
2810 continue;
2811
2812 case 'r': if (!checkforcmd(&eap->cmd, "rightbelow", 6))
2813 break;
2814 cmdmod.split |= WSP_BELOW;
2815 continue;
2816
2817 case 's': if (checkforcmd(&eap->cmd, "sandbox", 3))
2818 {
2819#ifdef HAVE_SANDBOX
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002820 if (!skip_only)
2821 {
2822 if (!eap->did_sandbox)
2823 ++sandbox;
2824 eap->did_sandbox = TRUE;
2825 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002826#endif
2827 continue;
2828 }
2829 if (!checkforcmd(&eap->cmd, "silent", 3))
2830 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002831 if (!skip_only)
2832 {
2833 if (eap->save_msg_silent == -1)
2834 eap->save_msg_silent = msg_silent;
2835 ++msg_silent;
2836 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002837 if (*eap->cmd == '!' && !VIM_ISWHITE(eap->cmd[-1]))
2838 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002839 // ":silent!", but not "silent !cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002840 eap->cmd = skipwhite(eap->cmd + 1);
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002841 if (!skip_only)
2842 {
2843 ++emsg_silent;
2844 ++eap->did_esilent;
2845 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002846 }
2847 continue;
2848
2849 case 't': if (checkforcmd(&p, "tab", 3))
2850 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002851 if (!skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002852 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002853 long tabnr = get_address(eap, &eap->cmd,
2854 ADDR_TABS, eap->skip,
2855 skip_only, FALSE, 1);
2856 if (tabnr == MAXLNUM)
2857 cmdmod.tab = tabpage_index(curtab) + 1;
2858 else
Bram Moolenaareffed932018-08-14 13:38:17 +02002859 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002860 if (tabnr < 0 || tabnr > LAST_TAB_NR)
2861 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002862 *errormsg = _(e_invrange);
Bram Moolenaar548e5982018-12-26 21:45:00 +01002863 return FAIL;
2864 }
2865 cmdmod.tab = tabnr + 1;
Bram Moolenaareffed932018-08-14 13:38:17 +02002866 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002867 }
2868 eap->cmd = p;
2869 continue;
2870 }
2871 if (!checkforcmd(&eap->cmd, "topleft", 2))
2872 break;
2873 cmdmod.split |= WSP_TOP;
2874 continue;
2875
2876 case 'u': if (!checkforcmd(&eap->cmd, "unsilent", 3))
2877 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002878 if (!skip_only)
2879 {
2880 if (eap->save_msg_silent == -1)
2881 eap->save_msg_silent = msg_silent;
2882 msg_silent = 0;
2883 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002884 continue;
2885
2886 case 'v': if (checkforcmd(&eap->cmd, "vertical", 4))
2887 {
2888 cmdmod.split |= WSP_VERT;
2889 continue;
2890 }
2891 if (!checkforcmd(&p, "verbose", 4))
2892 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002893 if (!skip_only)
2894 {
2895 if (eap->verbose_save < 0)
2896 eap->verbose_save = p_verbose;
2897 if (vim_isdigit(*eap->cmd))
2898 p_verbose = atoi((char *)eap->cmd);
2899 else
2900 p_verbose = 1;
2901 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002902 eap->cmd = p;
2903 continue;
2904 }
2905 break;
2906 }
2907
2908 return OK;
2909}
2910
2911/*
Bram Moolenaarb074e8b2020-07-11 13:40:45 +02002912 * Undo and free contents of "cmdmod".
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002913 */
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002914 void
2915undo_cmdmod(exarg_T *eap, int save_msg_scroll)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002916{
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002917 if (eap->verbose_save >= 0)
2918 p_verbose = eap->verbose_save;
2919
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002920 if (cmdmod.save_ei != NULL)
2921 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002922 // Restore 'eventignore' to the value before ":noautocmd".
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002923 set_string_option_direct((char_u *)"ei", -1, cmdmod.save_ei,
2924 OPT_FREE, SID_NONE);
2925 free_string_option(cmdmod.save_ei);
2926 }
2927
2928 if (cmdmod.filter_regmatch.regprog != NULL)
2929 vim_regfree(cmdmod.filter_regmatch.regprog);
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002930
2931 if (eap->save_msg_silent != -1)
2932 {
2933 // messages could be enabled for a serious error, need to check if the
2934 // counters don't become negative
2935 if (!did_emsg || msg_silent > eap->save_msg_silent)
2936 msg_silent = eap->save_msg_silent;
2937 emsg_silent -= eap->did_esilent;
2938 if (emsg_silent < 0)
2939 emsg_silent = 0;
2940 // Restore msg_scroll, it's set by file I/O commands, even when no
2941 // message is actually displayed.
2942 msg_scroll = save_msg_scroll;
2943
2944 // "silent reg" or "silent echo x" inside "redir" leaves msg_col
2945 // somewhere in the line. Put it back in the first column.
2946 if (redirecting())
2947 msg_col = 0;
2948 }
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002949}
2950
2951/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002952 * Parse the address range, if any, in "eap".
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02002953 * May set the last search pattern, unless "silent" is TRUE.
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002954 * Return FAIL and set "errormsg" or return OK.
2955 */
2956 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002957parse_cmd_address(exarg_T *eap, char **errormsg, int silent)
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002958{
2959 int address_count = 1;
2960 linenr_T lnum;
2961
2962 // Repeat for all ',' or ';' separated addresses.
2963 for (;;)
2964 {
2965 eap->line1 = eap->line2;
2966 switch (eap->addr_type)
2967 {
2968 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02002969 case ADDR_OTHER:
Bram Moolenaarb5383b12020-05-18 19:46:48 +02002970 // Default is the cursor line number. Avoid using an invalid
2971 // line number though.
2972 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
2973 eap->line2 = curbuf->b_ml.ml_line_count;
2974 else
2975 eap->line2 = curwin->w_cursor.lnum;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002976 break;
2977 case ADDR_WINDOWS:
2978 eap->line2 = CURRENT_WIN_NR;
2979 break;
2980 case ADDR_ARGUMENTS:
2981 eap->line2 = curwin->w_arg_idx + 1;
2982 if (eap->line2 > ARGCOUNT)
2983 eap->line2 = ARGCOUNT;
2984 break;
2985 case ADDR_LOADED_BUFFERS:
2986 case ADDR_BUFFERS:
2987 eap->line2 = curbuf->b_fnum;
2988 break;
2989 case ADDR_TABS:
2990 eap->line2 = CURRENT_TAB_NR;
2991 break;
2992 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02002993 case ADDR_UNSIGNED:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002994 eap->line2 = 1;
2995 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002996 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002997#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02002998 eap->line2 = qf_get_cur_idx(eap);
2999#endif
3000 break;
3001 case ADDR_QUICKFIX_VALID:
3002#ifdef FEAT_QUICKFIX
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003003 eap->line2 = qf_get_cur_valid_idx(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003004#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003005 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003006 case ADDR_NONE:
3007 // Will give an error later if a range is found.
3008 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003009 }
3010 eap->cmd = skipwhite(eap->cmd);
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003011 lnum = get_address(eap, &eap->cmd, eap->addr_type, eap->skip, silent,
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003012 eap->addr_count == 0, address_count++);
3013 if (eap->cmd == NULL) // error detected
3014 return FAIL;
3015 if (lnum == MAXLNUM)
3016 {
3017 if (*eap->cmd == '%') // '%' - all lines
3018 {
3019 ++eap->cmd;
3020 switch (eap->addr_type)
3021 {
3022 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003023 case ADDR_OTHER:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003024 eap->line1 = 1;
3025 eap->line2 = curbuf->b_ml.ml_line_count;
3026 break;
3027 case ADDR_LOADED_BUFFERS:
3028 {
3029 buf_T *buf = firstbuf;
3030
3031 while (buf->b_next != NULL
3032 && buf->b_ml.ml_mfp == NULL)
3033 buf = buf->b_next;
3034 eap->line1 = buf->b_fnum;
3035 buf = lastbuf;
3036 while (buf->b_prev != NULL
3037 && buf->b_ml.ml_mfp == NULL)
3038 buf = buf->b_prev;
3039 eap->line2 = buf->b_fnum;
3040 break;
3041 }
3042 case ADDR_BUFFERS:
3043 eap->line1 = firstbuf->b_fnum;
3044 eap->line2 = lastbuf->b_fnum;
3045 break;
3046 case ADDR_WINDOWS:
3047 case ADDR_TABS:
3048 if (IS_USER_CMDIDX(eap->cmdidx))
3049 {
3050 eap->line1 = 1;
3051 eap->line2 = eap->addr_type == ADDR_WINDOWS
3052 ? LAST_WIN_NR : LAST_TAB_NR;
3053 }
3054 else
3055 {
3056 // there is no Vim command which uses '%' and
3057 // ADDR_WINDOWS or ADDR_TABS
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003058 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003059 return FAIL;
3060 }
3061 break;
3062 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003063 case ADDR_UNSIGNED:
3064 case ADDR_QUICKFIX:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003065 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003066 return FAIL;
3067 case ADDR_ARGUMENTS:
3068 if (ARGCOUNT == 0)
3069 eap->line1 = eap->line2 = 0;
3070 else
3071 {
3072 eap->line1 = 1;
3073 eap->line2 = ARGCOUNT;
3074 }
3075 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003076 case ADDR_QUICKFIX_VALID:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003077#ifdef FEAT_QUICKFIX
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003078 eap->line1 = 1;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003079 eap->line2 = qf_get_valid_size(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003080 if (eap->line2 == 0)
3081 eap->line2 = 1;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003082#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003083 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003084 case ADDR_NONE:
3085 // Will give an error later if a range is found.
3086 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003087 }
3088 ++eap->addr_count;
3089 }
3090 else if (*eap->cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
3091 {
3092 pos_T *fp;
3093
3094 // '*' - visual area
3095 if (eap->addr_type != ADDR_LINES)
3096 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003097 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003098 return FAIL;
3099 }
3100
3101 ++eap->cmd;
3102 if (!eap->skip)
3103 {
3104 fp = getmark('<', FALSE);
3105 if (check_mark(fp) == FAIL)
3106 return FAIL;
3107 eap->line1 = fp->lnum;
3108 fp = getmark('>', FALSE);
3109 if (check_mark(fp) == FAIL)
3110 return FAIL;
3111 eap->line2 = fp->lnum;
3112 ++eap->addr_count;
3113 }
3114 }
3115 }
3116 else
3117 eap->line2 = lnum;
3118 eap->addr_count++;
3119
3120 if (*eap->cmd == ';')
3121 {
3122 if (!eap->skip)
3123 {
3124 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar0e717042020-04-27 19:29:01 +02003125 // Don't leave the cursor on an illegal line or column, but do
3126 // accept zero as address, so 0;/PATTERN/ works correctly.
3127 if (eap->line2 > 0)
3128 check_cursor();
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003129 }
3130 }
3131 else if (*eap->cmd != ',')
3132 break;
3133 ++eap->cmd;
3134 }
3135
3136 // One address given: set start and end lines.
3137 if (eap->addr_count == 1)
3138 {
3139 eap->line1 = eap->line2;
3140 // ... but only implicit: really no address given
3141 if (lnum == MAXLNUM)
3142 eap->addr_count = 0;
3143 }
3144 return OK;
3145}
3146
3147/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003148 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003149 * If there is a match advance "pp" to the argument and return TRUE.
3150 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003151 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003152checkforcmd(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003153 char_u **pp, // start of command
3154 char *cmd, // name of command
3155 int len) // required length
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156{
3157 int i;
3158
3159 for (i = 0; cmd[i] != NUL; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003160 if (((char_u *)cmd)[i] != (*pp)[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161 break;
3162 if (i >= len && !isalpha((*pp)[i]))
3163 {
3164 *pp = skipwhite(*pp + i);
3165 return TRUE;
3166 }
3167 return FALSE;
3168}
3169
3170/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003171 * Append "cmd" to the error message in IObuff.
3172 * Takes care of limiting the length and handling 0xa0, which would be
3173 * invisible otherwise.
3174 */
3175 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003176append_command(char_u *cmd)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003177{
3178 char_u *s = cmd;
3179 char_u *d;
3180
3181 STRCAT(IObuff, ": ");
3182 d = IObuff + STRLEN(IObuff);
3183 while (*s != NUL && d - IObuff < IOSIZE - 7)
3184 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003185 if (enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) : *s == 0xa0)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003186 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003187 s += enc_utf8 ? 2 : 1;
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003188 STRCPY(d, "<a0>");
3189 d += 4;
3190 }
3191 else
3192 MB_COPY_CHAR(s, d);
3193 }
3194 *d = NUL;
3195}
3196
3197/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003198 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003199 * Start of the name can be found at eap->cmd.
Bram Moolenaar25190db2019-05-04 15:05:28 +02003200 * Sets eap->cmdidx and returns a pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003201 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003202 *
3203 * If "lookup" is not NULL recognize expression without "eval" or "call" and
3204 * assignment without "let". Sets eap->cmdidx to the command while returning
3205 * "eap->cmd".
3206 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003207 * Returns NULL for an ambiguous user command.
3208 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003209 char_u *
3210find_ex_command(
3211 exarg_T *eap,
Bram Moolenaara494f562020-04-18 17:45:38 +02003212 int *full UNUSED,
Bram Moolenaarb84a3812020-05-01 15:44:29 +02003213 void *(*lookup)(char_u *, size_t, cctx_T *) UNUSED,
Bram Moolenaara494f562020-04-18 17:45:38 +02003214 cctx_T *cctx UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215{
3216 int len;
3217 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003218 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003219
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003220#ifdef FEAT_EVAL
3221 /*
3222 * Recognize a Vim9 script function/method call and assignment:
3223 * "lvar = value", "lvar(arg)", "[1, 2 3]->Func()"
3224 */
Bram Moolenaar0c6ceaf2020-02-22 18:36:32 +01003225 p = eap->cmd;
Bram Moolenaard2ef6b32020-07-02 21:11:34 +02003226 if (lookup != NULL && (*p == '(' || *p == '{'
3227 || ((p = to_name_const_end(eap->cmd)) > eap->cmd && *p != NUL)
3228 || *p == '['))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003229 {
3230 int oplen;
3231 int heredoc;
3232
3233 // "funcname(" is always a function call.
3234 // "varname[]" is an expression.
3235 // "g:varname" is an expression.
3236 // "varname->expr" is an expression.
Bram Moolenaar0a47e092020-07-08 18:30:06 +02003237 // "varname.expr" is an expression.
Bram Moolenaar0c6ceaf2020-02-22 18:36:32 +01003238 // "(..." is an expression.
Bram Moolenaar5f195932020-07-01 20:07:14 +02003239 // "{..." is an dict expression.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003240 if (*p == '('
Bram Moolenaar5f195932020-07-01 20:07:14 +02003241 || *p == '{'
Bram Moolenaard2ef6b32020-07-02 21:11:34 +02003242 || (*p == '[' && p > eap->cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003243 || p[1] == ':'
Bram Moolenaar0a47e092020-07-08 18:30:06 +02003244 || (*p == '-' && p[1] == '>')
3245 || (*p == '.' && ASCII_ISALPHA(p[1])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003246 {
3247 eap->cmdidx = CMD_eval;
3248 return eap->cmd;
3249 }
3250
Bram Moolenaard2ef6b32020-07-02 21:11:34 +02003251 // "[...]->Method()" is a list expression, but "[a, b] = Func()" is
3252 // an assignment.
3253 // If there is no line break inside the "[...]" then "p" is advanced to
3254 // after the "]" by to_name_const_end(): check if a "=" follows.
3255 // If "[...]" has a line break "p" still points at the "[" and it can't
3256 // be an assignment.
3257 if (*eap->cmd == '[' && (p == eap->cmd || *skipwhite(p) != '='))
3258 {
3259 eap->cmdidx = CMD_eval;
3260 return eap->cmd;
3261 }
3262
Bram Moolenaar5f195932020-07-01 20:07:14 +02003263 // Recognize an assignment if we recognize the variable name:
3264 // "g:var = expr"
3265 // "var = expr" where "var" is a local var name.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003266 oplen = assignment_len(skipwhite(p), &heredoc);
3267 if (oplen > 0)
3268 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003269 if (((p - eap->cmd) > 2 && eap->cmd[1] == ':')
Bram Moolenaarb84a3812020-05-01 15:44:29 +02003270 || lookup(eap->cmd, p - eap->cmd, cctx) != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003271 {
3272 eap->cmdidx = CMD_let;
3273 return eap->cmd;
3274 }
3275 }
3276 }
3277#endif
3278
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279 /*
3280 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00003281 * Exceptions:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282 * - the 'k' command can directly be followed by any character.
3283 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003284 * but :sre[wind] is another command, as are :scr[iptnames],
Bram Moolenaar071d4272004-06-13 20:20:40 +00003285 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00003286 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003287 */
3288 p = eap->cmd;
3289 if (*p == 'k')
3290 {
3291 eap->cmdidx = CMD_k;
3292 ++p;
3293 }
3294 else if (p[0] == 's'
Bram Moolenaar204b93f2015-08-04 22:02:51 +02003295 && ((p[1] == 'c' && (p[2] == NUL || (p[2] != 's' && p[2] != 'r'
3296 && (p[3] == NUL || (p[3] != 'i' && p[4] != 'p')))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297 || p[1] == 'g'
3298 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
3299 || p[1] == 'I'
3300 || (p[1] == 'r' && p[2] != 'e')))
3301 {
3302 eap->cmdidx = CMD_substitute;
3303 ++p;
3304 }
3305 else
3306 {
3307 while (ASCII_ISALPHA(*p))
3308 ++p;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003309 // for python 3.x support ":py3", ":python3", ":py3file", etc.
Bram Moolenaar55d5c032010-07-17 23:52:29 +02003310 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003311 {
Bram Moolenaarb6590522010-07-21 16:00:43 +02003312 while (ASCII_ISALNUM(*p))
3313 ++p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003314 }
3315 else if (*p == '9' && STRNCMP("vim9", eap->cmd, 4) == 0)
3316 {
3317 // include "9" for "vim9script"
3318 ++p;
3319 while (ASCII_ISALPHA(*p))
3320 ++p;
3321 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02003322
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003323 // check for non-alpha command
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3325 ++p;
3326 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003327 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
3328 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003329 // Check for ":dl", ":dell", etc. to ":deletel": that's
3330 // :delete with the 'l' flag. Same for 'p'.
Bram Moolenaardf177f62005-02-22 08:39:57 +00003331 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003332 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00003333 break;
3334 if (i == len - 1)
3335 {
3336 --len;
3337 if (p[-1] == 'l')
3338 eap->flags |= EXFLAG_LIST;
3339 else
3340 eap->flags |= EXFLAG_PRINT;
3341 }
3342 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003344 if (ASCII_ISLOWER(eap->cmd[0]))
3345 {
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003346 int c1 = eap->cmd[0];
Bram Moolenaar94f82cb2019-07-24 22:30:27 +02003347 int c2 = len == 1 ? NUL : eap->cmd[1];
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003348
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003349 if (command_count != (int)CMD_SIZE)
3350 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003351 iemsg(_("E943: Command table needs to be updated, run 'make cmdidxs'"));
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003352 getout(1);
3353 }
3354
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003355 // Use a precomputed index for fast look-up in cmdnames[]
3356 // taking into account the first 2 letters of eap->cmd.
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003357 eap->cmdidx = cmdidxs1[CharOrdLow(c1)];
3358 if (ASCII_ISLOWER(c2))
3359 eap->cmdidx += cmdidxs2[CharOrdLow(c1)][CharOrdLow(c2)];
3360 }
Bram Moolenaara494f562020-04-18 17:45:38 +02003361 else if (ASCII_ISUPPER(eap->cmd[0]))
3362 eap->cmdidx = CMD_Next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363 else
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003364 eap->cmdidx = CMD_bang;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365
3366 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
3367 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
3368 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
3369 (size_t)len) == 0)
3370 {
3371#ifdef FEAT_EVAL
3372 if (full != NULL
3373 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
3374 *full = TRUE;
3375#endif
3376 break;
3377 }
3378
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003379 // Look for a user defined command as a last resort. Let ":Print" be
3380 // overruled by a user defined command.
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01003381 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
3382 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383 {
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003384 // User defined commands may contain digits.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385 while (ASCII_ISALNUM(*p))
3386 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003387 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003389 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390 eap->cmdidx = CMD_SIZE;
3391 }
3392
3393 return p;
3394}
3395
3396#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003397static struct cmdmod
3398{
3399 char *name;
3400 int minlen;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003401 int has_count; // :123verbose :3tab
Bram Moolenaared53fb92007-11-24 20:50:24 +00003402} cmdmods[] = {
3403 {"aboveleft", 3, FALSE},
3404 {"belowright", 3, FALSE},
3405 {"botright", 2, FALSE},
3406 {"browse", 3, FALSE},
3407 {"confirm", 4, FALSE},
Bram Moolenaar7b668e82016-08-23 23:51:21 +02003408 {"filter", 4, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003409 {"hide", 3, FALSE},
3410 {"keepalt", 5, FALSE},
3411 {"keepjumps", 5, FALSE},
3412 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01003413 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003414 {"leftabove", 5, FALSE},
3415 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003416 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003417 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003418 {"rightbelow", 6, FALSE},
3419 {"sandbox", 3, FALSE},
3420 {"silent", 3, FALSE},
3421 {"tab", 3, TRUE},
3422 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003423 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003424 {"verbose", 4, TRUE},
3425 {"vertical", 4, FALSE},
3426};
3427
3428/*
3429 * Return length of a command modifier (including optional count).
3430 * Return zero when it's not a modifier.
3431 */
3432 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003433modifier_len(char_u *cmd)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003434{
3435 int i, j;
3436 char_u *p = cmd;
3437
3438 if (VIM_ISDIGIT(*cmd))
3439 p = skipwhite(skipdigits(cmd));
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003440 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003441 {
3442 for (j = 0; p[j] != NUL; ++j)
3443 if (p[j] != cmdmods[i].name[j])
3444 break;
Bram Moolenaar2d473ab2013-06-12 17:12:24 +02003445 if (!ASCII_ISALPHA(p[j]) && j >= cmdmods[i].minlen
Bram Moolenaared53fb92007-11-24 20:50:24 +00003446 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003447 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003448 }
3449 return 0;
3450}
3451
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452/*
3453 * Return > 0 if an Ex command "name" exists.
3454 * Return 2 if there is an exact match.
3455 * Return 3 if there is an ambiguous match.
3456 */
3457 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003458cmd_exists(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459{
3460 exarg_T ea;
3461 int full = FALSE;
3462 int i;
3463 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003464 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003466 // Check command modifiers.
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003467 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468 {
3469 for (j = 0; name[j] != NUL; ++j)
3470 if (name[j] != cmdmods[i].name[j])
3471 break;
3472 if (name[j] == NUL && j >= cmdmods[i].minlen)
3473 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3474 }
3475
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003476 // Check built-in commands and user defined commands.
3477 // For ":2match" and ":3match" we need to skip the number.
Bram Moolenaara9587612006-05-04 21:47:50 +00003478 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003480 p = find_ex_command(&ea, &full, NULL, NULL);
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003481 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003483 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3484 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003485 if (*skipwhite(p) != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003486 return 0; // trailing garbage
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3488}
3489#endif
3490
Bram Moolenaard0190392019-08-23 21:17:35 +02003491 cmdidx_T
3492excmd_get_cmdidx(char_u *cmd, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493{
Bram Moolenaard0190392019-08-23 21:17:35 +02003494 cmdidx_T idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495
Bram Moolenaard0190392019-08-23 21:17:35 +02003496 for (idx = (cmdidx_T)0; (int)idx < (int)CMD_SIZE;
3497 idx = (cmdidx_T)((int)idx + 1))
3498 if (STRNCMP(cmdnames[(int)idx].cmd_name, cmd, (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003499 break;
3500
Bram Moolenaard0190392019-08-23 21:17:35 +02003501 return idx;
3502}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503
Bram Moolenaard0190392019-08-23 21:17:35 +02003504 long
3505excmd_get_argt(cmdidx_T idx)
3506{
3507 return (long)cmdnames[(int)idx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508}
3509
3510/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003511 * Skip a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512 *
3513 * Backslashed delimiters after / or ? will be skipped, and commands will
3514 * not be expanded between /'s and ?'s or after "'".
3515 *
Bram Moolenaardf069ee2020-06-22 23:02:51 +02003516 * Also skip white space and ":" characters after the range.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003517 * Returns the "cmd" pointer advanced to beyond the range.
3518 */
3519 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003520skip_range(
3521 char_u *cmd,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003522 int *ctx) // pointer to xp_context or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003524 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01003526 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;\\", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527 {
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01003528 if (*cmd == '\\')
3529 {
3530 if (cmd[1] == '?' || cmd[1] == '/' || cmd[1] == '&')
3531 ++cmd;
3532 else
3533 break;
3534 }
3535 else if (*cmd == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536 {
3537 if (*++cmd == NUL && ctx != NULL)
3538 *ctx = EXPAND_NOTHING;
3539 }
3540 else if (*cmd == '/' || *cmd == '?')
3541 {
3542 delim = *cmd++;
3543 while (*cmd != NUL && *cmd != delim)
3544 if (*cmd++ == '\\' && *cmd != NUL)
3545 ++cmd;
3546 if (*cmd == NUL && ctx != NULL)
3547 *ctx = EXPAND_NOTHING;
3548 }
3549 if (*cmd != NUL)
3550 ++cmd;
3551 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00003552
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003553 // Skip ":" and white space.
Bram Moolenaardf177f62005-02-22 08:39:57 +00003554 while (*cmd == ':')
3555 cmd = skipwhite(cmd + 1);
3556
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557 return cmd;
3558}
3559
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003560 static void
3561addr_error(cmd_addr_T addr_type)
3562{
3563 if (addr_type == ADDR_NONE)
3564 emsg(_(e_norange));
3565 else
3566 emsg(_(e_invrange));
3567}
3568
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569/*
Bram Moolenaar198cb662018-09-06 21:44:17 +02003570 * Get a single EX address.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571 *
3572 * Set ptr to the next character after the part that was interpreted.
3573 * Set ptr to NULL when an error is encountered.
Bram Moolenaar198cb662018-09-06 21:44:17 +02003574 * This may set the last used search pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575 *
3576 * Return MAXLNUM when no Ex address was found.
3577 */
3578 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003579get_address(
3580 exarg_T *eap UNUSED,
3581 char_u **ptr,
Bram Moolenaar1b03a192019-12-08 17:08:29 +01003582 cmd_addr_T addr_type,
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003583 int skip, // only skip the address, don't use it
3584 int silent, // no errors or side effects
3585 int to_other_file, // flag: may jump to other file
3586 int address_count UNUSED) // 1 for first address, >1 after comma
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587{
3588 int c;
3589 int i;
3590 long n;
3591 char_u *cmd;
3592 pos_T pos;
3593 pos_T *fp;
3594 linenr_T lnum;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003595 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003596
3597 cmd = skipwhite(*ptr);
3598 lnum = MAXLNUM;
3599 do
3600 {
3601 switch (*cmd)
3602 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003603 case '.': // '.' - Cursor position
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003604 ++cmd;
3605 switch (addr_type)
3606 {
3607 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003608 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003609 lnum = curwin->w_cursor.lnum;
3610 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003611 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003612 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003613 break;
3614 case ADDR_ARGUMENTS:
3615 lnum = curwin->w_arg_idx + 1;
3616 break;
3617 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003618 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003619 lnum = curbuf->b_fnum;
3620 break;
3621 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003622 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003623 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003624 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003625 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003626 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003627 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003628 cmd = NULL;
3629 goto error;
3630 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003631 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003632#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02003633 lnum = qf_get_cur_idx(eap);
3634#endif
3635 break;
3636 case ADDR_QUICKFIX_VALID:
3637#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003638 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02003639#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003640 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003641 }
3642 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003644 case '$': // '$' - last line
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003645 ++cmd;
3646 switch (addr_type)
3647 {
3648 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003649 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650 lnum = curbuf->b_ml.ml_line_count;
3651 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003652 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003653 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003654 break;
3655 case ADDR_ARGUMENTS:
3656 lnum = ARGCOUNT;
3657 break;
3658 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003659 buf = lastbuf;
3660 while (buf->b_ml.ml_mfp == NULL)
3661 {
3662 if (buf->b_prev == NULL)
3663 break;
3664 buf = buf->b_prev;
3665 }
3666 lnum = buf->b_fnum;
3667 break;
3668 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003669 lnum = lastbuf->b_fnum;
3670 break;
3671 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003672 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003673 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003674 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003675 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003676 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003677 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003678 cmd = NULL;
3679 goto error;
3680 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003681 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003682#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003683 lnum = qf_get_size(eap);
3684 if (lnum == 0)
3685 lnum = 1;
Bram Moolenaare906c502015-09-09 21:10:39 +02003686#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003687 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003688 case ADDR_QUICKFIX_VALID:
3689#ifdef FEAT_QUICKFIX
3690 lnum = qf_get_valid_size(eap);
3691 if (lnum == 0)
3692 lnum = 1;
3693#endif
3694 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003695 }
3696 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003697
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003698 case '\'': // ''' - mark
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003699 if (*++cmd == NUL)
3700 {
3701 cmd = NULL;
3702 goto error;
3703 }
3704 if (addr_type != ADDR_LINES)
3705 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003706 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003707 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003708 goto error;
3709 }
3710 if (skip)
3711 ++cmd;
3712 else
3713 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003714 // Only accept a mark in another file when it is
3715 // used by itself: ":'M".
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003716 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
3717 ++cmd;
3718 if (fp == (pos_T *)-1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003719 // Jumped to another file.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003720 lnum = curwin->w_cursor.lnum;
3721 else
3722 {
3723 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724 {
3725 cmd = NULL;
3726 goto error;
3727 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003728 lnum = fp->lnum;
3729 }
3730 }
3731 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003732
3733 case '/':
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003734 case '?': // '/' or '?' - search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003735 c = *cmd++;
3736 if (addr_type != ADDR_LINES)
3737 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003738 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003739 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003740 goto error;
3741 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003742 if (skip) // skip "/pat/"
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003743 {
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02003744 cmd = skip_regexp(cmd, c, (int)p_magic);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003745 if (*cmd == c)
3746 ++cmd;
3747 }
3748 else
3749 {
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003750 int flags;
3751
3752 pos = curwin->w_cursor; // save curwin->w_cursor
3753
3754 // When '/' or '?' follows another address, start from
3755 // there.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003756 if (lnum != MAXLNUM)
3757 curwin->w_cursor.lnum = lnum;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003758
3759 // Start a forward search at the end of the line (unless
3760 // before the first line).
3761 // Start a backward search at the start of the line.
3762 // This makes sure we never match in the current
3763 // line, and can match anywhere in the
3764 // next/previous line.
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01003765 if (c == '/' && curwin->w_cursor.lnum > 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003766 curwin->w_cursor.col = MAXCOL;
3767 else
3768 curwin->w_cursor.col = 0;
3769 searchcmdlen = 0;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003770 flags = silent ? 0 : SEARCH_HIS | SEARCH_MSG;
Bram Moolenaarc036e872020-02-21 21:30:52 +01003771 if (!do_search(NULL, c, c, cmd, 1L, flags, NULL))
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003772 {
3773 curwin->w_cursor = pos;
3774 cmd = NULL;
3775 goto error;
3776 }
3777 lnum = curwin->w_cursor.lnum;
3778 curwin->w_cursor = pos;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003779 // adjust command string pointer
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003780 cmd += searchcmdlen;
3781 }
3782 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003783
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003784 case '\\': // "\?", "\/" or "\&", repeat search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003785 ++cmd;
3786 if (addr_type != ADDR_LINES)
3787 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003788 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003789 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003790 goto error;
3791 }
3792 if (*cmd == '&')
3793 i = RE_SUBST;
3794 else if (*cmd == '?' || *cmd == '/')
3795 i = RE_SEARCH;
3796 else
3797 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003798 emsg(_(e_backslash));
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003799 cmd = NULL;
3800 goto error;
3801 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003802
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003803 if (!skip)
3804 {
3805 /*
3806 * When search follows another address, start from
3807 * there.
3808 */
3809 if (lnum != MAXLNUM)
3810 pos.lnum = lnum;
3811 else
3812 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003813
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003814 /*
3815 * Start the search just like for the above
3816 * do_search().
3817 */
3818 if (*cmd != '?')
3819 pos.col = MAXCOL;
3820 else
3821 pos.col = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02003822 pos.coladd = 0;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01003823 if (searchit(curwin, curbuf, &pos, NULL,
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003824 *cmd == '?' ? BACKWARD : FORWARD,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02003825 (char_u *)"", 1L, SEARCH_MSG, i, NULL) != FAIL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003826 lnum = pos.lnum;
3827 else
3828 {
3829 cmd = NULL;
3830 goto error;
3831 }
3832 }
3833 ++cmd;
3834 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003835
3836 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003837 if (VIM_ISDIGIT(*cmd)) // absolute line number
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003838 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003839 }
3840
3841 for (;;)
3842 {
3843 cmd = skipwhite(cmd);
3844 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
3845 break;
3846
3847 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003848 {
3849 switch (addr_type)
3850 {
3851 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003852 case ADDR_OTHER:
3853 // "+1" is same as ".+1"
Bram Moolenaarf240e182014-11-27 18:33:02 +01003854 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003855 break;
3856 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003857 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003858 break;
3859 case ADDR_ARGUMENTS:
3860 lnum = curwin->w_arg_idx + 1;
3861 break;
3862 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003863 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003864 lnum = curbuf->b_fnum;
3865 break;
3866 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003867 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003868 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003869 case ADDR_TABS_RELATIVE:
3870 lnum = 1;
3871 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003872 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003873#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02003874 lnum = qf_get_cur_idx(eap);
3875#endif
3876 break;
3877 case ADDR_QUICKFIX_VALID:
3878#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003879 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02003880#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003881 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003882 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003883 case ADDR_UNSIGNED:
3884 lnum = 0;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003885 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003886 }
3887 }
3888
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889 if (VIM_ISDIGIT(*cmd))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003890 i = '+'; // "number" is same as "+number"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003891 else
3892 i = *cmd++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003893 if (!VIM_ISDIGIT(*cmd)) // '+' is '+1', but '+0' is not '+1'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894 n = 1;
3895 else
3896 n = getdigits(&cmd);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003897
3898 if (addr_type == ADDR_TABS_RELATIVE)
3899 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003900 emsg(_(e_invrange));
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003901 cmd = NULL;
3902 goto error;
3903 }
3904 else if (addr_type == ADDR_LOADED_BUFFERS
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003905 || addr_type == ADDR_BUFFERS)
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003906 lnum = compute_buffer_local_count(
3907 addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003908 else
Bram Moolenaarded27822017-01-02 14:27:34 +01003909 {
3910#ifdef FEAT_FOLDING
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003911 // Relative line addressing, need to adjust for folded lines
3912 // now, but only do it after the first address.
Bram Moolenaarded27822017-01-02 14:27:34 +01003913 if (addr_type == ADDR_LINES && (i == '-' || i == '+')
3914 && address_count >= 2)
3915 (void)hasFolding(lnum, NULL, &lnum);
3916#endif
3917 if (i == '-')
3918 lnum -= n;
3919 else
3920 lnum += n;
3921 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003922 }
3923 } while (*cmd == '/' || *cmd == '?');
3924
3925error:
3926 *ptr = cmd;
3927 return lnum;
3928}
3929
3930/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00003931 * Get flags from an Ex command argument.
3932 */
3933 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003934get_flags(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00003935{
3936 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
3937 {
3938 if (*eap->arg == 'l')
3939 eap->flags |= EXFLAG_LIST;
3940 else if (*eap->arg == 'p')
3941 eap->flags |= EXFLAG_PRINT;
3942 else
3943 eap->flags |= EXFLAG_NR;
3944 eap->arg = skipwhite(eap->arg + 1);
3945 }
3946}
3947
3948/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949 * Function called for command which is Not Implemented. NI!
3950 */
3951 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003952ex_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003953{
3954 if (!eap->skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003955 eap->errmsg = N_("E319: Sorry, the command is not available in this version");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956}
3957
Bram Moolenaar7bb75552007-07-16 18:39:49 +00003958#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003959/*
3960 * Function called for script command which is Not Implemented. NI!
3961 * Skips over ":perl <<EOF" constructs.
3962 */
3963 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003964ex_script_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003965{
3966 if (!eap->skip)
3967 ex_ni(eap);
3968 else
3969 vim_free(script_get(eap, eap->arg));
3970}
3971#endif
3972
3973/*
3974 * Check range in Ex command for validity.
3975 * Return NULL when valid, error message when invalid.
3976 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003977 static char *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003978invalid_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003979{
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003980 buf_T *buf;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003981
Bram Moolenaar071d4272004-06-13 20:20:40 +00003982 if ( eap->line1 < 0
3983 || eap->line2 < 0
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003984 || eap->line1 > eap->line2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003985 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003986
Bram Moolenaar8071cb22019-07-12 17:58:01 +02003987 if (eap->argt & EX_RANGE)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003988 {
Bram Moolenaarb7316892019-05-01 18:08:42 +02003989 switch (eap->addr_type)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003990 {
3991 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003992 if (eap->line2 > curbuf->b_ml.ml_line_count
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003993#ifdef FEAT_DIFF
3994 + (eap->cmdidx == CMD_diffget)
3995#endif
3996 )
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003997 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003998 break;
3999 case ADDR_ARGUMENTS:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004000 // add 1 if ARGCOUNT is 0
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004001 if (eap->line2 > ARGCOUNT + (!ARGCOUNT))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004002 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004003 break;
4004 case ADDR_BUFFERS:
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02004005 // Only a boundary check, not whether the buffers actually
4006 // exist.
4007 if (eap->line1 < 1 || eap->line2 > get_highest_fnum())
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004008 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004009 break;
4010 case ADDR_LOADED_BUFFERS:
4011 buf = firstbuf;
4012 while (buf->b_ml.ml_mfp == NULL)
4013 {
4014 if (buf->b_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004015 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004016 buf = buf->b_next;
4017 }
4018 if (eap->line1 < buf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004019 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004020 buf = lastbuf;
4021 while (buf->b_ml.ml_mfp == NULL)
4022 {
4023 if (buf->b_prev == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004024 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004025 buf = buf->b_prev;
4026 }
4027 if (eap->line2 > buf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004028 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004029 break;
4030 case ADDR_WINDOWS:
Bram Moolenaar8be63882015-01-14 11:25:05 +01004031 if (eap->line2 > LAST_WIN_NR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004032 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004033 break;
4034 case ADDR_TABS:
4035 if (eap->line2 > LAST_TAB_NR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004036 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004037 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004038 case ADDR_TABS_RELATIVE:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004039 case ADDR_OTHER:
4040 // Any range is OK.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004041 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004042 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004043#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004044 // No error for value that is too big, will use the last entry.
4045 if (eap->line2 <= 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004046 return _(e_invrange);
Bram Moolenaare906c502015-09-09 21:10:39 +02004047#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004048 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004049 case ADDR_QUICKFIX_VALID:
4050#ifdef FEAT_QUICKFIX
4051 if ((eap->line2 != 1 && eap->line2 > qf_get_valid_size(eap))
4052 || eap->line2 < 0)
4053 return _(e_invrange);
4054#endif
4055 break;
4056 case ADDR_UNSIGNED:
4057 if (eap->line2 < 0)
4058 return _(e_invrange);
4059 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004060 case ADDR_NONE:
4061 // Will give an error elsewhere.
4062 break;
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004063 }
4064 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065 return NULL;
4066}
4067
4068/*
4069 * Correct the range for zero line number, if required.
4070 */
4071 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004072correct_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004073{
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004074 if (!(eap->argt & EX_ZEROR)) // zero in range not allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004075 {
4076 if (eap->line1 == 0)
4077 eap->line1 = 1;
4078 if (eap->line2 == 0)
4079 eap->line2 = 1;
4080 }
4081}
4082
Bram Moolenaar748bf032005-02-02 23:04:36 +00004083#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004084/*
4085 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4086 * pattern. Otherwise return eap->arg.
4087 */
4088 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004089skip_grep_pat(exarg_T *eap)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004090{
4091 char_u *p = eap->arg;
4092
Bram Moolenaara37420f2006-02-04 22:37:47 +00004093 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4094 || eap->cmdidx == CMD_vimgrepadd
4095 || eap->cmdidx == CMD_lvimgrepadd
4096 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004097 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004098 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004099 if (p == NULL)
4100 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004101 }
4102 return p;
4103}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004104
4105/*
4106 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4107 * in the command line, so that things like % get expanded.
4108 */
4109 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004110replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004111{
4112 char_u *new_cmdline;
4113 char_u *program;
4114 char_u *pos;
4115 char_u *ptr;
4116 int len;
4117 int i;
4118
4119 /*
4120 * Don't do it when ":vimgrep" is used for ":grep".
4121 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004122 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4123 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4124 || eap->cmdidx == CMD_grepadd
4125 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004126 && !grep_internal(eap->cmdidx))
4127 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004128 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4129 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004130 {
4131 if (*curbuf->b_p_gp == NUL)
4132 program = p_gp;
4133 else
4134 program = curbuf->b_p_gp;
4135 }
4136 else
4137 {
4138 if (*curbuf->b_p_mp == NUL)
4139 program = p_mp;
4140 else
4141 program = curbuf->b_p_mp;
4142 }
4143
4144 p = skipwhite(p);
4145
4146 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4147 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004148 // replace $* by given arguments
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004149 i = 1;
4150 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4151 ++i;
4152 len = (int)STRLEN(p);
Bram Moolenaar51e14382019-05-25 20:21:28 +02004153 new_cmdline = alloc(STRLEN(program) + i * (len - 2) + 1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004154 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004155 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004156 ptr = new_cmdline;
4157 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4158 {
4159 i = (int)(pos - program);
4160 STRNCPY(ptr, program, i);
4161 STRCPY(ptr += i, p);
4162 ptr += len;
4163 program = pos + 2;
4164 }
4165 STRCPY(ptr, program);
4166 }
4167 else
4168 {
Bram Moolenaar51e14382019-05-25 20:21:28 +02004169 new_cmdline = alloc(STRLEN(program) + STRLEN(p) + 2);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004170 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004171 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004172 STRCPY(new_cmdline, program);
4173 STRCAT(new_cmdline, " ");
4174 STRCAT(new_cmdline, p);
4175 }
4176 msg_make(p);
4177
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004178 // 'eap->cmd' is not set here, because it is not used at CMD_make
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004179 vim_free(*cmdlinep);
4180 *cmdlinep = new_cmdline;
4181 p = new_cmdline;
4182 }
4183 return p;
4184}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004185#endif
4186
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187/*
4188 * Expand file name in Ex command argument.
Bram Moolenaar0abb4272019-06-15 16:06:00 +02004189 * When an error is detected, "errormsgp" is set to a non-NULL pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190 * Return FAIL for failure, OK otherwise.
4191 */
4192 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004193expand_filename(
4194 exarg_T *eap,
4195 char_u **cmdlinep,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004196 char **errormsgp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004197{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004198 int has_wildcards; // need to expand wildcards
Bram Moolenaar071d4272004-06-13 20:20:40 +00004199 char_u *repl;
4200 int srclen;
4201 char_u *p;
4202 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004203 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204
Bram Moolenaar748bf032005-02-02 23:04:36 +00004205#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004206 // Skip a regexp pattern for ":vimgrep[add] pat file..."
Bram Moolenaar748bf032005-02-02 23:04:36 +00004207 p = skip_grep_pat(eap);
4208#else
4209 p = eap->arg;
4210#endif
4211
Bram Moolenaar071d4272004-06-13 20:20:40 +00004212 /*
4213 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4214 * the file name contains a wildcard it should not cause expanding.
4215 * (it will be expanded anyway if there is a wildcard before replacing).
4216 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004217 has_wildcards = mch_has_wildcard(p);
4218 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004220#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004221 // Skip over `=expr`, wildcards in it are not expanded.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004222 if (p[0] == '`' && p[1] == '=')
4223 {
4224 p += 2;
4225 (void)skip_expr(&p);
4226 if (*p == '`')
4227 ++p;
4228 continue;
4229 }
4230#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231 /*
4232 * Quick check if this cannot be the start of a special string.
4233 * Also removes backslash before '%', '#' and '<'.
4234 */
4235 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4236 {
4237 ++p;
4238 continue;
4239 }
4240
4241 /*
4242 * Try to find a match at this position.
4243 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00004244 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
4245 errormsgp, &escaped);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004246 if (*errormsgp != NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00004247 return FAIL;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004248 if (repl == NULL) // no match found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249 {
4250 p += srclen;
4251 continue;
4252 }
4253
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004254 // Wildcards won't be expanded below, the replacement is taken
4255 // literally. But do expand "~/file", "~user/file" and "$HOME/file".
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004256 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4257 {
4258 char_u *l = repl;
4259
4260 repl = expand_env_save(repl);
4261 vim_free(l);
4262 }
4263
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004264 // Need to escape white space et al. with a backslash.
4265 // Don't do this for:
4266 // - replacement that already has been escaped: "##"
4267 // - shell commands (may have to use quotes instead).
4268 // - non-unix systems when there is a single argument (spaces don't
4269 // separate arguments then).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004270 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00004271 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00004272 && eap->cmdidx != CMD_bang
Bram Moolenaar071d4272004-06-13 20:20:40 +00004273 && eap->cmdidx != CMD_grep
4274 && eap->cmdidx != CMD_grepadd
Bram Moolenaarbf15b8d2017-06-04 20:43:48 +02004275 && eap->cmdidx != CMD_hardcopy
Bram Moolenaar67883b42017-07-27 22:57:00 +02004276 && eap->cmdidx != CMD_lgrep
4277 && eap->cmdidx != CMD_lgrepadd
4278 && eap->cmdidx != CMD_lmake
4279 && eap->cmdidx != CMD_make
4280 && eap->cmdidx != CMD_terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281#ifndef UNIX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004282 && !(eap->argt & EX_NOSPC)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004283#endif
4284 )
4285 {
4286 char_u *l;
4287#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004288 // Don't escape a backslash here, because rem_backslash() doesn't
4289 // remove it later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004290 static char_u *nobslash = (char_u *)" \t\"|";
4291# define ESCAPE_CHARS nobslash
4292#else
4293# define ESCAPE_CHARS escape_chars
4294#endif
4295
4296 for (l = repl; *l; ++l)
4297 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
4298 {
4299 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
4300 if (l != NULL)
4301 {
4302 vim_free(repl);
4303 repl = l;
4304 }
4305 break;
4306 }
4307 }
4308
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004309 // For a shell command a '!' must be escaped.
Bram Moolenaar67883b42017-07-27 22:57:00 +02004310 if ((eap->usefilter || eap->cmdidx == CMD_bang
4311 || eap->cmdidx == CMD_terminal)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004312 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004313 {
4314 char_u *l;
4315
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004316 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004317 if (l != NULL)
4318 {
4319 vim_free(repl);
4320 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004321 }
4322 }
4323
4324 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
4325 vim_free(repl);
4326 if (p == NULL)
4327 return FAIL;
4328 }
4329
4330 /*
4331 * One file argument: Expand wildcards.
4332 * Don't do this with ":r !command" or ":w !command".
4333 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004334 if ((eap->argt & EX_NOSPC) && !eap->usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004335 {
4336 /*
4337 * May do this twice:
4338 * 1. Replace environment variables.
4339 * 2. Replace any other wildcards, remove backslashes.
4340 */
4341 for (n = 1; n <= 2; ++n)
4342 {
4343 if (n == 2)
4344 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004345 /*
4346 * Halve the number of backslashes (this is Vi compatible).
4347 * For Unix and OS/2, when wildcards are expanded, this is
4348 * done by ExpandOne() below.
4349 */
Bram Moolenaare7fedb62015-12-31 19:07:19 +01004350#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004351 if (!has_wildcards)
4352#endif
4353 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004354 }
4355
4356 if (has_wildcards)
4357 {
4358 if (n == 1)
4359 {
4360 /*
4361 * First loop: May expand environment variables. This
4362 * can be done much faster with expand_env() than with
4363 * something else (e.g., calling a shell).
4364 * After expanding environment variables, check again
4365 * if there are still wildcards present.
4366 */
4367 if (vim_strchr(eap->arg, '$') != NULL
4368 || vim_strchr(eap->arg, '~') != NULL)
4369 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00004370 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004371 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004372 has_wildcards = mch_has_wildcard(NameBuff);
4373 p = NameBuff;
4374 }
4375 else
4376 p = NULL;
4377 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004378 else // n == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004379 {
4380 expand_T xpc;
Bram Moolenaarb40c2572019-10-19 21:01:05 +02004381 int options = WILD_LIST_NOTFOUND
4382 | WILD_NOERROR | WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383
4384 ExpandInit(&xpc);
4385 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004386 if (p_wic)
4387 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004388 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01004389 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004390 if (p == NULL)
4391 return FAIL;
4392 }
4393 if (p != NULL)
4394 {
4395 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
4396 p, cmdlinep);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004397 if (n == 2) // p came from ExpandOne()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004398 vim_free(p);
4399 }
4400 }
4401 }
4402 }
4403 return OK;
4404}
4405
4406/*
4407 * Replace part of the command line, keeping eap->cmd, eap->arg and
4408 * eap->nextcmd correct.
4409 * "src" points to the part that is to be replaced, of length "srclen".
4410 * "repl" is the replacement string.
4411 * Returns a pointer to the character after the replaced string.
4412 * Returns NULL for failure.
4413 */
4414 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004415repl_cmdline(
4416 exarg_T *eap,
4417 char_u *src,
4418 int srclen,
4419 char_u *repl,
4420 char_u **cmdlinep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004421{
4422 int len;
4423 int i;
4424 char_u *new_cmdline;
4425
4426 /*
4427 * The new command line is build in new_cmdline[].
4428 * First allocate it.
4429 * Careful: a "+cmd" argument may have been NUL terminated.
4430 */
4431 len = (int)STRLEN(repl);
4432 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004433 if (eap->nextcmd != NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004434 i += (int)STRLEN(eap->nextcmd);// add space for next command
Bram Moolenaar964b3742019-05-24 18:54:09 +02004435 if ((new_cmdline = alloc(i)) == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004436 return NULL; // out of memory!
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437
4438 /*
4439 * Copy the stuff before the expanded part.
4440 * Copy the expanded stuff.
4441 * Copy what came after the expanded part.
4442 * Copy the next commands, if there are any.
4443 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004444 i = (int)(src - *cmdlinep); // length of part before match
Bram Moolenaar071d4272004-06-13 20:20:40 +00004445 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00004446
Bram Moolenaar071d4272004-06-13 20:20:40 +00004447 mch_memmove(new_cmdline + i, repl, (size_t)len);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004448 i += len; // remember the end of the string
Bram Moolenaar071d4272004-06-13 20:20:40 +00004449 STRCPY(new_cmdline + i, src + srclen);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004450 src = new_cmdline + i; // remember where to continue
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004452 if (eap->nextcmd != NULL) // append next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00004453 {
4454 i = (int)STRLEN(new_cmdline) + 1;
4455 STRCPY(new_cmdline + i, eap->nextcmd);
4456 eap->nextcmd = new_cmdline + i;
4457 }
4458 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
4459 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
4460 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
4461 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
4462 vim_free(*cmdlinep);
4463 *cmdlinep = new_cmdline;
4464
4465 return src;
4466}
4467
4468/*
4469 * Check for '|' to separate commands and '"' to start comments.
4470 */
4471 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004472separate_nextcmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004473{
4474 char_u *p;
4475
Bram Moolenaar86b68352004-12-27 21:59:20 +00004476#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004477 p = skip_grep_pat(eap);
4478#else
4479 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004480#endif
4481
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004482 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004483 {
4484 if (*p == Ctrl_V)
4485 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004486 if (eap->argt & (EX_CTRLV | EX_XFILE))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004487 ++p; // skip CTRL-V and next char
Bram Moolenaar071d4272004-06-13 20:20:40 +00004488 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004489 // remove CTRL-V and skip next char
Bram Moolenaara7241f52008-06-24 20:39:31 +00004490 STRMOVE(p, p + 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004491 if (*p == NUL) // stop at NUL after CTRL-V
Bram Moolenaar071d4272004-06-13 20:20:40 +00004492 break;
4493 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004494
4495#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004496 // Skip over `=expr` when wildcards are expanded.
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004497 else if (p[0] == '`' && p[1] == '=' && (eap->argt & EX_XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004498 {
4499 p += 2;
4500 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004501 }
4502#endif
4503
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004504 // Check for '"': start of comment or '|': next command
4505 // :@" and :*" do not start a comment!
4506 // :redir @" doesn't either.
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004507 else if ((*p == '"' && !(eap->argt & EX_NOTRLCOM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004508 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
4509 || p != eap->arg)
4510 && (eap->cmdidx != CMD_redir
4511 || p != eap->arg + 1 || p[-1] != '@'))
Bram Moolenaara26b9702020-04-18 19:53:28 +02004512#ifdef FEAT_EVAL
4513 || (*p == '#' && in_vim9script()
4514 && p[1] != '{' && p > eap->cmd && VIM_ISWHITE(p[-1]))
4515#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004516 || *p == '|' || *p == '\n')
4517 {
4518 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004519 * We remove the '\' before the '|', unless EX_CTRLV is used
Bram Moolenaar071d4272004-06-13 20:20:40 +00004520 * AND 'b' is present in 'cpoptions'.
4521 */
4522 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004523 || !(eap->argt & EX_CTRLV)) && *(p - 1) == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004524 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004525 STRMOVE(p - 1, p); // remove the '\'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004526 --p;
4527 }
4528 else
4529 {
4530 eap->nextcmd = check_nextcmd(p);
4531 *p = NUL;
4532 break;
4533 }
4534 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004535 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004536
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004537 if (!(eap->argt & EX_NOTRLCOM)) // remove trailing spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00004538 del_trailing_spaces(eap->arg);
4539}
4540
4541/*
4542 * get + command from ex argument
4543 */
4544 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004545getargcmd(char_u **argp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004546{
4547 char_u *arg = *argp;
4548 char_u *command = NULL;
4549
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004550 if (*arg == '+') // +[command]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004551 {
4552 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02004553 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004554 command = dollar_command;
4555 else
4556 {
4557 command = arg;
4558 arg = skip_cmd_arg(command, TRUE);
4559 if (*arg != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004560 *arg++ = NUL; // terminate command with NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004561 }
4562
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004563 arg = skipwhite(arg); // skip over spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00004564 *argp = arg;
4565 }
4566 return command;
4567}
4568
4569/*
4570 * Find end of "+command" argument. Skip over "\ " and "\\".
4571 */
Bram Moolenaard0190392019-08-23 21:17:35 +02004572 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004573skip_cmd_arg(
4574 char_u *p,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004575 int rembs) // TRUE to halve the number of backslashes
Bram Moolenaar071d4272004-06-13 20:20:40 +00004576{
4577 while (*p && !vim_isspace(*p))
4578 {
4579 if (*p == '\\' && p[1] != NUL)
4580 {
4581 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00004582 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004583 else
4584 ++p;
4585 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004586 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004587 }
4588 return p;
4589}
4590
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004591 int
4592get_bad_opt(char_u *p, exarg_T *eap)
4593{
4594 if (STRICMP(p, "keep") == 0)
4595 eap->bad_char = BAD_KEEP;
4596 else if (STRICMP(p, "drop") == 0)
4597 eap->bad_char = BAD_DROP;
4598 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
4599 eap->bad_char = *p;
Bram Moolenaar75808492018-06-12 12:39:41 +02004600 else
4601 return FAIL;
4602 return OK;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004603}
4604
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605/*
4606 * Get "++opt=arg" argument.
4607 * Return FAIL or OK.
4608 */
4609 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004610getargopt(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004611{
4612 char_u *arg = eap->arg + 2;
4613 int *pp = NULL;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004614 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004615 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004616
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004617 // ":edit ++[no]bin[ary] file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004618 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
4619 {
4620 if (*arg == 'n')
4621 {
4622 arg += 2;
4623 eap->force_bin = FORCE_NOBIN;
4624 }
4625 else
4626 eap->force_bin = FORCE_BIN;
4627 if (!checkforcmd(&arg, "binary", 3))
4628 return FAIL;
4629 eap->arg = skipwhite(arg);
4630 return OK;
4631 }
4632
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004633 // ":read ++edit file"
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004634 if (STRNCMP(arg, "edit", 4) == 0)
4635 {
4636 eap->read_edit = TRUE;
4637 eap->arg = skipwhite(arg + 4);
4638 return OK;
4639 }
4640
Bram Moolenaar071d4272004-06-13 20:20:40 +00004641 if (STRNCMP(arg, "ff", 2) == 0)
4642 {
4643 arg += 2;
4644 pp = &eap->force_ff;
4645 }
4646 else if (STRNCMP(arg, "fileformat", 10) == 0)
4647 {
4648 arg += 10;
4649 pp = &eap->force_ff;
4650 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004651 else if (STRNCMP(arg, "enc", 3) == 0)
4652 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01004653 if (STRNCMP(arg, "encoding", 8) == 0)
4654 arg += 8;
4655 else
4656 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004657 pp = &eap->force_enc;
4658 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004659 else if (STRNCMP(arg, "bad", 3) == 0)
4660 {
4661 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004662 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004663 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004664
4665 if (pp == NULL || *arg != '=')
4666 return FAIL;
4667
4668 ++arg;
4669 *pp = (int)(arg - eap->cmd);
4670 arg = skip_cmd_arg(arg, FALSE);
4671 eap->arg = skipwhite(arg);
4672 *arg = NUL;
4673
Bram Moolenaar071d4272004-06-13 20:20:40 +00004674 if (pp == &eap->force_ff)
4675 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004676 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
4677 return FAIL;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004678 eap->force_ff = eap->cmd[eap->force_ff];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004679 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004680 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004681 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004682 // Make 'fileencoding' lower case.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004683 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
4684 *p = TOLOWER_ASC(*p);
4685 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004686 else
4687 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004688 // Check ++bad= argument. Must be a single-byte character, "keep" or
4689 // "drop".
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004690 if (get_bad_opt(eap->cmd + bad_char_idx, eap) == FAIL)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004691 return FAIL;
4692 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004693
4694 return OK;
4695}
4696
Bram Moolenaar071d4272004-06-13 20:20:40 +00004697 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004698ex_autocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004699{
4700 /*
Bram Moolenaar26d4b892018-07-04 22:26:28 +02004701 * Disallow autocommands from .exrc and .vimrc in current
Bram Moolenaar071d4272004-06-13 20:20:40 +00004702 * directory for security reasons.
4703 */
4704 if (secure)
4705 {
4706 secure = 2;
4707 eap->errmsg = e_curdir;
4708 }
4709 else if (eap->cmdidx == CMD_autocmd)
4710 do_autocmd(eap->arg, eap->forceit);
4711 else
4712 do_augroup(eap->arg, eap->forceit);
4713}
4714
4715/*
4716 * ":doautocmd": Apply the automatic commands to the current buffer.
4717 */
4718 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004719ex_doautocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004720{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01004721 char_u *arg = eap->arg;
4722 int call_do_modelines = check_nomodeline(&arg);
Bram Moolenaar1610d052016-06-09 22:53:01 +02004723 int did_aucmd;
Bram Moolenaar60542ac2012-02-12 20:14:01 +01004724
Bram Moolenaar1610d052016-06-09 22:53:01 +02004725 (void)do_doautocmd(arg, TRUE, &did_aucmd);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004726 // Only when there is no <nomodeline>.
Bram Moolenaar1610d052016-06-09 22:53:01 +02004727 if (call_do_modelines && did_aucmd)
Bram Moolenaar60542ac2012-02-12 20:14:01 +01004728 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004729}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004730
Bram Moolenaar071d4272004-06-13 20:20:40 +00004731/*
4732 * :[N]bunload[!] [N] [bufname] unload buffer
4733 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
4734 * :[N]bwipeout[!] [N] [bufname] delete buffer really
4735 */
4736 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004737ex_bunload(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004738{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004739 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02004740 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004741 eap->errmsg = do_bufdel(
4742 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
4743 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
4744 : DOBUF_UNLOAD, eap->arg,
4745 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
4746}
4747
4748/*
4749 * :[N]buffer [N] to buffer N
4750 * :[N]sbuffer [N] to buffer N
4751 */
4752 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004753ex_buffer(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004754{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004755 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02004756 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004757 if (*eap->arg)
4758 eap->errmsg = e_trailing;
4759 else
4760 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004761 if (eap->addr_count == 0) // default is current buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00004762 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
4763 else
4764 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004765 if (eap->do_ecmd_cmd != NULL)
4766 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004767 }
4768}
4769
4770/*
4771 * :[N]bmodified [N] to next mod. buffer
4772 * :[N]sbmodified [N] to next mod. buffer
4773 */
4774 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004775ex_bmodified(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004776{
4777 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004778 if (eap->do_ecmd_cmd != NULL)
4779 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004780}
4781
4782/*
4783 * :[N]bnext [N] to next buffer
4784 * :[N]sbnext [N] split and to next buffer
4785 */
4786 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004787ex_bnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004788{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004789 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01004790 return;
4791
Bram Moolenaar071d4272004-06-13 20:20:40 +00004792 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004793 if (eap->do_ecmd_cmd != NULL)
4794 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004795}
4796
4797/*
4798 * :[N]bNext [N] to previous buffer
4799 * :[N]bprevious [N] to previous buffer
4800 * :[N]sbNext [N] split and to previous buffer
4801 * :[N]sbprevious [N] split and to previous buffer
4802 */
4803 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004804ex_bprevious(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, BACKWARD, (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 * :brewind to first buffer
4816 * :bfirst to first buffer
4817 * :sbrewind split and to first buffer
4818 * :sbfirst split and to first buffer
4819 */
4820 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004821ex_brewind(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_FIRST, FORWARD, 0);
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 * :blast to last buffer
4833 * :sblast split and to last buffer
4834 */
4835 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004836ex_blast(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004837{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004838 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01004839 return;
4840
Bram Moolenaar071d4272004-06-13 20:20:40 +00004841 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004842 if (eap->do_ecmd_cmd != NULL)
4843 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004844}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004845
Bram Moolenaar7a092242020-04-16 22:10:49 +02004846/*
4847 * Check if "c" ends an Ex command.
Bram Moolenaara494f562020-04-18 17:45:38 +02004848 * In Vim9 script does not check for white space before # or #{.
Bram Moolenaar7a092242020-04-16 22:10:49 +02004849 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004850 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004851ends_excmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004852{
Bram Moolenaarfbda69b2020-04-13 15:06:53 +02004853#ifdef FEAT_EVAL
Bram Moolenaar2c330432020-04-13 14:41:35 +02004854 if (c == '#')
Bram Moolenaar7a092242020-04-16 22:10:49 +02004855 return in_vim9script();
4856#endif
4857 return (c == NUL || c == '|' || c == '"' || c == '\n');
4858}
4859
4860/*
4861 * Like ends_excmd() but checks that a # in Vim9 script either has "cmd" equal
4862 * to "cmd_start" or has a white space character before it.
4863 */
4864 int
Bram Moolenaar21cfe502020-04-16 23:01:50 +02004865ends_excmd2(char_u *cmd_start UNUSED, char_u *cmd)
Bram Moolenaar7a092242020-04-16 22:10:49 +02004866{
4867 int c = *cmd;
4868
4869#ifdef FEAT_EVAL
Bram Moolenaara26b9702020-04-18 19:53:28 +02004870 if (c == '#' && cmd[1] != '{' && (cmd == cmd_start || VIM_ISWHITE(cmd[-1])))
Bram Moolenaar2c330432020-04-13 14:41:35 +02004871 return in_vim9script();
Bram Moolenaarfbda69b2020-04-13 15:06:53 +02004872#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004873 return (c == NUL || c == '|' || c == '"' || c == '\n');
4874}
4875
4876#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
4877 || defined(PROTO)
4878/*
4879 * Return the next command, after the first '|' or '\n'.
4880 * Return NULL if not found.
4881 */
4882 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004883find_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004884{
4885 while (*p != '|' && *p != '\n')
4886 {
4887 if (*p == NUL)
4888 return NULL;
4889 ++p;
4890 }
4891 return (p + 1);
4892}
4893#endif
4894
4895/*
Bram Moolenaar2256c992016-11-15 21:17:07 +01004896 * Check if *p is a separator between Ex commands, skipping over white space.
4897 * Return NULL if it isn't, the following character if it is.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004898 */
4899 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004900check_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004901{
Bram Moolenaar2256c992016-11-15 21:17:07 +01004902 char_u *s = skipwhite(p);
4903
4904 if (*s == '|' || *s == '\n')
4905 return (s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004906 else
4907 return NULL;
4908}
4909
4910/*
4911 * - if there are more files to edit
4912 * - and this is the last window
4913 * - and forceit not used
4914 * - and not repeated twice on a row
4915 * return FAIL and give error message if 'message' TRUE
4916 * return OK otherwise
4917 */
4918 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004919check_more(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004920 int message, // when FALSE check only, no messages
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004921 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004922{
4923 int n = ARGCOUNT - curwin->w_arg_idx - 1;
4924
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00004925 if (!forceit && only_one_window()
4926 && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927 {
4928 if (message)
4929 {
4930#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
4931 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
4932 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02004933 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004934
Bram Moolenaarda6e8912018-08-21 15:12:14 +02004935 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
4936 NGETTEXT("%d more file to edit. Quit anyway?",
4937 "%d more files to edit. Quit anyway?", n), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
4939 return OK;
4940 return FAIL;
4941 }
4942#endif
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01004943 semsg(NGETTEXT("E173: %d more file to edit",
4944 "E173: %d more files to edit", n), n);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004945 quitmore = 2; // next try to quit is allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004946 }
4947 return FAIL;
4948 }
4949 return OK;
4950}
4951
Bram Moolenaar071d4272004-06-13 20:20:40 +00004952/*
4953 * Function given to ExpandGeneric() to obtain the list of command names.
4954 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004955 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004956get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004957{
4958 if (idx >= (int)CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004959 return get_user_command_name(idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004960 return cmdnames[idx].cmd_name;
4961}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962
Bram Moolenaar071d4272004-06-13 20:20:40 +00004963 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004964ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004965{
Bram Moolenaare6850792010-05-14 15:28:44 +02004966 if (*eap->arg == NUL)
4967 {
4968#ifdef FEAT_EVAL
4969 char_u *expr = vim_strsave((char_u *)"g:colors_name");
4970 char_u *p = NULL;
4971
4972 if (expr != NULL)
4973 {
4974 ++emsg_off;
Bram Moolenaarb171fb12020-06-24 20:34:03 +02004975 p = eval_to_string(expr, FALSE);
Bram Moolenaare6850792010-05-14 15:28:44 +02004976 --emsg_off;
4977 vim_free(expr);
4978 }
4979 if (p != NULL)
4980 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004981 msg((char *)p);
Bram Moolenaare6850792010-05-14 15:28:44 +02004982 vim_free(p);
4983 }
4984 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01004985 msg("default");
Bram Moolenaare6850792010-05-14 15:28:44 +02004986#else
Bram Moolenaar32526b32019-01-19 17:43:09 +01004987 msg(_("unknown"));
Bram Moolenaare6850792010-05-14 15:28:44 +02004988#endif
4989 }
4990 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004991 semsg(_("E185: Cannot find color scheme '%s'"), eap->arg);
Bram Moolenaarf58d81a2019-01-28 20:19:05 +01004992
4993#ifdef FEAT_VTP
4994 else if (has_vtp_working())
4995 {
4996 // background color change requires clear + redraw
4997 update_screen(CLEAR);
4998 redrawcmd();
4999 }
5000#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005001}
5002
5003 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005004ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005005{
5006 if (*eap->arg == NUL && eap->cmd[2] == '!')
Bram Moolenaar32526b32019-01-19 17:43:09 +01005007 msg(_("Greetings, Vim user!"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005008 do_highlight(eap->arg, eap->forceit, FALSE);
5009}
5010
5011
5012/*
5013 * Call this function if we thought we were going to exit, but we won't
5014 * (because of an error). May need to restore the terminal mode.
5015 */
5016 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005017not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005018{
5019 exiting = FALSE;
5020 settmode(TMODE_RAW);
5021}
5022
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005023 static int
5024before_quit_autocmds(win_T *wp, int quit_all, int forceit)
5025{
5026 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, wp->w_buffer);
5027
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02005028 // Bail out when autocommands closed the window.
5029 // Refuse to quit when the buffer in the last window is being closed (can
5030 // only happen in autocommands).
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005031 if (!win_valid(wp)
5032 || curbuf_locked()
5033 || (wp->w_buffer->b_nwindows == 1 && wp->w_buffer->b_locked > 0))
5034 return TRUE;
5035
5036 if (quit_all || (check_more(FALSE, forceit) == OK && only_one_window()))
5037 {
5038 apply_autocmds(EVENT_EXITPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02005039 // Refuse to quit when locked or when the window was closed or the
5040 // buffer in the last window is being closed (can only happen in
5041 // autocommands).
5042 if (!win_valid(wp) || curbuf_locked()
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005043 || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
5044 return TRUE;
5045 }
5046
5047 return FALSE;
5048}
5049
Bram Moolenaar071d4272004-06-13 20:20:40 +00005050/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01005051 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005052 * ":{nr}quit": quit window {nr}
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02005053 * Also used when closing a terminal window that's the last one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054 */
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02005055 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005056ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005058 win_T *wp;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005059
Bram Moolenaar071d4272004-06-13 20:20:40 +00005060#ifdef FEAT_CMDWIN
5061 if (cmdwin_type != 0)
5062 {
5063 cmdwin_result = Ctrl_C;
5064 return;
5065 }
5066#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005067 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005068 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005069 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005070 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005071 return;
5072 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005073 if (eap->addr_count > 0)
5074 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01005075 int wnr = eap->line2;
5076
5077 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
5078 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005079 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005080 }
5081 else
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005082 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01005083
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005084 // Refuse to quit when locked.
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02005085 if (curbuf_locked())
5086 return;
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005087
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005088 // Trigger QuitPre and maybe ExitPre
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005089 if (before_quit_autocmds(wp, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005090 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005091
5092#ifdef FEAT_NETBEANS_INTG
5093 netbeansForcedQuit = eap->forceit;
5094#endif
5095
5096 /*
5097 * If there are more files or windows we won't exit.
5098 */
5099 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
5100 exiting = TRUE;
Bram Moolenaarff930ca2017-10-19 17:12:10 +02005101 if ((!buf_hide(wp->w_buffer)
5102 && check_changed(wp->w_buffer, (p_awa ? CCGD_AW : 0)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01005103 | (eap->forceit ? CCGD_FORCEIT : 0)
5104 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005105 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01005106 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005107 {
5108 not_exiting();
5109 }
5110 else
5111 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005112 // quit last window
5113 // Note: only_one_window() returns true, even so a help window is
5114 // still open. In that case only quit, if no address has been
5115 // specified. Example:
5116 // :h|wincmd w|1q - don't quit
5117 // :h|wincmd w|q - quit
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01005118 if (only_one_window() && (ONE_WINDOW || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005119 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02005120 not_exiting();
Bram Moolenaar4033c552017-09-16 20:54:51 +02005121#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005122 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005123#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005124 // close window; may free buffer
Bram Moolenaareb44a682017-08-03 22:44:55 +02005125 win_close(wp, !buf_hide(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005126 }
5127}
5128
5129/*
5130 * ":cquit".
5131 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005132 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005133ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005134{
Bram Moolenaar1860bde2020-01-06 21:47:21 +01005135 // this does not always pass on the exit code to the Manx compiler. why?
5136 getout(eap->addr_count > 0 ? (int)eap->line2 : EXIT_FAILURE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005137}
5138
5139/*
5140 * ":qall": try to quit all windows
5141 */
5142 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005143ex_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005144{
5145# ifdef FEAT_CMDWIN
5146 if (cmdwin_type != 0)
5147 {
5148 if (eap->forceit)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005149 cmdwin_result = K_XF1; // ex_window() takes care of this
Bram Moolenaar071d4272004-06-13 20:20:40 +00005150 else
5151 cmdwin_result = K_XF2;
5152 return;
5153 }
5154# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005155
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005156 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005157 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005158 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005159 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005160 return;
5161 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005162
5163 if (before_quit_autocmds(curwin, TRUE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005164 return;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005165
Bram Moolenaar071d4272004-06-13 20:20:40 +00005166 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01005167 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005168 getout(0);
5169 not_exiting();
5170}
5171
Bram Moolenaar071d4272004-06-13 20:20:40 +00005172/*
5173 * ":close": close current window, unless it is the last one
5174 */
5175 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005176ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005177{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005178 win_T *win;
5179 int winnr = 0;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005180#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00005181 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02005182 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005183 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005184#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005185 if (!text_locked() && !curbuf_locked())
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005186 {
5187 if (eap->addr_count == 0)
5188 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar6d41c782018-06-06 09:11:12 +02005189 else
5190 {
Bram Moolenaar29323592016-07-24 22:04:11 +02005191 FOR_ALL_WINDOWS(win)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005192 {
5193 winnr++;
5194 if (winnr == eap->line2)
5195 break;
5196 }
5197 if (win == NULL)
5198 win = lastwin;
5199 ex_win_close(eap->forceit, win, NULL);
5200 }
5201 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005202}
5203
Bram Moolenaar4033c552017-09-16 20:54:51 +02005204#ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005205/*
5206 * ":pclose": Close any preview window.
5207 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005208 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005209ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005210{
5211 win_T *win;
5212
Bram Moolenaar79648732019-07-18 21:43:07 +02005213 // First close any normal window.
Bram Moolenaar29323592016-07-24 22:04:11 +02005214 FOR_ALL_WINDOWS(win)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005215 if (win->w_p_pvw)
5216 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005217 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar79648732019-07-18 21:43:07 +02005218 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005219 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005220# ifdef FEAT_PROP_POPUP
Bram Moolenaar79648732019-07-18 21:43:07 +02005221 // Also when 'previewpopup' is empty, it might have been cleared.
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02005222 popup_close_preview();
Bram Moolenaar79648732019-07-18 21:43:07 +02005223# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005224}
Bram Moolenaar4033c552017-09-16 20:54:51 +02005225#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005226
Bram Moolenaarf740b292006-02-16 22:11:02 +00005227/*
5228 * Close window "win" and take care of handling closing the last window for a
5229 * modified buffer.
5230 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005231 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005232ex_win_close(
5233 int forceit,
5234 win_T *win,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005235 tabpage_T *tp) // NULL or the tab page "win" is in
Bram Moolenaar071d4272004-06-13 20:20:40 +00005236{
5237 int need_hide;
5238 buf_T *buf = win->w_buffer;
5239
Bram Moolenaarcf844172020-06-26 19:44:06 +02005240 // Never close the autocommand window.
5241 if (win == aucmd_win)
5242 {
5243 emsg(_(e_autocmd_close));
5244 return;
5245 }
5246
Bram Moolenaar071d4272004-06-13 20:20:40 +00005247 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02005248 if (need_hide && !buf_hide(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005249 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02005250#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005251 if ((p_confirm || cmdmod.confirm) && p_write)
5252 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005253 bufref_T bufref;
5254
5255 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005256 dialog_changed(buf, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005257 if (bufref_valid(&bufref) && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005258 return;
5259 need_hide = FALSE;
5260 }
5261 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005262#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005263 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02005264 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005265 return;
5266 }
5267 }
5268
Bram Moolenaar4033c552017-09-16 20:54:51 +02005269#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005270 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005271#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005272
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005273 // free buffer when not hiding it or when it's a scratch buffer
Bram Moolenaarf740b292006-02-16 22:11:02 +00005274 if (tp == NULL)
Bram Moolenaareb44a682017-08-03 22:44:55 +02005275 win_close(win, !need_hide && !buf_hide(buf));
Bram Moolenaarf740b292006-02-16 22:11:02 +00005276 else
Bram Moolenaareb44a682017-08-03 22:44:55 +02005277 win_close_othertab(win, !need_hide && !buf_hide(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005278}
5279
Bram Moolenaar071d4272004-06-13 20:20:40 +00005280/*
Bram Moolenaardea25702017-01-29 15:18:10 +01005281 * Handle the argument for a tabpage related ex command.
5282 * Returns a tabpage number.
5283 * When an error is encountered then eap->errmsg is set.
5284 */
5285 static int
5286get_tabpage_arg(exarg_T *eap)
5287{
5288 int tab_number;
5289 int unaccept_arg0 = (eap->cmdidx == CMD_tabmove) ? 0 : 1;
5290
5291 if (eap->arg && *eap->arg != NUL)
5292 {
5293 char_u *p = eap->arg;
5294 char_u *p_save;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005295 int relative = 0; // argument +N/-N means: go to N places to the
5296 // right/left relative to the current position.
Bram Moolenaardea25702017-01-29 15:18:10 +01005297
5298 if (*p == '-')
5299 {
5300 relative = -1;
5301 p++;
5302 }
5303 else if (*p == '+')
5304 {
5305 relative = 1;
5306 p++;
5307 }
5308
5309 p_save = p;
5310 tab_number = getdigits(&p);
5311
5312 if (relative == 0)
5313 {
5314 if (STRCMP(p, "$") == 0)
5315 tab_number = LAST_TAB_NR;
5316 else if (p == p_save || *p_save == '-' || *p != NUL
5317 || tab_number > LAST_TAB_NR)
5318 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005319 // No numbers as argument.
Bram Moolenaardea25702017-01-29 15:18:10 +01005320 eap->errmsg = e_invarg;
5321 goto theend;
5322 }
5323 }
5324 else
5325 {
5326 if (*p_save == NUL)
5327 tab_number = 1;
5328 else if (p == p_save || *p_save == '-' || *p != NUL
5329 || tab_number == 0)
5330 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005331 // No numbers as argument.
Bram Moolenaardea25702017-01-29 15:18:10 +01005332 eap->errmsg = e_invarg;
5333 goto theend;
5334 }
5335 tab_number = tab_number * relative + tabpage_index(curtab);
5336 if (!unaccept_arg0 && relative == -1)
5337 --tab_number;
5338 }
5339 if (tab_number < unaccept_arg0 || tab_number > LAST_TAB_NR)
5340 eap->errmsg = e_invarg;
5341 }
5342 else if (eap->addr_count > 0)
5343 {
5344 if (unaccept_arg0 && eap->line2 == 0)
Bram Moolenaarc6251552017-01-29 21:42:20 +01005345 {
Bram Moolenaardea25702017-01-29 15:18:10 +01005346 eap->errmsg = e_invrange;
Bram Moolenaarc6251552017-01-29 21:42:20 +01005347 tab_number = 0;
5348 }
Bram Moolenaardea25702017-01-29 15:18:10 +01005349 else
5350 {
5351 tab_number = eap->line2;
Bram Moolenaar82a12462019-01-22 22:41:42 +01005352 if (!unaccept_arg0 && *skipwhite(*eap->cmdlinep) == '-')
Bram Moolenaardea25702017-01-29 15:18:10 +01005353 {
5354 --tab_number;
5355 if (tab_number < unaccept_arg0)
5356 eap->errmsg = e_invarg;
5357 }
5358 }
5359 }
5360 else
5361 {
5362 switch (eap->cmdidx)
5363 {
5364 case CMD_tabnext:
5365 tab_number = tabpage_index(curtab) + 1;
5366 if (tab_number > LAST_TAB_NR)
5367 tab_number = 1;
5368 break;
5369 case CMD_tabmove:
5370 tab_number = LAST_TAB_NR;
5371 break;
5372 default:
5373 tab_number = tabpage_index(curtab);
5374 }
5375 }
5376
5377theend:
5378 return tab_number;
5379}
5380
5381/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00005382 * ":tabclose": close current tab page, unless it is the last one.
5383 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005384 */
5385 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005386ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005387{
Bram Moolenaarf740b292006-02-16 22:11:02 +00005388 tabpage_T *tp;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005389 int tab_number;
Bram Moolenaarf740b292006-02-16 22:11:02 +00005390
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005391# ifdef FEAT_CMDWIN
5392 if (cmdwin_type != 0)
5393 cmdwin_result = K_IGNORE;
5394 else
5395# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005396 if (first_tabpage->tp_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005397 emsg(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00005398 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005399 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005400 tab_number = get_tabpage_arg(eap);
5401 if (eap->errmsg == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005402 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005403 tp = find_tabpage(tab_number);
Bram Moolenaarf740b292006-02-16 22:11:02 +00005404 if (tp == NULL)
5405 {
5406 beep_flush();
5407 return;
5408 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005409 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005410 {
5411 tabpage_close_other(tp, eap->forceit);
5412 return;
5413 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005414 else if (!text_locked() && !curbuf_locked())
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005415 tabpage_close(eap->forceit);
5416 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005417 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005418}
5419
5420/*
5421 * ":tabonly": close all tab pages except the current one
5422 */
5423 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005424ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005425{
5426 tabpage_T *tp;
5427 int done;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005428 int tab_number;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005429
5430# ifdef FEAT_CMDWIN
5431 if (cmdwin_type != 0)
5432 cmdwin_result = K_IGNORE;
5433 else
5434# endif
5435 if (first_tabpage->tp_next == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005436 msg(_("Already only one tab page"));
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005437 else
5438 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005439 tab_number = get_tabpage_arg(eap);
5440 if (eap->errmsg == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005441 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005442 goto_tabpage(tab_number);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005443 // Repeat this up to a 1000 times, because autocommands may
5444 // mess up the lists.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005445 for (done = 0; done < 1000; ++done)
5446 {
5447 FOR_ALL_TABPAGES(tp)
5448 if (tp->tp_topframe != topframe)
5449 {
5450 tabpage_close_other(tp, eap->forceit);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005451 // if we failed to close it quit
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005452 if (valid_tabpage(tp))
5453 done = 1000;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005454 // start over, "tp" is now invalid
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005455 break;
5456 }
5457 if (first_tabpage->tp_next == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005458 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005459 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005460 }
5461 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005462}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005463
5464/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00005465 * Close the current tab page.
5466 */
5467 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005468tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005469{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005470 // First close all the windows but the current one. If that worked then
5471 // close the last window in this tab, that will close it.
Bram Moolenaar459ca562016-11-10 18:16:33 +01005472 if (!ONE_WINDOW)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005473 close_others(TRUE, forceit);
Bram Moolenaar459ca562016-11-10 18:16:33 +01005474 if (ONE_WINDOW)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005475 ex_win_close(forceit, curwin, NULL);
5476# ifdef FEAT_GUI
5477 need_mouse_correct = TRUE;
5478# endif
5479}
5480
5481/*
5482 * Close tab page "tp", which is not the current tab page.
5483 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005484 * Also takes care of the tab pages line disappearing when closing the
5485 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00005486 */
5487 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005488tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005489{
5490 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005491 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005492 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00005493
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005494 // Limit to 1000 windows, autocommands may add a window while we close
5495 // one. OK, so I'm paranoid...
Bram Moolenaarf740b292006-02-16 22:11:02 +00005496 while (++done < 1000)
5497 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005498 wp = tp->tp_firstwin;
5499 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00005500
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005501 // Autocommands may delete the tab page under our fingers and we may
5502 // fail to close a window with a modified buffer.
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005503 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005504 break;
5505 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005506
Bram Moolenaar29323592016-07-24 22:04:11 +02005507 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar29323592016-07-24 22:04:11 +02005508
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005509 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005510 if (h != tabline_height())
5511 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00005512}
5513
5514/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005515 * ":only".
5516 */
5517 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005518ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005519{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005520 win_T *wp;
5521 int wnr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005522# ifdef FEAT_GUI
5523 need_mouse_correct = TRUE;
5524# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005525 if (eap->addr_count > 0)
5526 {
5527 wnr = eap->line2;
5528 for (wp = firstwin; --wnr > 0; )
5529 {
5530 if (wp->w_next == NULL)
5531 break;
5532 else
5533 wp = wp->w_next;
5534 }
5535 win_goto(wp);
5536 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005537 close_others(TRUE, eap->forceit);
5538}
5539
Bram Moolenaar071d4272004-06-13 20:20:40 +00005540 static void
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01005541ex_hide(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005542{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005543 // ":hide" or ":hide | cmd": hide current window
Bram Moolenaar2256c992016-11-15 21:17:07 +01005544 if (!eap->skip)
5545 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02005546#ifdef FEAT_GUI
Bram Moolenaar2256c992016-11-15 21:17:07 +01005547 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005548#endif
Bram Moolenaar2256c992016-11-15 21:17:07 +01005549 if (eap->addr_count == 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005550 win_close(curwin, FALSE); // don't free buffer
Bram Moolenaar2256c992016-11-15 21:17:07 +01005551 else
5552 {
5553 int winnr = 0;
5554 win_T *win;
Bram Moolenaarf240e182014-11-27 18:33:02 +01005555
Bram Moolenaar2256c992016-11-15 21:17:07 +01005556 FOR_ALL_WINDOWS(win)
5557 {
5558 winnr++;
5559 if (winnr == eap->line2)
5560 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005561 }
Bram Moolenaar2256c992016-11-15 21:17:07 +01005562 if (win == NULL)
5563 win = lastwin;
5564 win_close(win, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005565 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005566 }
5567}
5568
5569/*
5570 * ":stop" and ":suspend": Suspend Vim.
5571 */
5572 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005573ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005574{
5575 /*
5576 * Disallow suspending for "rvim".
5577 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005578 if (!check_restricted())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005579 {
5580 if (!eap->forceit)
5581 autowrite_all();
5582 windgoto((int)Rows - 1, 0);
5583 out_char('\n');
5584 out_flush();
5585 stoptermcap();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005586 out_flush(); // needed for SUN to restore xterm buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00005587#ifdef FEAT_TITLE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005588 mch_restore_title(SAVE_RESTORE_BOTH); // restore window titles
Bram Moolenaar071d4272004-06-13 20:20:40 +00005589#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005590 ui_suspend(); // call machine specific function
Bram Moolenaar071d4272004-06-13 20:20:40 +00005591#ifdef FEAT_TITLE
5592 maketitle();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005593 resettitle(); // force updating the title
Bram Moolenaar071d4272004-06-13 20:20:40 +00005594#endif
5595 starttermcap();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005596 scroll_start(); // scroll screen before redrawing
Bram Moolenaar071d4272004-06-13 20:20:40 +00005597 redraw_later_clear();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005598 shell_resized(); // may have resized window
Bram Moolenaar071d4272004-06-13 20:20:40 +00005599 }
5600}
5601
5602/*
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005603 * ":exit", ":xit" and ":wq": Write file and quite the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005604 */
5605 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005606ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005607{
5608#ifdef FEAT_CMDWIN
5609 if (cmdwin_type != 0)
5610 {
5611 cmdwin_result = Ctrl_C;
5612 return;
5613 }
5614#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005615 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005616 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005617 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005618 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005619 return;
5620 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005621
5622 if (before_quit_autocmds(curwin, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005623 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005624
5625 /*
5626 * if more files or windows we won't exit
5627 */
5628 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
5629 exiting = TRUE;
5630 if ( ((eap->cmdidx == CMD_wq
5631 || curbufIsChanged())
5632 && do_write(eap) == FAIL)
5633 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01005634 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005635 {
5636 not_exiting();
5637 }
5638 else
5639 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005640 if (only_one_window()) // quit last window, exit Vim
Bram Moolenaar071d4272004-06-13 20:20:40 +00005641 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02005642 not_exiting();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005643# ifdef FEAT_GUI
5644 need_mouse_correct = TRUE;
5645# endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005646 // Quit current window, may free the buffer.
Bram Moolenaareb44a682017-08-03 22:44:55 +02005647 win_close(curwin, !buf_hide(curwin->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005648 }
5649}
5650
5651/*
5652 * ":print", ":list", ":number".
5653 */
5654 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005655ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005656{
Bram Moolenaardf177f62005-02-22 08:39:57 +00005657 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005658 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +00005659 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005660 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00005661 for ( ;!got_int; ui_breakcheck())
5662 {
5663 print_line(eap->line1,
5664 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
5665 || (eap->flags & EXFLAG_NR)),
5666 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
5667 if (++eap->line1 > eap->line2)
5668 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005669 out_flush(); // show one line at a time
Bram Moolenaardf177f62005-02-22 08:39:57 +00005670 }
5671 setpcmark();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005672 // put cursor at last line
Bram Moolenaardf177f62005-02-22 08:39:57 +00005673 curwin->w_cursor.lnum = eap->line2;
5674 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005675 }
5676
Bram Moolenaar071d4272004-06-13 20:20:40 +00005677 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005678}
5679
5680#ifdef FEAT_BYTEOFF
5681 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005682ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005683{
5684 goto_byte(eap->line2);
5685}
5686#endif
5687
5688/*
5689 * ":shell".
5690 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005691 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005692ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005693{
5694 do_shell(NULL, 0);
5695}
5696
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005697#if defined(HAVE_DROP_FILE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005698
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005699static int drop_busy = FALSE;
5700static int drop_filec;
5701static char_u **drop_filev = NULL;
5702static int drop_split;
5703static void (*drop_callback)(void *);
5704static void *drop_cookie;
5705
5706 static void
5707handle_drop_internal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005708{
5709 exarg_T ea;
5710 int save_msg_scroll = msg_scroll;
5711
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005712 // Setting the argument list may cause screen updates and being called
5713 // recursively. Avoid that by setting drop_busy.
5714 drop_busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005715
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005716 // Check whether the current buffer is changed. If so, we will need
5717 // to split the current window or data could be lost.
5718 // We don't need to check if the 'hidden' option is set, as in this
5719 // case the buffer won't be lost.
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005720 if (!buf_hide(curbuf) && !drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005721 {
5722 ++emsg_off;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005723 drop_split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005724 --emsg_off;
5725 }
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005726 if (drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005727 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005728 if (win_split(0, 0) == FAIL)
5729 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02005730 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005731
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005732 // When splitting the window, create a new alist. Otherwise the
5733 // existing one is overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005734 alist_unlink(curwin->w_alist);
5735 alist_new();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005736 }
5737
5738 /*
5739 * Set up the new argument list.
5740 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005741 alist_set(ALIST(curwin), drop_filec, drop_filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005742
5743 /*
5744 * Move to the first file.
5745 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005746 // Fake up a minimal "next" command for do_argfile()
Bram Moolenaara80faa82020-04-12 19:37:17 +02005747 CLEAR_FIELD(ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005748 ea.cmd = (char_u *)"next";
5749 do_argfile(&ea, 0);
5750
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005751 // do_ecmd() may set need_start_insertmode, but since we never left Insert
5752 // mode that is not needed here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005753 need_start_insertmode = FALSE;
5754
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005755 // Restore msg_scroll, otherwise a following command may cause scrolling
5756 // unexpectedly. The screen will be redrawn by the caller, thus
5757 // msg_scroll being set by displaying a message is irrelevant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005758 msg_scroll = save_msg_scroll;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005759
5760 if (drop_callback != NULL)
5761 drop_callback(drop_cookie);
5762
5763 drop_filev = NULL;
5764 drop_busy = FALSE;
5765}
5766
5767/*
5768 * Handle a file drop. The code is here because a drop is *nearly* like an
5769 * :args command, but not quite (we have a list of exact filenames, so we
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01005770 * don't want to (a) parse a command line, or (b) expand wildcards). So the
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005771 * code is very similar to :args and hence needs access to a lot of the static
5772 * functions in this file.
5773 *
5774 * The "filev" list must have been allocated using alloc(), as should each item
5775 * in the list. This function takes over responsibility for freeing the "filev"
5776 * list.
5777 */
5778 void
5779handle_drop(
5780 int filec, // the number of files dropped
5781 char_u **filev, // the list of files dropped
5782 int split, // force splitting the window
5783 void (*callback)(void *), // to be called after setting the argument
5784 // list
5785 void *cookie) // argument for "callback" (allocated)
5786{
5787 // Cannot handle recursive drops, finish the pending one.
5788 if (drop_busy)
5789 {
5790 FreeWild(filec, filev);
5791 vim_free(cookie);
5792 return;
5793 }
5794
5795 // When calling handle_drop() more than once in a row we only use the last
5796 // one.
5797 if (drop_filev != NULL)
5798 {
5799 FreeWild(drop_filec, drop_filev);
5800 vim_free(drop_cookie);
5801 }
5802
5803 drop_filec = filec;
5804 drop_filev = filev;
5805 drop_split = split;
5806 drop_callback = callback;
5807 drop_cookie = cookie;
5808
5809 // Postpone this when:
5810 // - editing the command line
5811 // - not possible to change the current buffer
5812 // - updating the screen
5813 // As it may change buffers and window structures that are in use and cause
5814 // freed memory to be used.
5815 if (text_locked() || curbuf_locked() || updating_screen)
5816 return;
5817
5818 handle_drop_internal();
5819}
5820
5821/*
5822 * To be called when text is unlocked, curbuf is unlocked or updating_screen is
5823 * reset: Handle a postponed drop.
5824 */
5825 void
5826handle_any_postponed_drop(void)
5827{
5828 if (!drop_busy && drop_filev != NULL
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02005829 && !text_locked() && !curbuf_locked() && !updating_screen)
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005830 handle_drop_internal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005831}
5832#endif
5833
Bram Moolenaar071d4272004-06-13 20:20:40 +00005834/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005835 * ":preserve".
5836 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005837 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005838ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005839{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005840 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005841 ml_preserve(curbuf, TRUE);
5842}
5843
5844/*
5845 * ":recover".
5846 */
5847 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005848ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005849{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005850 // Set recoverymode right away to avoid the ATTENTION prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005851 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01005852 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
5853 | CCGD_MULTWIN
5854 | (eap->forceit ? CCGD_FORCEIT : 0)
5855 | CCGD_EXCMD)
5856
Bram Moolenaar071d4272004-06-13 20:20:40 +00005857 && (*eap->arg == NUL
5858 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
Bram Moolenaar99499b12019-05-23 21:35:48 +02005859 ml_recover(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005860 recoverymode = FALSE;
5861}
5862
5863/*
5864 * Command modifier used in a wrong way.
5865 */
5866 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005867ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005868{
5869 eap->errmsg = e_invcmd;
5870}
5871
Bram Moolenaar071d4272004-06-13 20:20:40 +00005872/*
5873 * :sview [+command] file split window with new file, read-only
5874 * :split [[+command] file] split window with current or new file
5875 * :vsplit [[+command] file] split window vertically with current or new file
5876 * :new [[+command] file] split window with no or new file
5877 * :vnew [[+command] file] split vertically window with no or new file
5878 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005879 *
5880 * :tabedit open new Tab page with empty window
5881 * :tabedit [+command] file open new Tab page and edit "file"
5882 * :tabnew [[+command] file] just like :tabedit
5883 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005884 */
5885 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005886ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005887{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005888 win_T *old_curwin = curwin;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005889#if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005890 char_u *fname = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005891#endif
5892#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005893 int browse_flag = cmdmod.browse;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005894#endif
Bram Moolenaar39902a02018-06-21 22:10:08 +02005895 int use_tab = eap->cmdidx == CMD_tabedit
5896 || eap->cmdidx == CMD_tabfind
5897 || eap->cmdidx == CMD_tabnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005898
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005899 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005900 return;
5901
Bram Moolenaar4033c552017-09-16 20:54:51 +02005902#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005903 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005904#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005905
Bram Moolenaar4033c552017-09-16 20:54:51 +02005906#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005907 // A ":split" in the quickfix window works like ":new". Don't want two
5908 // quickfix windows. But it's OK when doing ":tab split".
Bram Moolenaar05bb9532008-07-04 09:44:11 +00005909 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005910 {
5911 if (eap->cmdidx == CMD_split)
5912 eap->cmdidx = CMD_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005913 if (eap->cmdidx == CMD_vsplit)
5914 eap->cmdidx = CMD_vnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005915 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02005916#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005917
Bram Moolenaar4033c552017-09-16 20:54:51 +02005918#ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005919 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005920 {
5921 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
5922 FNAME_MESS, TRUE, curbuf->b_ffname);
5923 if (fname == NULL)
5924 goto theend;
5925 eap->arg = fname;
5926 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005927# ifdef FEAT_BROWSE
Bram Moolenaar4033c552017-09-16 20:54:51 +02005928 else
5929# endif
5930#endif
5931#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005932 if (cmdmod.browse
Bram Moolenaar071d4272004-06-13 20:20:40 +00005933 && eap->cmdidx != CMD_vnew
Bram Moolenaar071d4272004-06-13 20:20:40 +00005934 && eap->cmdidx != CMD_new)
5935 {
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005936 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005937# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005938 !gui.in_use &&
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005939# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005940 au_has_group((char_u *)"FileExplorer"))
5941 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005942 // No browsing supported but we do have the file explorer:
5943 // Edit the directory.
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005944 if (*eap->arg == NUL || !mch_isdir(eap->arg))
5945 eap->arg = (char_u *)".";
5946 }
5947 else
5948 {
Bram Moolenaar39902a02018-06-21 22:10:08 +02005949 fname = do_browse(0, (char_u *)(use_tab
5950 ? _("Edit File in new tab page")
5951 : _("Edit File in new window")),
Bram Moolenaar071d4272004-06-13 20:20:40 +00005952 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005953 if (fname == NULL)
5954 goto theend;
5955 eap->arg = fname;
5956 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005957 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005958 cmdmod.browse = FALSE; // Don't browse again in do_ecmd().
Bram Moolenaar4033c552017-09-16 20:54:51 +02005959#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005960
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005961 /*
5962 * Either open new tab page or split the window.
5963 */
Bram Moolenaar39902a02018-06-21 22:10:08 +02005964 if (use_tab)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005965 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005966 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
5967 : eap->addr_count == 0 ? 0
5968 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005969 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00005970 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005971
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005972 // set the alternate buffer for the window we came from
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005973 if (curwin != old_curwin
5974 && win_valid(old_curwin)
5975 && old_curwin->w_buffer != curbuf
5976 && !cmdmod.keepalt)
5977 old_curwin->w_alt_fnum = curbuf->b_fnum;
5978 }
5979 }
5980 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005981 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
5982 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005983 // Reset 'scrollbind' when editing another file, but keep it when
5984 // doing ":split" without arguments.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005985 if (*eap->arg != NUL
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01005986# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005987 || cmdmod.browse
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01005988# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005989 )
Bram Moolenaar3368ea22010-09-21 16:56:35 +02005990 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005991 else
5992 do_check_scrollbind(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005993 do_exedit(eap, old_curwin);
5994 }
5995
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005996# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005997 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005998# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005999
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006000# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006001theend:
6002 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006003# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006004}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006005
6006/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006007 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006008 */
6009 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006010tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006011{
6012 exarg_T ea;
6013
Bram Moolenaara80faa82020-04-12 19:37:17 +02006014 CLEAR_FIELD(ea);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006015 ea.cmdidx = CMD_tabnew;
6016 ea.cmd = (char_u *)"tabn";
6017 ea.arg = (char_u *)"";
6018 ex_splitview(&ea);
6019}
6020
6021/*
6022 * :tabnext command
6023 */
6024 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006025ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006026{
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006027 int tab_number;
6028
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02006029 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006030 return;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006031 switch (eap->cmdidx)
6032 {
6033 case CMD_tabfirst:
6034 case CMD_tabrewind:
6035 goto_tabpage(1);
6036 break;
6037 case CMD_tablast:
6038 goto_tabpage(9999);
6039 break;
6040 case CMD_tabprevious:
6041 case CMD_tabNext:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006042 if (eap->arg && *eap->arg != NUL)
6043 {
6044 char_u *p = eap->arg;
6045 char_u *p_save = p;
6046
6047 tab_number = getdigits(&p);
6048 if (p == p_save || *p_save == '-' || *p != NUL
6049 || tab_number == 0)
6050 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006051 // No numbers as argument.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006052 eap->errmsg = e_invarg;
6053 return;
6054 }
6055 }
6056 else
6057 {
6058 if (eap->addr_count == 0)
6059 tab_number = 1;
6060 else
6061 {
6062 tab_number = eap->line2;
6063 if (tab_number < 1)
6064 {
6065 eap->errmsg = e_invrange;
6066 return;
6067 }
6068 }
6069 }
6070 goto_tabpage(-tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006071 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006072 default: // CMD_tabnext
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006073 tab_number = get_tabpage_arg(eap);
6074 if (eap->errmsg == NULL)
6075 goto_tabpage(tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006076 break;
6077 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006078}
6079
6080/*
6081 * :tabmove command
6082 */
6083 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006084ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006085{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02006086 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02006087
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006088 tab_number = get_tabpage_arg(eap);
6089 if (eap->errmsg == NULL)
6090 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006091}
6092
6093/*
6094 * :tabs command: List tabs and their contents.
6095 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006096 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006097ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006098{
6099 tabpage_T *tp;
6100 win_T *wp;
6101 int tabcount = 1;
6102
6103 msg_start();
6104 msg_scroll = TRUE;
6105 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
6106 {
6107 msg_putchar('\n');
6108 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
Bram Moolenaar8820b482017-03-16 17:23:31 +01006109 msg_outtrans_attr(IObuff, HL_ATTR(HLF_T));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006110 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006111 ui_breakcheck();
6112
Bram Moolenaar030f0df2006-02-21 22:02:53 +00006113 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006114 wp = firstwin;
6115 else
6116 wp = tp->tp_firstwin;
6117 for ( ; wp != NULL && !got_int; wp = wp->w_next)
6118 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006119 msg_putchar('\n');
6120 msg_putchar(wp == curwin ? '>' : ' ');
6121 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006122 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
6123 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006124 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02006125 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006126 else
6127 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
6128 IObuff, IOSIZE, TRUE);
6129 msg_outtrans(IObuff);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006130 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006131 ui_breakcheck();
6132 }
6133 }
6134}
6135
Bram Moolenaar071d4272004-06-13 20:20:40 +00006136/*
6137 * ":mode": Set screen mode.
6138 * If no argument given, just get the screen size and redraw.
6139 */
6140 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006141ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006142{
6143 if (*eap->arg == NUL)
6144 shell_resized();
6145 else
Bram Moolenaar3c71aec2020-01-17 19:32:20 +01006146 emsg(_(e_screenmode));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006147}
6148
Bram Moolenaar071d4272004-06-13 20:20:40 +00006149/*
6150 * ":resize".
6151 * set, increment or decrement current window height
6152 */
6153 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006154ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006155{
6156 int n;
6157 win_T *wp = curwin;
6158
6159 if (eap->addr_count > 0)
6160 {
6161 n = eap->line2;
6162 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
6163 ;
6164 }
6165
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006166# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006167 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006168# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006169 n = atol((char *)eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006170 if (cmdmod.split & WSP_VERT)
6171 {
6172 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar02631462017-09-22 15:20:32 +02006173 n += curwin->w_width;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006174 else if (n == 0 && eap->arg[0] == NUL) // default is very wide
Bram Moolenaar071d4272004-06-13 20:20:40 +00006175 n = 9999;
6176 win_setwidth_win((int)n, wp);
6177 }
6178 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006179 {
6180 if (*eap->arg == '-' || *eap->arg == '+')
6181 n += curwin->w_height;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006182 else if (n == 0 && eap->arg[0] == NUL) // default is very high
Bram Moolenaar071d4272004-06-13 20:20:40 +00006183 n = 9999;
6184 win_setheight_win((int)n, wp);
6185 }
6186}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006187
6188/*
6189 * ":find [+command] <file>" command.
6190 */
6191 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006192ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006193{
6194#ifdef FEAT_SEARCHPATH
6195 char_u *fname;
6196 int count;
6197
6198 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
6199 TRUE, curbuf->b_ffname);
6200 if (eap->addr_count > 0)
6201 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006202 // Repeat finding the file "count" times. This matters when it
6203 // appears several times in the path.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006204 count = eap->line2;
6205 while (fname != NULL && --count > 0)
6206 {
6207 vim_free(fname);
6208 fname = find_file_in_path(NULL, 0, FNAME_MESS,
6209 FALSE, curbuf->b_ffname);
6210 }
6211 }
6212
6213 if (fname != NULL)
6214 {
6215 eap->arg = fname;
6216#endif
6217 do_exedit(eap, NULL);
6218#ifdef FEAT_SEARCHPATH
6219 vim_free(fname);
6220 }
6221#endif
6222}
6223
6224/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00006225 * ":open" simulation: for now just work like ":visual".
6226 */
6227 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006228ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006229{
6230 regmatch_T regmatch;
6231 char_u *p;
6232
6233 curwin->w_cursor.lnum = eap->line2;
6234 beginline(BL_SOL | BL_FIX);
6235 if (*eap->arg == '/')
6236 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006237 // ":open /pattern/": put cursor in column found with pattern
Bram Moolenaardf177f62005-02-22 08:39:57 +00006238 ++eap->arg;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006239 p = skip_regexp(eap->arg, '/', p_magic);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006240 *p = NUL;
6241 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
6242 if (regmatch.regprog != NULL)
6243 {
6244 regmatch.rm_ic = p_ic;
6245 p = ml_get_curline();
6246 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006247 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006248 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006249 emsg(_(e_nomatch));
Bram Moolenaar473de612013-06-08 18:19:48 +02006250 vim_regfree(regmatch.regprog);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006251 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006252 // Move to the NUL, ignore any other arguments.
Bram Moolenaardf177f62005-02-22 08:39:57 +00006253 eap->arg += STRLEN(eap->arg);
6254 }
6255 check_cursor();
6256
6257 eap->cmdidx = CMD_visual;
6258 do_exedit(eap, NULL);
6259}
6260
6261/*
6262 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006263 */
6264 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006265ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006266{
6267 do_exedit(eap, NULL);
6268}
6269
6270/*
Bram Moolenaar4b96df52020-01-26 22:00:26 +01006271 * ":edit <file>" command and alike.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006272 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006273 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006274do_exedit(
6275 exarg_T *eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006276 win_T *old_curwin) // curwin before doing a split or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006277{
6278 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006279 int need_hide;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006280 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006281
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01006282 if ((eap->cmdidx != CMD_pedit && ERROR_IF_POPUP_WINDOW)
6283 || ERROR_IF_TERM_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006284 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006285 /*
6286 * ":vi" command ends Ex mode.
6287 */
6288 if (exmode_active && (eap->cmdidx == CMD_visual
6289 || eap->cmdidx == CMD_view))
6290 {
6291 exmode_active = FALSE;
Bram Moolenaar158ea172020-06-18 17:28:39 +02006292 ex_pressedreturn = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006293 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006294 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006295 // Special case: ":global/pat/visual\NLvi-commands"
Bram Moolenaardf177f62005-02-22 08:39:57 +00006296 if (global_busy)
6297 {
6298 int rd = RedrawingDisabled;
6299 int nwr = no_wait_return;
6300 int ms = msg_scroll;
6301#ifdef FEAT_GUI
6302 int he = hold_gui_events;
6303#endif
6304
6305 if (eap->nextcmd != NULL)
6306 {
6307 stuffReadbuff(eap->nextcmd);
6308 eap->nextcmd = NULL;
6309 }
6310
6311 if (exmode_was != EXMODE_VIM)
6312 settmode(TMODE_RAW);
6313 RedrawingDisabled = 0;
6314 no_wait_return = 0;
6315 need_wait_return = FALSE;
6316 msg_scroll = 0;
6317#ifdef FEAT_GUI
6318 hold_gui_events = 0;
6319#endif
6320 must_redraw = CLEAR;
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01006321 pending_exmode_active = TRUE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006322
6323 main_loop(FALSE, TRUE);
6324
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01006325 pending_exmode_active = FALSE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006326 RedrawingDisabled = rd;
6327 no_wait_return = nwr;
6328 msg_scroll = ms;
6329#ifdef FEAT_GUI
6330 hold_gui_events = he;
6331#endif
6332 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006333 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006334 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006335 }
6336
6337 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006338 || eap->cmdidx == CMD_tabnew
6339 || eap->cmdidx == CMD_tabedit
Bram Moolenaar4033c552017-09-16 20:54:51 +02006340 || eap->cmdidx == CMD_vnew) && *eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006341 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006342 // ":new" or ":tabnew" without argument: edit an new empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006343 setpcmark();
6344 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006345 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
6346 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006347 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006348 else if ((eap->cmdidx != CMD_split && eap->cmdidx != CMD_vsplit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006349 || *eap->arg != NUL
6350#ifdef FEAT_BROWSE
6351 || cmdmod.browse
6352#endif
6353 )
6354 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006355 // Can't edit another file when "curbuf_lock" is set. Only ":edit"
6356 // can bring us here, others are stopped earlier.
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006357 if (*eap->arg != NUL && curbuf_locked())
6358 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006359
Bram Moolenaar071d4272004-06-13 20:20:40 +00006360 n = readonlymode;
6361 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
6362 readonlymode = TRUE;
6363 else if (eap->cmdidx == CMD_enew)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006364 readonlymode = FALSE; // 'readonly' doesn't make sense in an
6365 // empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006366 setpcmark();
6367 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
6368 NULL, eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006369 // ":edit" goes to first line if Vi compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00006370 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
6371 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
6372 ? ECMD_ONE : eap->do_ecmd_lnum,
Bram Moolenaareb44a682017-08-03 22:44:55 +02006373 (buf_hide(curbuf) ? ECMD_HIDE : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006374 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006375 // after a split we can use an existing buffer
Bram Moolenaar7b449342014-03-25 13:03:48 +01006376 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006377 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006378 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006379 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006380 // Editing the file failed. If the window was split, close it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006381 if (old_curwin != NULL)
6382 {
6383 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02006384 if (!need_hide || buf_hide(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006385 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006386#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006387 cleanup_T cs;
6388
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006389 // Reset the error/interrupt/exception state here so that
6390 // aborting() returns FALSE when closing a window.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006391 enter_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02006392#endif
6393#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006394 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006395#endif
Bram Moolenaareb44a682017-08-03 22:44:55 +02006396 win_close(curwin, !need_hide && !buf_hide(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006397
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006398#if defined(FEAT_EVAL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006399 // Restore the error/interrupt/exception state if not
6400 // discarded by a new aborting error, interrupt, or
6401 // uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006402 leave_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02006403#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006404 }
6405 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006406 }
6407 else if (readonlymode && curbuf->b_nwindows == 1)
6408 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006409 // When editing an already visited buffer, 'readonly' won't be set
6410 // but the previous value is kept. With ":view" and ":sview" we
6411 // want the file to be readonly, except when another window is
6412 // editing the same buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006413 curbuf->b_p_ro = TRUE;
6414 }
6415 readonlymode = n;
6416 }
6417 else
6418 {
6419 if (eap->do_ecmd_cmd != NULL)
6420 do_cmdline_cmd(eap->do_ecmd_cmd);
6421#ifdef FEAT_TITLE
6422 n = curwin->w_arg_idx_invalid;
6423#endif
6424 check_arg_idx(curwin);
6425#ifdef FEAT_TITLE
6426 if (n != curwin->w_arg_idx_invalid)
6427 maketitle();
6428#endif
6429 }
6430
Bram Moolenaar071d4272004-06-13 20:20:40 +00006431 /*
6432 * if ":split file" worked, set alternate file name in old window to new
6433 * file
6434 */
6435 if (old_curwin != NULL
6436 && *eap->arg != NUL
6437 && curwin != old_curwin
6438 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00006439 && old_curwin->w_buffer != curbuf
6440 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006441 old_curwin->w_alt_fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006442
6443 ex_no_reprint = TRUE;
6444}
6445
6446#ifndef FEAT_GUI
6447/*
6448 * ":gui" and ":gvim" when there is no GUI.
6449 */
6450 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006451ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006452{
6453 eap->errmsg = e_nogvim;
6454}
6455#endif
6456
Bram Moolenaar4f974752019-02-17 17:44:42 +01006457#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006458 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006459ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006460{
6461 gui_make_tearoff(eap->arg);
6462}
6463#endif
6464
Bram Moolenaar29a2c082018-03-05 21:06:23 +01006465#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
6466 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006467 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006468ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006469{
Bram Moolenaar29a2c082018-03-05 21:06:23 +01006470# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
6471 if (gui.in_use)
6472 gui_make_popup(eap->arg, eap->forceit);
6473# ifdef FEAT_TERM_POPUP_MENU
6474 else
6475# endif
6476# endif
6477# ifdef FEAT_TERM_POPUP_MENU
6478 pum_make_popup(eap->arg, eap->forceit);
6479# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006480}
6481#endif
6482
Bram Moolenaar071d4272004-06-13 20:20:40 +00006483 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006484ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006485{
6486 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006487 msg(_("No swap file"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006488 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01006489 msg((char *)curbuf->b_ml.ml_mfp->mf_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006490}
6491
6492/*
6493 * ":syncbind" forces all 'scrollbind' windows to have the same relative
6494 * offset.
6495 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
6496 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006497 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006498ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006499{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006500 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006501 win_T *save_curwin = curwin;
6502 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006503 long topline;
6504 long y;
6505 linenr_T old_linenr = curwin->w_cursor.lnum;
6506
6507 setpcmark();
6508
6509 /*
6510 * determine max topline
6511 */
6512 if (curwin->w_p_scb)
6513 {
6514 topline = curwin->w_topline;
Bram Moolenaar29323592016-07-24 22:04:11 +02006515 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006516 {
6517 if (wp->w_p_scb && wp->w_buffer)
6518 {
Bram Moolenaar375e3392019-01-31 18:26:10 +01006519 y = wp->w_buffer->b_ml.ml_line_count - get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006520 if (topline > y)
6521 topline = y;
6522 }
6523 }
6524 if (topline < 1)
6525 topline = 1;
6526 }
6527 else
6528 {
6529 topline = 1;
6530 }
6531
6532
6533 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006534 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006535 */
Bram Moolenaar29323592016-07-24 22:04:11 +02006536 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006537 {
6538 if (curwin->w_p_scb)
6539 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006540 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006541 y = topline - curwin->w_topline;
6542 if (y > 0)
6543 scrollup(y, TRUE);
6544 else
6545 scrolldown(-y, TRUE);
6546 curwin->w_scbind_pos = topline;
6547 redraw_later(VALID);
6548 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006549 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006550 }
6551 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006552 curwin = save_curwin;
6553 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006554 if (curwin->w_p_scb)
6555 {
6556 did_syncbind = TRUE;
6557 checkpcmark();
6558 if (old_linenr != curwin->w_cursor.lnum)
6559 {
6560 char_u ctrl_o[2];
6561
6562 ctrl_o[0] = Ctrl_O;
6563 ctrl_o[1] = 0;
6564 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
6565 }
6566 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006567}
6568
6569
6570 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006571ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006572{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006573 int i;
6574 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
6575 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006576
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006577 if (eap->usefilter) // :r!cmd
Bram Moolenaar071d4272004-06-13 20:20:40 +00006578 do_bang(1, eap, FALSE, FALSE, TRUE);
6579 else
6580 {
6581 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
6582 return;
6583
6584#ifdef FEAT_BROWSE
6585 if (cmdmod.browse)
6586 {
6587 char_u *browseFile;
6588
Bram Moolenaar7171abe2004-10-11 10:06:20 +00006589 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006590 NULL, NULL, NULL, curbuf);
6591 if (browseFile != NULL)
6592 {
6593 i = readfile(browseFile, NULL,
6594 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
6595 vim_free(browseFile);
6596 }
6597 else
6598 i = OK;
6599 }
6600 else
6601#endif
6602 if (*eap->arg == NUL)
6603 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006604 if (check_fname() == FAIL) // check for no file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00006605 return;
6606 i = readfile(curbuf->b_ffname, curbuf->b_fname,
6607 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
6608 }
6609 else
6610 {
6611 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
6612 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
6613 i = readfile(eap->arg, NULL,
6614 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
6615
6616 }
Bram Moolenaare13b9af2017-01-13 22:01:02 +01006617 if (i != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006618 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006619#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006620 if (!aborting())
6621#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006622 semsg(_(e_notopen), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006623 }
6624 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00006625 {
6626 if (empty && exmode_active)
6627 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006628 // Delete the empty line that remains. Historically ex does
6629 // this but vi doesn't.
Bram Moolenaardf177f62005-02-22 08:39:57 +00006630 if (eap->line2 == 0)
6631 lnum = curbuf->b_ml.ml_line_count;
6632 else
6633 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00006634 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006635 {
Bram Moolenaarca70c072020-05-30 20:30:46 +02006636 ml_delete(lnum);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00006637 if (curwin->w_cursor.lnum > 1
6638 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006639 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00006640 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006641 }
6642 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006643 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006644 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006645 }
6646}
6647
Bram Moolenaarea408852005-06-25 22:49:46 +00006648static char_u *prev_dir = NULL;
6649
6650#if defined(EXITFREE) || defined(PROTO)
6651 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006652free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00006653{
Bram Moolenaard23a8232018-02-10 18:45:26 +01006654 VIM_CLEAR(prev_dir);
6655 VIM_CLEAR(globaldir);
Bram Moolenaarea408852005-06-25 22:49:46 +00006656}
6657#endif
6658
Bram Moolenaarf4258302013-06-02 18:20:17 +02006659/*
Bram Moolenaar002bc792020-06-05 22:33:42 +02006660 * Get the previous directory for the given chdir scope.
6661 */
6662 static char_u *
6663get_prevdir(cdscope_T scope)
6664{
6665 if (scope == CDSCOPE_WINDOW)
6666 return curwin->w_prevdir;
6667 else if (scope == CDSCOPE_TABPAGE)
6668 return curtab->tp_prevdir;
6669 return prev_dir;
6670}
6671
6672/*
Bram Moolenaarf4258302013-06-02 18:20:17 +02006673 * Deal with the side effects of changing the current directory.
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006674 * When 'scope' is CDSCOPE_TABPAGE then this was after an ":tcd" command.
6675 * When 'scope' is CDSCOPE_WINDOW then this was after an ":lcd" command.
Bram Moolenaarf4258302013-06-02 18:20:17 +02006676 */
6677 void
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006678post_chdir(cdscope_T scope)
Bram Moolenaarf4258302013-06-02 18:20:17 +02006679{
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006680 if (scope != CDSCOPE_WINDOW)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006681 // Clear tab local directory for both :cd and :tcd
6682 VIM_CLEAR(curtab->tp_localdir);
Bram Moolenaard23a8232018-02-10 18:45:26 +01006683 VIM_CLEAR(curwin->w_localdir);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006684 if (scope != CDSCOPE_GLOBAL)
Bram Moolenaarf4258302013-06-02 18:20:17 +02006685 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02006686 char_u *pdir = get_prevdir(scope);
6687
6688 // If still in the global directory, need to remember current
6689 // directory as the global directory.
6690 if (globaldir == NULL && pdir != NULL)
6691 globaldir = vim_strsave(pdir);
6692
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006693 // Remember this local directory for the window.
Bram Moolenaarf4258302013-06-02 18:20:17 +02006694 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006695 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006696 if (scope == CDSCOPE_TABPAGE)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006697 curtab->tp_localdir = vim_strsave(NameBuff);
6698 else
6699 curwin->w_localdir = vim_strsave(NameBuff);
6700 }
Bram Moolenaarf4258302013-06-02 18:20:17 +02006701 }
6702 else
6703 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02006704 // We are now in the global directory, no need to remember its name.
Bram Moolenaard23a8232018-02-10 18:45:26 +01006705 VIM_CLEAR(globaldir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006706 }
6707
6708 shorten_fnames(TRUE);
6709}
6710
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006711/*
6712 * Change directory function used by :cd/:tcd/:lcd Ex commands and the
Bram Moolenaar95058722020-06-01 16:26:19 +02006713 * chdir() function.
6714 * scope == CDSCOPE_WINDOW: changes the window-local directory
6715 * scope == CDSCOPE_TABPAGE: changes the tab-local directory
6716 * Otherwise: changes the global directory
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006717 * Returns TRUE if the directory is successfully changed.
6718 */
6719 int
6720changedir_func(
6721 char_u *new_dir,
6722 int forceit,
6723 cdscope_T scope)
6724{
6725 char_u *tofree;
Bram Moolenaar002bc792020-06-05 22:33:42 +02006726 char_u *pdir = NULL;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006727 int dir_differs;
6728 int retval = FALSE;
6729
Bram Moolenaar7cc96922020-01-31 22:41:38 +01006730 if (new_dir == NULL || allbuf_locked())
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006731 return FALSE;
6732
6733 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged() && !forceit)
6734 {
6735 emsg(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
6736 return FALSE;
6737 }
6738
6739 // ":cd -": Change to previous directory
6740 if (STRCMP(new_dir, "-") == 0)
6741 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02006742 pdir = get_prevdir(scope);
6743 if (pdir == NULL)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006744 {
6745 emsg(_("E186: No previous directory"));
6746 return FALSE;
6747 }
Bram Moolenaar002bc792020-06-05 22:33:42 +02006748 new_dir = pdir;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006749 }
6750
Bram Moolenaar002bc792020-06-05 22:33:42 +02006751 // Free the previous directory
6752 tofree = get_prevdir(scope);
6753
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006754 // Save current directory for next ":cd -"
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006755 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar002bc792020-06-05 22:33:42 +02006756 pdir = vim_strsave(NameBuff);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006757 else
Bram Moolenaar002bc792020-06-05 22:33:42 +02006758 pdir = NULL;
6759 if (scope == CDSCOPE_WINDOW)
6760 curwin->w_prevdir = pdir;
6761 else if (scope == CDSCOPE_TABPAGE)
6762 curtab->tp_prevdir = pdir;
6763 else
6764 prev_dir = pdir;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006765
6766#if defined(UNIX) || defined(VMS)
6767 // for UNIX ":cd" means: go to home directory
6768 if (*new_dir == NUL)
6769 {
6770 // use NameBuff for home directory name
6771# ifdef VMS
6772 char_u *p;
6773
6774 p = mch_getenv((char_u *)"SYS$LOGIN");
6775 if (p == NULL || *p == NUL) // empty is the same as not set
6776 NameBuff[0] = NUL;
6777 else
6778 vim_strncpy(NameBuff, p, MAXPATHL - 1);
6779# else
6780 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
6781# endif
6782 new_dir = NameBuff;
6783 }
6784#endif
Bram Moolenaar002bc792020-06-05 22:33:42 +02006785 dir_differs = new_dir == NULL || pdir == NULL
6786 || pathcmp((char *)pdir, (char *)new_dir, -1) != 0;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006787 if (new_dir == NULL || (dir_differs && vim_chdir(new_dir)))
6788 emsg(_(e_failed));
6789 else
6790 {
6791 char_u *acmd_fname;
6792
6793 post_chdir(scope);
6794
6795 if (dir_differs)
6796 {
6797 if (scope == CDSCOPE_WINDOW)
6798 acmd_fname = (char_u *)"window";
6799 else if (scope == CDSCOPE_TABPAGE)
6800 acmd_fname = (char_u *)"tabpage";
6801 else
6802 acmd_fname = (char_u *)"global";
6803 apply_autocmds(EVENT_DIRCHANGED, acmd_fname, new_dir, FALSE,
6804 curbuf);
6805 }
6806 retval = TRUE;
6807 }
6808 vim_free(tofree);
6809
6810 return retval;
6811}
Bram Moolenaarea408852005-06-25 22:49:46 +00006812
Bram Moolenaar071d4272004-06-13 20:20:40 +00006813/*
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006814 * ":cd", ":tcd", ":lcd", ":chdir" ":tchdir" and ":lchdir".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006815 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00006816 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006817ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006818{
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01006819 char_u *new_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006820
6821 new_dir = eap->arg;
6822#if !defined(UNIX) && !defined(VMS)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006823 // for non-UNIX ":cd" means: print current directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006824 if (*new_dir == NUL)
6825 ex_pwd(NULL);
6826 else
6827#endif
6828 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006829 cdscope_T scope = CDSCOPE_GLOBAL;
6830
6831 if (eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir)
6832 scope = CDSCOPE_WINDOW;
6833 else if (eap->cmdidx == CMD_tcd || eap->cmdidx == CMD_tchdir)
6834 scope = CDSCOPE_TABPAGE;
6835
6836 if (changedir_func(new_dir, eap->forceit, scope))
Bram Moolenaardf177f62005-02-22 08:39:57 +00006837 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006838 // Echo the new current directory if the command was typed.
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00006839 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006840 ex_pwd(eap);
6841 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006842 }
6843}
6844
6845/*
6846 * ":pwd".
6847 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006848 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006849ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006850{
6851 if (mch_dirname(NameBuff, MAXPATHL) == OK)
6852 {
6853#ifdef BACKSLASH_IN_FILENAME
6854 slash_adjust(NameBuff);
6855#endif
Bram Moolenaar95058722020-06-01 16:26:19 +02006856 if (p_verbose > 0)
6857 {
6858 char *context = "global";
6859
6860 if (curwin->w_localdir != NULL)
6861 context = "window";
6862 else if (curtab->tp_localdir != NULL)
6863 context = "tabpage";
6864 smsg("[%s] %s", context, (char *)NameBuff);
6865 }
6866 else
6867 msg((char *)NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006868 }
6869 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006870 emsg(_("E187: Unknown"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006871}
6872
6873/*
6874 * ":=".
6875 */
6876 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006877ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006878{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006879 smsg("%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006880 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006881}
6882
6883 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006884ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006885{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006886 int n;
6887 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006888
6889 if (cursor_valid())
6890 {
6891 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
6892 if (n >= 0)
Bram Moolenaar53f81742017-09-22 14:35:51 +02006893 windgoto((int)n, curwin->w_wincol + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006894 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006895
6896 len = eap->line2;
6897 switch (*eap->arg)
6898 {
6899 case 'm': break;
6900 case NUL: len *= 1000L; break;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006901 default: semsg(_(e_invarg2), eap->arg); return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006902 }
6903 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006904}
6905
6906/*
6907 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
6908 */
6909 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006910do_sleep(long msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006911{
Bram Moolenaar52953192019-08-16 10:27:13 +02006912 long done = 0;
Bram Moolenaar975b5272016-03-15 23:10:59 +01006913 long wait_now;
Bram Moolenaar52953192019-08-16 10:27:13 +02006914# ifdef ELAPSED_FUNC
6915 elapsed_T start_tv;
6916
6917 // Remember at what time we started, so that we know how much longer we
6918 // should wait after waiting for a bit.
6919 ELAPSED_INIT(start_tv);
6920# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006921
6922 cursor_on();
Bram Moolenaarf45d7472018-09-30 18:22:26 +02006923 out_flush_cursor(FALSE, FALSE);
Bram Moolenaar52953192019-08-16 10:27:13 +02006924 while (!got_int && done < msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006925 {
Bram Moolenaar975b5272016-03-15 23:10:59 +01006926 wait_now = msec - done > 1000L ? 1000L : msec - done;
6927#ifdef FEAT_TIMERS
6928 {
6929 long due_time = check_due_timer();
6930
6931 if (due_time > 0 && due_time < wait_now)
6932 wait_now = due_time;
6933 }
6934#endif
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02006935#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar28e67e02019-08-15 23:05:49 +02006936 if (has_any_channel() && wait_now > 20L)
6937 wait_now = 20L;
6938#endif
6939#ifdef FEAT_SOUND
6940 if (has_any_sound_callback() && wait_now > 20L)
6941 wait_now = 20L;
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02006942#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +01006943 ui_delay(wait_now, TRUE);
Bram Moolenaar52953192019-08-16 10:27:13 +02006944
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02006945#ifdef FEAT_JOB_CHANNEL
6946 if (has_any_channel())
6947 ui_breakcheck_force(TRUE);
6948 else
6949#endif
6950 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006951#ifdef MESSAGE_QUEUE
Bram Moolenaar52953192019-08-16 10:27:13 +02006952 // Process the netbeans and clientserver messages that may have been
6953 // received in the call to ui_breakcheck() when the GUI is in use. This
6954 // may occur when running a test case.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006955 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02006956#endif
Bram Moolenaar52953192019-08-16 10:27:13 +02006957
6958# ifdef ELAPSED_FUNC
6959 // actual time passed
6960 done = ELAPSED_FUNC(start_tv);
6961# else
6962 // guestimate time passed (will actually be more)
6963 done += wait_now;
6964# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006965 }
Bram Moolenaar1ebff3d2018-07-07 16:18:13 +02006966
6967 // If CTRL-C was typed to interrupt the sleep, drop the CTRL-C from the
6968 // input buffer, otherwise a following call to input() fails.
6969 if (got_int)
6970 (void)vpeekc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006971}
6972
Bram Moolenaar071d4272004-06-13 20:20:40 +00006973/*
6974 * ":winsize" command (obsolete).
6975 */
6976 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006977ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006978{
6979 int w, h;
6980 char_u *arg = eap->arg;
6981 char_u *p;
6982
6983 w = getdigits(&arg);
6984 arg = skipwhite(arg);
6985 p = arg;
6986 h = getdigits(&arg);
6987 if (*p != NUL && *arg == NUL)
6988 set_shellsize(w, h, TRUE);
6989 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006990 emsg(_("E465: :winsize requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006991}
6992
Bram Moolenaar071d4272004-06-13 20:20:40 +00006993 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006994ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006995{
6996 int xchar = NUL;
6997 char_u *p;
6998
6999 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
7000 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007001 // CTRL-W g and CTRL-W CTRL-G have an extra command character
Bram Moolenaar071d4272004-06-13 20:20:40 +00007002 if (eap->arg[1] == NUL)
7003 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007004 emsg(_(e_invarg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007005 return;
7006 }
7007 xchar = eap->arg[1];
7008 p = eap->arg + 2;
7009 }
7010 else
7011 p = eap->arg + 1;
7012
7013 eap->nextcmd = check_nextcmd(p);
7014 p = skipwhite(p);
7015 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007016 emsg(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02007017 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007018 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007019 // Pass flags on for ":vertical wincmd ]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007020 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00007021 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007022 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
7023 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00007024 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007025 }
7026}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007027
Bram Moolenaar843ee412004-06-30 16:16:41 +00007028#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007029/*
7030 * ":winpos".
7031 */
7032 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007033ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007034{
7035 int x, y;
7036 char_u *arg = eap->arg;
7037 char_u *p;
7038
7039 if (*arg == NUL)
7040 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00007041# if defined(FEAT_GUI) || defined(MSWIN)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007042# ifdef VIMDLL
7043 if (gui.in_use ? gui_mch_get_winpos(&x, &y) != FAIL :
7044 mch_get_winpos(&x, &y) != FAIL)
7045# elif defined(FEAT_GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007046 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00007047# else
7048 if (mch_get_winpos(&x, &y) != FAIL)
7049# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007050 {
7051 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
Bram Moolenaar32526b32019-01-19 17:43:09 +01007052 msg((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007053 }
7054 else
7055# endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007056 emsg(_("E188: Obtaining window position not implemented for this platform"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007057 }
7058 else
7059 {
7060 x = getdigits(&arg);
7061 arg = skipwhite(arg);
7062 p = arg;
7063 y = getdigits(&arg);
7064 if (*p == NUL || *arg != NUL)
7065 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007066 emsg(_("E466: :winpos requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007067 return;
7068 }
7069# ifdef FEAT_GUI
7070 if (gui.in_use)
7071 gui_mch_set_winpos(x, y);
7072 else if (gui.starting)
7073 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007074 // Remember the coordinates for when the window is opened.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007075 gui_win_x = x;
7076 gui_win_y = y;
7077 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007078# if defined(HAVE_TGETENT) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007079 else
7080# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007081# endif
7082# if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar843ee412004-06-30 16:16:41 +00007083 mch_set_winpos(x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007084# endif
7085# ifdef HAVE_TGETENT
7086 if (*T_CWP)
7087 term_set_winpos(x, y);
7088# endif
7089 }
7090}
7091#endif
7092
7093/*
7094 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
7095 */
7096 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007097ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007098{
7099 oparg_T oa;
7100
7101 clear_oparg(&oa);
7102 oa.regname = eap->regname;
7103 oa.start.lnum = eap->line1;
7104 oa.end.lnum = eap->line2;
7105 oa.line_count = eap->line2 - eap->line1 + 1;
7106 oa.motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007107 virtual_op = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007108 if (eap->cmdidx != CMD_yank) // position cursor for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00007109 {
7110 setpcmark();
7111 curwin->w_cursor.lnum = eap->line1;
7112 beginline(BL_SOL | BL_FIX);
7113 }
7114
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007115 if (VIsual_active)
7116 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007117
Bram Moolenaar071d4272004-06-13 20:20:40 +00007118 switch (eap->cmdidx)
7119 {
7120 case CMD_delete:
7121 oa.op_type = OP_DELETE;
7122 op_delete(&oa);
7123 break;
7124
7125 case CMD_yank:
7126 oa.op_type = OP_YANK;
7127 (void)op_yank(&oa, FALSE, TRUE);
7128 break;
7129
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007130 default: // CMD_rshift or CMD_lshift
Bram Moolenaar6e707362013-06-14 19:15:58 +02007131 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007132#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02007133 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
7134#else
7135 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00007136#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02007137 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007138 oa.op_type = OP_RSHIFT;
7139 else
7140 oa.op_type = OP_LSHIFT;
7141 op_shift(&oa, FALSE, eap->amount);
7142 break;
7143 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007144 virtual_op = MAYBE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007145 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007146}
7147
7148/*
7149 * ":put".
7150 */
7151 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007152ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007153{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007154 // ":0put" works like ":1put!".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007155 if (eap->line2 == 0)
7156 {
7157 eap->line2 = 1;
7158 eap->forceit = TRUE;
7159 }
7160 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007161 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
7162 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007163}
7164
7165/*
7166 * Handle ":copy" and ":move".
7167 */
7168 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007169ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007170{
7171 long n;
7172
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02007173 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE, FALSE, 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007174 if (eap->arg == NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00007175 {
7176 eap->nextcmd = NULL;
7177 return;
7178 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00007179 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007180
7181 /*
7182 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
7183 */
7184 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
7185 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02007186 emsg(_(e_invrange));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007187 return;
7188 }
7189
7190 if (eap->cmdidx == CMD_move)
7191 {
7192 if (do_move(eap->line1, eap->line2, n) == FAIL)
7193 return;
7194 }
7195 else
7196 ex_copy(eap->line1, eap->line2, n);
7197 u_clearline();
7198 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007199 ex_may_print(eap);
7200}
7201
7202/*
7203 * Print the current line if flags were given to the Ex command.
7204 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02007205 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007206ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007207{
7208 if (eap->flags != 0)
7209 {
7210 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
7211 (eap->flags & EXFLAG_LIST));
7212 ex_no_reprint = TRUE;
7213 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007214}
7215
7216/*
7217 * ":smagic" and ":snomagic".
7218 */
7219 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007220ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007221{
7222 int magic_save = p_magic;
7223
7224 p_magic = (eap->cmdidx == CMD_smagic);
7225 do_sub(eap);
7226 p_magic = magic_save;
7227}
7228
7229/*
7230 * ":join".
7231 */
7232 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007233ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007234{
7235 curwin->w_cursor.lnum = eap->line1;
7236 if (eap->line1 == eap->line2)
7237 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007238 if (eap->addr_count >= 2) // :2,2join does nothing
Bram Moolenaar071d4272004-06-13 20:20:40 +00007239 return;
7240 if (eap->line2 == curbuf->b_ml.ml_line_count)
7241 {
7242 beep_flush();
7243 return;
7244 }
7245 ++eap->line2;
7246 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02007247 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007248 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007249 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007250}
7251
7252/*
7253 * ":[addr]@r" or ":[addr]*r": execute register
7254 */
7255 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007256ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007257{
7258 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00007259 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007260
7261 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4930a762016-09-11 14:39:53 +02007262 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007263
7264#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007265 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00007266#endif
7267
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007268 // get the register name. No name means to use the previous one
Bram Moolenaar071d4272004-06-13 20:20:40 +00007269 c = *eap->arg;
7270 if (c == NUL || (c == '*' && *eap->cmd == '*'))
7271 c = '@';
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007272 // Put the register in the typeahead buffer with the "silent" flag.
Bram Moolenaard333d1e2006-11-07 17:43:47 +00007273 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
7274 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007275 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007276 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00007277 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007278 else
7279 {
7280 int save_efr = exec_from_reg;
7281
7282 exec_from_reg = TRUE;
7283
7284 /*
7285 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00007286 * Continue until the stuff buffer is empty and all added characters
7287 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007288 */
Bram Moolenaar60462872009-11-03 11:40:19 +00007289 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007290 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
7291
7292 exec_from_reg = save_efr;
7293 }
7294}
7295
7296/*
7297 * ":!".
7298 */
7299 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007300ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007301{
7302 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
7303}
7304
7305/*
7306 * ":undo".
7307 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007308 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01007309ex_undo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007310{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007311 if (eap->addr_count == 1) // :undo 123
Bram Moolenaar730cde92010-06-27 05:18:54 +02007312 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00007313 else
7314 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007315}
7316
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007317#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02007318 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007319ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007320{
7321 char_u hash[UNDO_HASH_SIZE];
7322
7323 u_compute_hash(hash);
7324 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
7325}
7326
Bram Moolenaar6df6f472010-07-18 18:04:50 +02007327 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007328ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007329{
7330 char_u hash[UNDO_HASH_SIZE];
7331
7332 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02007333 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007334}
7335#endif
7336
Bram Moolenaar071d4272004-06-13 20:20:40 +00007337/*
7338 * ":redo".
7339 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007340 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007341ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007342{
7343 u_redo(1);
7344}
7345
7346/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007347 * ":earlier" and ":later".
7348 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007349 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007350ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007351{
7352 long count = 0;
7353 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02007354 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007355 char_u *p = eap->arg;
7356
7357 if (*p == NUL)
7358 count = 1;
7359 else if (isdigit(*p))
7360 {
7361 count = getdigits(&p);
7362 switch (*p)
7363 {
7364 case 's': ++p; sec = TRUE; break;
7365 case 'm': ++p; sec = TRUE; count *= 60; break;
7366 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02007367 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
7368 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007369 }
7370 }
7371
7372 if (*p != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007373 semsg(_(e_invarg2), eap->arg);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007374 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02007375 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
7376 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007377}
7378
7379/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007380 * ":redir": start/stop redirection.
7381 */
7382 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007383ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007384{
7385 char *mode;
7386 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00007387 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007388
Bram Moolenaarba768492016-07-08 15:32:54 +02007389#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02007390 if (redir_execute)
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007391 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007392 emsg(_("E930: Cannot use :redir inside execute()"));
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007393 return;
7394 }
Bram Moolenaarba768492016-07-08 15:32:54 +02007395#endif
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007396
Bram Moolenaar071d4272004-06-13 20:20:40 +00007397 if (STRICMP(eap->arg, "END") == 0)
7398 close_redir();
7399 else
7400 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007401 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007402 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007403 ++arg;
7404 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007405 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007406 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007407 mode = "a";
7408 }
7409 else
7410 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00007411 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007412
7413 close_redir();
7414
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007415 // Expand environment variables and "~/".
Bram Moolenaarca472992005-01-21 11:46:23 +00007416 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007417 if (fname == NULL)
7418 return;
7419#ifdef FEAT_BROWSE
7420 if (cmdmod.browse)
7421 {
7422 char_u *browseFile;
7423
Bram Moolenaar7171abe2004-10-11 10:06:20 +00007424 browseFile = do_browse(BROWSE_SAVE,
7425 (char_u *)_("Save Redirection"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +02007426 fname, NULL, NULL,
7427 (char_u *)_(BROWSE_FILTER_ALL_FILES), curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007428 if (browseFile == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007429 return; // operation cancelled
Bram Moolenaar071d4272004-06-13 20:20:40 +00007430 vim_free(fname);
7431 fname = browseFile;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007432 eap->forceit = TRUE; // since dialog already asked
Bram Moolenaar071d4272004-06-13 20:20:40 +00007433 }
7434#endif
7435
7436 redir_fd = open_exfile(fname, eap->forceit, mode);
7437 vim_free(fname);
7438 }
7439#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00007440 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007441 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007442 // redirect to a register a-z (resp. A-Z for appending)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007443 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00007444 ++arg;
7445 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007446# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00007447 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00007448 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00007449# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00007450 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007451 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007452 redir_reg = *arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007453 if (*arg == '>' && arg[1] == '>') // append
Bram Moolenaard9d30582005-05-18 22:10:28 +00007454 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00007455 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007456 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007457 // Can use both "@a" and "@a>".
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00007458 if (*arg == '>')
7459 arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007460 // Make register empty when not using @A-@Z and the
7461 // command is valid.
Bram Moolenaarc188b882007-10-19 14:20:54 +00007462 if (*arg == NUL && !isupper(redir_reg))
7463 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007464 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00007465 }
7466 if (*arg != NUL)
7467 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007468 redir_reg = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007469 semsg(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007470 }
7471 }
7472 else if (*arg == '=' && arg[1] == '>')
7473 {
7474 int append;
7475
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007476 // redirect to a variable
Bram Moolenaardf177f62005-02-22 08:39:57 +00007477 close_redir();
7478 arg += 2;
7479
7480 if (*arg == '>')
7481 {
7482 ++arg;
7483 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007484 }
7485 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00007486 append = FALSE;
7487
7488 if (var_redir_start(skipwhite(arg), append) == OK)
7489 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007490 }
7491#endif
7492
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007493 // TODO: redirect to a buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00007494
Bram Moolenaar071d4272004-06-13 20:20:40 +00007495 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007496 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007497 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00007498
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007499 // Make sure redirection is not off. Can happen for cmdline completion
7500 // that indirectly invokes a command to catch its output.
Bram Moolenaar29b2d262006-09-10 19:07:28 +00007501 if (redir_fd != NULL
7502#ifdef FEAT_EVAL
7503 || redir_reg || redir_vname
7504#endif
7505 )
7506 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007507}
7508
7509/*
7510 * ":redraw": force redraw
7511 */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01007512 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007513ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007514{
7515 int r = RedrawingDisabled;
7516 int p = p_lz;
7517
7518 RedrawingDisabled = 0;
7519 p_lz = FALSE;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01007520 validate_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007521 update_topline();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007522 update_screen(eap->forceit ? CLEAR : VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007523#ifdef FEAT_TITLE
7524 if (need_maketitle)
7525 maketitle();
7526#endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007527#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
7528# ifdef VIMDLL
7529 if (!gui.in_use)
7530# endif
7531 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01007532#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007533 RedrawingDisabled = r;
7534 p_lz = p;
7535
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007536 // Reset msg_didout, so that a message that's there is overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007537 msg_didout = FALSE;
7538 msg_col = 0;
7539
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007540 // No need to wait after an intentional redraw.
Bram Moolenaar56667a52013-07-17 11:54:28 +02007541 need_wait_return = FALSE;
7542
Bram Moolenaar071d4272004-06-13 20:20:40 +00007543 out_flush();
7544}
7545
7546/*
7547 * ":redrawstatus": force redraw of status line(s)
7548 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007549 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007550ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007551{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007552 int r = RedrawingDisabled;
7553 int p = p_lz;
7554
7555 RedrawingDisabled = 0;
7556 p_lz = FALSE;
7557 if (eap->forceit)
7558 status_redraw_all();
7559 else
7560 status_redraw_curbuf();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007561 update_screen(VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007562 RedrawingDisabled = r;
7563 p_lz = p;
7564 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007565}
7566
Bram Moolenaare12bab32019-01-08 22:02:56 +01007567/*
7568 * ":redrawtabline": force redraw of the tabline
7569 */
7570 static void
7571ex_redrawtabline(exarg_T *eap UNUSED)
7572{
7573 int r = RedrawingDisabled;
7574 int p = p_lz;
7575
7576 RedrawingDisabled = 0;
7577 p_lz = FALSE;
7578
7579 draw_tabline();
7580
7581 RedrawingDisabled = r;
7582 p_lz = p;
7583 out_flush();
7584}
7585
Bram Moolenaar071d4272004-06-13 20:20:40 +00007586 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007587close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007588{
7589 if (redir_fd != NULL)
7590 {
7591 fclose(redir_fd);
7592 redir_fd = NULL;
7593 }
7594#ifdef FEAT_EVAL
7595 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007596 if (redir_vname)
7597 {
7598 var_redir_stop();
7599 redir_vname = 0;
7600 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007601#endif
7602}
7603
Bram Moolenaarf96ae0b2019-07-28 15:21:55 +02007604#if (defined(FEAT_SESSION) || defined(FEAT_EVAL)) || defined(PROTO)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007605 int
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02007606vim_mkdir_emsg(char_u *name, int prot UNUSED)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007607{
7608 if (vim_mkdir(name, prot) != 0)
7609 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007610 semsg(_("E739: Cannot create directory: %s"), name);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007611 return FAIL;
7612 }
7613 return OK;
7614}
7615#endif
7616
Bram Moolenaar071d4272004-06-13 20:20:40 +00007617/*
7618 * Open a file for writing for an Ex command, with some checks.
7619 * Return file descriptor, or NULL on failure.
7620 */
7621 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007622open_exfile(
7623 char_u *fname,
7624 int forceit,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007625 char *mode) // "w" for create new file or "a" for append
Bram Moolenaar071d4272004-06-13 20:20:40 +00007626{
7627 FILE *fd;
7628
7629#ifdef UNIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007630 // with Unix it is possible to open a directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007631 if (mch_isdir(fname))
7632 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007633 semsg(_(e_isadir2), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007634 return NULL;
7635 }
7636#endif
7637 if (!forceit && *mode != 'a' && vim_fexists(fname))
7638 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007639 semsg(_("E189: \"%s\" exists (add ! to override)"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007640 return NULL;
7641 }
7642
7643 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007644 semsg(_("E190: Cannot open \"%s\" for writing"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007645
7646 return fd;
7647}
7648
7649/*
7650 * ":mark" and ":k".
7651 */
7652 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007653ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007654{
7655 pos_T pos;
7656
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007657 if (*eap->arg == NUL) // No argument?
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007658 emsg(_(e_argreq));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007659 else if (eap->arg[1] != NUL) // more than one character?
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007660 emsg(_(e_trailing));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007661 else
7662 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007663 pos = curwin->w_cursor; // save curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00007664 curwin->w_cursor.lnum = eap->line2;
7665 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007666 if (setmark(*eap->arg) == FAIL) // set mark
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007667 emsg(_("E191: Argument must be a letter or forward/backward quote"));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007668 curwin->w_cursor = pos; // restore curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00007669 }
7670}
7671
7672/*
7673 * Update w_topline, w_leftcol and the cursor position.
7674 */
7675 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007676update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007677{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007678 check_cursor(); // put cursor on valid line
Bram Moolenaar071d4272004-06-13 20:20:40 +00007679 update_topline();
7680 if (!curwin->w_p_wrap)
7681 validate_cursor();
7682 update_curswant();
7683}
7684
Bram Moolenaar071d4272004-06-13 20:20:40 +00007685/*
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007686 * Save the current State and go to Normal mode.
7687 * Return TRUE if the typeahead could be saved.
7688 */
7689 int
7690save_current_state(save_state_T *sst)
7691{
7692 sst->save_msg_scroll = msg_scroll;
7693 sst->save_restart_edit = restart_edit;
7694 sst->save_msg_didout = msg_didout;
7695 sst->save_State = State;
7696 sst->save_insertmode = p_im;
7697 sst->save_finish_op = finish_op;
7698 sst->save_opcount = opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01007699 sst->save_reg_executing = reg_executing;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007700
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007701 msg_scroll = FALSE; // no msg scrolling in Normal mode
7702 restart_edit = 0; // don't go to Insert mode
7703 p_im = FALSE; // don't use 'insertmode'
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007704
7705 /*
7706 * Save the current typeahead. This is required to allow using ":normal"
7707 * from an event handler and makes sure we don't hang when the argument
7708 * ends with half a command.
7709 */
7710 save_typeahead(&sst->tabuf);
7711 return sst->tabuf.typebuf_valid;
7712}
7713
7714 void
7715restore_current_state(save_state_T *sst)
7716{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007717 // Restore the previous typeahead.
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007718 restore_typeahead(&sst->tabuf);
7719
7720 msg_scroll = sst->save_msg_scroll;
7721 restart_edit = sst->save_restart_edit;
7722 p_im = sst->save_insertmode;
7723 finish_op = sst->save_finish_op;
7724 opcount = sst->save_opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01007725 reg_executing = sst->save_reg_executing;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007726 msg_didout |= sst->save_msg_didout; // don't reset msg_didout now
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007727
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007728 // Restore the state (needed when called from a function executed for
7729 // 'indentexpr'). Update the mouse and cursor, they may have changed.
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007730 State = sst->save_State;
7731#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007732 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007733#endif
7734}
7735
7736/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007737 * ":normal[!] {commands}": Execute normal mode commands.
7738 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01007739 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007740ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007741{
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007742 save_state_T save_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007743 char_u *arg = NULL;
7744 int l;
7745 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007746
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00007747 if (ex_normal_lock > 0)
7748 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007749 emsg(_(e_secure));
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00007750 return;
7751 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007752 if (ex_normal_busy >= p_mmd)
7753 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007754 emsg(_("E192: Recursive use of :normal too deep"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007755 return;
7756 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007757
Bram Moolenaar071d4272004-06-13 20:20:40 +00007758 /*
7759 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
7760 * this for the K_SPECIAL leading byte, otherwise special keys will not
7761 * work.
7762 */
7763 if (has_mbyte)
7764 {
7765 int len = 0;
7766
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007767 // Count the number of characters to be escaped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007768 for (p = eap->arg; *p != NUL; ++p)
7769 {
Bram Moolenaar13505972019-01-24 15:04:48 +01007770#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007771 if (*p == CSI) // leadbyte CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007772 len += 2;
Bram Moolenaar13505972019-01-24 15:04:48 +01007773#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007774 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007775 if (*++p == K_SPECIAL // trailbyte K_SPECIAL or CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01007776#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007777 || *p == CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01007778#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007779 )
7780 len += 2;
7781 }
7782 if (len > 0)
7783 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02007784 arg = alloc(STRLEN(eap->arg) + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007785 if (arg != NULL)
7786 {
7787 len = 0;
7788 for (p = eap->arg; *p != NUL; ++p)
7789 {
7790 arg[len++] = *p;
Bram Moolenaar13505972019-01-24 15:04:48 +01007791#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007792 if (*p == CSI)
7793 {
7794 arg[len++] = KS_EXTRA;
7795 arg[len++] = (int)KE_CSI;
7796 }
Bram Moolenaar13505972019-01-24 15:04:48 +01007797#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007798 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007799 {
7800 arg[len++] = *++p;
7801 if (*p == K_SPECIAL)
7802 {
7803 arg[len++] = KS_SPECIAL;
7804 arg[len++] = KE_FILLER;
7805 }
Bram Moolenaar13505972019-01-24 15:04:48 +01007806#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007807 else if (*p == CSI)
7808 {
7809 arg[len++] = KS_EXTRA;
7810 arg[len++] = (int)KE_CSI;
7811 }
Bram Moolenaar13505972019-01-24 15:04:48 +01007812#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007813 }
7814 arg[len] = NUL;
7815 }
7816 }
7817 }
7818 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007819
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007820 ++ex_normal_busy;
7821 if (save_current_state(&save_state))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007822 {
7823 /*
7824 * Repeat the :normal command for each line in the range. When no
7825 * range given, execute it just once, without positioning the cursor
7826 * first.
7827 */
7828 do
7829 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007830 if (eap->addr_count != 0)
7831 {
7832 curwin->w_cursor.lnum = eap->line1++;
7833 curwin->w_cursor.col = 0;
Bram Moolenaard5d37532017-03-27 23:02:07 +02007834 check_cursor_moved(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007835 }
7836
Bram Moolenaar13505972019-01-24 15:04:48 +01007837 exec_normal_cmd(arg != NULL
7838 ? arg
7839 : eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007840 }
7841 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
7842 }
7843
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007844 // Might not return to the main loop when in an event handler.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007845 update_topline_cursor();
7846
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007847 restore_current_state(&save_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007848 --ex_normal_busy;
Bram Moolenaareda73602014-11-05 09:53:23 +01007849 setmouse();
Bram Moolenaareda73602014-11-05 09:53:23 +01007850#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007851 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaareda73602014-11-05 09:53:23 +01007852#endif
7853
Bram Moolenaar071d4272004-06-13 20:20:40 +00007854 vim_free(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007855}
7856
7857/*
Bram Moolenaar64969662005-12-14 21:59:55 +00007858 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007859 */
7860 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007861ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007862{
Bram Moolenaarfd371682005-01-14 21:42:54 +00007863 if (eap->forceit)
7864 {
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007865 // cursor line can be zero on startup
Bram Moolenaar09ca9322017-09-26 17:40:45 +02007866 if (!curwin->w_cursor.lnum)
7867 curwin->w_cursor.lnum = 1;
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007868 set_cursor_for_append_to_line();
Bram Moolenaarfd371682005-01-14 21:42:54 +00007869 }
Bram Moolenaareb58a242020-04-19 18:13:19 +02007870#ifdef FEAT_TERMINAL
7871 // Ignore this when running in an active terminal.
7872 if (term_job_running(curbuf->b_term))
7873 return;
7874#endif
Bram Moolenaarfd371682005-01-14 21:42:54 +00007875
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007876 // Ignore the command when already in Insert mode. Inserting an
7877 // expression register that invokes a function can do this.
Bram Moolenaara40c5002005-01-09 21:16:21 +00007878 if (State & INSERT)
7879 return;
7880
Bram Moolenaar64969662005-12-14 21:59:55 +00007881 if (eap->cmdidx == CMD_startinsert)
7882 restart_edit = 'a';
7883 else if (eap->cmdidx == CMD_startreplace)
7884 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00007885 else
Bram Moolenaar64969662005-12-14 21:59:55 +00007886 restart_edit = 'V';
7887
7888 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007889 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00007890 if (eap->cmdidx == CMD_startinsert)
7891 restart_edit = 'i';
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007892 curwin->w_curswant = 0; // avoid MAXCOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007893 }
7894}
7895
7896/*
7897 * ":stopinsert"
7898 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007899 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007900ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007901{
7902 restart_edit = 0;
7903 stop_insert_mode = TRUE;
Bram Moolenaarfd773e92016-04-02 19:39:16 +02007904 clearmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007905}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007906
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007907/*
7908 * Execute normal mode command "cmd".
7909 * "remap" can be REMAP_NONE or REMAP_YES.
7910 */
7911 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007912exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007913{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007914 // Stuff the argument into the typeahead buffer.
Bram Moolenaar25281632016-01-21 23:32:32 +01007915 ins_typebuf(cmd, remap, 0, TRUE, silent);
Bram Moolenaar586c70c2018-10-02 16:23:58 +02007916 exec_normal(FALSE, FALSE, FALSE);
Bram Moolenaar25281632016-01-21 23:32:32 +01007917}
Bram Moolenaar25281632016-01-21 23:32:32 +01007918
Bram Moolenaar25281632016-01-21 23:32:32 +01007919/*
7920 * Execute normal_cmd() until there is no typeahead left.
Bram Moolenaar586c70c2018-10-02 16:23:58 +02007921 * When "use_vpeekc" is TRUE use vpeekc() to check for available chars.
Bram Moolenaar25281632016-01-21 23:32:32 +01007922 */
7923 void
Bram Moolenaar586c70c2018-10-02 16:23:58 +02007924exec_normal(int was_typed, int use_vpeekc, int may_use_terminal_loop UNUSED)
Bram Moolenaar25281632016-01-21 23:32:32 +01007925{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007926 oparg_T oa;
Bram Moolenaar905dd902019-04-07 14:21:47 +02007927 int c;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007928
Bram Moolenaar905dd902019-04-07 14:21:47 +02007929 // When calling vpeekc() from feedkeys() it will return Ctrl_C when there
7930 // is nothing to get, so also check for Ctrl_C.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007931 clear_oparg(&oa);
7932 finish_op = FALSE;
Bram Moolenaar586c70c2018-10-02 16:23:58 +02007933 while ((!stuff_empty()
7934 || ((was_typed || !typebuf_typed()) && typebuf.tb_len > 0)
Bram Moolenaar905dd902019-04-07 14:21:47 +02007935 || (use_vpeekc && (c = vpeekc()) != NUL && c != Ctrl_C))
Bram Moolenaar586c70c2018-10-02 16:23:58 +02007936 && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007937 {
7938 update_topline_cursor();
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02007939#ifdef FEAT_TERMINAL
Bram Moolenaar655a82a2018-05-08 22:01:07 +02007940 if (may_use_terminal_loop && term_use_loop()
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02007941 && oa.op_type == OP_NOP && oa.regname == NUL
7942 && !VIsual_active)
7943 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007944 // If terminal_loop() returns OK we got a key that is handled
7945 // in Normal model. With FAIL we first need to position the
7946 // cursor and the screen needs to be redrawn.
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02007947 if (terminal_loop(TRUE) == OK)
7948 normal_cmd(&oa, TRUE);
7949 }
7950 else
7951#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007952 // execute a Normal mode cmd
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02007953 normal_cmd(&oa, TRUE);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007954 }
7955}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007956
Bram Moolenaar071d4272004-06-13 20:20:40 +00007957#ifdef FEAT_FIND_ID
7958 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007959ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007960{
7961 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
7962 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
7963 (linenr_T)1, (linenr_T)MAXLNUM);
7964}
7965
Bram Moolenaar4033c552017-09-16 20:54:51 +02007966#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007967/*
7968 * ":psearch"
7969 */
7970 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007971ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007972{
7973 g_do_tagpreview = p_pvh;
7974 ex_findpat(eap);
7975 g_do_tagpreview = 0;
7976}
7977#endif
7978
7979 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007980ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007981{
7982 int whole = TRUE;
7983 long n;
7984 char_u *p;
7985 int action;
7986
7987 switch (cmdnames[eap->cmdidx].cmd_name[2])
7988 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007989 case 'e': // ":psearch", ":isearch" and ":dsearch"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007990 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
7991 action = ACTION_GOTO;
7992 else
7993 action = ACTION_SHOW;
7994 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007995 case 'i': // ":ilist" and ":dlist"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007996 action = ACTION_SHOW_ALL;
7997 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007998 case 'u': // ":ijump" and ":djump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007999 action = ACTION_GOTO;
8000 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008001 default: // ":isplit" and ":dsplit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008002 action = ACTION_SPLIT;
8003 break;
8004 }
8005
8006 n = 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008007 if (vim_isdigit(*eap->arg)) // get count
Bram Moolenaar071d4272004-06-13 20:20:40 +00008008 {
8009 n = getdigits(&eap->arg);
8010 eap->arg = skipwhite(eap->arg);
8011 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008012 if (*eap->arg == '/') // Match regexp, not just whole words
Bram Moolenaar071d4272004-06-13 20:20:40 +00008013 {
8014 whole = FALSE;
8015 ++eap->arg;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02008016 p = skip_regexp(eap->arg, '/', p_magic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008017 if (*p)
8018 {
8019 *p++ = NUL;
8020 p = skipwhite(p);
8021
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008022 // Check for trailing illegal characters
Bram Moolenaarfaac4102020-04-20 17:46:14 +02008023 if (!ends_excmd2(eap->arg, p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008024 eap->errmsg = e_trailing;
8025 else
8026 eap->nextcmd = check_nextcmd(p);
8027 }
8028 }
8029 if (!eap->skip)
8030 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
8031 whole, !eap->forceit,
8032 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
8033 n, action, eap->line1, eap->line2);
8034}
8035#endif
8036
Bram Moolenaar071d4272004-06-13 20:20:40 +00008037
Bram Moolenaar4033c552017-09-16 20:54:51 +02008038#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00008039/*
8040 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
8041 */
8042 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008043ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008044{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008045 g_do_tagpreview = p_pvh; // will be reset to 0 in ex_tag_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00008046 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
8047}
8048
8049/*
8050 * ":pedit"
8051 */
8052 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008053ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008054{
8055 win_T *curwin_save = curwin;
8056
Bram Moolenaar3a2505c2020-03-09 16:40:41 +01008057 if (ERROR_IF_ANY_POPUP_WINDOW)
8058 return;
8059
Bram Moolenaar026587b2019-08-17 15:08:00 +02008060 // Open the preview window or popup and make it the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008061 g_do_tagpreview = p_pvh;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02008062 prepare_tagpreview(TRUE, TRUE, FALSE);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008063
Bram Moolenaar026587b2019-08-17 15:08:00 +02008064 // Edit the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008065 do_exedit(eap, NULL);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008066
Bram Moolenaar071d4272004-06-13 20:20:40 +00008067 if (curwin != curwin_save && win_valid(curwin_save))
8068 {
Bram Moolenaar026587b2019-08-17 15:08:00 +02008069 // Return cursor to where we were
Bram Moolenaar071d4272004-06-13 20:20:40 +00008070 validate_cursor();
8071 redraw_later(VALID);
8072 win_enter(curwin_save, TRUE);
8073 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01008074# ifdef FEAT_PROP_POPUP
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008075 else if (WIN_IS_POPUP(curwin))
8076 {
8077 // can't keep focus in popup window
8078 win_enter(firstwin, TRUE);
8079 }
8080# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008081 g_do_tagpreview = 0;
8082}
Bram Moolenaar4033c552017-09-16 20:54:51 +02008083#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008084
8085/*
8086 * ":stag", ":stselect" and ":stjump".
8087 */
8088 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008089ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008090{
8091 postponed_split = -1;
8092 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008093 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008094 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
8095 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008096 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008097}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008098
8099/*
8100 * ":tag", ":tselect", ":tjump", ":tnext", etc.
8101 */
8102 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008103ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008104{
8105 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
8106}
8107
8108 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008109ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008110{
8111 int cmd;
8112
8113 switch (name[1])
8114 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008115 case 'j': cmd = DT_JUMP; // ":tjump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008116 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008117 case 's': cmd = DT_SELECT; // ":tselect"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008118 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008119 case 'p': cmd = DT_PREV; // ":tprevious"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008120 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008121 case 'N': cmd = DT_PREV; // ":tNext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008122 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008123 case 'n': cmd = DT_NEXT; // ":tnext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008124 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008125 case 'o': cmd = DT_POP; // ":pop"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008126 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008127 case 'f': // ":tfirst"
8128 case 'r': cmd = DT_FIRST; // ":trewind"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008129 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008130 case 'l': cmd = DT_LAST; // ":tlast"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008131 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008132 default: // ":tag"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008133#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +00008134 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008135 {
Bram Moolenaard4db7712016-11-12 19:16:46 +01008136 ex_cstag(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008137 return;
8138 }
8139#endif
8140 cmd = DT_TAG;
8141 break;
8142 }
8143
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00008144 if (name[0] == 'l')
8145 {
8146#ifndef FEAT_QUICKFIX
8147 ex_ni(eap);
8148 return;
8149#else
8150 cmd = DT_LTAG;
8151#endif
8152 }
8153
Bram Moolenaar071d4272004-06-13 20:20:40 +00008154 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
8155 eap->forceit, TRUE);
8156}
8157
8158/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008159 * Check "str" for starting with a special cmdline variable.
8160 * If found return one of the SPEC_ values and set "*usedlen" to the length of
8161 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
8162 */
8163 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008164find_cmdline_var(char_u *src, int *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008165{
8166 int len;
8167 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +00008168 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008169 "%",
8170#define SPEC_PERC 0
8171 "#",
Bram Moolenaar65f08472017-09-10 18:16:20 +02008172#define SPEC_HASH (SPEC_PERC + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008173 "<cword>", // cursor word
Bram Moolenaar65f08472017-09-10 18:16:20 +02008174#define SPEC_CWORD (SPEC_HASH + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008175 "<cWORD>", // cursor WORD
Bram Moolenaar65f08472017-09-10 18:16:20 +02008176#define SPEC_CCWORD (SPEC_CWORD + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008177 "<cexpr>", // expr under cursor
Bram Moolenaar65f08472017-09-10 18:16:20 +02008178#define SPEC_CEXPR (SPEC_CCWORD + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008179 "<cfile>", // cursor path name
Bram Moolenaar65f08472017-09-10 18:16:20 +02008180#define SPEC_CFILE (SPEC_CEXPR + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008181 "<sfile>", // ":so" file name
Bram Moolenaar65f08472017-09-10 18:16:20 +02008182#define SPEC_SFILE (SPEC_CFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008183 "<slnum>", // ":so" file line number
Bram Moolenaar65f08472017-09-10 18:16:20 +02008184#define SPEC_SLNUM (SPEC_SFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008185 "<afile>", // autocommand file name
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008186#define SPEC_AFILE (SPEC_SLNUM + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008187 "<abuf>", // autocommand buffer number
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008188#define SPEC_ABUF (SPEC_AFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008189 "<amatch>", // autocommand match name
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008190#define SPEC_AMATCH (SPEC_ABUF + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008191 "<sflnum>", // script file line number
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008192#define SPEC_SFLNUM (SPEC_AMATCH + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008193#ifdef FEAT_CLIENTSERVER
8194 "<client>"
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008195# define SPEC_CLIENT (SPEC_SFLNUM + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008196#endif
8197 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008198
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008199 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008200 {
8201 len = (int)STRLEN(spec_str[i]);
8202 if (STRNCMP(src, spec_str[i], len) == 0)
8203 {
8204 *usedlen = len;
8205 return i;
8206 }
8207 }
8208 return -1;
8209}
8210
8211/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008212 * Evaluate cmdline variables.
8213 *
8214 * change '%' to curbuf->b_ffname
8215 * '#' to curwin->w_altfile
8216 * '<cword>' to word under the cursor
8217 * '<cWORD>' to WORD under the cursor
Bram Moolenaar8071cb22019-07-12 17:58:01 +02008218 * '<cexpr>' to C-expression under the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00008219 * '<cfile>' to path name under the cursor
8220 * '<sfile>' to sourced file name
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008221 * '<slnum>' to sourced file line number
Bram Moolenaar071d4272004-06-13 20:20:40 +00008222 * '<afile>' to file name for autocommand
8223 * '<abuf>' to buffer number for autocommand
8224 * '<amatch>' to matching name for autocommand
8225 *
8226 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
8227 * "" for error without a message) and NULL is returned.
8228 * Returns an allocated string if a valid match was found.
8229 * Returns NULL if no match was found. "usedlen" then still contains the
8230 * number of characters to skip.
8231 */
8232 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008233eval_vars(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008234 char_u *src, // pointer into commandline
8235 char_u *srcstart, // beginning of valid memory for src
8236 int *usedlen, // characters after src that are used
8237 linenr_T *lnump, // line number for :e command, or NULL
8238 char **errormsg, // pointer to error message
8239 int *escaped) // return value has escaped white space (can
8240 // be NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008241{
8242 int i;
8243 char_u *s;
8244 char_u *result;
8245 char_u *resultbuf = NULL;
8246 int resultlen;
8247 buf_T *buf;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008248 int valid = VALID_HEAD + VALID_PATH; // assume valid result
Bram Moolenaar071d4272004-06-13 20:20:40 +00008249 int spec_idx;
Bram Moolenaarfd249462018-07-28 16:14:30 +02008250 int tilde_file = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008251 int skip_mod = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008252 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008253
8254 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +00008255 if (escaped != NULL)
8256 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008257
8258 /*
8259 * Check if there is something to do.
8260 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008261 spec_idx = find_cmdline_var(src, usedlen);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008262 if (spec_idx < 0) // no match
Bram Moolenaar071d4272004-06-13 20:20:40 +00008263 {
8264 *usedlen = 1;
8265 return NULL;
8266 }
8267
8268 /*
8269 * Skip when preceded with a backslash "\%" and "\#".
8270 * Note: In "\\%" the % is also not recognized!
8271 */
8272 if (src > srcstart && src[-1] == '\\')
8273 {
8274 *usedlen = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008275 STRMOVE(src - 1, src); // remove backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00008276 return NULL;
8277 }
8278
8279 /*
8280 * word or WORD under cursor
8281 */
Bram Moolenaar65f08472017-09-10 18:16:20 +02008282 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD
8283 || spec_idx == SPEC_CEXPR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008284 {
Bram Moolenaar65f08472017-09-10 18:16:20 +02008285 resultlen = find_ident_under_cursor(&result,
8286 spec_idx == SPEC_CWORD ? (FIND_IDENT | FIND_STRING)
8287 : spec_idx == SPEC_CEXPR ? (FIND_IDENT | FIND_STRING | FIND_EVAL)
8288 : FIND_STRING);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008289 if (resultlen == 0)
8290 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008291 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008292 return NULL;
8293 }
8294 }
8295
8296 /*
8297 * '#': Alternate file name
8298 * '%': Current file name
8299 * File name under the cursor
8300 * File name for autocommand
8301 * and following modifiers
8302 */
8303 else
8304 {
8305 switch (spec_idx)
8306 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008307 case SPEC_PERC: // '%': current file
Bram Moolenaar071d4272004-06-13 20:20:40 +00008308 if (curbuf->b_fname == NULL)
8309 {
8310 result = (char_u *)"";
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008311 valid = 0; // Must have ":p:h" to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00008312 }
8313 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008314 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008315 result = curbuf->b_fname;
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008316 tilde_file = STRCMP(result, "~") == 0;
8317 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008318 break;
8319
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008320 case SPEC_HASH: // '#' or "#99": alternate file
8321 if (src[1] == '#') // "##": the argument list
Bram Moolenaar071d4272004-06-13 20:20:40 +00008322 {
8323 result = arg_all();
8324 resultbuf = result;
8325 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +00008326 if (escaped != NULL)
8327 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008328 skip_mod = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008329 break;
8330 }
8331 s = src + 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008332 if (*s == '<') // "#<99" uses v:oldfiles
Bram Moolenaard812df62008-11-09 12:46:09 +00008333 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008334 i = (int)getdigits(&s);
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008335 if (s == src + 2 && src[1] == '-')
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008336 // just a minus sign, don't skip over it
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008337 s--;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008338 *usedlen = (int)(s - src); // length of what we expand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008339
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008340 if (src[1] == '<' && i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008341 {
Bram Moolenaard812df62008-11-09 12:46:09 +00008342 if (*usedlen < 2)
8343 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008344 // Should we give an error message for #<text?
Bram Moolenaard812df62008-11-09 12:46:09 +00008345 *usedlen = 1;
8346 return NULL;
8347 }
8348#ifdef FEAT_EVAL
8349 result = list_find_str(get_vim_var_list(VV_OLDFILES),
8350 (long)i);
8351 if (result == NULL)
8352 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008353 *errormsg = "";
Bram Moolenaard812df62008-11-09 12:46:09 +00008354 return NULL;
8355 }
8356#else
Bram Moolenaar8e481e82019-01-15 20:07:48 +01008357 *errormsg = _("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008358 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00008359#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008360 }
8361 else
Bram Moolenaard812df62008-11-09 12:46:09 +00008362 {
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008363 if (i == 0 && src[1] == '<' && *usedlen > 1)
8364 *usedlen = 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00008365 buf = buflist_findnr(i);
8366 if (buf == NULL)
8367 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008368 *errormsg = _("E194: No alternate file name to substitute for '#'");
Bram Moolenaard812df62008-11-09 12:46:09 +00008369 return NULL;
8370 }
8371 if (lnump != NULL)
8372 *lnump = ECMD_LAST;
8373 if (buf->b_fname == NULL)
8374 {
8375 result = (char_u *)"";
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008376 valid = 0; // Must have ":p:h" to be valid
Bram Moolenaard812df62008-11-09 12:46:09 +00008377 }
8378 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008379 {
Bram Moolenaard812df62008-11-09 12:46:09 +00008380 result = buf->b_fname;
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008381 tilde_file = STRCMP(result, "~") == 0;
8382 }
Bram Moolenaard812df62008-11-09 12:46:09 +00008383 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008384 break;
8385
8386#ifdef FEAT_SEARCHPATH
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008387 case SPEC_CFILE: // file name under cursor
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008388 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008389 if (result == NULL)
8390 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008391 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008392 return NULL;
8393 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008394 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00008395 break;
8396#endif
8397
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008398 case SPEC_AFILE: // file name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008399 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +00008400 if (result != NULL && !autocmd_fname_full)
8401 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008402 // Still need to turn the fname into a full path. It is
8403 // postponed to avoid a delay when <afile> is not used.
Bram Moolenaarf6dad432008-09-18 19:29:58 +00008404 autocmd_fname_full = TRUE;
8405 result = FullName_save(autocmd_fname, FALSE);
8406 vim_free(autocmd_fname);
8407 autocmd_fname = result;
8408 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008409 if (result == NULL)
8410 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008411 *errormsg = _("E495: no autocommand file name to substitute for \"<afile>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008412 return NULL;
8413 }
Bram Moolenaara0174af2008-01-02 20:08:25 +00008414 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008415 break;
8416
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008417 case SPEC_ABUF: // buffer number for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008418 if (autocmd_bufnr <= 0)
8419 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008420 *errormsg = _("E496: no autocommand buffer number to substitute for \"<abuf>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008421 return NULL;
8422 }
8423 sprintf((char *)strbuf, "%d", autocmd_bufnr);
8424 result = strbuf;
8425 break;
8426
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008427 case SPEC_AMATCH: // match name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008428 result = autocmd_match;
8429 if (result == NULL)
8430 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008431 *errormsg = _("E497: no autocommand match name to substitute for \"<amatch>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008432 return NULL;
8433 }
8434 break;
8435
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008436 case SPEC_SFILE: // file name for ":so" command
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008437 result = estack_sfile();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008438 if (result == NULL)
8439 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008440 *errormsg = _("E498: no :source file name to substitute for \"<sfile>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008441 return NULL;
8442 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008443 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00008444 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008445
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008446 case SPEC_SLNUM: // line in file for ":so" command
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008447 if (SOURCING_NAME == NULL || SOURCING_LNUM == 0)
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008448 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008449 *errormsg = _("E842: no line number to use for \"<slnum>\"");
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008450 return NULL;
8451 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008452 sprintf((char *)strbuf, "%ld", SOURCING_LNUM);
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008453 result = strbuf;
8454 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008455
8456#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008457 case SPEC_SFLNUM: // line in script file
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008458 if (current_sctx.sc_lnum + SOURCING_LNUM == 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008459 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008460 *errormsg = _("E961: no line number to use for \"<sflnum>\"");
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008461 return NULL;
8462 }
8463 sprintf((char *)strbuf, "%ld",
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008464 (long)(current_sctx.sc_lnum + SOURCING_LNUM));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008465 result = strbuf;
8466 break;
8467#endif
8468
8469#ifdef FEAT_CLIENTSERVER
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008470 case SPEC_CLIENT: // Source of last submitted input
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008471 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
8472 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008473 result = strbuf;
8474 break;
8475#endif
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008476
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02008477 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008478 result = (char_u *)""; // avoid gcc warning
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02008479 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008480 }
8481
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008482 resultlen = (int)STRLEN(result); // length of new string
8483 if (src[*usedlen] == '<') // remove the file name extension
Bram Moolenaar071d4272004-06-13 20:20:40 +00008484 {
8485 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008486 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008487 resultlen = (int)(s - result);
8488 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008489 else if (!skip_mod)
8490 {
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008491 valid |= modify_fname(src, tilde_file, usedlen, &result, &resultbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008492 &resultlen);
8493 if (result == NULL)
8494 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008495 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008496 return NULL;
8497 }
8498 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008499 }
8500
8501 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
8502 {
8503 if (valid != VALID_HEAD + VALID_PATH)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008504 // xgettext:no-c-format
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008505 *errormsg = _("E499: Empty file name for '%' or '#', only works with \":p:h\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008506 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008507 *errormsg = _("E500: Evaluates to an empty string");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008508 result = NULL;
8509 }
8510 else
8511 result = vim_strnsave(result, resultlen);
8512 vim_free(resultbuf);
8513 return result;
8514}
8515
8516/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008517 * Expand the <sfile> string in "arg".
8518 *
8519 * Returns an allocated string, or NULL for any error.
8520 */
8521 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008522expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008523{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008524 char *errormsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008525 int len;
8526 char_u *result;
8527 char_u *newres;
8528 char_u *repl;
8529 int srclen;
8530 char_u *p;
8531
8532 result = vim_strsave(arg);
8533 if (result == NULL)
8534 return NULL;
8535
8536 for (p = result; *p; )
8537 {
8538 if (STRNCMP(p, "<sfile>", 7) != 0)
8539 ++p;
8540 else
8541 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008542 // replace "<sfile>" with the sourced file name, and do ":" stuff
Bram Moolenaar63b92542007-03-27 14:57:09 +00008543 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008544 if (errormsg != NULL)
8545 {
8546 if (*errormsg)
8547 emsg(errormsg);
8548 vim_free(result);
8549 return NULL;
8550 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008551 if (repl == NULL) // no match (cannot happen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008552 {
8553 p += srclen;
8554 continue;
8555 }
8556 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
8557 newres = alloc(len);
8558 if (newres == NULL)
8559 {
8560 vim_free(repl);
8561 vim_free(result);
8562 return NULL;
8563 }
8564 mch_memmove(newres, result, (size_t)(p - result));
8565 STRCPY(newres + (p - result), repl);
8566 len = (int)STRLEN(newres);
8567 STRCAT(newres, p + srclen);
8568 vim_free(repl);
8569 vim_free(result);
8570 result = newres;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008571 p = newres + len; // continue after the match
Bram Moolenaar071d4272004-06-13 20:20:40 +00008572 }
8573 }
8574
8575 return result;
8576}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008577
Bram Moolenaar071d4272004-06-13 20:20:40 +00008578#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00008579/*
Bram Moolenaard9462e32011-04-11 21:35:11 +02008580 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +00008581 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +00008582 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008583 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008584dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008585{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008586 if (fname == NULL)
8587 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +02008588 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008589}
8590#endif
8591
8592/*
8593 * ":behave {mswin,xterm}"
8594 */
8595 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008596ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008597{
8598 if (STRCMP(eap->arg, "mswin") == 0)
8599 {
8600 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
8601 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
8602 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
8603 set_option_value((char_u *)"keymodel", 0L,
8604 (char_u *)"startsel,stopsel", 0);
8605 }
8606 else if (STRCMP(eap->arg, "xterm") == 0)
8607 {
8608 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
8609 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
8610 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
8611 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
8612 }
8613 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008614 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008615}
8616
Bram Moolenaar071d4272004-06-13 20:20:40 +00008617static int filetype_detect = FALSE;
8618static int filetype_plugin = FALSE;
8619static int filetype_indent = FALSE;
8620
8621/*
8622 * ":filetype [plugin] [indent] {on,off,detect}"
8623 * on: Load the filetype.vim file to install autocommands for file types.
8624 * off: Load the ftoff.vim file to remove all autocommands for file types.
8625 * plugin on: load filetype.vim and ftplugin.vim
8626 * plugin off: load ftplugof.vim
8627 * indent on: load filetype.vim and indent.vim
8628 * indent off: load indoff.vim
8629 */
8630 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008631ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008632{
8633 char_u *arg = eap->arg;
8634 int plugin = FALSE;
8635 int indent = FALSE;
8636
8637 if (*eap->arg == NUL)
8638 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008639 // Print current status.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008640 smsg("filetype detection:%s plugin:%s indent:%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00008641 filetype_detect ? "ON" : "OFF",
8642 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
8643 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
8644 return;
8645 }
8646
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008647 // Accept "plugin" and "indent" in any order.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008648 for (;;)
8649 {
8650 if (STRNCMP(arg, "plugin", 6) == 0)
8651 {
8652 plugin = TRUE;
8653 arg = skipwhite(arg + 6);
8654 continue;
8655 }
8656 if (STRNCMP(arg, "indent", 6) == 0)
8657 {
8658 indent = TRUE;
8659 arg = skipwhite(arg + 6);
8660 continue;
8661 }
8662 break;
8663 }
8664 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
8665 {
8666 if (*arg == 'o' || !filetype_detect)
8667 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008668 source_runtime((char_u *)FILETYPE_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008669 filetype_detect = TRUE;
8670 if (plugin)
8671 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008672 source_runtime((char_u *)FTPLUGIN_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008673 filetype_plugin = TRUE;
8674 }
8675 if (indent)
8676 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008677 source_runtime((char_u *)INDENT_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008678 filetype_indent = TRUE;
8679 }
8680 }
8681 if (*arg == 'd')
8682 {
Bram Moolenaar1610d052016-06-09 22:53:01 +02008683 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +00008684 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008685 }
8686 }
8687 else if (STRCMP(arg, "off") == 0)
8688 {
8689 if (plugin || indent)
8690 {
8691 if (plugin)
8692 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008693 source_runtime((char_u *)FTPLUGOF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008694 filetype_plugin = FALSE;
8695 }
8696 if (indent)
8697 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008698 source_runtime((char_u *)INDOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008699 filetype_indent = FALSE;
8700 }
8701 }
8702 else
8703 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008704 source_runtime((char_u *)FTOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008705 filetype_detect = FALSE;
8706 }
8707 }
8708 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008709 semsg(_(e_invarg2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008710}
8711
8712/*
Bram Moolenaar3e545692017-06-04 19:00:32 +02008713 * ":setfiletype [FALLBACK] {name}"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008714 */
8715 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008716ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008717{
8718 if (!did_filetype)
Bram Moolenaar3e545692017-06-04 19:00:32 +02008719 {
8720 char_u *arg = eap->arg;
8721
8722 if (STRNCMP(arg, "FALLBACK ", 9) == 0)
8723 arg += 9;
8724
8725 set_option_value((char_u *)"filetype", 0L, arg, OPT_LOCAL);
8726 if (arg != eap->arg)
8727 did_filetype = FALSE;
8728 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008729}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008730
Bram Moolenaar071d4272004-06-13 20:20:40 +00008731 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008732ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008733{
8734#ifdef FEAT_DIGRAPHS
8735 if (*eap->arg != NUL)
8736 putdigraph(eap->arg);
8737 else
Bram Moolenaareae8ae12018-12-14 18:53:02 +01008738 listdigraphs(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008739#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008740 emsg(_("E196: No digraphs in this version"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008741#endif
8742}
8743
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02008744#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
8745 void
8746set_no_hlsearch(int flag)
8747{
8748 no_hlsearch = flag;
8749# ifdef FEAT_EVAL
8750 set_vim_var_nr(VV_HLSEARCH, !no_hlsearch && p_hls);
8751# endif
8752}
8753
Bram Moolenaar071d4272004-06-13 20:20:40 +00008754/*
8755 * ":nohlsearch"
8756 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008757 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008758ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008759{
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02008760 set_no_hlsearch(TRUE);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00008761 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008762}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008763#endif
8764
8765#ifdef FEAT_CRYPT
8766/*
8767 * ":X": Get crypt key
8768 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008769 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008770ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008771{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +01008772 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02008773 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008774}
8775#endif
8776
8777#ifdef FEAT_FOLDING
8778 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008779ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008780{
8781 if (foldManualAllowed(TRUE))
8782 foldCreate(eap->line1, eap->line2);
8783}
8784
8785 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008786ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008787{
8788 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
8789 eap->forceit, FALSE);
8790}
8791
8792 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008793ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008794{
8795 linenr_T lnum;
8796
Bram Moolenaar4facea32019-10-12 20:17:40 +02008797# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02008798 start_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +02008799# endif
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02008800
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008801 // First set the marks for all lines closed/open.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008802 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
8803 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
8804 ml_setmarked(lnum);
8805
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008806 // Execute the command on the marked lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008807 global_exe(eap->arg);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008808 ml_clearmarked(); // clear rest of the marks
Bram Moolenaar4facea32019-10-12 20:17:40 +02008809# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02008810 end_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +02008811# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008812}
8813#endif
Bram Moolenaarf5291f32017-09-14 22:55:37 +02008814
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008815#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar39665952018-08-15 20:59:48 +02008816/*
8817 * Returns TRUE if the supplied Ex cmdidx is for a location list command
8818 * instead of a quickfix command.
8819 */
8820 int
8821is_loclist_cmd(int cmdidx)
8822{
Bram Moolenaar74c8be22018-08-23 22:51:40 +02008823 if (cmdidx < 0 || cmdidx >= CMD_SIZE)
Bram Moolenaar39665952018-08-15 20:59:48 +02008824 return FALSE;
8825 return cmdnames[cmdidx].cmd_name[0] == 'l';
8826}
8827#endif
8828
Bram Moolenaar4facea32019-10-12 20:17:40 +02008829#if defined(FEAT_TIMERS) || defined(PROTO)
Bram Moolenaarf5291f32017-09-14 22:55:37 +02008830 int
8831get_pressedreturn(void)
8832{
8833 return ex_pressedreturn;
8834}
8835
8836 void
8837set_pressedreturn(int val)
8838{
8839 ex_pressedreturn = val;
8840}
8841#endif