blob: 0ec63e24e491a7be76af982e8ed5f1b8aa8ca67a [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 Moolenaar33c4dbb2018-08-14 16:06:16 +020028static void free_cmdmod(void);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010029static void append_command(char_u *cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000030
Bram Moolenaar071d4272004-06-13 20:20:40 +000031#ifndef FEAT_MENU
32# define ex_emenu ex_ni
33# define ex_menu ex_ni
34# define ex_menutranslate ex_ni
35#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010036static void ex_autocmd(exarg_T *eap);
37static void ex_doautocmd(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010038static void ex_bunload(exarg_T *eap);
39static void ex_buffer(exarg_T *eap);
40static void ex_bmodified(exarg_T *eap);
41static void ex_bnext(exarg_T *eap);
42static void ex_bprevious(exarg_T *eap);
43static void ex_brewind(exarg_T *eap);
44static void ex_blast(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010045static char_u *getargcmd(char_u **);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010046static int getargopt(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000047#ifndef FEAT_QUICKFIX
48# define ex_make ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +000049# define ex_cbuffer ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000050# define ex_cc ex_ni
51# define ex_cnext ex_ni
Bram Moolenaar3ff33112019-05-03 21:56:35 +020052# define ex_cbelow ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000053# define ex_cfile ex_ni
54# define qf_list ex_ni
55# define qf_age ex_ni
Bram Moolenaarf6acffb2016-07-16 16:54:24 +020056# define qf_history ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000057# define ex_helpgrep ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +000058# define ex_vimgrep ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000059#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +020060#if !defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +000061# define ex_cclose ex_ni
62# define ex_copen ex_ni
63# define ex_cwindow ex_ni
Bram Moolenaardcb17002016-07-07 18:58:59 +020064# define ex_cbottom ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000065#endif
Bram Moolenaar1e015462005-09-25 22:16:38 +000066#if !defined(FEAT_QUICKFIX) || !defined(FEAT_EVAL)
67# define ex_cexpr ex_ni
68#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000069
Bram Moolenaarb7316892019-05-01 18:08:42 +020070static 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 +010071static void get_flags(exarg_T *eap);
Bram Moolenaar85363ab2010-07-18 13:58:26 +020072#if !defined(FEAT_PERL) \
73 || !defined(FEAT_PYTHON) || !defined(FEAT_PYTHON3) \
74 || !defined(FEAT_TCL) \
75 || !defined(FEAT_RUBY) \
76 || !defined(FEAT_LUA) \
77 || !defined(FEAT_MZSCHEME)
Bram Moolenaar7bb75552007-07-16 18:39:49 +000078# define HAVE_EX_SCRIPT_NI
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010079static void ex_script_ni(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000080#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010081static char *invalid_range(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010082static void correct_range(exarg_T *eap);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000083#ifdef FEAT_QUICKFIX
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010084static char_u *replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000085#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010086static char_u *repl_cmdline(exarg_T *eap, char_u *src, int srclen, char_u *repl, char_u **cmdlinep);
87static void ex_highlight(exarg_T *eap);
88static void ex_colorscheme(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010089static void ex_cquit(exarg_T *eap);
90static void ex_quit_all(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010091static void ex_close(exarg_T *eap);
92static void ex_win_close(int forceit, win_T *win, tabpage_T *tp);
93static void ex_only(exarg_T *eap);
94static void ex_resize(exarg_T *eap);
95static void ex_stag(exarg_T *eap);
96static void ex_tabclose(exarg_T *eap);
97static void ex_tabonly(exarg_T *eap);
98static void ex_tabnext(exarg_T *eap);
99static void ex_tabmove(exarg_T *eap);
100static void ex_tabs(exarg_T *eap);
Bram Moolenaar4033c552017-09-16 20:54:51 +0200101#if defined(FEAT_QUICKFIX)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100102static void ex_pclose(exarg_T *eap);
103static void ex_ptag(exarg_T *eap);
104static void ex_pedit(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000105#else
106# define ex_pclose ex_ni
107# define ex_ptag ex_ni
108# define ex_pedit ex_ni
109#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100110static void ex_hide(exarg_T *eap);
111static void ex_stop(exarg_T *eap);
112static void ex_exit(exarg_T *eap);
113static void ex_print(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000114#ifdef FEAT_BYTEOFF
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100115static void ex_goto(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000116#else
117# define ex_goto ex_ni
118#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100119static void ex_shell(exarg_T *eap);
120static void ex_preserve(exarg_T *eap);
121static void ex_recover(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100122static void ex_mode(exarg_T *eap);
123static void ex_wrongmodifier(exarg_T *eap);
124static void ex_find(exarg_T *eap);
125static void ex_open(exarg_T *eap);
126static void ex_edit(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000127#ifndef FEAT_GUI
128# define ex_gui ex_nogui
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100129static void ex_nogui(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000130#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +0100131#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100132static void ex_tearoff(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000133#else
134# define ex_tearoff ex_ni
135#endif
Bram Moolenaar29a2c082018-03-05 21:06:23 +0100136#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
137 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100138static void ex_popup(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000139#else
140# define ex_popup ex_ni
141#endif
142#ifndef FEAT_GUI_MSWIN
143# define ex_simalt ex_ni
144#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000145#if !defined(FEAT_GUI_MSWIN) && !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000146# define gui_mch_find_dialog ex_ni
147# define gui_mch_replace_dialog ex_ni
148#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000149#if !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000150# define ex_helpfind ex_ni
151#endif
152#ifndef FEAT_CSCOPE
Bram Moolenaard4db7712016-11-12 19:16:46 +0100153# define ex_cscope ex_ni
154# define ex_scscope ex_ni
155# define ex_cstag ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000156#endif
157#ifndef FEAT_SYN_HL
158# define ex_syntax ex_ni
Bram Moolenaar860cae12010-06-05 23:22:07 +0200159# define ex_ownsyntax ex_ni
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000160#endif
Bram Moolenaarf7512552013-06-06 14:55:19 +0200161#if !defined(FEAT_SYN_HL) || !defined(FEAT_PROFILE)
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +0200162# define ex_syntime ex_ni
163#endif
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000164#ifndef FEAT_SPELL
Bram Moolenaarb765d632005-06-07 21:00:02 +0000165# define ex_spell ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000166# define ex_mkspell ex_ni
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000167# define ex_spelldump ex_ni
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000168# define ex_spellinfo ex_ni
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000169# define ex_spellrepall ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000170#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200171#ifndef FEAT_PERSISTENT_UNDO
172# define ex_rundo ex_ni
173# define ex_wundo ex_ni
174#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200175#ifndef FEAT_LUA
176# define ex_lua ex_script_ni
177# define ex_luado ex_ni
178# define ex_luafile ex_ni
179#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000180#ifndef FEAT_MZSCHEME
181# define ex_mzscheme ex_script_ni
182# define ex_mzfile ex_ni
183#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000184#ifndef FEAT_PERL
185# define ex_perl ex_script_ni
186# define ex_perldo ex_ni
187#endif
188#ifndef FEAT_PYTHON
189# define ex_python ex_script_ni
Bram Moolenaard620aa92013-05-17 16:40:06 +0200190# define ex_pydo ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000191# define ex_pyfile ex_ni
192#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200193#ifndef FEAT_PYTHON3
Bram Moolenaar368373e2010-07-19 20:46:22 +0200194# define ex_py3 ex_script_ni
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200195# define ex_py3do ex_ni
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200196# define ex_py3file ex_ni
197#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100198#if !defined(FEAT_PYTHON) && !defined(FEAT_PYTHON3)
199# define ex_pyx ex_script_ni
200# define ex_pyxdo ex_ni
201# define ex_pyxfile ex_ni
202#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000203#ifndef FEAT_TCL
204# define ex_tcl ex_script_ni
205# define ex_tcldo ex_ni
206# define ex_tclfile ex_ni
207#endif
208#ifndef FEAT_RUBY
209# define ex_ruby ex_script_ni
210# define ex_rubydo ex_ni
211# define ex_rubyfile ex_ni
212#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000213#ifndef FEAT_KEYMAP
214# define ex_loadkeymap ex_ni
215#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100216static void ex_swapname(exarg_T *eap);
217static void ex_syncbind(exarg_T *eap);
218static void ex_read(exarg_T *eap);
219static void ex_pwd(exarg_T *eap);
220static void ex_equal(exarg_T *eap);
221static void ex_sleep(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100222static void ex_winsize(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100223static void ex_wincmd(exarg_T *eap);
Bram Moolenaar843ee412004-06-30 16:16:41 +0000224#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100225static void ex_winpos(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000226#else
227# define ex_winpos ex_ni
228#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100229static void ex_operators(exarg_T *eap);
230static void ex_put(exarg_T *eap);
231static void ex_copymove(exarg_T *eap);
232static void ex_submagic(exarg_T *eap);
233static void ex_join(exarg_T *eap);
234static void ex_at(exarg_T *eap);
235static void ex_bang(exarg_T *eap);
236static void ex_undo(exarg_T *eap);
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200237#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100238static void ex_wundo(exarg_T *eap);
239static void ex_rundo(exarg_T *eap);
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200240#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100241static void ex_redo(exarg_T *eap);
242static void ex_later(exarg_T *eap);
243static void ex_redir(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100244static void ex_redrawstatus(exarg_T *eap);
Bram Moolenaare12bab32019-01-08 22:02:56 +0100245static void ex_redrawtabline(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100246static void close_redir(void);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100247static void ex_mark(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100248static void ex_startinsert(exarg_T *eap);
249static void ex_stopinsert(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000250#ifdef FEAT_FIND_ID
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100251static void ex_checkpath(exarg_T *eap);
252static void ex_findpat(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000253#else
254# define ex_findpat ex_ni
255# define ex_checkpath ex_ni
256#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +0200257#if defined(FEAT_FIND_ID) && defined(FEAT_QUICKFIX)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100258static void ex_psearch(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000259#else
260# define ex_psearch ex_ni
261#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100262static void ex_tag(exarg_T *eap);
263static void ex_tag_cmd(exarg_T *eap, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000264#ifndef FEAT_EVAL
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200265# define ex_break ex_ni
266# define ex_breakadd ex_ni
267# define ex_breakdel ex_ni
268# define ex_breaklist ex_ni
269# define ex_call ex_ni
270# define ex_catch ex_ni
271# define ex_compiler ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200272# define ex_continue ex_ni
273# define ex_debug ex_ni
274# define ex_debuggreedy ex_ni
Bram Moolenaar822ba242020-05-24 23:00:18 +0200275# define ex_def ex_ni
276# define ex_defcompile ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200277# define ex_delfunction ex_ni
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100278# define ex_disassemble ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000279# define ex_echo ex_ni
280# define ex_echohl ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000281# define ex_else ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200282# define ex_endfunction ex_ni
283# define ex_endif ex_ni
284# define ex_endtry ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000285# define ex_endwhile ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200286# define ex_eval ex_ni
287# define ex_execute ex_ni
288# define ex_finally ex_ni
289# define ex_finish ex_ni
290# define ex_function ex_ni
291# define ex_if ex_ni
292# define ex_let ex_ni
293# define ex_lockvar ex_ni
294# define ex_oldfiles ex_ni
295# define ex_options ex_ni
296# define ex_packadd ex_ni
297# define ex_packloadall ex_ni
298# define ex_return ex_ni
299# define ex_scriptnames ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000300# define ex_throw ex_ni
301# define ex_try ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000302# define ex_unlet ex_ni
Bram Moolenaar65c1b012005-01-31 19:02:28 +0000303# define ex_unlockvar ex_ni
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100304# define ex_vim9script ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200305# define ex_while ex_ni
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100306# define ex_import ex_ni
307# define ex_export ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000308#endif
Bram Moolenaar84538072019-07-28 14:15:42 +0200309#ifndef FEAT_SESSION
Bram Moolenaar071d4272004-06-13 20:20:40 +0000310# define ex_loadview ex_ni
311#endif
Bram Moolenaardefa0672019-07-21 19:25:37 +0200312#ifndef FEAT_VIMINFO
Bram Moolenaar071d4272004-06-13 20:20:40 +0000313# define ex_viminfo ex_ni
314#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100315static void ex_behave(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100316static void ex_filetype(exarg_T *eap);
317static void ex_setfiletype(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000318#ifndef FEAT_DIFF
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000319# define ex_diffoff ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000320# define ex_diffpatch ex_ni
321# define ex_diffgetput ex_ni
322# define ex_diffsplit ex_ni
323# define ex_diffthis ex_ni
324# define ex_diffupdate ex_ni
325#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100326static void ex_digraphs(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000327#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100328static void ex_nohlsearch(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000329#else
330# define ex_nohlsearch ex_ni
331# define ex_match ex_ni
332#endif
333#ifdef FEAT_CRYPT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100334static void ex_X(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000335#else
336# define ex_X ex_ni
337#endif
338#ifdef FEAT_FOLDING
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100339static void ex_fold(exarg_T *eap);
340static void ex_foldopen(exarg_T *eap);
341static void ex_folddo(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000342#else
343# define ex_fold ex_ni
344# define ex_foldopen ex_ni
345# define ex_folddo ex_ni
346#endif
Bram Moolenaar13505972019-01-24 15:04:48 +0100347#if !(defined(HAVE_LOCALE_H) || defined(X_LOCALE))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000348# define ex_language ex_ni
349#endif
350#ifndef FEAT_SIGNS
351# define ex_sign ex_ni
352#endif
Bram Moolenaar009b2592004-10-24 19:18:58 +0000353#ifndef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200354# define ex_nbclose ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000355# define ex_nbkey ex_ni
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200356# define ex_nbstart ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000357#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000358
Bram Moolenaar071d4272004-06-13 20:20:40 +0000359#ifndef FEAT_JUMPLIST
360# define ex_jumps ex_ni
Bram Moolenaar2d358992016-06-12 21:20:54 +0200361# define ex_clearjumps ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000362# define ex_changes ex_ni
363#endif
364
Bram Moolenaar05159a02005-02-26 23:04:13 +0000365#ifndef FEAT_PROFILE
366# define ex_profile ex_ni
367#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200368#ifndef FEAT_TERMINAL
369# define ex_terminal ex_ni
370#endif
Bram Moolenaard4aa83a2019-05-09 18:59:31 +0200371#if !defined(FEAT_X11) || !defined(FEAT_XCLIPBOARD)
372# define ex_xrestore ex_ni
373#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100374#if !defined(FEAT_PROP_POPUP)
Bram Moolenaar682725c2019-05-25 20:10:37 +0200375# define ex_popupclear ex_ni
376#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +0000377
Bram Moolenaar071d4272004-06-13 20:20:40 +0000378/*
379 * Declare cmdnames[].
380 */
381#define DO_DECLARE_EXCMD
382#include "ex_cmds.h"
Bram Moolenaar6de5e122017-04-20 21:55:44 +0200383#include "ex_cmdidxs.h"
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +0100384
Bram Moolenaar071d4272004-06-13 20:20:40 +0000385static char_u dollar_command[2] = {'$', 0};
386
387
388#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100389// Struct for storing a line inside a while/for loop
Bram Moolenaar071d4272004-06-13 20:20:40 +0000390typedef struct
391{
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100392 char_u *line; // command line
393 linenr_T lnum; // sourcing_lnum of the line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000394} wcmd_T;
395
396/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000397 * Structure used to store info for line position in a while or for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000398 * This is required, because do_one_cmd() may invoke ex_function(), which
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000399 * reads more lines that may come from the while/for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000400 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000401struct loop_cookie
Bram Moolenaar071d4272004-06-13 20:20:40 +0000402{
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100403 garray_T *lines_gap; // growarray with line info
404 int current_line; // last read line from growarray
405 int repeating; // TRUE when looping a second time
406 // When "repeating" is FALSE use "getline" and "cookie" to get lines
Bram Moolenaare96a2492019-06-25 04:12:16 +0200407 char_u *(*getline)(int, void *, int, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000408 void *cookie;
409};
410
Bram Moolenaare96a2492019-06-25 04:12:16 +0200411static char_u *get_loop_line(int c, void *cookie, int indent, int do_concat);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100412static int store_loop_line(garray_T *gap, char_u *line);
413static void free_cmdlines(garray_T *gap);
Bram Moolenaared203462004-06-16 11:19:22 +0000414
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100415// Struct to save a few things while debugging. Used in do_cmdline() only.
Bram Moolenaared203462004-06-16 11:19:22 +0000416struct dbg_stuff
417{
418 int trylevel;
419 int force_abort;
420 except_T *caught_stack;
421 char_u *vv_exception;
422 char_u *vv_throwpoint;
423 int did_emsg;
424 int got_int;
425 int did_throw;
426 int need_rethrow;
427 int check_cstack;
428 except_T *current_exception;
429};
430
Bram Moolenaared203462004-06-16 11:19:22 +0000431 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100432save_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000433{
434 dsp->trylevel = trylevel; trylevel = 0;
435 dsp->force_abort = force_abort; force_abort = FALSE;
436 dsp->caught_stack = caught_stack; caught_stack = NULL;
437 dsp->vv_exception = v_exception(NULL);
438 dsp->vv_throwpoint = v_throwpoint(NULL);
439
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100440 // Necessary for debugging an inactive ":catch", ":finally", ":endtry"
Bram Moolenaared203462004-06-16 11:19:22 +0000441 dsp->did_emsg = did_emsg; did_emsg = FALSE;
442 dsp->got_int = got_int; got_int = FALSE;
443 dsp->did_throw = did_throw; did_throw = FALSE;
444 dsp->need_rethrow = need_rethrow; need_rethrow = FALSE;
445 dsp->check_cstack = check_cstack; check_cstack = FALSE;
446 dsp->current_exception = current_exception; current_exception = NULL;
447}
448
449 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100450restore_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000451{
452 suppress_errthrow = FALSE;
453 trylevel = dsp->trylevel;
454 force_abort = dsp->force_abort;
455 caught_stack = dsp->caught_stack;
456 (void)v_exception(dsp->vv_exception);
457 (void)v_throwpoint(dsp->vv_throwpoint);
458 did_emsg = dsp->did_emsg;
459 got_int = dsp->got_int;
460 did_throw = dsp->did_throw;
461 need_rethrow = dsp->need_rethrow;
462 check_cstack = dsp->check_cstack;
463 current_exception = dsp->current_exception;
464}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000465#endif
466
Bram Moolenaar071d4272004-06-13 20:20:40 +0000467/*
468 * do_exmode(): Repeatedly get commands for the "Ex" mode, until the ":vi"
469 * command is given.
470 */
471 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100472do_exmode(
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100473 int improved) // TRUE for "improved Ex" mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000474{
475 int save_msg_scroll;
476 int prev_msg_row;
477 linenr_T prev_line;
Bram Moolenaar79518e22017-02-17 16:31:35 +0100478 varnumber_T changedtick;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000479
480 if (improved)
481 exmode_active = EXMODE_VIM;
482 else
483 exmode_active = EXMODE_NORMAL;
484 State = NORMAL;
485
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100486 // When using ":global /pat/ visual" and then "Q" we return to continue
487 // the :global command.
Bram Moolenaardf177f62005-02-22 08:39:57 +0000488 if (global_busy)
489 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000490
491 save_msg_scroll = msg_scroll;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100492 ++RedrawingDisabled; // don't redisplay the window
493 ++no_wait_return; // don't wait for return
Bram Moolenaar071d4272004-06-13 20:20:40 +0000494#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100495 // Ignore scrollbar and mouse events in Ex mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000496 ++hold_gui_events;
497#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000498
Bram Moolenaar32526b32019-01-19 17:43:09 +0100499 msg(_("Entering Ex mode. Type \"visual\" to go to Normal mode."));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000500 while (exmode_active)
501 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100502 // Check for a ":normal" command and no more characters left.
Bram Moolenaar7c626922005-02-07 22:01:03 +0000503 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
504 {
505 exmode_active = FALSE;
506 break;
507 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000508 msg_scroll = TRUE;
509 need_wait_return = FALSE;
510 ex_pressedreturn = FALSE;
511 ex_no_reprint = FALSE;
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100512 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000513 prev_msg_row = msg_row;
514 prev_line = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000515 if (improved)
516 {
517 cmdline_row = msg_row;
518 do_cmdline(NULL, getexline, NULL, 0);
519 }
520 else
521 do_cmdline(NULL, getexmodeline, NULL, DOCMD_NOWAIT);
522 lines_left = Rows - 1;
523
Bram Moolenaardf177f62005-02-22 08:39:57 +0000524 if ((prev_line != curwin->w_cursor.lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100525 || changedtick != CHANGEDTICK(curbuf)) && !ex_no_reprint)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000526 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000527 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100528 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000529 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000530 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000531 if (ex_pressedreturn)
532 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100533 // go up one line, to overwrite the ":<CR>" line, so the
534 // output doesn't contain empty lines.
Bram Moolenaardf177f62005-02-22 08:39:57 +0000535 msg_row = prev_msg_row;
536 if (prev_msg_row == Rows - 1)
537 msg_row--;
538 }
539 msg_col = 0;
540 print_line_no_prefix(curwin->w_cursor.lnum, FALSE, FALSE);
541 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000542 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000543 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100544 else if (ex_pressedreturn && !ex_no_reprint) // must be at EOF
Bram Moolenaardf177f62005-02-22 08:39:57 +0000545 {
546 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100547 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000548 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100549 emsg(_("E501: At end-of-file"));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000550 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000551 }
552
553#ifdef FEAT_GUI
554 --hold_gui_events;
555#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000556 --RedrawingDisabled;
557 --no_wait_return;
558 update_screen(CLEAR);
559 need_wait_return = FALSE;
560 msg_scroll = save_msg_scroll;
561}
562
563/*
Bram Moolenaar4facea32019-10-12 20:17:40 +0200564 * Print the executed command for when 'verbose' is set.
565 * When "lnum" is 0 only print the command.
566 */
567 static void
568msg_verbose_cmd(linenr_T lnum, char_u *cmd)
569{
570 ++no_wait_return;
571 verbose_enter_scroll();
572
573 if (lnum == 0)
574 smsg(_("Executing: %s"), cmd);
575 else
576 smsg(_("line %ld: %s"), (long)lnum, cmd);
577 if (msg_silent == 0)
578 msg_puts("\n"); // don't overwrite this
579
580 verbose_leave_scroll();
581 --no_wait_return;
582}
583
584/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000585 * Execute a simple command line. Used for translated commands like "*".
586 */
587 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100588do_cmdline_cmd(char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000589{
590 return do_cmdline(cmd, NULL, NULL,
591 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
592}
593
594/*
595 * do_cmdline(): execute one Ex command line
596 *
597 * 1. Execute "cmdline" when it is not NULL.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100598 * If "cmdline" is NULL, or more lines are needed, fgetline() is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000599 * 2. Split up in parts separated with '|'.
600 *
601 * This function can be called recursively!
602 *
603 * flags:
604 * DOCMD_VERBOSE - The command will be included in the error message.
605 * DOCMD_NOWAIT - Don't call wait_return() and friends.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100606 * DOCMD_REPEAT - Repeat execution until fgetline() returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607 * DOCMD_KEYTYPED - Don't reset KeyTyped.
608 * DOCMD_EXCRESET - Reset the exception environment (used for debugging).
609 * DOCMD_KEEPLINE - Store first typed line (for repeating with ".").
610 *
611 * return FAIL if cmdline could not be executed, OK otherwise
612 */
613 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100614do_cmdline(
615 char_u *cmdline,
Bram Moolenaare96a2492019-06-25 04:12:16 +0200616 char_u *(*fgetline)(int, void *, int, int),
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100617 void *cookie, // argument for fgetline()
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100618 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000619{
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100620 char_u *next_cmdline; // next cmd to execute
621 char_u *cmdline_copy = NULL; // copy of cmd line
622 int used_getline = FALSE; // used "fgetline" to obtain command
623 static int recursive = 0; // recursive depth
Bram Moolenaar071d4272004-06-13 20:20:40 +0000624 int msg_didout_before_start = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100625 int count = 0; // line number count
626 int did_inc = FALSE; // incremented RedrawingDisabled
Bram Moolenaar071d4272004-06-13 20:20:40 +0000627 int retval = OK;
628#ifdef FEAT_EVAL
Bram Moolenaarddef1292019-12-16 17:10:33 +0100629 cstack_T cstack; // conditional stack
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100630 garray_T lines_ga; // keep lines for ":while"/":for"
631 int current_line = 0; // active line in lines_ga
Bram Moolenaard5053d02020-06-28 15:51:16 +0200632 int current_line_before = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100633 char_u *fname = NULL; // function or script name
634 linenr_T *breakpoint = NULL; // ptr to breakpoint field in cookie
635 int *dbg_tick = NULL; // ptr to dbg_tick field in cookie
636 struct dbg_stuff debug_saved; // saved things for debug mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000637 int initial_trylevel;
Bram Moolenaar25e0f582020-05-25 22:36:50 +0200638 msglist_T **saved_msg_list = NULL;
639 msglist_T *private_msg_list;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000640
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100641 // "fgetline" and "cookie" passed to do_one_cmd()
Bram Moolenaare96a2492019-06-25 04:12:16 +0200642 char_u *(*cmd_getline)(int, void *, int, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000643 void *cmd_cookie;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000644 struct loop_cookie cmd_loop_cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000645 void *real_cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000646 int getline_is_func;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000647#else
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100648# define cmd_getline fgetline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649# define cmd_cookie cookie
650#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100651 static int call_depth = 0; // recursiveness
Bram Moolenaar2196bce2020-04-12 20:01:11 +0200652#ifdef FEAT_EVAL
Bram Moolenaare31ee862020-01-07 20:59:34 +0100653 ESTACK_CHECK_DECLARATION
Bram Moolenaar071d4272004-06-13 20:20:40 +0000654
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100655 // For every pair of do_cmdline()/do_one_cmd() calls, use an extra memory
656 // location for storing error messages to be converted to an exception.
657 // This ensures that the do_errthrow() call in do_one_cmd() does not
658 // combine the messages stored by an earlier invocation of do_one_cmd()
659 // with the command name of the later one. This would happen when
660 // BufWritePost autocommands are executed after a write error.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000661 saved_msg_list = msg_list;
662 msg_list = &private_msg_list;
663 private_msg_list = NULL;
664#endif
665
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100666 // It's possible to create an endless loop with ":execute", catch that
667 // here. The value of 200 allows nested function calls, ":source", etc.
668 // Allow 200 or 'maxfuncdepth', whatever is larger.
Bram Moolenaarb094ff42017-01-02 16:16:39 +0100669 if (call_depth >= 200
670#ifdef FEAT_EVAL
671 && call_depth >= p_mfd
672#endif
673 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000674 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100675 emsg(_("E169: Command too recursive"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000676#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100677 // When converting to an exception, we do not include the command name
678 // since this is not an error of the specific command.
Bram Moolenaarddef1292019-12-16 17:10:33 +0100679 do_errthrow((cstack_T *)NULL, (char_u *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000680 msg_list = saved_msg_list;
681#endif
682 return FAIL;
683 }
684 ++call_depth;
685
686#ifdef FEAT_EVAL
687 cstack.cs_idx = -1;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000688 cstack.cs_looplevel = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000689 cstack.cs_trylevel = 0;
690 cstack.cs_emsg_silent_list = NULL;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000691 cstack.cs_lflags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692 ga_init2(&lines_ga, (int)sizeof(wcmd_T), 10);
693
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100694 real_cookie = getline_cookie(fgetline, cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000695
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100696 // Inside a function use a higher nesting level.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100697 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000698 if (getline_is_func && ex_nesting_level == func_level(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000699 ++ex_nesting_level;
700
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100701 // Get the function or script name and the address where the next breakpoint
702 // line and the debug tick for a function or script are stored.
Bram Moolenaar05159a02005-02-26 23:04:13 +0000703 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000704 {
705 fname = func_name(real_cookie);
706 breakpoint = func_breakpoint(real_cookie);
707 dbg_tick = func_dbg_tick(real_cookie);
708 }
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100709 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000710 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100711 fname = SOURCING_NAME;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000712 breakpoint = source_breakpoint(real_cookie);
713 dbg_tick = source_dbg_tick(real_cookie);
714 }
715
716 /*
717 * Initialize "force_abort" and "suppress_errthrow" at the top level.
718 */
719 if (!recursive)
720 {
721 force_abort = FALSE;
722 suppress_errthrow = FALSE;
723 }
724
725 /*
726 * If requested, store and reset the global values controlling the
Bram Moolenaar89d40322006-08-29 15:30:07 +0000727 * exception handling (used when debugging). Otherwise clear it to avoid
728 * a bogus compiler warning when the optimizer uses inline functions...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000729 */
Bram Moolenaarb4872942006-05-13 10:32:52 +0000730 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +0000731 save_dbg_stuff(&debug_saved);
Bram Moolenaar89d40322006-08-29 15:30:07 +0000732 else
Bram Moolenaara80faa82020-04-12 19:37:17 +0200733 CLEAR_FIELD(debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000734
735 initial_trylevel = trylevel;
736
737 /*
738 * "did_throw" will be set to TRUE when an exception is being thrown.
739 */
740 did_throw = FALSE;
741#endif
742 /*
743 * "did_emsg" will be set to TRUE when emsg() is used, in which case we
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000744 * cancel the whole command line, and any if/endif or loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000745 * If force_abort is set, we cancel everything.
746 */
747 did_emsg = FALSE;
748
749 /*
750 * KeyTyped is only set when calling vgetc(). Reset it here when not
751 * calling vgetc() (sourced command lines).
752 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100753 if (!(flags & DOCMD_KEYTYPED)
754 && !getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755 KeyTyped = FALSE;
756
757 /*
758 * Continue executing command lines:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000759 * - when inside an ":if", ":while" or ":for"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000760 * - for multiple commands on one line, separated with '|'
761 * - when repeating until there are no more lines (for ":source")
762 */
763 next_cmdline = cmdline;
764 do
765 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000766#ifdef FEAT_EVAL
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100767 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000768#endif
769
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100770 // stop skipping cmds for an error msg after all endif/while/for
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771 if (next_cmdline == NULL
772#ifdef FEAT_EVAL
773 && !force_abort
774 && cstack.cs_idx < 0
Bram Moolenaar05159a02005-02-26 23:04:13 +0000775 && !(getline_is_func && func_has_abort(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000776#endif
777 )
778 did_emsg = FALSE;
779
780 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000781 * 1. If repeating a line in a loop, get a line from lines_ga.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100782 * 2. If no line given: Get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783 * 3. If a line is given: Make a copy, so we can mess with it.
784 */
785
786#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100787 // 1. If repeating, get a previous line from lines_ga.
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000788 if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000789 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100790 // Each '|' separated command is stored separately in lines_ga, to
791 // be able to jump to it. Don't use next_cmdline now.
Bram Moolenaard23a8232018-02-10 18:45:26 +0100792 VIM_CLEAR(cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100794 // Check if a function has returned or, unless it has an unclosed
795 // try conditional, aborted.
Bram Moolenaar05159a02005-02-26 23:04:13 +0000796 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000797 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000798# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000799 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000800 func_line_end(real_cookie);
801# endif
802 if (func_has_ended(real_cookie))
803 {
804 retval = FAIL;
805 break;
806 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000807 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000808#ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000809 else if (do_profiling == PROF_YES
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100810 && getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000811 script_line_end();
812#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100814 // Check if a sourced file hit a ":finish" command.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100815 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816 {
817 retval = FAIL;
818 break;
819 }
820
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100821 // If breakpoints have been added/deleted need to check for it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822 if (breakpoint != NULL && dbg_tick != NULL
823 && *dbg_tick != debug_tick)
824 {
825 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100826 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100827 fname, SOURCING_LNUM);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000828 *dbg_tick = debug_tick;
829 }
830
831 next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100832 SOURCING_LNUM = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000833
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100834 // Did we encounter a breakpoint?
Bram Moolenaar071d4272004-06-13 20:20:40 +0000835 if (breakpoint != NULL && *breakpoint != 0
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100836 && *breakpoint <= SOURCING_LNUM)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100838 dbg_breakpoint(fname, SOURCING_LNUM);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100839 // Find next breakpoint.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100841 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100842 fname, SOURCING_LNUM);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000843 *dbg_tick = debug_tick;
844 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000845# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000846 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000847 {
848 if (getline_is_func)
849 func_line_start(real_cookie);
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100850 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000851 script_line_start();
852 }
853# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855#endif
856
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100857 // 2. If no line given, get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000858 if (next_cmdline == NULL)
859 {
860 /*
861 * Need to set msg_didout for the first line after an ":if",
862 * otherwise the ":if" will be overwritten.
863 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100864 if (count == 1 && getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000865 msg_didout = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100866 if (fgetline == NULL || (next_cmdline = fgetline(':', cookie,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000867#ifdef FEAT_EVAL
868 cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2
869#else
870 0
871#endif
Bram Moolenaare96a2492019-06-25 04:12:16 +0200872 , TRUE)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000873 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100874 // Don't call wait_return for aborted command line. The NULL
875 // returned for the end of a sourced file or executed function
876 // doesn't do this.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000877 if (KeyTyped && !(flags & DOCMD_REPEAT))
878 need_wait_return = FALSE;
879 retval = FAIL;
880 break;
881 }
882 used_getline = TRUE;
883
884 /*
885 * Keep the first typed line. Clear it when more lines are typed.
886 */
887 if (flags & DOCMD_KEEPLINE)
888 {
889 vim_free(repeat_cmdline);
890 if (count == 0)
891 repeat_cmdline = vim_strsave(next_cmdline);
892 else
893 repeat_cmdline = NULL;
894 }
895 }
896
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100897 // 3. Make a copy of the command so we can mess with it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000898 else if (cmdline_copy == NULL)
899 {
900 next_cmdline = vim_strsave(next_cmdline);
901 if (next_cmdline == NULL)
902 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100903 emsg(_(e_outofmem));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000904 retval = FAIL;
905 break;
906 }
907 }
908 cmdline_copy = next_cmdline;
909
910#ifdef FEAT_EVAL
911 /*
Bram Moolenaard5053d02020-06-28 15:51:16 +0200912 * Inside a while/for loop, and when the command looks like a ":while"
913 * or ":for", the line is stored, because we may need it later when
914 * looping.
915 *
916 * When there is a '|' and another command, it is stored separately,
917 * because we need to be able to jump back to it from an
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000918 * :endwhile/:endfor.
Bram Moolenaard5053d02020-06-28 15:51:16 +0200919 *
920 * Pass a different "fgetline" function to do_one_cmd() below,
921 * that it stores lines in or reads them from "lines_ga". Makes it
922 * possible to define a function inside a while/for loop and handles
923 * line continuation.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000924 */
Bram Moolenaard5053d02020-06-28 15:51:16 +0200925 if ((cstack.cs_looplevel > 0 || has_loop_cmd(next_cmdline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926 {
Bram Moolenaard5053d02020-06-28 15:51:16 +0200927 cmd_getline = get_loop_line;
928 cmd_cookie = (void *)&cmd_loop_cookie;
929 cmd_loop_cookie.lines_gap = &lines_ga;
930 cmd_loop_cookie.current_line = current_line;
931 cmd_loop_cookie.getline = fgetline;
932 cmd_loop_cookie.cookie = cookie;
933 cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len);
934
935 // Save the current line when encountering it the first time.
936 if (current_line == lines_ga.ga_len
937 && store_loop_line(&lines_ga, next_cmdline) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000938 {
939 retval = FAIL;
940 break;
941 }
Bram Moolenaard5053d02020-06-28 15:51:16 +0200942 current_line_before = current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000943 }
Bram Moolenaard5053d02020-06-28 15:51:16 +0200944 else
945 {
946 cmd_getline = fgetline;
947 cmd_cookie = cookie;
948 }
949
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950 did_endif = FALSE;
951#endif
952
953 if (count++ == 0)
954 {
955 /*
956 * All output from the commands is put below each other, without
957 * waiting for a return. Don't do this when executing commands
958 * from a script or when being called recursive (e.g. for ":e
959 * +command file").
960 */
961 if (!(flags & DOCMD_NOWAIT) && !recursive)
962 {
963 msg_didout_before_start = msg_didout;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100964 msg_didany = FALSE; // no output yet
Bram Moolenaar071d4272004-06-13 20:20:40 +0000965 msg_start();
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100966 msg_scroll = TRUE; // put messages below each other
967 ++no_wait_return; // don't wait for return until finished
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968 ++RedrawingDisabled;
969 did_inc = TRUE;
970 }
971 }
972
Bram Moolenaar823654b2020-05-29 23:03:09 +0200973 if ((p_verbose >= 15 && SOURCING_NAME != NULL) || p_verbose >= 16)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100974 msg_verbose_cmd(SOURCING_LNUM, cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000975
976 /*
977 * 2. Execute one '|' separated command.
978 * do_one_cmd() will return NULL if there is no trailing '|'.
979 * "cmdline_copy" can change, e.g. for '%' and '#' expansion.
980 */
981 ++recursive;
982 next_cmdline = do_one_cmd(&cmdline_copy, flags & DOCMD_VERBOSE,
983#ifdef FEAT_EVAL
984 &cstack,
985#endif
986 cmd_getline, cmd_cookie);
987 --recursive;
988
989#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000990 if (cmd_cookie == (void *)&cmd_loop_cookie)
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100991 // Use "current_line" from "cmd_loop_cookie", it may have been
992 // incremented when defining a function.
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000993 current_line = cmd_loop_cookie.current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994#endif
995
996 if (next_cmdline == NULL)
997 {
Bram Moolenaard23a8232018-02-10 18:45:26 +0100998 VIM_CLEAR(cmdline_copy);
Bram Moolenaard7663c22019-08-06 21:59:57 +0200999
Bram Moolenaar071d4272004-06-13 20:20:40 +00001000 /*
1001 * If the command was typed, remember it for the ':' register.
1002 * Do this AFTER executing the command to make :@: work.
1003 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001004 if (getline_equal(fgetline, cookie, getexline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005 && new_last_cmdline != NULL)
1006 {
1007 vim_free(last_cmdline);
1008 last_cmdline = new_last_cmdline;
1009 new_last_cmdline = NULL;
1010 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001011 }
1012 else
1013 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001014 // need to copy the command after the '|' to cmdline_copy, for the
1015 // next do_one_cmd()
Bram Moolenaara7241f52008-06-24 20:39:31 +00001016 STRMOVE(cmdline_copy, next_cmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017 next_cmdline = cmdline_copy;
1018 }
1019
1020
1021#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001022 // reset did_emsg for a function that is not aborted by an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023 if (did_emsg && !force_abort
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001024 && getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025 && !func_has_abort(real_cookie))
1026 did_emsg = FALSE;
1027
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001028 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029 {
1030 ++current_line;
1031
1032 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001033 * An ":endwhile", ":endfor" and ":continue" is handled here.
1034 * If we were executing commands, jump back to the ":while" or
1035 * ":for".
1036 * If we were not executing commands, decrement cs_looplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001037 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001038 if (cstack.cs_lflags & (CSL_HAD_CONT | CSL_HAD_ENDLOOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001039 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001040 cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001042 // Jump back to the matching ":while" or ":for". Be careful
1043 // not to use a cs_line[] from an entry that isn't a ":while"
1044 // or ":for": It would make "current_line" invalid and can
1045 // cause a crash.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001046 if (!did_emsg && !got_int && !did_throw
1047 && cstack.cs_idx >= 0
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001048 && (cstack.cs_flags[cstack.cs_idx]
1049 & (CSF_WHILE | CSF_FOR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050 && cstack.cs_line[cstack.cs_idx] >= 0
1051 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE))
1052 {
1053 current_line = cstack.cs_line[cstack.cs_idx];
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001054 // remember we jumped there
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001055 cstack.cs_lflags |= CSL_HAD_LOOP;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001056 line_breakcheck(); // check if CTRL-C typed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001058 // Check for the next breakpoint at or after the ":while"
1059 // or ":for".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060 if (breakpoint != NULL)
1061 {
1062 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001063 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001064 fname,
1065 ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1);
1066 *dbg_tick = debug_tick;
1067 }
1068 }
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001069 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001070 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001071 // can only get here with ":endwhile" or ":endfor"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072 if (cstack.cs_idx >= 0)
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001073 rewind_conditionals(&cstack, cstack.cs_idx - 1,
1074 CSF_WHILE | CSF_FOR, &cstack.cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001075 }
1076 }
1077
1078 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001079 * For a ":while" or ":for" we need to remember the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001081 else if (cstack.cs_lflags & CSL_HAD_LOOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001083 cstack.cs_lflags &= ~CSL_HAD_LOOP;
Bram Moolenaard5053d02020-06-28 15:51:16 +02001084 cstack.cs_line[cstack.cs_idx] = current_line_before;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085 }
1086 }
1087
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001088 // Check for the next breakpoint after a watchexpression
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01001089 if (breakpoint != NULL && has_watchexpr())
1090 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001091 *breakpoint = dbg_find_breakpoint(FALSE, fname, SOURCING_LNUM);
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01001092 *dbg_tick = debug_tick;
1093 }
1094
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095 /*
1096 * When not inside any ":while" loop, clear remembered lines.
1097 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001098 if (cstack.cs_looplevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099 {
1100 if (lines_ga.ga_len > 0)
1101 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001102 SOURCING_LNUM =
Bram Moolenaar071d4272004-06-13 20:20:40 +00001103 ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum;
1104 free_cmdlines(&lines_ga);
1105 }
1106 current_line = 0;
1107 }
1108
1109 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001110 * A ":finally" makes did_emsg, got_int, and did_throw pending for
1111 * being restored at the ":endtry". Reset them here and set the
1112 * ACTIVE and FINALLY flags, so that the finally clause gets executed.
1113 * This includes the case where a missing ":endif", ":endwhile" or
1114 * ":endfor" was detected by the ":finally" itself.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001116 if (cstack.cs_lflags & CSL_HAD_FINA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001117 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001118 cstack.cs_lflags &= ~CSL_HAD_FINA;
1119 report_make_pending(cstack.cs_pending[cstack.cs_idx]
1120 & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001121 did_throw ? (void *)current_exception : NULL);
1122 did_emsg = got_int = did_throw = FALSE;
1123 cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY;
1124 }
1125
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001126 // Update global "trylevel" for recursive calls to do_cmdline() from
1127 // within this loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128 trylevel = initial_trylevel + cstack.cs_trylevel;
1129
1130 /*
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001131 * If the outermost try conditional (across function calls and sourced
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132 * files) is aborted because of an error, an interrupt, or an uncaught
1133 * exception, cancel everything. If it is left normally, reset
1134 * force_abort to get the non-EH compatible abortion behavior for
1135 * the rest of the script.
1136 */
1137 if (trylevel == 0 && !did_emsg && !got_int && !did_throw)
1138 force_abort = FALSE;
1139
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001140 // Convert an interrupt to an exception if appropriate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001141 (void)do_intthrow(&cstack);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001142#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001143
1144 }
1145 /*
1146 * Continue executing command lines when:
1147 * - no CTRL-C typed, no aborting error, no exception thrown or try
1148 * conditionals need to be checked for executing finally clauses or
1149 * catching an interrupt exception
1150 * - didn't get an error message or lines are not typed
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001151 * - there is a command after '|', inside a :if, :while, :for or :try, or
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152 * looping for ":source" command or function call.
1153 */
1154 while (!((got_int
1155#ifdef FEAT_EVAL
1156 || (did_emsg && force_abort) || did_throw
1157#endif
1158 )
1159#ifdef FEAT_EVAL
1160 && cstack.cs_trylevel == 0
1161#endif
1162 )
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001163 && !(did_emsg
1164#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001165 // Keep going when inside try/catch, so that the error can be
1166 // deal with, except when it is a syntax error, it may cause
1167 // the :endtry to be missed.
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001168 && (cstack.cs_trylevel == 0 || did_emsg_syntax)
1169#endif
1170 && used_getline
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001171 && (getline_equal(fgetline, cookie, getexmodeline)
1172 || getline_equal(fgetline, cookie, getexline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173 && (next_cmdline != NULL
1174#ifdef FEAT_EVAL
1175 || cstack.cs_idx >= 0
1176#endif
1177 || (flags & DOCMD_REPEAT)));
1178
1179 vim_free(cmdline_copy);
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001180 did_emsg_syntax = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181#ifdef FEAT_EVAL
1182 free_cmdlines(&lines_ga);
1183 ga_clear(&lines_ga);
1184
1185 if (cstack.cs_idx >= 0)
1186 {
1187 /*
1188 * If a sourced file or executed function ran to its end, report the
1189 * unclosed conditional.
1190 */
1191 if (!got_int && !did_throw
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001192 && ((getline_equal(fgetline, cookie, getsourceline)
1193 && !source_finished(fgetline, cookie))
1194 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195 && !func_has_ended(real_cookie))))
1196 {
1197 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001198 emsg(_(e_endtry));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001200 emsg(_(e_endwhile));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001201 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001202 emsg(_(e_endfor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001204 emsg(_(e_endif));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205 }
1206
1207 /*
1208 * Reset "trylevel" in case of a ":finish" or ":return" or a missing
1209 * ":endtry" in a sourced file or executed function. If the try
1210 * conditional is in its finally clause, ignore anything pending.
1211 * If it is in a catch clause, finish the caught exception.
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001212 * Also cleanup any "cs_forinfo" structures.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001213 */
1214 do
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001215 {
1216 int idx = cleanup_conditionals(&cstack, 0, TRUE);
1217
Bram Moolenaar89e5d682005-01-17 22:06:23 +00001218 if (idx >= 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001219 --idx; // remove try block not in its finally clause
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001220 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
1221 &cstack.cs_looplevel);
1222 }
1223 while (cstack.cs_idx >= 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224 trylevel = initial_trylevel;
1225 }
1226
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001227 // If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
1228 // lack was reported above and the error message is to be converted to an
1229 // exception, do this now after rewinding the cstack.
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001230 do_errthrow(&cstack, getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001231 ? (char_u *)"endfunction" : (char_u *)NULL);
1232
1233 if (trylevel == 0)
1234 {
1235 /*
1236 * When an exception is being thrown out of the outermost try
1237 * conditional, discard the uncaught exception, disable the conversion
1238 * of interrupts or errors to exceptions, and ensure that no more
1239 * commands are executed.
1240 */
1241 if (did_throw)
1242 {
1243 void *p = NULL;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02001244 msglist_T *messages = NULL, *next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001245
1246 /*
1247 * If the uncaught exception is a user exception, report it as an
1248 * error. If it is an error exception, display the saved error
1249 * message now. For an interrupt exception, do nothing; the
1250 * interrupt message is given elsewhere.
1251 */
1252 switch (current_exception->type)
1253 {
1254 case ET_USER:
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001255 vim_snprintf((char *)IObuff, IOSIZE,
1256 _("E605: Exception not caught: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001257 current_exception->value);
1258 p = vim_strsave(IObuff);
1259 break;
1260 case ET_ERROR:
1261 messages = current_exception->messages;
1262 current_exception->messages = NULL;
1263 break;
1264 case ET_INTERRUPT:
1265 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001266 }
1267
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001268 estack_push(ETYPE_EXCEPT, current_exception->throw_name,
1269 current_exception->throw_lnum);
Bram Moolenaare31ee862020-01-07 20:59:34 +01001270 ESTACK_CHECK_SETUP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001271 current_exception->throw_name = NULL;
1272
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001273 discard_current_exception(); // uses IObuff if 'verbose'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274 suppress_errthrow = TRUE;
1275 force_abort = TRUE;
1276
1277 if (messages != NULL)
1278 {
1279 do
1280 {
1281 next = messages->next;
1282 emsg(messages->msg);
1283 vim_free(messages->msg);
Bram Moolenaar5fbf3bc2020-06-01 21:13:11 +02001284 vim_free(messages->sfile);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285 vim_free(messages);
1286 messages = next;
1287 }
1288 while (messages != NULL);
1289 }
1290 else if (p != NULL)
1291 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01001292 emsg(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001293 vim_free(p);
1294 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001295 vim_free(SOURCING_NAME);
Bram Moolenaare31ee862020-01-07 20:59:34 +01001296 ESTACK_CHECK_NOW
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001297 estack_pop();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298 }
1299
1300 /*
1301 * On an interrupt or an aborting error not converted to an exception,
1302 * disable the conversion of errors to exceptions. (Interrupts are not
Bram Moolenaar2196bce2020-04-12 20:01:11 +02001303 * converted anymore, here.) This enables also the interrupt message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001304 * when force_abort is set and did_emsg unset in case of an interrupt
1305 * from a finally clause after an error.
1306 */
1307 else if (got_int || (did_emsg && force_abort))
1308 suppress_errthrow = TRUE;
1309 }
1310
1311 /*
1312 * The current cstack will be freed when do_cmdline() returns. An uncaught
1313 * exception will have to be rethrown in the previous cstack. If a function
1314 * has just returned or a script file was just finished and the previous
1315 * cstack belongs to the same function or, respectively, script file, it
1316 * will have to be checked for finally clauses to be executed due to the
1317 * ":return" or ":finish". This is done in do_one_cmd().
1318 */
1319 if (did_throw)
1320 need_rethrow = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001321 if ((getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322 && ex_nesting_level > source_level(real_cookie))
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001323 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001324 && ex_nesting_level > func_level(real_cookie) + 1))
1325 {
1326 if (!did_throw)
1327 check_cstack = TRUE;
1328 }
1329 else
1330 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001331 // When leaving a function, reduce nesting level.
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001332 if (getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333 --ex_nesting_level;
1334 /*
1335 * Go to debug mode when returning from a function in which we are
1336 * single-stepping.
1337 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001338 if ((getline_equal(fgetline, cookie, getsourceline)
1339 || getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340 && ex_nesting_level + 1 <= debug_break_level)
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001341 do_debug(getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342 ? (char_u *)_("End of sourced file")
1343 : (char_u *)_("End of function"));
1344 }
1345
1346 /*
1347 * Restore the exception environment (done after returning from the
1348 * debugger).
1349 */
1350 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001351 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352
1353 msg_list = saved_msg_list;
Bram Moolenaar292b90d2020-03-18 15:23:16 +01001354
1355 // Cleanup if "cs_emsg_silent_list" remains.
1356 if (cstack.cs_emsg_silent_list != NULL)
1357 {
1358 eslist_T *elem, *temp;
1359
1360 for (elem = cstack.cs_emsg_silent_list; elem != NULL; elem = temp)
1361 {
1362 temp = elem->next;
1363 vim_free(elem);
1364 }
1365 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001366#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367
1368 /*
1369 * If there was too much output to fit on the command line, ask the user to
1370 * hit return before redrawing the screen. With the ":global" command we do
1371 * this only once after the command is finished.
1372 */
1373 if (did_inc)
1374 {
1375 --RedrawingDisabled;
1376 --no_wait_return;
1377 msg_scroll = FALSE;
1378
1379 /*
1380 * When just finished an ":if"-":else" which was typed, no need to
1381 * wait for hit-return. Also for an error situation.
1382 */
1383 if (retval == FAIL
1384#ifdef FEAT_EVAL
1385 || (did_endif && KeyTyped && !did_emsg)
1386#endif
1387 )
1388 {
1389 need_wait_return = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001390 msg_didany = FALSE; // don't wait when restarting edit
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391 }
1392 else if (need_wait_return)
1393 {
1394 /*
1395 * The msg_start() above clears msg_didout. The wait_return we do
1396 * here should not overwrite the command that may be shown before
1397 * doing that.
1398 */
1399 msg_didout |= msg_didout_before_start;
1400 wait_return(FALSE);
1401 }
1402 }
1403
Bram Moolenaar2a942252012-11-28 23:03:07 +01001404#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001405 did_endif = FALSE; // in case do_cmdline used recursively
Bram Moolenaar2a942252012-11-28 23:03:07 +01001406#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407 /*
1408 * Reset if_level, in case a sourced script file contains more ":if" than
1409 * ":endif" (could be ":if x | foo | endif").
1410 */
1411 if_level = 0;
Bram Moolenaar2e18a122012-11-28 19:10:54 +01001412#endif
Bram Moolenaard4ad0d42012-11-28 17:34:48 +01001413
Bram Moolenaar071d4272004-06-13 20:20:40 +00001414 --call_depth;
1415 return retval;
1416}
1417
1418#ifdef FEAT_EVAL
1419/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001420 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421 */
1422 static char_u *
Bram Moolenaare96a2492019-06-25 04:12:16 +02001423get_loop_line(int c, void *cookie, int indent, int do_concat)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001424{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001425 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001426 wcmd_T *wp;
1427 char_u *line;
1428
1429 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1430 {
1431 if (cp->repeating)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001432 return NULL; // trying to read past ":endwhile"/":endfor"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001433
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001434 // First time inside the ":while"/":for": get line normally.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001435 if (cp->getline == NULL)
Bram Moolenaare96a2492019-06-25 04:12:16 +02001436 line = getcmdline(c, 0L, indent, do_concat);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001437 else
Bram Moolenaare96a2492019-06-25 04:12:16 +02001438 line = cp->getline(c, cp->cookie, indent, do_concat);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001439 if (line != NULL && store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001440 ++cp->current_line;
1441
1442 return line;
1443 }
1444
1445 KeyTyped = FALSE;
1446 ++cp->current_line;
1447 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001448 SOURCING_LNUM = wp->lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001449 return vim_strsave(wp->line);
1450}
1451
1452/*
1453 * Store a line in "gap" so that a ":while" loop can execute it again.
1454 */
1455 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001456store_loop_line(garray_T *gap, char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001457{
1458 if (ga_grow(gap, 1) == FAIL)
1459 return FAIL;
1460 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001461 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = SOURCING_LNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001462 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001463 return OK;
1464}
1465
1466/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001467 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001468 */
1469 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001470free_cmdlines(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001471{
1472 while (gap->ga_len > 0)
1473 {
1474 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1475 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476 }
1477}
1478#endif
1479
1480/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001481 * If "fgetline" is get_loop_line(), return TRUE if the getline it uses equals
1482 * "func". * Otherwise return TRUE when "fgetline" equals "func".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001485getline_equal(
Bram Moolenaare96a2492019-06-25 04:12:16 +02001486 char_u *(*fgetline)(int, void *, int, int),
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001487 void *cookie UNUSED, // argument for fgetline()
Bram Moolenaare96a2492019-06-25 04:12:16 +02001488 char_u *(*func)(int, void *, int, int))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001489{
1490#ifdef FEAT_EVAL
Bram Moolenaare96a2492019-06-25 04:12:16 +02001491 char_u *(*gp)(int, void *, int, int);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001492 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001493
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001494 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1495 // function that's originally used to obtain the lines. This may be
1496 // nested several levels.
Bram Moolenaar89d40322006-08-29 15:30:07 +00001497 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001498 cp = (struct loop_cookie *)cookie;
1499 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001500 {
1501 gp = cp->getline;
1502 cp = cp->cookie;
1503 }
1504 return gp == func;
1505#else
Bram Moolenaar89d40322006-08-29 15:30:07 +00001506 return fgetline == func;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001507#endif
1508}
1509
Bram Moolenaar071d4272004-06-13 20:20:40 +00001510/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001511 * If "fgetline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001512 * getline function. Otherwise return "cookie".
1513 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001514 void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001515getline_cookie(
Bram Moolenaare96a2492019-06-25 04:12:16 +02001516 char_u *(*fgetline)(int, void *, int, int) UNUSED,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001517 void *cookie) // argument for fgetline()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518{
Bram Moolenaar13505972019-01-24 15:04:48 +01001519#ifdef FEAT_EVAL
Bram Moolenaare96a2492019-06-25 04:12:16 +02001520 char_u *(*gp)(int, void *, int, int);
Bram Moolenaard5053d02020-06-28 15:51:16 +02001521 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001522
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001523 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1524 // cookie that's originally used to obtain the lines. This may be nested
1525 // several levels.
Bram Moolenaar89d40322006-08-29 15:30:07 +00001526 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001527 cp = (struct loop_cookie *)cookie;
1528 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001529 {
1530 gp = cp->getline;
1531 cp = cp->cookie;
1532 }
1533 return cp;
Bram Moolenaar13505972019-01-24 15:04:48 +01001534#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001535 return cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001536#endif
Bram Moolenaar13505972019-01-24 15:04:48 +01001537}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001538
Bram Moolenaard5053d02020-06-28 15:51:16 +02001539#if defined(FEAT_EVAL) || defined(PROT)
1540/*
1541 * Get the next line source line without advancing.
1542 */
1543 char_u *
1544getline_peek(
1545 char_u *(*fgetline)(int, void *, int, int) UNUSED,
1546 void *cookie) // argument for fgetline()
1547{
1548 char_u *(*gp)(int, void *, int, int);
1549 struct loop_cookie *cp;
1550 wcmd_T *wp;
1551
1552 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1553 // cookie that's originally used to obtain the lines. This may be nested
1554 // several levels.
1555 gp = fgetline;
1556 cp = (struct loop_cookie *)cookie;
1557 while (gp == get_loop_line)
1558 {
1559 if (cp->current_line + 1 < cp->lines_gap->ga_len)
1560 {
1561 // executing lines a second time, use the stored copy
1562 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line + 1;
1563 return wp->line;
1564 }
1565 gp = cp->getline;
1566 cp = cp->cookie;
1567 }
1568 if (gp == getsourceline)
1569 return source_nextline(cp);
1570 return NULL;
1571}
1572#endif
1573
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001574
1575/*
1576 * Helper function to apply an offset for buffer commands, i.e. ":bdelete",
1577 * ":bwipeout", etc.
1578 * Returns the buffer number.
1579 */
1580 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001581compute_buffer_local_count(int addr_type, int lnum, int offset)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001582{
1583 buf_T *buf;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001584 buf_T *nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001585 int count = offset;
1586
1587 buf = firstbuf;
1588 while (buf->b_next != NULL && buf->b_fnum < lnum)
1589 buf = buf->b_next;
1590 while (count != 0)
1591 {
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001592 count += (offset < 0) ? 1 : -1;
1593 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1594 if (nextbuf == NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001595 break;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001596 buf = nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001597 if (addr_type == ADDR_LOADED_BUFFERS)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001598 // skip over unloaded buffers
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001599 while (buf->b_ml.ml_mfp == NULL)
1600 {
1601 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1602 if (nextbuf == NULL)
1603 break;
1604 buf = nextbuf;
1605 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001606 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001607 // we might have gone too far, last buffer is not loadedd
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001608 if (addr_type == ADDR_LOADED_BUFFERS)
1609 while (buf->b_ml.ml_mfp == NULL)
1610 {
1611 nextbuf = (offset >= 0) ? buf->b_prev : buf->b_next;
1612 if (nextbuf == NULL)
1613 break;
1614 buf = nextbuf;
1615 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001616 return buf->b_fnum;
1617}
1618
Bram Moolenaarb7316892019-05-01 18:08:42 +02001619/*
1620 * Return the window number of "win".
1621 * When "win" is NULL return the number of windows.
1622 */
Bram Moolenaarf240e182014-11-27 18:33:02 +01001623 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001624current_win_nr(win_T *win)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001625{
1626 win_T *wp;
1627 int nr = 0;
1628
Bram Moolenaar29323592016-07-24 22:04:11 +02001629 FOR_ALL_WINDOWS(wp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001630 {
1631 ++nr;
1632 if (wp == win)
1633 break;
1634 }
1635 return nr;
1636}
1637
1638 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001639current_tab_nr(tabpage_T *tab)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001640{
1641 tabpage_T *tp;
1642 int nr = 0;
1643
Bram Moolenaar29323592016-07-24 22:04:11 +02001644 FOR_ALL_TABPAGES(tp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001645 {
1646 ++nr;
1647 if (tp == tab)
1648 break;
1649 }
1650 return nr;
1651}
1652
1653# define CURRENT_WIN_NR current_win_nr(curwin)
1654# define LAST_WIN_NR current_win_nr(NULL)
1655# define CURRENT_TAB_NR current_tab_nr(curtab)
1656# define LAST_TAB_NR current_tab_nr(NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001657
Bram Moolenaar071d4272004-06-13 20:20:40 +00001658/*
1659 * Execute one Ex command.
1660 *
1661 * If 'sourcing' is TRUE, the command will be included in the error message.
1662 *
1663 * 1. skip comment lines and leading space
1664 * 2. handle command modifiers
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001665 * 3. find the command
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001666 * 4. parse range
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001667 * 5. Parse the command.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001668 * 6. parse arguments
1669 * 7. switch on command name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001670 *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001671 * Note: "fgetline" can be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001672 *
1673 * This function may be called recursively!
1674 */
1675#if (_MSC_VER == 1200)
1676/*
Bram Moolenaared203462004-06-16 11:19:22 +00001677 * Avoid optimisation bug in VC++ version 6.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678 */
Bram Moolenaar281bdce2005-01-25 21:53:18 +00001679 #pragma optimize( "g", off )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001680#endif
1681 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001682do_one_cmd(
Bram Moolenaarddef1292019-12-16 17:10:33 +01001683 char_u **cmdlinep,
1684 int sourcing,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001685#ifdef FEAT_EVAL
Bram Moolenaarddef1292019-12-16 17:10:33 +01001686 cstack_T *cstack,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687#endif
Bram Moolenaarddef1292019-12-16 17:10:33 +01001688 char_u *(*fgetline)(int, void *, int, int),
1689 void *cookie) // argument for fgetline()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001690{
Bram Moolenaarddef1292019-12-16 17:10:33 +01001691 char_u *p;
1692 linenr_T lnum;
1693 long n;
1694 char *errormsg = NULL; // error message
1695 char_u *after_modifier = NULL;
1696 exarg_T ea; // Ex command arguments
1697 int save_msg_scroll = msg_scroll;
1698 cmdmod_T save_cmdmod;
1699 int save_reg_executing = reg_executing;
1700 int ni; // set when Not Implemented
1701 char_u *cmd;
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001702#ifdef FEAT_EVAL
1703 int starts_with_colon;
1704#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001705
Bram Moolenaara80faa82020-04-12 19:37:17 +02001706 CLEAR_FIELD(ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001707 ea.line1 = 1;
1708 ea.line2 = 1;
1709#ifdef FEAT_EVAL
1710 ++ex_nesting_level;
1711#endif
1712
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001713 // When the last file has not been edited :q has to be typed twice.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714 if (quitmore
1715#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001716 // avoid that a function call in 'statusline' does this
Bram Moolenaar89d40322006-08-29 15:30:07 +00001717 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001718#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001719 // avoid that an autocommand, e.g. QuitPre, does this
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001720 && !getline_equal(fgetline, cookie, getnextac))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721 --quitmore;
1722
1723 /*
1724 * Reset browse, confirm, etc.. They are restored when returning, for
1725 * recursive calls.
1726 */
1727 save_cmdmod = cmdmod;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001728
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001729 // "#!anything" is handled like a comment.
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001730 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1731 goto doend;
1732
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02001733/*
1734 * 1. Skip comment lines and leading white space and colons.
1735 * 2. Handle command modifiers.
1736 */
1737 // The "ea" structure holds the arguments that can be used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738 ea.cmd = *cmdlinep;
Bram Moolenaareffed932018-08-14 13:38:17 +02001739 ea.cmdlinep = cmdlinep;
1740 ea.getline = fgetline;
1741 ea.cookie = cookie;
1742#ifdef FEAT_EVAL
1743 ea.cstack = cstack;
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001744 starts_with_colon = *skipwhite(ea.cmd) == ':';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745#endif
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02001746 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaareffed932018-08-14 13:38:17 +02001747 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001748
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001749 after_modifier = ea.cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001750
1751#ifdef FEAT_EVAL
1752 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
1753 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
1754#else
1755 ea.skip = (if_level > 0);
1756#endif
1757
Bram Moolenaar071d4272004-06-13 20:20:40 +00001758/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001759 * 3. Skip over the range to find the command. Let "p" point to after it.
1760 *
1761 * We need the command to know what kind of range it uses.
1762 */
1763 cmd = ea.cmd;
1764 ea.cmd = skip_range(ea.cmd, NULL);
1765 if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
1766 ea.cmd = skipwhite(ea.cmd + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001767
1768#ifdef FEAT_EVAL
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001769 if (current_sctx.sc_version == SCRIPT_VERSION_VIM9 && !starts_with_colon)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02001770 {
1771 if (ea.cmd > cmd)
1772 {
1773 emsg(_(e_colon_required));
1774 goto doend;
1775 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001776 p = find_ex_command(&ea, NULL, lookup_scriptvar, NULL);
Bram Moolenaardf069ee2020-06-22 23:02:51 +02001777 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001778 else
1779#endif
1780 p = find_ex_command(&ea, NULL, NULL, NULL);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001781
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001782#ifdef FEAT_EVAL
1783# ifdef FEAT_PROFILE
1784 // Count this line for profiling if skip is TRUE.
1785 if (do_profiling == PROF_YES
1786 && (!ea.skip || cstack->cs_idx == 0 || (cstack->cs_idx > 0
1787 && (cstack->cs_flags[cstack->cs_idx - 1] & CSF_ACTIVE))))
1788 {
1789 int skip = did_emsg || got_int || did_throw;
1790
1791 if (ea.cmdidx == CMD_catch)
1792 skip = !skip && !(cstack->cs_idx >= 0
1793 && (cstack->cs_flags[cstack->cs_idx] & CSF_THROWN)
1794 && !(cstack->cs_flags[cstack->cs_idx] & CSF_CAUGHT));
1795 else if (ea.cmdidx == CMD_else || ea.cmdidx == CMD_elseif)
1796 skip = skip || !(cstack->cs_idx >= 0
1797 && !(cstack->cs_flags[cstack->cs_idx]
1798 & (CSF_ACTIVE | CSF_TRUE)));
1799 else if (ea.cmdidx == CMD_finally)
1800 skip = FALSE;
1801 else if (ea.cmdidx != CMD_endif
1802 && ea.cmdidx != CMD_endfor
1803 && ea.cmdidx != CMD_endtry
1804 && ea.cmdidx != CMD_endwhile)
1805 skip = ea.skip;
1806
1807 if (!skip)
1808 {
1809 if (getline_equal(fgetline, cookie, get_func_line))
1810 func_line_exec(getline_cookie(fgetline, cookie));
1811 else if (getline_equal(fgetline, cookie, getsourceline))
1812 script_line_exec();
1813 }
1814 }
1815# endif
1816
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001817 // May go to debug mode. If this happens and the ">quit" debug command is
1818 // used, throw an interrupt exception and skip the next command.
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001819 dbg_check_breakpoint(&ea);
1820 if (!ea.skip && got_int)
1821 {
1822 ea.skip = TRUE;
1823 (void)do_intthrow(cstack);
1824 }
1825#endif
1826
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001827/*
1828 * 4. parse a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00001829 *
1830 * where 'addr' is:
1831 *
1832 * % (entire file)
1833 * $ [+-NUM]
1834 * 'x [+-NUM] (where x denotes a currently defined mark)
1835 * . [+-NUM]
1836 * [+-NUM]..
1837 * NUM
1838 *
1839 * The ea.cmd pointer is updated to point to the first character following the
1840 * range spec. If an initial address is found, but no second, the upper bound
1841 * is equal to the lower.
1842 */
1843
Bram Moolenaar25190db2019-05-04 15:05:28 +02001844 // ea.addr_type for user commands is set by find_ucmd
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001845 if (!IS_USER_CMDIDX(ea.cmdidx))
1846 {
1847 if (ea.cmdidx != CMD_SIZE)
1848 ea.addr_type = cmdnames[(int)ea.cmdidx].cmd_addr_type;
1849 else
1850 ea.addr_type = ADDR_LINES;
1851
Bram Moolenaar25190db2019-05-04 15:05:28 +02001852 // :wincmd range depends on the argument.
Bram Moolenaar164f3262015-01-14 21:22:01 +01001853 if (ea.cmdidx == CMD_wincmd && p != NULL)
1854 get_wincmd_addr_type(skipwhite(p), &ea);
Bram Moolenaar25190db2019-05-04 15:05:28 +02001855#ifdef FEAT_QUICKFIX
1856 // :.cc in quickfix window uses line number
1857 if ((ea.cmdidx == CMD_cc || ea.cmdidx == CMD_ll) && bt_quickfix(curbuf))
1858 ea.addr_type = ADDR_OTHER;
1859#endif
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001860 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001861
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001862 ea.cmd = cmd;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02001863 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaaree8415b2018-08-10 23:13:12 +02001864 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001865
Bram Moolenaar071d4272004-06-13 20:20:40 +00001866/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001867 * 5. Parse the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868 */
1869
1870 /*
1871 * Skip ':' and any white space
1872 */
1873 ea.cmd = skipwhite(ea.cmd);
1874 while (*ea.cmd == ':')
1875 ea.cmd = skipwhite(ea.cmd + 1);
1876
1877 /*
1878 * If we got a line, but no command, then go to the line.
1879 * If we find a '|' or '\n' we set ea.nextcmd.
1880 */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001881 if (*ea.cmd == NUL || *ea.cmd == '"'
Bram Moolenaar7a092242020-04-16 22:10:49 +02001882#ifdef FEAT_EVAL
Bram Moolenaara26b9702020-04-18 19:53:28 +02001883 || (*ea.cmd == '#' && ea.cmd[1] != '{'
1884 && !starts_with_colon && in_vim9script())
Bram Moolenaar7a092242020-04-16 22:10:49 +02001885#endif
1886 || (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887 {
1888 /*
1889 * strange vi behaviour:
1890 * ":3" jumps to line 3
1891 * ":3|..." prints line 3
1892 * ":|" prints current line
1893 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001894 if (ea.skip) // skip this if inside :if
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001896 if (*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897 {
1898 ea.cmdidx = CMD_print;
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001899 ea.argt = EX_RANGE+EX_COUNT+EX_TRLBAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900 if ((errormsg = invalid_range(&ea)) == NULL)
1901 {
1902 correct_range(&ea);
1903 ex_print(&ea);
1904 }
1905 }
1906 else if (ea.addr_count != 0)
1907 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001908 if (ea.line2 > curbuf->b_ml.ml_line_count)
1909 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001910 // With '-' in 'cpoptions' a line number past the file is an
1911 // error, otherwise put it at the end of the file.
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001912 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
1913 ea.line2 = -1;
1914 else
1915 ea.line2 = curbuf->b_ml.ml_line_count;
1916 }
1917
1918 if (ea.line2 < 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001919 errormsg = _(e_invrange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001920 else
1921 {
1922 if (ea.line2 == 0)
1923 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924 else
1925 curwin->w_cursor.lnum = ea.line2;
1926 beginline(BL_SOL | BL_FIX);
1927 }
1928 }
1929 goto doend;
1930 }
1931
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001932 // If this looks like an undefined user command and there are CmdUndefined
1933 // autocommands defined, trigger the matching autocommands.
Bram Moolenaard5005162014-08-22 23:05:54 +02001934 if (p != NULL && ea.cmdidx == CMD_SIZE && !ea.skip
1935 && ASCII_ISUPPER(*ea.cmd)
1936 && has_cmdundefined())
1937 {
Bram Moolenaard5005162014-08-22 23:05:54 +02001938 int ret;
1939
Bram Moolenaar5a31b462014-08-23 14:16:20 +02001940 p = ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02001941 while (ASCII_ISALNUM(*p))
1942 ++p;
Bram Moolenaar158ea172020-06-18 17:28:39 +02001943 p = vim_strnsave(ea.cmd, p - ea.cmd);
Bram Moolenaard5005162014-08-22 23:05:54 +02001944 ret = apply_autocmds(EVENT_CMDUNDEFINED, p, p, TRUE, NULL);
1945 vim_free(p);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001946 // If the autocommands did something and didn't cause an error, try
1947 // finding the command again.
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001948 p = (ret
1949#ifdef FEAT_EVAL
1950 && !aborting()
Bram Moolenaard5005162014-08-22 23:05:54 +02001951#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001952 ) ? find_ex_command(&ea, NULL, NULL, NULL) : ea.cmd;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001953 }
Bram Moolenaard5005162014-08-22 23:05:54 +02001954
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955 if (p == NULL)
1956 {
1957 if (!ea.skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001958 errormsg = _("E464: Ambiguous use of user-defined command");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001959 goto doend;
1960 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001961 // Check for wrong commands.
Bram Moolenaar6f9a4762017-06-22 20:39:17 +02001962 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78
1963 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001964 {
1965 errormsg = uc_fun_cmd();
1966 goto doend;
1967 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001968
Bram Moolenaar071d4272004-06-13 20:20:40 +00001969 if (ea.cmdidx == CMD_SIZE)
1970 {
1971 if (!ea.skip)
1972 {
1973 STRCPY(IObuff, _("E492: Not an editor command"));
1974 if (!sourcing)
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001975 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001976 // If the modifier was parsed OK the error must be in the
1977 // following command
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001978 if (after_modifier != NULL)
1979 append_command(after_modifier);
1980 else
1981 append_command(*cmdlinep);
1982 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001983 errormsg = (char *)IObuff;
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001984 did_emsg_syntax = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001985 }
1986 goto doend;
1987 }
1988
Bram Moolenaar958636c2014-10-21 20:01:58 +02001989 ni = (!IS_USER_CMDIDX(ea.cmdidx)
1990 && (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00001991#ifdef HAVE_EX_SCRIPT_NI
1992 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
1993#endif
1994 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995
1996#ifndef FEAT_EVAL
1997 /*
1998 * When the expression evaluation is disabled, recognize the ":if" and
1999 * ":endif" commands and ignore everything in between it.
2000 */
2001 if (ea.cmdidx == CMD_if)
2002 ++if_level;
2003 if (if_level)
2004 {
2005 if (ea.cmdidx == CMD_endif)
2006 --if_level;
2007 goto doend;
2008 }
2009
2010#endif
2011
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002012 // forced commands
Bram Moolenaar196b3b02008-06-20 16:51:41 +00002013 if (*p == '!' && ea.cmdidx != CMD_substitute
2014 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015 {
2016 ++p;
2017 ea.forceit = TRUE;
2018 }
2019 else
2020 ea.forceit = FALSE;
2021
2022/*
Bram Moolenaarb7316892019-05-01 18:08:42 +02002023 * 6. Parse arguments. Then check for errors.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002024 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02002025 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002026 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002027
2028 if (!ea.skip)
2029 {
2030#ifdef HAVE_SANDBOX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002031 if (sandbox != 0 && !(ea.argt & EX_SBOXOK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002032 {
Bram Moolenaar8c62a082019-02-08 14:34:10 +01002033 // Command not allowed in sandbox.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002034 errormsg = _(e_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002035 goto doend;
2036 }
2037#endif
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002038 if (restricted != 0 && (ea.argt & EX_RESTRICT))
Bram Moolenaar8c62a082019-02-08 14:34:10 +01002039 {
2040 errormsg = _("E981: Command not allowed in rvim");
2041 goto doend;
2042 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002043 if (!curbuf->b_p_ma && (ea.argt & EX_MODIFY))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002045 // Command not allowed in non-'modifiable' buffer
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002046 errormsg = _(e_modifiable);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002047 goto doend;
2048 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002049
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002050 if (text_locked() && !(ea.argt & EX_CMDWIN)
Bram Moolenaar958636c2014-10-21 20:01:58 +02002051 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002052 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002053 // Command not allowed when editing the command line.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002054 errormsg = _(get_text_locked_msg());
Bram Moolenaar071d4272004-06-13 20:20:40 +00002055 goto doend;
2056 }
Bram Moolenaar379fb762018-08-30 15:58:28 +02002057
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002058 // Disallow editing another buffer when "curbuf_lock" is set.
2059 // Do allow ":checktime" (it is postponed).
2060 // Do allow ":edit" (check for an argument later).
2061 // Do allow ":file" with no arguments (check for an argument later).
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002062 if (!(ea.argt & EX_CMDWIN)
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002063 && ea.cmdidx != CMD_checktime
Bram Moolenaar379fb762018-08-30 15:58:28 +02002064 && ea.cmdidx != CMD_edit
2065 && ea.cmdidx != CMD_file
Bram Moolenaar958636c2014-10-21 20:01:58 +02002066 && !IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002067 && curbuf_locked())
2068 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002070 if (!ni && !(ea.argt & EX_RANGE) && ea.addr_count > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002072 // no range allowed
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002073 errormsg = _(e_norange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002074 goto doend;
2075 }
2076 }
2077
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002078 if (!ni && !(ea.argt & EX_BANG) && ea.forceit) // no <!> allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002079 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002080 errormsg = _(e_nobang);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002081 goto doend;
2082 }
2083
2084 /*
2085 * Don't complain about the range if it is not used
2086 * (could happen if line_count is accidentally set to 0).
2087 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002088 if (!ea.skip && !ni && (ea.argt & EX_RANGE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002089 {
2090 /*
2091 * If the range is backwards, ask for confirmation and, if given, swap
2092 * ea.line1 & ea.line2 so it's forwards again.
2093 * When global command is busy, don't ask, will fail below.
2094 */
2095 if (!global_busy && ea.line1 > ea.line2)
2096 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002097 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002098 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002099 if (sourcing || exmode_active)
2100 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002101 errormsg = _("E493: Backwards range given");
Bram Moolenaara5792f52005-11-23 21:25:05 +00002102 goto doend;
2103 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002104 if (ask_yesno((char_u *)
2105 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002106 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002107 }
2108 lnum = ea.line1;
2109 ea.line1 = ea.line2;
2110 ea.line2 = lnum;
2111 }
2112 if ((errormsg = invalid_range(&ea)) != NULL)
2113 goto doend;
2114 }
2115
Bram Moolenaarb7316892019-05-01 18:08:42 +02002116 if ((ea.addr_type == ADDR_OTHER) && ea.addr_count == 0)
2117 // default is 1, not cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118 ea.line2 = 1;
2119
2120 correct_range(&ea);
2121
2122#ifdef FEAT_FOLDING
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002123 if (((ea.argt & EX_WHOLEFOLD) || ea.addr_count >= 2) && !global_busy
Bram Moolenaara3306952016-01-02 21:41:06 +01002124 && ea.addr_type == ADDR_LINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002125 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002126 // Put the first line at the start of a closed fold, put the last line
2127 // at the end of a closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002128 (void)hasFolding(ea.line1, &ea.line1, NULL);
2129 (void)hasFolding(ea.line2, NULL, &ea.line2);
2130 }
2131#endif
2132
2133#ifdef FEAT_QUICKFIX
2134 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002135 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002136 * option here, so things like % get expanded.
2137 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002138 p = replace_makeprg(&ea, p, cmdlinep);
2139 if (p == NULL)
2140 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002141#endif
2142
2143 /*
2144 * Skip to start of argument.
2145 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2146 */
2147 if (ea.cmdidx == CMD_bang)
2148 ea.arg = p;
2149 else
2150 ea.arg = skipwhite(p);
2151
Bram Moolenaar379fb762018-08-30 15:58:28 +02002152 // ":file" cannot be run with an argument when "curbuf_lock" is set
2153 if (ea.cmdidx == CMD_file && *ea.arg != NUL && curbuf_locked())
2154 goto doend;
2155
Bram Moolenaar071d4272004-06-13 20:20:40 +00002156 /*
2157 * Check for "++opt=val" argument.
2158 * Must be first, allow ":w ++enc=utf8 !cmd"
2159 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002160 if (ea.argt & EX_ARGOPT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002161 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2162 if (getargopt(&ea) == FAIL && !ni)
2163 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002164 errormsg = _(e_invarg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165 goto doend;
2166 }
2167
2168 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2169 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002170 if (*ea.arg == '>') // append
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002172 if (*++ea.arg != '>') // typed wrong
Bram Moolenaar071d4272004-06-13 20:20:40 +00002173 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002174 errormsg = _("E494: Use w or w>>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175 goto doend;
2176 }
2177 ea.arg = skipwhite(ea.arg + 1);
2178 ea.append = TRUE;
2179 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002180 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) // :w !filter
Bram Moolenaar071d4272004-06-13 20:20:40 +00002181 {
2182 ++ea.arg;
2183 ea.usefilter = TRUE;
2184 }
2185 }
2186
2187 if (ea.cmdidx == CMD_read)
2188 {
2189 if (ea.forceit)
2190 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002191 ea.usefilter = TRUE; // :r! filter if ea.forceit
Bram Moolenaar071d4272004-06-13 20:20:40 +00002192 ea.forceit = FALSE;
2193 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002194 else if (*ea.arg == '!') // :r !filter
Bram Moolenaar071d4272004-06-13 20:20:40 +00002195 {
2196 ++ea.arg;
2197 ea.usefilter = TRUE;
2198 }
2199 }
2200
2201 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2202 {
2203 ea.amount = 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002204 while (*ea.arg == *ea.cmd) // count number of '>' or '<'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002205 {
2206 ++ea.arg;
2207 ++ea.amount;
2208 }
2209 ea.arg = skipwhite(ea.arg);
2210 }
2211
2212 /*
2213 * Check for "+command" argument, before checking for next command.
2214 * Don't do this for ":read !cmd" and ":write !cmd".
2215 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002216 if ((ea.argt & EX_CMDARG) && !ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002217 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2218
2219 /*
2220 * Check for '|' to separate commands and '"' to start comments.
2221 * Don't do this for ":read !cmd" and ":write !cmd".
2222 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002223 if ((ea.argt & EX_TRLBAR) && !ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224 separate_nextcmd(&ea);
2225
2226 /*
2227 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002228 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2229 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002230 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002231 else if (ea.cmdidx == CMD_bang
Bram Moolenaar67883b42017-07-27 22:57:00 +02002232 || ea.cmdidx == CMD_terminal
Bram Moolenaardf177f62005-02-22 08:39:57 +00002233 || ea.cmdidx == CMD_global
2234 || ea.cmdidx == CMD_vglobal
2235 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002236 {
2237 for (p = ea.arg; *p; ++p)
2238 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002239 // Remove one backslash before a newline, so that it's possible to
2240 // pass a newline to the shell and also a newline that is preceded
2241 // with a backslash. This makes it impossible to end a shell
2242 // command in a backslash, but that doesn't appear useful.
2243 // Halving the number of backslashes is incompatible with previous
2244 // versions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002245 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002246 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002247 else if (*p == '\n')
2248 {
2249 ea.nextcmd = p + 1;
2250 *p = NUL;
2251 break;
2252 }
2253 }
2254 }
2255
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002256 if ((ea.argt & EX_DFLALL) && ea.addr_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002257 {
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002258 buf_T *buf;
2259
Bram Moolenaar071d4272004-06-13 20:20:40 +00002260 ea.line1 = 1;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002261 switch (ea.addr_type)
2262 {
2263 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02002264 case ADDR_OTHER:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002265 ea.line2 = curbuf->b_ml.ml_line_count;
2266 break;
2267 case ADDR_LOADED_BUFFERS:
2268 buf = firstbuf;
2269 while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL)
2270 buf = buf->b_next;
2271 ea.line1 = buf->b_fnum;
2272 buf = lastbuf;
2273 while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL)
2274 buf = buf->b_prev;
2275 ea.line2 = buf->b_fnum;
2276 break;
2277 case ADDR_BUFFERS:
2278 ea.line1 = firstbuf->b_fnum;
2279 ea.line2 = lastbuf->b_fnum;
2280 break;
2281 case ADDR_WINDOWS:
2282 ea.line2 = LAST_WIN_NR;
2283 break;
2284 case ADDR_TABS:
2285 ea.line2 = LAST_TAB_NR;
2286 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01002287 case ADDR_TABS_RELATIVE:
2288 ea.line2 = 1;
2289 break;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002290 case ADDR_ARGUMENTS:
2291 if (ARGCOUNT == 0)
2292 ea.line1 = ea.line2 = 0;
2293 else
2294 ea.line2 = ARGCOUNT;
2295 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02002296 case ADDR_QUICKFIX_VALID:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002297#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02002298 ea.line2 = qf_get_valid_size(&ea);
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002299 if (ea.line2 == 0)
2300 ea.line2 = 1;
Bram Moolenaare906c502015-09-09 21:10:39 +02002301#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002302 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02002303 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02002304 case ADDR_UNSIGNED:
2305 case ADDR_QUICKFIX:
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002306 iemsg(_("INTERNAL: Cannot use EX_DFLALL with ADDR_NONE, ADDR_UNSIGNED or ADDR_QUICKFIX"));
Bram Moolenaarb7316892019-05-01 18:08:42 +02002307 break;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002308 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309 }
2310
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002311 // accept numbered register only when no count allowed (:put)
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002312 if ( (ea.argt & EX_REGSTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313 && *ea.arg != NUL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002314 // Do not allow register = for user commands
Bram Moolenaar958636c2014-10-21 20:01:58 +02002315 && (!IS_USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002316 && !((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002318#ifndef FEAT_CLIPBOARD
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002319 // check these explicitly for a more specific error message
Bram Moolenaar85de2062011-05-05 14:26:41 +02002320 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002321 {
Bram Moolenaar99b12722019-01-14 20:16:40 +01002322 errormsg = _(e_invalidreg);
Bram Moolenaar85de2062011-05-05 14:26:41 +02002323 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324 }
2325#endif
Bram Moolenaar958636c2014-10-21 20:01:58 +02002326 if (valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2327 && !IS_USER_CMDIDX(ea.cmdidx))))
Bram Moolenaar85de2062011-05-05 14:26:41 +02002328 {
2329 ea.regname = *ea.arg++;
2330#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002331 // for '=' register: accept the rest of the line as an expression
Bram Moolenaar85de2062011-05-05 14:26:41 +02002332 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2333 {
2334 set_expr_line(vim_strsave(ea.arg));
2335 ea.arg += STRLEN(ea.arg);
2336 }
2337#endif
2338 ea.arg = skipwhite(ea.arg);
2339 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002340 }
2341
2342 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002343 * Check for a count. When accepting a EX_BUFNAME, don't use "123foo" as a
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344 * count, it's a buffer name.
2345 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002346 if ((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)
2347 && (!(ea.argt & EX_BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01002348 || VIM_ISWHITE(*p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002349 {
2350 n = getdigits(&ea.arg);
2351 ea.arg = skipwhite(ea.arg);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002352 if (n <= 0 && !ni && (ea.argt & EX_ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002353 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002354 errormsg = _(e_zerocount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355 goto doend;
2356 }
Bram Moolenaarb7316892019-05-01 18:08:42 +02002357 if (ea.addr_type != ADDR_LINES) // e.g. :buffer 2, :sleep 3
Bram Moolenaar071d4272004-06-13 20:20:40 +00002358 {
2359 ea.line2 = n;
2360 if (ea.addr_count == 0)
2361 ea.addr_count = 1;
2362 }
2363 else
2364 {
2365 ea.line1 = ea.line2;
2366 ea.line2 += n - 1;
2367 ++ea.addr_count;
2368 /*
2369 * Be vi compatible: no error message for out of range.
2370 */
Bram Moolenaarb7316892019-05-01 18:08:42 +02002371 if (ea.line2 > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372 ea.line2 = curbuf->b_ml.ml_line_count;
2373 }
2374 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002375
2376 /*
2377 * Check for flags: 'l', 'p' and '#'.
2378 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002379 if (ea.argt & EX_FLAGS)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002380 get_flags(&ea);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002381 if (!ni && !(ea.argt & EX_EXTRA) && *ea.arg != NUL
2382 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & EX_TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002383 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002384 // no arguments allowed but there is something
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002385 errormsg = _(e_trailing);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002386 goto doend;
2387 }
2388
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002389 if (!ni && (ea.argt & EX_NEEDARG) && *ea.arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002390 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002391 errormsg = _(e_argreq);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392 goto doend;
2393 }
2394
2395#ifdef FEAT_EVAL
2396 /*
2397 * Skip the command when it's not going to be executed.
2398 * The commands like :if, :endif, etc. always need to be executed.
2399 * Also make an exception for commands that handle a trailing command
2400 * themselves.
2401 */
2402 if (ea.skip)
2403 {
2404 switch (ea.cmdidx)
2405 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002406 // commands that need evaluation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407 case CMD_while:
2408 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002409 case CMD_for:
2410 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411 case CMD_if:
2412 case CMD_elseif:
2413 case CMD_else:
2414 case CMD_endif:
2415 case CMD_try:
2416 case CMD_catch:
2417 case CMD_finally:
2418 case CMD_endtry:
2419 case CMD_function:
Bram Moolenaarab55c682020-03-01 19:41:43 +01002420 case CMD_def:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421 break;
2422
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002423 // Commands that handle '|' themselves. Check: A command should
2424 // either have the EX_TRLBAR flag, appear in this list or appear in
2425 // the list at ":help :bar".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002426 case CMD_aboveleft:
2427 case CMD_and:
2428 case CMD_belowright:
2429 case CMD_botright:
2430 case CMD_browse:
2431 case CMD_call:
2432 case CMD_confirm:
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01002433 case CMD_const:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434 case CMD_delfunction:
2435 case CMD_djump:
2436 case CMD_dlist:
2437 case CMD_dsearch:
2438 case CMD_dsplit:
2439 case CMD_echo:
2440 case CMD_echoerr:
2441 case CMD_echomsg:
2442 case CMD_echon:
Bram Moolenaara76b3152020-02-16 16:20:21 +01002443 case CMD_eval:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002444 case CMD_execute:
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002445 case CMD_filter:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002446 case CMD_help:
2447 case CMD_hide:
2448 case CMD_ijump:
2449 case CMD_ilist:
2450 case CMD_isearch:
2451 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002452 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453 case CMD_keepjumps:
2454 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01002455 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456 case CMD_leftabove:
2457 case CMD_let:
2458 case CMD_lockmarks:
Bram Moolenaarcc4423a2019-11-26 17:05:00 +01002459 case CMD_lockvar:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002460 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002462 case CMD_mzscheme:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01002463 case CMD_noautocmd:
2464 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465 case CMD_perl:
2466 case CMD_psearch:
2467 case CMD_python:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002468 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002469 case CMD_python3:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002470 case CMD_return:
2471 case CMD_rightbelow:
2472 case CMD_ruby:
2473 case CMD_silent:
2474 case CMD_smagic:
2475 case CMD_snomagic:
2476 case CMD_substitute:
2477 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002478 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002479 case CMD_tcl:
2480 case CMD_throw:
2481 case CMD_tilde:
2482 case CMD_topleft:
2483 case CMD_unlet:
Bram Moolenaarcc4423a2019-11-26 17:05:00 +01002484 case CMD_unlockvar:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485 case CMD_verbose:
2486 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002487 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488 break;
2489
2490 default: goto doend;
2491 }
2492 }
2493#endif
2494
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002495 if (ea.argt & EX_XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002496 {
2497 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2498 goto doend;
2499 }
2500
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501 /*
2502 * Accept buffer name. Cannot be used at the same time with a buffer
2503 * number. Don't do this for a user command.
2504 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002505 if ((ea.argt & EX_BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
Bram Moolenaar958636c2014-10-21 20:01:58 +02002506 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002507 {
2508 /*
2509 * :bdelete, :bwipeout and :bunload take several arguments, separated
2510 * by spaces: find next space (skipping over escaped characters).
2511 * The others take one argument: ignore trailing spaces.
2512 */
2513 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2514 || ea.cmdidx == CMD_bunload)
2515 p = skiptowhite_esc(ea.arg);
2516 else
2517 {
2518 p = ea.arg + STRLEN(ea.arg);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002519 while (p > ea.arg && VIM_ISWHITE(p[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520 --p;
2521 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002522 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & EX_BUFUNL) != 0,
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002523 FALSE, FALSE);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002524 if (ea.line2 < 0) // failed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002525 goto doend;
2526 ea.addr_count = 1;
2527 ea.arg = skipwhite(p);
2528 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002529
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002530 // The :try command saves the emsg_silent flag, reset it here when
2531 // ":silent! try" was used, it should only apply to :try itself.
Bram Moolenaareffed932018-08-14 13:38:17 +02002532 if (ea.cmdidx == CMD_try && ea.did_esilent > 0)
Bram Moolenaar2be57332018-02-13 18:05:18 +01002533 {
Bram Moolenaareffed932018-08-14 13:38:17 +02002534 emsg_silent -= ea.did_esilent;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002535 if (emsg_silent < 0)
2536 emsg_silent = 0;
Bram Moolenaareffed932018-08-14 13:38:17 +02002537 ea.did_esilent = 0;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002538 }
2539
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540/*
Bram Moolenaar2be57332018-02-13 18:05:18 +01002541 * 7. Execute the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002543
Bram Moolenaar958636c2014-10-21 20:01:58 +02002544 if (IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002545 {
2546 /*
2547 * Execute a user-defined command.
2548 */
2549 do_ucmd(&ea);
2550 }
2551 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002552 {
2553 /*
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002554 * Call the function to execute the builtin command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002555 */
2556 ea.errmsg = NULL;
2557 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2558 if (ea.errmsg != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002559 errormsg = _(ea.errmsg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002560 }
2561
2562#ifdef FEAT_EVAL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002563 // Set flag that any command was executed, used by ex_vim9script().
Bram Moolenaar16531552020-02-08 16:00:46 +01002564 if (getline_equal(ea.getline, ea.cookie, getsourceline)
2565 && current_sctx.sc_sid > 0)
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002566 SCRIPT_ITEM(current_sctx.sc_sid)->sn_had_command = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002567
Bram Moolenaar071d4272004-06-13 20:20:40 +00002568 /*
2569 * If the command just executed called do_cmdline(), any throw or ":return"
2570 * or ":finish" encountered there must also check the cstack of the still
2571 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2572 * exception, or reanimate a returned function or finished script file and
2573 * return or finish it again.
2574 */
2575 if (need_rethrow)
2576 do_throw(cstack);
2577 else if (check_cstack)
2578 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002579 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002581 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582 && current_func_returned())
2583 do_return(&ea, TRUE, FALSE, NULL);
2584 }
2585 need_rethrow = check_cstack = FALSE;
2586#endif
2587
2588doend:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002589 if (curwin->w_cursor.lnum == 0) // can happen with zero line number
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002590 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002591 curwin->w_cursor.lnum = 1;
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002592 curwin->w_cursor.col = 0;
2593 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002594
2595 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2596 {
2597 if (sourcing)
2598 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002599 if (errormsg != (char *)IObuff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002600 {
2601 STRCPY(IObuff, errormsg);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002602 errormsg = (char *)IObuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002603 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002604 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002605 }
2606 emsg(errormsg);
2607 }
2608#ifdef FEAT_EVAL
2609 do_errthrow(cstack,
Bram Moolenaar958636c2014-10-21 20:01:58 +02002610 (ea.cmdidx != CMD_SIZE && !IS_USER_CMDIDX(ea.cmdidx))
2611 ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002612#endif
2613
Bram Moolenaareffed932018-08-14 13:38:17 +02002614 if (ea.verbose_save >= 0)
2615 p_verbose = ea.verbose_save;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002616
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002617 free_cmdmod();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002618 cmdmod = save_cmdmod;
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01002619 reg_executing = save_reg_executing;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002620
Bram Moolenaareffed932018-08-14 13:38:17 +02002621 if (ea.save_msg_silent != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002622 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002623 // messages could be enabled for a serious error, need to check if the
2624 // counters don't become negative
Bram Moolenaareffed932018-08-14 13:38:17 +02002625 if (!did_emsg || msg_silent > ea.save_msg_silent)
2626 msg_silent = ea.save_msg_silent;
2627 emsg_silent -= ea.did_esilent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002628 if (emsg_silent < 0)
2629 emsg_silent = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002630 // Restore msg_scroll, it's set by file I/O commands, even when no
2631 // message is actually displayed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002632 msg_scroll = save_msg_scroll;
Bram Moolenaar77ab2802009-04-22 12:44:48 +00002633
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002634 // "silent reg" or "silent echo x" inside "redir" leaves msg_col
2635 // somewhere in the line. Put it back in the first column.
Bram Moolenaar77ab2802009-04-22 12:44:48 +00002636 if (redirecting())
2637 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002638 }
2639
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002640#ifdef HAVE_SANDBOX
Bram Moolenaareffed932018-08-14 13:38:17 +02002641 if (ea.did_sandbox)
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002642 --sandbox;
2643#endif
2644
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002645 if (ea.nextcmd && *ea.nextcmd == NUL) // not really a next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00002646 ea.nextcmd = NULL;
2647
2648#ifdef FEAT_EVAL
2649 --ex_nesting_level;
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002650 vim_free(ea.cmdline_tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651#endif
2652
2653 return ea.nextcmd;
2654}
2655#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00002656 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002657#endif
2658
2659/*
Bram Moolenaareffed932018-08-14 13:38:17 +02002660 * Parse and skip over command modifiers:
2661 * - update eap->cmd
2662 * - store flags in "cmdmod".
2663 * - Set ex_pressedreturn for an empty command line.
2664 * - set msg_silent for ":silent"
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002665 * - set 'eventignore' to "all" for ":noautocmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002666 * - set p_verbose for ":verbose"
2667 * - Increment "sandbox" for ":sandbox"
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002668 * When "skip_only" is TRUE the global variables are not changed, except for
2669 * "cmdmod".
Bram Moolenaareffed932018-08-14 13:38:17 +02002670 * Return FAIL when the command is not to be executed.
2671 * May set "errormsg" to an error message.
2672 */
2673 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002674parse_command_modifiers(exarg_T *eap, char **errormsg, int skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002675{
2676 char_u *p;
2677
Bram Moolenaara80faa82020-04-12 19:37:17 +02002678 CLEAR_FIELD(cmdmod);
Bram Moolenaareffed932018-08-14 13:38:17 +02002679 eap->verbose_save = -1;
2680 eap->save_msg_silent = -1;
2681
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002682 // Repeat until no more command modifiers are found.
Bram Moolenaareffed932018-08-14 13:38:17 +02002683 for (;;)
2684 {
Bram Moolenaareffed932018-08-14 13:38:17 +02002685 while (*eap->cmd == ' ' || *eap->cmd == '\t' || *eap->cmd == ':')
2686 ++eap->cmd;
2687
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002688 // in ex mode, an empty line works like :+
Bram Moolenaareffed932018-08-14 13:38:17 +02002689 if (*eap->cmd == NUL && exmode_active
2690 && (getline_equal(eap->getline, eap->cookie, getexmodeline)
2691 || getline_equal(eap->getline, eap->cookie, getexline))
2692 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2693 {
2694 eap->cmd = (char_u *)"+";
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002695 if (!skip_only)
2696 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002697 }
2698
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002699 // ignore comment and empty lines
Bram Moolenaareffed932018-08-14 13:38:17 +02002700 if (*eap->cmd == '"')
2701 return FAIL;
2702 if (*eap->cmd == NUL)
2703 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002704 if (!skip_only)
2705 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002706 return FAIL;
2707 }
2708
Bram Moolenaareffed932018-08-14 13:38:17 +02002709 p = skip_range(eap->cmd, NULL);
2710 switch (*p)
2711 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002712 // When adding an entry, also modify cmd_exists().
Bram Moolenaareffed932018-08-14 13:38:17 +02002713 case 'a': if (!checkforcmd(&eap->cmd, "aboveleft", 3))
2714 break;
2715 cmdmod.split |= WSP_ABOVE;
2716 continue;
2717
2718 case 'b': if (checkforcmd(&eap->cmd, "belowright", 3))
2719 {
2720 cmdmod.split |= WSP_BELOW;
2721 continue;
2722 }
2723 if (checkforcmd(&eap->cmd, "browse", 3))
2724 {
2725#ifdef FEAT_BROWSE_CMD
2726 cmdmod.browse = TRUE;
2727#endif
2728 continue;
2729 }
2730 if (!checkforcmd(&eap->cmd, "botright", 2))
2731 break;
2732 cmdmod.split |= WSP_BOT;
2733 continue;
2734
2735 case 'c': if (!checkforcmd(&eap->cmd, "confirm", 4))
2736 break;
2737#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2738 cmdmod.confirm = TRUE;
2739#endif
2740 continue;
2741
2742 case 'k': if (checkforcmd(&eap->cmd, "keepmarks", 3))
2743 {
2744 cmdmod.keepmarks = TRUE;
2745 continue;
2746 }
2747 if (checkforcmd(&eap->cmd, "keepalt", 5))
2748 {
2749 cmdmod.keepalt = TRUE;
2750 continue;
2751 }
2752 if (checkforcmd(&eap->cmd, "keeppatterns", 5))
2753 {
2754 cmdmod.keeppatterns = TRUE;
2755 continue;
2756 }
2757 if (!checkforcmd(&eap->cmd, "keepjumps", 5))
2758 break;
2759 cmdmod.keepjumps = TRUE;
2760 continue;
2761
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002762 case 'f': // only accept ":filter {pat} cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002763 {
2764 char_u *reg_pat;
2765
2766 if (!checkforcmd(&p, "filter", 4)
2767 || *p == NUL || ends_excmd(*p))
2768 break;
2769 if (*p == '!')
2770 {
2771 cmdmod.filter_force = TRUE;
2772 p = skipwhite(p + 1);
2773 if (*p == NUL || ends_excmd(*p))
2774 break;
2775 }
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002776 if (skip_only)
2777 p = skip_vimgrep_pat(p, NULL, NULL);
2778 else
2779 // NOTE: This puts a NUL after the pattern.
2780 p = skip_vimgrep_pat(p, &reg_pat, NULL);
Bram Moolenaareffed932018-08-14 13:38:17 +02002781 if (p == NULL || *p == NUL)
2782 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002783 if (!skip_only)
2784 {
2785 cmdmod.filter_regmatch.regprog =
Bram Moolenaareffed932018-08-14 13:38:17 +02002786 vim_regcomp(reg_pat, RE_MAGIC);
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002787 if (cmdmod.filter_regmatch.regprog == NULL)
2788 break;
2789 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002790 eap->cmd = p;
2791 continue;
2792 }
2793
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002794 // ":hide" and ":hide | cmd" are not modifiers
Bram Moolenaareffed932018-08-14 13:38:17 +02002795 case 'h': if (p != eap->cmd || !checkforcmd(&p, "hide", 3)
2796 || *p == NUL || ends_excmd(*p))
2797 break;
2798 eap->cmd = p;
2799 cmdmod.hide = TRUE;
2800 continue;
2801
2802 case 'l': if (checkforcmd(&eap->cmd, "lockmarks", 3))
2803 {
2804 cmdmod.lockmarks = TRUE;
2805 continue;
2806 }
2807
2808 if (!checkforcmd(&eap->cmd, "leftabove", 5))
2809 break;
2810 cmdmod.split |= WSP_ABOVE;
2811 continue;
2812
2813 case 'n': if (checkforcmd(&eap->cmd, "noautocmd", 3))
2814 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002815 if (cmdmod.save_ei == NULL && !skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002816 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002817 // Set 'eventignore' to "all". Restore the
2818 // existing option value later.
Bram Moolenaareffed932018-08-14 13:38:17 +02002819 cmdmod.save_ei = vim_strsave(p_ei);
2820 set_string_option_direct((char_u *)"ei", -1,
2821 (char_u *)"all", OPT_FREE, SID_NONE);
2822 }
2823 continue;
2824 }
2825 if (!checkforcmd(&eap->cmd, "noswapfile", 3))
2826 break;
2827 cmdmod.noswapfile = TRUE;
2828 continue;
2829
2830 case 'r': if (!checkforcmd(&eap->cmd, "rightbelow", 6))
2831 break;
2832 cmdmod.split |= WSP_BELOW;
2833 continue;
2834
2835 case 's': if (checkforcmd(&eap->cmd, "sandbox", 3))
2836 {
2837#ifdef HAVE_SANDBOX
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002838 if (!skip_only)
2839 {
2840 if (!eap->did_sandbox)
2841 ++sandbox;
2842 eap->did_sandbox = TRUE;
2843 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002844#endif
2845 continue;
2846 }
2847 if (!checkforcmd(&eap->cmd, "silent", 3))
2848 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002849 if (!skip_only)
2850 {
2851 if (eap->save_msg_silent == -1)
2852 eap->save_msg_silent = msg_silent;
2853 ++msg_silent;
2854 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002855 if (*eap->cmd == '!' && !VIM_ISWHITE(eap->cmd[-1]))
2856 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002857 // ":silent!", but not "silent !cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002858 eap->cmd = skipwhite(eap->cmd + 1);
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002859 if (!skip_only)
2860 {
2861 ++emsg_silent;
2862 ++eap->did_esilent;
2863 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002864 }
2865 continue;
2866
2867 case 't': if (checkforcmd(&p, "tab", 3))
2868 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002869 if (!skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002870 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002871 long tabnr = get_address(eap, &eap->cmd,
2872 ADDR_TABS, eap->skip,
2873 skip_only, FALSE, 1);
2874 if (tabnr == MAXLNUM)
2875 cmdmod.tab = tabpage_index(curtab) + 1;
2876 else
Bram Moolenaareffed932018-08-14 13:38:17 +02002877 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002878 if (tabnr < 0 || tabnr > LAST_TAB_NR)
2879 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002880 *errormsg = _(e_invrange);
Bram Moolenaar548e5982018-12-26 21:45:00 +01002881 return FAIL;
2882 }
2883 cmdmod.tab = tabnr + 1;
Bram Moolenaareffed932018-08-14 13:38:17 +02002884 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002885 }
2886 eap->cmd = p;
2887 continue;
2888 }
2889 if (!checkforcmd(&eap->cmd, "topleft", 2))
2890 break;
2891 cmdmod.split |= WSP_TOP;
2892 continue;
2893
2894 case 'u': if (!checkforcmd(&eap->cmd, "unsilent", 3))
2895 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002896 if (!skip_only)
2897 {
2898 if (eap->save_msg_silent == -1)
2899 eap->save_msg_silent = msg_silent;
2900 msg_silent = 0;
2901 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002902 continue;
2903
2904 case 'v': if (checkforcmd(&eap->cmd, "vertical", 4))
2905 {
2906 cmdmod.split |= WSP_VERT;
2907 continue;
2908 }
2909 if (!checkforcmd(&p, "verbose", 4))
2910 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002911 if (!skip_only)
2912 {
2913 if (eap->verbose_save < 0)
2914 eap->verbose_save = p_verbose;
2915 if (vim_isdigit(*eap->cmd))
2916 p_verbose = atoi((char *)eap->cmd);
2917 else
2918 p_verbose = 1;
2919 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002920 eap->cmd = p;
2921 continue;
2922 }
2923 break;
2924 }
2925
2926 return OK;
2927}
2928
2929/*
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002930 * Free contents of "cmdmod".
2931 */
2932 static void
2933free_cmdmod(void)
2934{
2935 if (cmdmod.save_ei != NULL)
2936 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002937 // Restore 'eventignore' to the value before ":noautocmd".
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002938 set_string_option_direct((char_u *)"ei", -1, cmdmod.save_ei,
2939 OPT_FREE, SID_NONE);
2940 free_string_option(cmdmod.save_ei);
2941 }
2942
2943 if (cmdmod.filter_regmatch.regprog != NULL)
2944 vim_regfree(cmdmod.filter_regmatch.regprog);
2945}
2946
2947/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002948 * Parse the address range, if any, in "eap".
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02002949 * May set the last search pattern, unless "silent" is TRUE.
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002950 * Return FAIL and set "errormsg" or return OK.
2951 */
2952 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002953parse_cmd_address(exarg_T *eap, char **errormsg, int silent)
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002954{
2955 int address_count = 1;
2956 linenr_T lnum;
2957
2958 // Repeat for all ',' or ';' separated addresses.
2959 for (;;)
2960 {
2961 eap->line1 = eap->line2;
2962 switch (eap->addr_type)
2963 {
2964 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02002965 case ADDR_OTHER:
Bram Moolenaarb5383b12020-05-18 19:46:48 +02002966 // Default is the cursor line number. Avoid using an invalid
2967 // line number though.
2968 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
2969 eap->line2 = curbuf->b_ml.ml_line_count;
2970 else
2971 eap->line2 = curwin->w_cursor.lnum;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002972 break;
2973 case ADDR_WINDOWS:
2974 eap->line2 = CURRENT_WIN_NR;
2975 break;
2976 case ADDR_ARGUMENTS:
2977 eap->line2 = curwin->w_arg_idx + 1;
2978 if (eap->line2 > ARGCOUNT)
2979 eap->line2 = ARGCOUNT;
2980 break;
2981 case ADDR_LOADED_BUFFERS:
2982 case ADDR_BUFFERS:
2983 eap->line2 = curbuf->b_fnum;
2984 break;
2985 case ADDR_TABS:
2986 eap->line2 = CURRENT_TAB_NR;
2987 break;
2988 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02002989 case ADDR_UNSIGNED:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002990 eap->line2 = 1;
2991 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002992 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002993#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02002994 eap->line2 = qf_get_cur_idx(eap);
2995#endif
2996 break;
2997 case ADDR_QUICKFIX_VALID:
2998#ifdef FEAT_QUICKFIX
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002999 eap->line2 = qf_get_cur_valid_idx(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003000#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003001 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003002 case ADDR_NONE:
3003 // Will give an error later if a range is found.
3004 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003005 }
3006 eap->cmd = skipwhite(eap->cmd);
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003007 lnum = get_address(eap, &eap->cmd, eap->addr_type, eap->skip, silent,
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003008 eap->addr_count == 0, address_count++);
3009 if (eap->cmd == NULL) // error detected
3010 return FAIL;
3011 if (lnum == MAXLNUM)
3012 {
3013 if (*eap->cmd == '%') // '%' - all lines
3014 {
3015 ++eap->cmd;
3016 switch (eap->addr_type)
3017 {
3018 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003019 case ADDR_OTHER:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003020 eap->line1 = 1;
3021 eap->line2 = curbuf->b_ml.ml_line_count;
3022 break;
3023 case ADDR_LOADED_BUFFERS:
3024 {
3025 buf_T *buf = firstbuf;
3026
3027 while (buf->b_next != NULL
3028 && buf->b_ml.ml_mfp == NULL)
3029 buf = buf->b_next;
3030 eap->line1 = buf->b_fnum;
3031 buf = lastbuf;
3032 while (buf->b_prev != NULL
3033 && buf->b_ml.ml_mfp == NULL)
3034 buf = buf->b_prev;
3035 eap->line2 = buf->b_fnum;
3036 break;
3037 }
3038 case ADDR_BUFFERS:
3039 eap->line1 = firstbuf->b_fnum;
3040 eap->line2 = lastbuf->b_fnum;
3041 break;
3042 case ADDR_WINDOWS:
3043 case ADDR_TABS:
3044 if (IS_USER_CMDIDX(eap->cmdidx))
3045 {
3046 eap->line1 = 1;
3047 eap->line2 = eap->addr_type == ADDR_WINDOWS
3048 ? LAST_WIN_NR : LAST_TAB_NR;
3049 }
3050 else
3051 {
3052 // there is no Vim command which uses '%' and
3053 // ADDR_WINDOWS or ADDR_TABS
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003054 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003055 return FAIL;
3056 }
3057 break;
3058 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003059 case ADDR_UNSIGNED:
3060 case ADDR_QUICKFIX:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003061 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003062 return FAIL;
3063 case ADDR_ARGUMENTS:
3064 if (ARGCOUNT == 0)
3065 eap->line1 = eap->line2 = 0;
3066 else
3067 {
3068 eap->line1 = 1;
3069 eap->line2 = ARGCOUNT;
3070 }
3071 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003072 case ADDR_QUICKFIX_VALID:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003073#ifdef FEAT_QUICKFIX
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003074 eap->line1 = 1;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003075 eap->line2 = qf_get_valid_size(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003076 if (eap->line2 == 0)
3077 eap->line2 = 1;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003078#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003079 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003080 case ADDR_NONE:
3081 // Will give an error later if a range is found.
3082 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003083 }
3084 ++eap->addr_count;
3085 }
3086 else if (*eap->cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
3087 {
3088 pos_T *fp;
3089
3090 // '*' - visual area
3091 if (eap->addr_type != ADDR_LINES)
3092 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003093 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003094 return FAIL;
3095 }
3096
3097 ++eap->cmd;
3098 if (!eap->skip)
3099 {
3100 fp = getmark('<', FALSE);
3101 if (check_mark(fp) == FAIL)
3102 return FAIL;
3103 eap->line1 = fp->lnum;
3104 fp = getmark('>', FALSE);
3105 if (check_mark(fp) == FAIL)
3106 return FAIL;
3107 eap->line2 = fp->lnum;
3108 ++eap->addr_count;
3109 }
3110 }
3111 }
3112 else
3113 eap->line2 = lnum;
3114 eap->addr_count++;
3115
3116 if (*eap->cmd == ';')
3117 {
3118 if (!eap->skip)
3119 {
3120 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar0e717042020-04-27 19:29:01 +02003121 // Don't leave the cursor on an illegal line or column, but do
3122 // accept zero as address, so 0;/PATTERN/ works correctly.
3123 if (eap->line2 > 0)
3124 check_cursor();
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003125 }
3126 }
3127 else if (*eap->cmd != ',')
3128 break;
3129 ++eap->cmd;
3130 }
3131
3132 // One address given: set start and end lines.
3133 if (eap->addr_count == 1)
3134 {
3135 eap->line1 = eap->line2;
3136 // ... but only implicit: really no address given
3137 if (lnum == MAXLNUM)
3138 eap->addr_count = 0;
3139 }
3140 return OK;
3141}
3142
3143/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003144 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003145 * If there is a match advance "pp" to the argument and return TRUE.
3146 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003147 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003148checkforcmd(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003149 char_u **pp, // start of command
3150 char *cmd, // name of command
3151 int len) // required length
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152{
3153 int i;
3154
3155 for (i = 0; cmd[i] != NUL; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003156 if (((char_u *)cmd)[i] != (*pp)[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003157 break;
3158 if (i >= len && !isalpha((*pp)[i]))
3159 {
3160 *pp = skipwhite(*pp + i);
3161 return TRUE;
3162 }
3163 return FALSE;
3164}
3165
3166/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003167 * Append "cmd" to the error message in IObuff.
3168 * Takes care of limiting the length and handling 0xa0, which would be
3169 * invisible otherwise.
3170 */
3171 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003172append_command(char_u *cmd)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003173{
3174 char_u *s = cmd;
3175 char_u *d;
3176
3177 STRCAT(IObuff, ": ");
3178 d = IObuff + STRLEN(IObuff);
3179 while (*s != NUL && d - IObuff < IOSIZE - 7)
3180 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003181 if (enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) : *s == 0xa0)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003182 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003183 s += enc_utf8 ? 2 : 1;
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003184 STRCPY(d, "<a0>");
3185 d += 4;
3186 }
3187 else
3188 MB_COPY_CHAR(s, d);
3189 }
3190 *d = NUL;
3191}
3192
3193/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003194 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003195 * Start of the name can be found at eap->cmd.
Bram Moolenaar25190db2019-05-04 15:05:28 +02003196 * Sets eap->cmdidx and returns a pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003197 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003198 *
3199 * If "lookup" is not NULL recognize expression without "eval" or "call" and
3200 * assignment without "let". Sets eap->cmdidx to the command while returning
3201 * "eap->cmd".
3202 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203 * Returns NULL for an ambiguous user command.
3204 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003205 char_u *
3206find_ex_command(
3207 exarg_T *eap,
Bram Moolenaara494f562020-04-18 17:45:38 +02003208 int *full UNUSED,
Bram Moolenaarb84a3812020-05-01 15:44:29 +02003209 void *(*lookup)(char_u *, size_t, cctx_T *) UNUSED,
Bram Moolenaara494f562020-04-18 17:45:38 +02003210 cctx_T *cctx UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003211{
3212 int len;
3213 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003214 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003216#ifdef FEAT_EVAL
3217 /*
3218 * Recognize a Vim9 script function/method call and assignment:
3219 * "lvar = value", "lvar(arg)", "[1, 2 3]->Func()"
3220 */
Bram Moolenaar0c6ceaf2020-02-22 18:36:32 +01003221 p = eap->cmd;
3222 if (lookup != NULL && (*p == '('
3223 || ((p = to_name_const_end(eap->cmd)) > eap->cmd && *p != NUL)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003224 {
3225 int oplen;
3226 int heredoc;
3227
3228 // "funcname(" is always a function call.
3229 // "varname[]" is an expression.
3230 // "g:varname" is an expression.
3231 // "varname->expr" is an expression.
Bram Moolenaar0c6ceaf2020-02-22 18:36:32 +01003232 // "(..." is an expression.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003233 if (*p == '('
3234 || *p == '['
3235 || p[1] == ':'
3236 || (*p == '-' && p[1] == '>'))
3237 {
3238 eap->cmdidx = CMD_eval;
3239 return eap->cmd;
3240 }
3241
3242 oplen = assignment_len(skipwhite(p), &heredoc);
3243 if (oplen > 0)
3244 {
3245 // Recognize an assignment if we recognize the variable name:
3246 // "g:var = expr"
3247 // "var = expr" where "var" is a local var name.
3248 if (((p - eap->cmd) > 2 && eap->cmd[1] == ':')
Bram Moolenaarb84a3812020-05-01 15:44:29 +02003249 || lookup(eap->cmd, p - eap->cmd, cctx) != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003250 {
3251 eap->cmdidx = CMD_let;
3252 return eap->cmd;
3253 }
3254 }
3255 }
3256#endif
3257
Bram Moolenaar071d4272004-06-13 20:20:40 +00003258 /*
3259 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00003260 * Exceptions:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003261 * - the 'k' command can directly be followed by any character.
3262 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003263 * but :sre[wind] is another command, as are :scr[iptnames],
Bram Moolenaar071d4272004-06-13 20:20:40 +00003264 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00003265 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003266 */
3267 p = eap->cmd;
3268 if (*p == 'k')
3269 {
3270 eap->cmdidx = CMD_k;
3271 ++p;
3272 }
3273 else if (p[0] == 's'
Bram Moolenaar204b93f2015-08-04 22:02:51 +02003274 && ((p[1] == 'c' && (p[2] == NUL || (p[2] != 's' && p[2] != 'r'
3275 && (p[3] == NUL || (p[3] != 'i' && p[4] != 'p')))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276 || p[1] == 'g'
3277 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
3278 || p[1] == 'I'
3279 || (p[1] == 'r' && p[2] != 'e')))
3280 {
3281 eap->cmdidx = CMD_substitute;
3282 ++p;
3283 }
3284 else
3285 {
3286 while (ASCII_ISALPHA(*p))
3287 ++p;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003288 // for python 3.x support ":py3", ":python3", ":py3file", etc.
Bram Moolenaar55d5c032010-07-17 23:52:29 +02003289 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003290 {
Bram Moolenaarb6590522010-07-21 16:00:43 +02003291 while (ASCII_ISALNUM(*p))
3292 ++p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003293 }
3294 else if (*p == '9' && STRNCMP("vim9", eap->cmd, 4) == 0)
3295 {
3296 // include "9" for "vim9script"
3297 ++p;
3298 while (ASCII_ISALPHA(*p))
3299 ++p;
3300 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02003301
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003302 // check for non-alpha command
Bram Moolenaar071d4272004-06-13 20:20:40 +00003303 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3304 ++p;
3305 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003306 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
3307 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003308 // Check for ":dl", ":dell", etc. to ":deletel": that's
3309 // :delete with the 'l' flag. Same for 'p'.
Bram Moolenaardf177f62005-02-22 08:39:57 +00003310 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003311 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00003312 break;
3313 if (i == len - 1)
3314 {
3315 --len;
3316 if (p[-1] == 'l')
3317 eap->flags |= EXFLAG_LIST;
3318 else
3319 eap->flags |= EXFLAG_PRINT;
3320 }
3321 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003323 if (ASCII_ISLOWER(eap->cmd[0]))
3324 {
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003325 int c1 = eap->cmd[0];
Bram Moolenaar94f82cb2019-07-24 22:30:27 +02003326 int c2 = len == 1 ? NUL : eap->cmd[1];
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003327
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003328 if (command_count != (int)CMD_SIZE)
3329 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003330 iemsg(_("E943: Command table needs to be updated, run 'make cmdidxs'"));
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003331 getout(1);
3332 }
3333
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003334 // Use a precomputed index for fast look-up in cmdnames[]
3335 // taking into account the first 2 letters of eap->cmd.
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003336 eap->cmdidx = cmdidxs1[CharOrdLow(c1)];
3337 if (ASCII_ISLOWER(c2))
3338 eap->cmdidx += cmdidxs2[CharOrdLow(c1)][CharOrdLow(c2)];
3339 }
Bram Moolenaara494f562020-04-18 17:45:38 +02003340 else if (ASCII_ISUPPER(eap->cmd[0]))
3341 eap->cmdidx = CMD_Next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342 else
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003343 eap->cmdidx = CMD_bang;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344
3345 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
3346 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
3347 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
3348 (size_t)len) == 0)
3349 {
3350#ifdef FEAT_EVAL
3351 if (full != NULL
3352 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
3353 *full = TRUE;
3354#endif
3355 break;
3356 }
3357
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003358 // Look for a user defined command as a last resort. Let ":Print" be
3359 // overruled by a user defined command.
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01003360 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
3361 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362 {
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003363 // User defined commands may contain digits.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364 while (ASCII_ISALNUM(*p))
3365 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003366 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003367 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003368 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003369 eap->cmdidx = CMD_SIZE;
3370 }
3371
3372 return p;
3373}
3374
3375#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003376static struct cmdmod
3377{
3378 char *name;
3379 int minlen;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003380 int has_count; // :123verbose :3tab
Bram Moolenaared53fb92007-11-24 20:50:24 +00003381} cmdmods[] = {
3382 {"aboveleft", 3, FALSE},
3383 {"belowright", 3, FALSE},
3384 {"botright", 2, FALSE},
3385 {"browse", 3, FALSE},
3386 {"confirm", 4, FALSE},
Bram Moolenaar7b668e82016-08-23 23:51:21 +02003387 {"filter", 4, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003388 {"hide", 3, FALSE},
3389 {"keepalt", 5, FALSE},
3390 {"keepjumps", 5, FALSE},
3391 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01003392 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003393 {"leftabove", 5, FALSE},
3394 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003395 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003396 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003397 {"rightbelow", 6, FALSE},
3398 {"sandbox", 3, FALSE},
3399 {"silent", 3, FALSE},
3400 {"tab", 3, TRUE},
3401 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003402 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003403 {"verbose", 4, TRUE},
3404 {"vertical", 4, FALSE},
3405};
3406
3407/*
3408 * Return length of a command modifier (including optional count).
3409 * Return zero when it's not a modifier.
3410 */
3411 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003412modifier_len(char_u *cmd)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003413{
3414 int i, j;
3415 char_u *p = cmd;
3416
3417 if (VIM_ISDIGIT(*cmd))
3418 p = skipwhite(skipdigits(cmd));
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003419 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003420 {
3421 for (j = 0; p[j] != NUL; ++j)
3422 if (p[j] != cmdmods[i].name[j])
3423 break;
Bram Moolenaar2d473ab2013-06-12 17:12:24 +02003424 if (!ASCII_ISALPHA(p[j]) && j >= cmdmods[i].minlen
Bram Moolenaared53fb92007-11-24 20:50:24 +00003425 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003426 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003427 }
3428 return 0;
3429}
3430
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431/*
3432 * Return > 0 if an Ex command "name" exists.
3433 * Return 2 if there is an exact match.
3434 * Return 3 if there is an ambiguous match.
3435 */
3436 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003437cmd_exists(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438{
3439 exarg_T ea;
3440 int full = FALSE;
3441 int i;
3442 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003443 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003445 // Check command modifiers.
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003446 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447 {
3448 for (j = 0; name[j] != NUL; ++j)
3449 if (name[j] != cmdmods[i].name[j])
3450 break;
3451 if (name[j] == NUL && j >= cmdmods[i].minlen)
3452 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3453 }
3454
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003455 // Check built-in commands and user defined commands.
3456 // For ":2match" and ":3match" we need to skip the number.
Bram Moolenaara9587612006-05-04 21:47:50 +00003457 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003459 p = find_ex_command(&ea, &full, NULL, NULL);
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003460 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003462 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3463 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003464 if (*skipwhite(p) != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003465 return 0; // trailing garbage
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3467}
3468#endif
3469
Bram Moolenaard0190392019-08-23 21:17:35 +02003470 cmdidx_T
3471excmd_get_cmdidx(char_u *cmd, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472{
Bram Moolenaard0190392019-08-23 21:17:35 +02003473 cmdidx_T idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474
Bram Moolenaard0190392019-08-23 21:17:35 +02003475 for (idx = (cmdidx_T)0; (int)idx < (int)CMD_SIZE;
3476 idx = (cmdidx_T)((int)idx + 1))
3477 if (STRNCMP(cmdnames[(int)idx].cmd_name, cmd, (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478 break;
3479
Bram Moolenaard0190392019-08-23 21:17:35 +02003480 return idx;
3481}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482
Bram Moolenaard0190392019-08-23 21:17:35 +02003483 long
3484excmd_get_argt(cmdidx_T idx)
3485{
3486 return (long)cmdnames[(int)idx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487}
3488
3489/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003490 * Skip a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491 *
3492 * Backslashed delimiters after / or ? will be skipped, and commands will
3493 * not be expanded between /'s and ?'s or after "'".
3494 *
Bram Moolenaardf069ee2020-06-22 23:02:51 +02003495 * Also skip white space and ":" characters after the range.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496 * Returns the "cmd" pointer advanced to beyond the range.
3497 */
3498 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003499skip_range(
3500 char_u *cmd,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003501 int *ctx) // pointer to xp_context or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003503 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01003505 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;\\", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506 {
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01003507 if (*cmd == '\\')
3508 {
3509 if (cmd[1] == '?' || cmd[1] == '/' || cmd[1] == '&')
3510 ++cmd;
3511 else
3512 break;
3513 }
3514 else if (*cmd == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003515 {
3516 if (*++cmd == NUL && ctx != NULL)
3517 *ctx = EXPAND_NOTHING;
3518 }
3519 else if (*cmd == '/' || *cmd == '?')
3520 {
3521 delim = *cmd++;
3522 while (*cmd != NUL && *cmd != delim)
3523 if (*cmd++ == '\\' && *cmd != NUL)
3524 ++cmd;
3525 if (*cmd == NUL && ctx != NULL)
3526 *ctx = EXPAND_NOTHING;
3527 }
3528 if (*cmd != NUL)
3529 ++cmd;
3530 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00003531
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003532 // Skip ":" and white space.
Bram Moolenaardf177f62005-02-22 08:39:57 +00003533 while (*cmd == ':')
3534 cmd = skipwhite(cmd + 1);
3535
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536 return cmd;
3537}
3538
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003539 static void
3540addr_error(cmd_addr_T addr_type)
3541{
3542 if (addr_type == ADDR_NONE)
3543 emsg(_(e_norange));
3544 else
3545 emsg(_(e_invrange));
3546}
3547
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548/*
Bram Moolenaar198cb662018-09-06 21:44:17 +02003549 * Get a single EX address.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550 *
3551 * Set ptr to the next character after the part that was interpreted.
3552 * Set ptr to NULL when an error is encountered.
Bram Moolenaar198cb662018-09-06 21:44:17 +02003553 * This may set the last used search pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554 *
3555 * Return MAXLNUM when no Ex address was found.
3556 */
3557 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003558get_address(
3559 exarg_T *eap UNUSED,
3560 char_u **ptr,
Bram Moolenaar1b03a192019-12-08 17:08:29 +01003561 cmd_addr_T addr_type,
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003562 int skip, // only skip the address, don't use it
3563 int silent, // no errors or side effects
3564 int to_other_file, // flag: may jump to other file
3565 int address_count UNUSED) // 1 for first address, >1 after comma
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566{
3567 int c;
3568 int i;
3569 long n;
3570 char_u *cmd;
3571 pos_T pos;
3572 pos_T *fp;
3573 linenr_T lnum;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003574 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575
3576 cmd = skipwhite(*ptr);
3577 lnum = MAXLNUM;
3578 do
3579 {
3580 switch (*cmd)
3581 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003582 case '.': // '.' - Cursor position
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003583 ++cmd;
3584 switch (addr_type)
3585 {
3586 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003587 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003588 lnum = curwin->w_cursor.lnum;
3589 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003590 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003591 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003592 break;
3593 case ADDR_ARGUMENTS:
3594 lnum = curwin->w_arg_idx + 1;
3595 break;
3596 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003597 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003598 lnum = curbuf->b_fnum;
3599 break;
3600 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003601 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003602 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003603 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003604 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003605 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003606 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003607 cmd = NULL;
3608 goto error;
3609 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003610 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003611#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02003612 lnum = qf_get_cur_idx(eap);
3613#endif
3614 break;
3615 case ADDR_QUICKFIX_VALID:
3616#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003617 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02003618#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003619 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003620 }
3621 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003623 case '$': // '$' - last line
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003624 ++cmd;
3625 switch (addr_type)
3626 {
3627 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003628 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629 lnum = curbuf->b_ml.ml_line_count;
3630 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003631 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003632 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003633 break;
3634 case ADDR_ARGUMENTS:
3635 lnum = ARGCOUNT;
3636 break;
3637 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003638 buf = lastbuf;
3639 while (buf->b_ml.ml_mfp == NULL)
3640 {
3641 if (buf->b_prev == NULL)
3642 break;
3643 buf = buf->b_prev;
3644 }
3645 lnum = buf->b_fnum;
3646 break;
3647 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003648 lnum = lastbuf->b_fnum;
3649 break;
3650 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003651 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003652 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003653 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003654 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003655 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003656 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003657 cmd = NULL;
3658 goto error;
3659 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003660 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003661#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003662 lnum = qf_get_size(eap);
3663 if (lnum == 0)
3664 lnum = 1;
Bram Moolenaare906c502015-09-09 21:10:39 +02003665#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003666 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003667 case ADDR_QUICKFIX_VALID:
3668#ifdef FEAT_QUICKFIX
3669 lnum = qf_get_valid_size(eap);
3670 if (lnum == 0)
3671 lnum = 1;
3672#endif
3673 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003674 }
3675 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003676
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003677 case '\'': // ''' - mark
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003678 if (*++cmd == NUL)
3679 {
3680 cmd = NULL;
3681 goto error;
3682 }
3683 if (addr_type != ADDR_LINES)
3684 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003685 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003686 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003687 goto error;
3688 }
3689 if (skip)
3690 ++cmd;
3691 else
3692 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003693 // Only accept a mark in another file when it is
3694 // used by itself: ":'M".
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003695 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
3696 ++cmd;
3697 if (fp == (pos_T *)-1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003698 // Jumped to another file.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003699 lnum = curwin->w_cursor.lnum;
3700 else
3701 {
3702 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003703 {
3704 cmd = NULL;
3705 goto error;
3706 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003707 lnum = fp->lnum;
3708 }
3709 }
3710 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711
3712 case '/':
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003713 case '?': // '/' or '?' - search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003714 c = *cmd++;
3715 if (addr_type != ADDR_LINES)
3716 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003717 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003718 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003719 goto error;
3720 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003721 if (skip) // skip "/pat/"
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003722 {
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02003723 cmd = skip_regexp(cmd, c, (int)p_magic);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003724 if (*cmd == c)
3725 ++cmd;
3726 }
3727 else
3728 {
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003729 int flags;
3730
3731 pos = curwin->w_cursor; // save curwin->w_cursor
3732
3733 // When '/' or '?' follows another address, start from
3734 // there.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003735 if (lnum != MAXLNUM)
3736 curwin->w_cursor.lnum = lnum;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003737
3738 // Start a forward search at the end of the line (unless
3739 // before the first line).
3740 // Start a backward search at the start of the line.
3741 // This makes sure we never match in the current
3742 // line, and can match anywhere in the
3743 // next/previous line.
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01003744 if (c == '/' && curwin->w_cursor.lnum > 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003745 curwin->w_cursor.col = MAXCOL;
3746 else
3747 curwin->w_cursor.col = 0;
3748 searchcmdlen = 0;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003749 flags = silent ? 0 : SEARCH_HIS | SEARCH_MSG;
Bram Moolenaarc036e872020-02-21 21:30:52 +01003750 if (!do_search(NULL, c, c, cmd, 1L, flags, NULL))
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003751 {
3752 curwin->w_cursor = pos;
3753 cmd = NULL;
3754 goto error;
3755 }
3756 lnum = curwin->w_cursor.lnum;
3757 curwin->w_cursor = pos;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003758 // adjust command string pointer
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003759 cmd += searchcmdlen;
3760 }
3761 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003762
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003763 case '\\': // "\?", "\/" or "\&", repeat search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003764 ++cmd;
3765 if (addr_type != ADDR_LINES)
3766 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003767 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003768 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003769 goto error;
3770 }
3771 if (*cmd == '&')
3772 i = RE_SUBST;
3773 else if (*cmd == '?' || *cmd == '/')
3774 i = RE_SEARCH;
3775 else
3776 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003777 emsg(_(e_backslash));
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003778 cmd = NULL;
3779 goto error;
3780 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003781
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003782 if (!skip)
3783 {
3784 /*
3785 * When search follows another address, start from
3786 * there.
3787 */
3788 if (lnum != MAXLNUM)
3789 pos.lnum = lnum;
3790 else
3791 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003792
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003793 /*
3794 * Start the search just like for the above
3795 * do_search().
3796 */
3797 if (*cmd != '?')
3798 pos.col = MAXCOL;
3799 else
3800 pos.col = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02003801 pos.coladd = 0;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01003802 if (searchit(curwin, curbuf, &pos, NULL,
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003803 *cmd == '?' ? BACKWARD : FORWARD,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02003804 (char_u *)"", 1L, SEARCH_MSG, i, NULL) != FAIL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003805 lnum = pos.lnum;
3806 else
3807 {
3808 cmd = NULL;
3809 goto error;
3810 }
3811 }
3812 ++cmd;
3813 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814
3815 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003816 if (VIM_ISDIGIT(*cmd)) // absolute line number
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003817 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818 }
3819
3820 for (;;)
3821 {
3822 cmd = skipwhite(cmd);
3823 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
3824 break;
3825
3826 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003827 {
3828 switch (addr_type)
3829 {
3830 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003831 case ADDR_OTHER:
3832 // "+1" is same as ".+1"
Bram Moolenaarf240e182014-11-27 18:33:02 +01003833 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003834 break;
3835 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003836 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003837 break;
3838 case ADDR_ARGUMENTS:
3839 lnum = curwin->w_arg_idx + 1;
3840 break;
3841 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003842 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003843 lnum = curbuf->b_fnum;
3844 break;
3845 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003846 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003847 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003848 case ADDR_TABS_RELATIVE:
3849 lnum = 1;
3850 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003851 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003852#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02003853 lnum = qf_get_cur_idx(eap);
3854#endif
3855 break;
3856 case ADDR_QUICKFIX_VALID:
3857#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003858 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02003859#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003860 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003861 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003862 case ADDR_UNSIGNED:
3863 lnum = 0;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003864 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003865 }
3866 }
3867
Bram Moolenaar071d4272004-06-13 20:20:40 +00003868 if (VIM_ISDIGIT(*cmd))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003869 i = '+'; // "number" is same as "+number"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870 else
3871 i = *cmd++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003872 if (!VIM_ISDIGIT(*cmd)) // '+' is '+1', but '+0' is not '+1'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003873 n = 1;
3874 else
3875 n = getdigits(&cmd);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003876
3877 if (addr_type == ADDR_TABS_RELATIVE)
3878 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003879 emsg(_(e_invrange));
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003880 cmd = NULL;
3881 goto error;
3882 }
3883 else if (addr_type == ADDR_LOADED_BUFFERS
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003884 || addr_type == ADDR_BUFFERS)
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003885 lnum = compute_buffer_local_count(
3886 addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003887 else
Bram Moolenaarded27822017-01-02 14:27:34 +01003888 {
3889#ifdef FEAT_FOLDING
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003890 // Relative line addressing, need to adjust for folded lines
3891 // now, but only do it after the first address.
Bram Moolenaarded27822017-01-02 14:27:34 +01003892 if (addr_type == ADDR_LINES && (i == '-' || i == '+')
3893 && address_count >= 2)
3894 (void)hasFolding(lnum, NULL, &lnum);
3895#endif
3896 if (i == '-')
3897 lnum -= n;
3898 else
3899 lnum += n;
3900 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003901 }
3902 } while (*cmd == '/' || *cmd == '?');
3903
3904error:
3905 *ptr = cmd;
3906 return lnum;
3907}
3908
3909/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00003910 * Get flags from an Ex command argument.
3911 */
3912 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003913get_flags(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00003914{
3915 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
3916 {
3917 if (*eap->arg == 'l')
3918 eap->flags |= EXFLAG_LIST;
3919 else if (*eap->arg == 'p')
3920 eap->flags |= EXFLAG_PRINT;
3921 else
3922 eap->flags |= EXFLAG_NR;
3923 eap->arg = skipwhite(eap->arg + 1);
3924 }
3925}
3926
3927/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003928 * Function called for command which is Not Implemented. NI!
3929 */
3930 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003931ex_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003932{
3933 if (!eap->skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003934 eap->errmsg = N_("E319: Sorry, the command is not available in this version");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935}
3936
Bram Moolenaar7bb75552007-07-16 18:39:49 +00003937#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938/*
3939 * Function called for script command which is Not Implemented. NI!
3940 * Skips over ":perl <<EOF" constructs.
3941 */
3942 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003943ex_script_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003944{
3945 if (!eap->skip)
3946 ex_ni(eap);
3947 else
3948 vim_free(script_get(eap, eap->arg));
3949}
3950#endif
3951
3952/*
3953 * Check range in Ex command for validity.
3954 * Return NULL when valid, error message when invalid.
3955 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003956 static char *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003957invalid_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003958{
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003959 buf_T *buf;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003960
Bram Moolenaar071d4272004-06-13 20:20:40 +00003961 if ( eap->line1 < 0
3962 || eap->line2 < 0
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003963 || eap->line1 > eap->line2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003964 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003965
Bram Moolenaar8071cb22019-07-12 17:58:01 +02003966 if (eap->argt & EX_RANGE)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003967 {
Bram Moolenaarb7316892019-05-01 18:08:42 +02003968 switch (eap->addr_type)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003969 {
3970 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003971 if (eap->line2 > curbuf->b_ml.ml_line_count
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003972#ifdef FEAT_DIFF
3973 + (eap->cmdidx == CMD_diffget)
3974#endif
3975 )
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003976 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003977 break;
3978 case ADDR_ARGUMENTS:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003979 // add 1 if ARGCOUNT is 0
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003980 if (eap->line2 > ARGCOUNT + (!ARGCOUNT))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003981 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003982 break;
3983 case ADDR_BUFFERS:
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02003984 // Only a boundary check, not whether the buffers actually
3985 // exist.
3986 if (eap->line1 < 1 || eap->line2 > get_highest_fnum())
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003987 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003988 break;
3989 case ADDR_LOADED_BUFFERS:
3990 buf = firstbuf;
3991 while (buf->b_ml.ml_mfp == NULL)
3992 {
3993 if (buf->b_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003994 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003995 buf = buf->b_next;
3996 }
3997 if (eap->line1 < buf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003998 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003999 buf = lastbuf;
4000 while (buf->b_ml.ml_mfp == NULL)
4001 {
4002 if (buf->b_prev == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004003 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004004 buf = buf->b_prev;
4005 }
4006 if (eap->line2 > buf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004007 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004008 break;
4009 case ADDR_WINDOWS:
Bram Moolenaar8be63882015-01-14 11:25:05 +01004010 if (eap->line2 > LAST_WIN_NR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004011 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004012 break;
4013 case ADDR_TABS:
4014 if (eap->line2 > LAST_TAB_NR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004015 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004016 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004017 case ADDR_TABS_RELATIVE:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004018 case ADDR_OTHER:
4019 // Any range is OK.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004020 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004021 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004022#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004023 // No error for value that is too big, will use the last entry.
4024 if (eap->line2 <= 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004025 return _(e_invrange);
Bram Moolenaare906c502015-09-09 21:10:39 +02004026#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004027 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004028 case ADDR_QUICKFIX_VALID:
4029#ifdef FEAT_QUICKFIX
4030 if ((eap->line2 != 1 && eap->line2 > qf_get_valid_size(eap))
4031 || eap->line2 < 0)
4032 return _(e_invrange);
4033#endif
4034 break;
4035 case ADDR_UNSIGNED:
4036 if (eap->line2 < 0)
4037 return _(e_invrange);
4038 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004039 case ADDR_NONE:
4040 // Will give an error elsewhere.
4041 break;
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004042 }
4043 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044 return NULL;
4045}
4046
4047/*
4048 * Correct the range for zero line number, if required.
4049 */
4050 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004051correct_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052{
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004053 if (!(eap->argt & EX_ZEROR)) // zero in range not allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054 {
4055 if (eap->line1 == 0)
4056 eap->line1 = 1;
4057 if (eap->line2 == 0)
4058 eap->line2 = 1;
4059 }
4060}
4061
Bram Moolenaar748bf032005-02-02 23:04:36 +00004062#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004063/*
4064 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4065 * pattern. Otherwise return eap->arg.
4066 */
4067 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004068skip_grep_pat(exarg_T *eap)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004069{
4070 char_u *p = eap->arg;
4071
Bram Moolenaara37420f2006-02-04 22:37:47 +00004072 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4073 || eap->cmdidx == CMD_vimgrepadd
4074 || eap->cmdidx == CMD_lvimgrepadd
4075 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004076 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004077 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004078 if (p == NULL)
4079 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004080 }
4081 return p;
4082}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004083
4084/*
4085 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4086 * in the command line, so that things like % get expanded.
4087 */
4088 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004089replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004090{
4091 char_u *new_cmdline;
4092 char_u *program;
4093 char_u *pos;
4094 char_u *ptr;
4095 int len;
4096 int i;
4097
4098 /*
4099 * Don't do it when ":vimgrep" is used for ":grep".
4100 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004101 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4102 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4103 || eap->cmdidx == CMD_grepadd
4104 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004105 && !grep_internal(eap->cmdidx))
4106 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004107 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4108 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004109 {
4110 if (*curbuf->b_p_gp == NUL)
4111 program = p_gp;
4112 else
4113 program = curbuf->b_p_gp;
4114 }
4115 else
4116 {
4117 if (*curbuf->b_p_mp == NUL)
4118 program = p_mp;
4119 else
4120 program = curbuf->b_p_mp;
4121 }
4122
4123 p = skipwhite(p);
4124
4125 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4126 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004127 // replace $* by given arguments
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004128 i = 1;
4129 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4130 ++i;
4131 len = (int)STRLEN(p);
Bram Moolenaar51e14382019-05-25 20:21:28 +02004132 new_cmdline = alloc(STRLEN(program) + i * (len - 2) + 1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004133 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004134 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004135 ptr = new_cmdline;
4136 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4137 {
4138 i = (int)(pos - program);
4139 STRNCPY(ptr, program, i);
4140 STRCPY(ptr += i, p);
4141 ptr += len;
4142 program = pos + 2;
4143 }
4144 STRCPY(ptr, program);
4145 }
4146 else
4147 {
Bram Moolenaar51e14382019-05-25 20:21:28 +02004148 new_cmdline = alloc(STRLEN(program) + STRLEN(p) + 2);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004149 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004150 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004151 STRCPY(new_cmdline, program);
4152 STRCAT(new_cmdline, " ");
4153 STRCAT(new_cmdline, p);
4154 }
4155 msg_make(p);
4156
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004157 // 'eap->cmd' is not set here, because it is not used at CMD_make
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004158 vim_free(*cmdlinep);
4159 *cmdlinep = new_cmdline;
4160 p = new_cmdline;
4161 }
4162 return p;
4163}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004164#endif
4165
Bram Moolenaar071d4272004-06-13 20:20:40 +00004166/*
4167 * Expand file name in Ex command argument.
Bram Moolenaar0abb4272019-06-15 16:06:00 +02004168 * When an error is detected, "errormsgp" is set to a non-NULL pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169 * Return FAIL for failure, OK otherwise.
4170 */
4171 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004172expand_filename(
4173 exarg_T *eap,
4174 char_u **cmdlinep,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004175 char **errormsgp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004176{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004177 int has_wildcards; // need to expand wildcards
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178 char_u *repl;
4179 int srclen;
4180 char_u *p;
4181 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004182 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004183
Bram Moolenaar748bf032005-02-02 23:04:36 +00004184#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004185 // Skip a regexp pattern for ":vimgrep[add] pat file..."
Bram Moolenaar748bf032005-02-02 23:04:36 +00004186 p = skip_grep_pat(eap);
4187#else
4188 p = eap->arg;
4189#endif
4190
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191 /*
4192 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4193 * the file name contains a wildcard it should not cause expanding.
4194 * (it will be expanded anyway if there is a wildcard before replacing).
4195 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004196 has_wildcards = mch_has_wildcard(p);
4197 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004198 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004199#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004200 // Skip over `=expr`, wildcards in it are not expanded.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004201 if (p[0] == '`' && p[1] == '=')
4202 {
4203 p += 2;
4204 (void)skip_expr(&p);
4205 if (*p == '`')
4206 ++p;
4207 continue;
4208 }
4209#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004210 /*
4211 * Quick check if this cannot be the start of a special string.
4212 * Also removes backslash before '%', '#' and '<'.
4213 */
4214 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4215 {
4216 ++p;
4217 continue;
4218 }
4219
4220 /*
4221 * Try to find a match at this position.
4222 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00004223 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
4224 errormsgp, &escaped);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004225 if (*errormsgp != NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00004226 return FAIL;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004227 if (repl == NULL) // no match found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228 {
4229 p += srclen;
4230 continue;
4231 }
4232
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004233 // Wildcards won't be expanded below, the replacement is taken
4234 // literally. But do expand "~/file", "~user/file" and "$HOME/file".
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004235 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4236 {
4237 char_u *l = repl;
4238
4239 repl = expand_env_save(repl);
4240 vim_free(l);
4241 }
4242
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004243 // Need to escape white space et al. with a backslash.
4244 // Don't do this for:
4245 // - replacement that already has been escaped: "##"
4246 // - shell commands (may have to use quotes instead).
4247 // - non-unix systems when there is a single argument (spaces don't
4248 // separate arguments then).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00004250 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00004251 && eap->cmdidx != CMD_bang
Bram Moolenaar071d4272004-06-13 20:20:40 +00004252 && eap->cmdidx != CMD_grep
4253 && eap->cmdidx != CMD_grepadd
Bram Moolenaarbf15b8d2017-06-04 20:43:48 +02004254 && eap->cmdidx != CMD_hardcopy
Bram Moolenaar67883b42017-07-27 22:57:00 +02004255 && eap->cmdidx != CMD_lgrep
4256 && eap->cmdidx != CMD_lgrepadd
4257 && eap->cmdidx != CMD_lmake
4258 && eap->cmdidx != CMD_make
4259 && eap->cmdidx != CMD_terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00004260#ifndef UNIX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004261 && !(eap->argt & EX_NOSPC)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262#endif
4263 )
4264 {
4265 char_u *l;
4266#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004267 // Don't escape a backslash here, because rem_backslash() doesn't
4268 // remove it later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004269 static char_u *nobslash = (char_u *)" \t\"|";
4270# define ESCAPE_CHARS nobslash
4271#else
4272# define ESCAPE_CHARS escape_chars
4273#endif
4274
4275 for (l = repl; *l; ++l)
4276 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
4277 {
4278 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
4279 if (l != NULL)
4280 {
4281 vim_free(repl);
4282 repl = l;
4283 }
4284 break;
4285 }
4286 }
4287
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004288 // For a shell command a '!' must be escaped.
Bram Moolenaar67883b42017-07-27 22:57:00 +02004289 if ((eap->usefilter || eap->cmdidx == CMD_bang
4290 || eap->cmdidx == CMD_terminal)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004291 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004292 {
4293 char_u *l;
4294
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004295 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004296 if (l != NULL)
4297 {
4298 vim_free(repl);
4299 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004300 }
4301 }
4302
4303 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
4304 vim_free(repl);
4305 if (p == NULL)
4306 return FAIL;
4307 }
4308
4309 /*
4310 * One file argument: Expand wildcards.
4311 * Don't do this with ":r !command" or ":w !command".
4312 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004313 if ((eap->argt & EX_NOSPC) && !eap->usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004314 {
4315 /*
4316 * May do this twice:
4317 * 1. Replace environment variables.
4318 * 2. Replace any other wildcards, remove backslashes.
4319 */
4320 for (n = 1; n <= 2; ++n)
4321 {
4322 if (n == 2)
4323 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004324 /*
4325 * Halve the number of backslashes (this is Vi compatible).
4326 * For Unix and OS/2, when wildcards are expanded, this is
4327 * done by ExpandOne() below.
4328 */
Bram Moolenaare7fedb62015-12-31 19:07:19 +01004329#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004330 if (!has_wildcards)
4331#endif
4332 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004333 }
4334
4335 if (has_wildcards)
4336 {
4337 if (n == 1)
4338 {
4339 /*
4340 * First loop: May expand environment variables. This
4341 * can be done much faster with expand_env() than with
4342 * something else (e.g., calling a shell).
4343 * After expanding environment variables, check again
4344 * if there are still wildcards present.
4345 */
4346 if (vim_strchr(eap->arg, '$') != NULL
4347 || vim_strchr(eap->arg, '~') != NULL)
4348 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00004349 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004350 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004351 has_wildcards = mch_has_wildcard(NameBuff);
4352 p = NameBuff;
4353 }
4354 else
4355 p = NULL;
4356 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004357 else // n == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004358 {
4359 expand_T xpc;
Bram Moolenaarb40c2572019-10-19 21:01:05 +02004360 int options = WILD_LIST_NOTFOUND
4361 | WILD_NOERROR | WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362
4363 ExpandInit(&xpc);
4364 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004365 if (p_wic)
4366 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004367 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01004368 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004369 if (p == NULL)
4370 return FAIL;
4371 }
4372 if (p != NULL)
4373 {
4374 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
4375 p, cmdlinep);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004376 if (n == 2) // p came from ExpandOne()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004377 vim_free(p);
4378 }
4379 }
4380 }
4381 }
4382 return OK;
4383}
4384
4385/*
4386 * Replace part of the command line, keeping eap->cmd, eap->arg and
4387 * eap->nextcmd correct.
4388 * "src" points to the part that is to be replaced, of length "srclen".
4389 * "repl" is the replacement string.
4390 * Returns a pointer to the character after the replaced string.
4391 * Returns NULL for failure.
4392 */
4393 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004394repl_cmdline(
4395 exarg_T *eap,
4396 char_u *src,
4397 int srclen,
4398 char_u *repl,
4399 char_u **cmdlinep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004400{
4401 int len;
4402 int i;
4403 char_u *new_cmdline;
4404
4405 /*
4406 * The new command line is build in new_cmdline[].
4407 * First allocate it.
4408 * Careful: a "+cmd" argument may have been NUL terminated.
4409 */
4410 len = (int)STRLEN(repl);
4411 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004412 if (eap->nextcmd != NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004413 i += (int)STRLEN(eap->nextcmd);// add space for next command
Bram Moolenaar964b3742019-05-24 18:54:09 +02004414 if ((new_cmdline = alloc(i)) == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004415 return NULL; // out of memory!
Bram Moolenaar071d4272004-06-13 20:20:40 +00004416
4417 /*
4418 * Copy the stuff before the expanded part.
4419 * Copy the expanded stuff.
4420 * Copy what came after the expanded part.
4421 * Copy the next commands, if there are any.
4422 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004423 i = (int)(src - *cmdlinep); // length of part before match
Bram Moolenaar071d4272004-06-13 20:20:40 +00004424 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00004425
Bram Moolenaar071d4272004-06-13 20:20:40 +00004426 mch_memmove(new_cmdline + i, repl, (size_t)len);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004427 i += len; // remember the end of the string
Bram Moolenaar071d4272004-06-13 20:20:40 +00004428 STRCPY(new_cmdline + i, src + srclen);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004429 src = new_cmdline + i; // remember where to continue
Bram Moolenaar071d4272004-06-13 20:20:40 +00004430
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004431 if (eap->nextcmd != NULL) // append next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00004432 {
4433 i = (int)STRLEN(new_cmdline) + 1;
4434 STRCPY(new_cmdline + i, eap->nextcmd);
4435 eap->nextcmd = new_cmdline + i;
4436 }
4437 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
4438 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
4439 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
4440 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
4441 vim_free(*cmdlinep);
4442 *cmdlinep = new_cmdline;
4443
4444 return src;
4445}
4446
4447/*
4448 * Check for '|' to separate commands and '"' to start comments.
4449 */
4450 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004451separate_nextcmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004452{
4453 char_u *p;
4454
Bram Moolenaar86b68352004-12-27 21:59:20 +00004455#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004456 p = skip_grep_pat(eap);
4457#else
4458 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004459#endif
4460
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004461 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462 {
4463 if (*p == Ctrl_V)
4464 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004465 if (eap->argt & (EX_CTRLV | EX_XFILE))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004466 ++p; // skip CTRL-V and next char
Bram Moolenaar071d4272004-06-13 20:20:40 +00004467 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004468 // remove CTRL-V and skip next char
Bram Moolenaara7241f52008-06-24 20:39:31 +00004469 STRMOVE(p, p + 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004470 if (*p == NUL) // stop at NUL after CTRL-V
Bram Moolenaar071d4272004-06-13 20:20:40 +00004471 break;
4472 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004473
4474#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004475 // Skip over `=expr` when wildcards are expanded.
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004476 else if (p[0] == '`' && p[1] == '=' && (eap->argt & EX_XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004477 {
4478 p += 2;
4479 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004480 }
4481#endif
4482
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004483 // Check for '"': start of comment or '|': next command
4484 // :@" and :*" do not start a comment!
4485 // :redir @" doesn't either.
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004486 else if ((*p == '"' && !(eap->argt & EX_NOTRLCOM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004487 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
4488 || p != eap->arg)
4489 && (eap->cmdidx != CMD_redir
4490 || p != eap->arg + 1 || p[-1] != '@'))
Bram Moolenaara26b9702020-04-18 19:53:28 +02004491#ifdef FEAT_EVAL
4492 || (*p == '#' && in_vim9script()
4493 && p[1] != '{' && p > eap->cmd && VIM_ISWHITE(p[-1]))
4494#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004495 || *p == '|' || *p == '\n')
4496 {
4497 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004498 * We remove the '\' before the '|', unless EX_CTRLV is used
Bram Moolenaar071d4272004-06-13 20:20:40 +00004499 * AND 'b' is present in 'cpoptions'.
4500 */
4501 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004502 || !(eap->argt & EX_CTRLV)) && *(p - 1) == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004503 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004504 STRMOVE(p - 1, p); // remove the '\'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004505 --p;
4506 }
4507 else
4508 {
4509 eap->nextcmd = check_nextcmd(p);
4510 *p = NUL;
4511 break;
4512 }
4513 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004514 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004515
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004516 if (!(eap->argt & EX_NOTRLCOM)) // remove trailing spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00004517 del_trailing_spaces(eap->arg);
4518}
4519
4520/*
4521 * get + command from ex argument
4522 */
4523 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004524getargcmd(char_u **argp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525{
4526 char_u *arg = *argp;
4527 char_u *command = NULL;
4528
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004529 if (*arg == '+') // +[command]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004530 {
4531 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02004532 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004533 command = dollar_command;
4534 else
4535 {
4536 command = arg;
4537 arg = skip_cmd_arg(command, TRUE);
4538 if (*arg != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004539 *arg++ = NUL; // terminate command with NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004540 }
4541
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004542 arg = skipwhite(arg); // skip over spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00004543 *argp = arg;
4544 }
4545 return command;
4546}
4547
4548/*
4549 * Find end of "+command" argument. Skip over "\ " and "\\".
4550 */
Bram Moolenaard0190392019-08-23 21:17:35 +02004551 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004552skip_cmd_arg(
4553 char_u *p,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004554 int rembs) // TRUE to halve the number of backslashes
Bram Moolenaar071d4272004-06-13 20:20:40 +00004555{
4556 while (*p && !vim_isspace(*p))
4557 {
4558 if (*p == '\\' && p[1] != NUL)
4559 {
4560 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00004561 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004562 else
4563 ++p;
4564 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004565 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004566 }
4567 return p;
4568}
4569
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004570 int
4571get_bad_opt(char_u *p, exarg_T *eap)
4572{
4573 if (STRICMP(p, "keep") == 0)
4574 eap->bad_char = BAD_KEEP;
4575 else if (STRICMP(p, "drop") == 0)
4576 eap->bad_char = BAD_DROP;
4577 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
4578 eap->bad_char = *p;
Bram Moolenaar75808492018-06-12 12:39:41 +02004579 else
4580 return FAIL;
4581 return OK;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004582}
4583
Bram Moolenaar071d4272004-06-13 20:20:40 +00004584/*
4585 * Get "++opt=arg" argument.
4586 * Return FAIL or OK.
4587 */
4588 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004589getargopt(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004590{
4591 char_u *arg = eap->arg + 2;
4592 int *pp = NULL;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004593 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004594 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004595
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004596 // ":edit ++[no]bin[ary] file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004597 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
4598 {
4599 if (*arg == 'n')
4600 {
4601 arg += 2;
4602 eap->force_bin = FORCE_NOBIN;
4603 }
4604 else
4605 eap->force_bin = FORCE_BIN;
4606 if (!checkforcmd(&arg, "binary", 3))
4607 return FAIL;
4608 eap->arg = skipwhite(arg);
4609 return OK;
4610 }
4611
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004612 // ":read ++edit file"
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004613 if (STRNCMP(arg, "edit", 4) == 0)
4614 {
4615 eap->read_edit = TRUE;
4616 eap->arg = skipwhite(arg + 4);
4617 return OK;
4618 }
4619
Bram Moolenaar071d4272004-06-13 20:20:40 +00004620 if (STRNCMP(arg, "ff", 2) == 0)
4621 {
4622 arg += 2;
4623 pp = &eap->force_ff;
4624 }
4625 else if (STRNCMP(arg, "fileformat", 10) == 0)
4626 {
4627 arg += 10;
4628 pp = &eap->force_ff;
4629 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004630 else if (STRNCMP(arg, "enc", 3) == 0)
4631 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01004632 if (STRNCMP(arg, "encoding", 8) == 0)
4633 arg += 8;
4634 else
4635 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004636 pp = &eap->force_enc;
4637 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004638 else if (STRNCMP(arg, "bad", 3) == 0)
4639 {
4640 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004641 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004642 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004643
4644 if (pp == NULL || *arg != '=')
4645 return FAIL;
4646
4647 ++arg;
4648 *pp = (int)(arg - eap->cmd);
4649 arg = skip_cmd_arg(arg, FALSE);
4650 eap->arg = skipwhite(arg);
4651 *arg = NUL;
4652
Bram Moolenaar071d4272004-06-13 20:20:40 +00004653 if (pp == &eap->force_ff)
4654 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004655 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
4656 return FAIL;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004657 eap->force_ff = eap->cmd[eap->force_ff];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004658 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004659 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004660 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004661 // Make 'fileencoding' lower case.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004662 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
4663 *p = TOLOWER_ASC(*p);
4664 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004665 else
4666 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004667 // Check ++bad= argument. Must be a single-byte character, "keep" or
4668 // "drop".
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004669 if (get_bad_opt(eap->cmd + bad_char_idx, eap) == FAIL)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004670 return FAIL;
4671 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004672
4673 return OK;
4674}
4675
Bram Moolenaar071d4272004-06-13 20:20:40 +00004676 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004677ex_autocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004678{
4679 /*
Bram Moolenaar26d4b892018-07-04 22:26:28 +02004680 * Disallow autocommands from .exrc and .vimrc in current
Bram Moolenaar071d4272004-06-13 20:20:40 +00004681 * directory for security reasons.
4682 */
4683 if (secure)
4684 {
4685 secure = 2;
4686 eap->errmsg = e_curdir;
4687 }
4688 else if (eap->cmdidx == CMD_autocmd)
4689 do_autocmd(eap->arg, eap->forceit);
4690 else
4691 do_augroup(eap->arg, eap->forceit);
4692}
4693
4694/*
4695 * ":doautocmd": Apply the automatic commands to the current buffer.
4696 */
4697 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004698ex_doautocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004699{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01004700 char_u *arg = eap->arg;
4701 int call_do_modelines = check_nomodeline(&arg);
Bram Moolenaar1610d052016-06-09 22:53:01 +02004702 int did_aucmd;
Bram Moolenaar60542ac2012-02-12 20:14:01 +01004703
Bram Moolenaar1610d052016-06-09 22:53:01 +02004704 (void)do_doautocmd(arg, TRUE, &did_aucmd);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004705 // Only when there is no <nomodeline>.
Bram Moolenaar1610d052016-06-09 22:53:01 +02004706 if (call_do_modelines && did_aucmd)
Bram Moolenaar60542ac2012-02-12 20:14:01 +01004707 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004708}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004709
Bram Moolenaar071d4272004-06-13 20:20:40 +00004710/*
4711 * :[N]bunload[!] [N] [bufname] unload buffer
4712 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
4713 * :[N]bwipeout[!] [N] [bufname] delete buffer really
4714 */
4715 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004716ex_bunload(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004717{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004718 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02004719 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004720 eap->errmsg = do_bufdel(
4721 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
4722 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
4723 : DOBUF_UNLOAD, eap->arg,
4724 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
4725}
4726
4727/*
4728 * :[N]buffer [N] to buffer N
4729 * :[N]sbuffer [N] to buffer N
4730 */
4731 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004732ex_buffer(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004733{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004734 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02004735 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004736 if (*eap->arg)
4737 eap->errmsg = e_trailing;
4738 else
4739 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004740 if (eap->addr_count == 0) // default is current buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00004741 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
4742 else
4743 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004744 if (eap->do_ecmd_cmd != NULL)
4745 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004746 }
4747}
4748
4749/*
4750 * :[N]bmodified [N] to next mod. buffer
4751 * :[N]sbmodified [N] to next mod. buffer
4752 */
4753 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004754ex_bmodified(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755{
4756 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004757 if (eap->do_ecmd_cmd != NULL)
4758 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004759}
4760
4761/*
4762 * :[N]bnext [N] to next buffer
4763 * :[N]sbnext [N] split and to next buffer
4764 */
4765 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004766ex_bnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004767{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004768 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01004769 return;
4770
Bram Moolenaar071d4272004-06-13 20:20:40 +00004771 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004772 if (eap->do_ecmd_cmd != NULL)
4773 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004774}
4775
4776/*
4777 * :[N]bNext [N] to previous buffer
4778 * :[N]bprevious [N] to previous buffer
4779 * :[N]sbNext [N] split and to previous buffer
4780 * :[N]sbprevious [N] split and to previous buffer
4781 */
4782 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004783ex_bprevious(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004784{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004785 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01004786 return;
4787
Bram Moolenaar071d4272004-06-13 20:20:40 +00004788 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004789 if (eap->do_ecmd_cmd != NULL)
4790 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004791}
4792
4793/*
4794 * :brewind to first buffer
4795 * :bfirst to first buffer
4796 * :sbrewind split and to first buffer
4797 * :sbfirst split and to first buffer
4798 */
4799 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004800ex_brewind(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004801{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004802 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01004803 return;
4804
Bram Moolenaar071d4272004-06-13 20:20:40 +00004805 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004806 if (eap->do_ecmd_cmd != NULL)
4807 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004808}
4809
4810/*
4811 * :blast to last buffer
4812 * :sblast split and to last buffer
4813 */
4814 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004815ex_blast(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004816{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004817 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01004818 return;
4819
Bram Moolenaar071d4272004-06-13 20:20:40 +00004820 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004821 if (eap->do_ecmd_cmd != NULL)
4822 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004823}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004824
Bram Moolenaar7a092242020-04-16 22:10:49 +02004825/*
4826 * Check if "c" ends an Ex command.
Bram Moolenaara494f562020-04-18 17:45:38 +02004827 * In Vim9 script does not check for white space before # or #{.
Bram Moolenaar7a092242020-04-16 22:10:49 +02004828 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004829 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004830ends_excmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004831{
Bram Moolenaarfbda69b2020-04-13 15:06:53 +02004832#ifdef FEAT_EVAL
Bram Moolenaar2c330432020-04-13 14:41:35 +02004833 if (c == '#')
Bram Moolenaar7a092242020-04-16 22:10:49 +02004834 return in_vim9script();
4835#endif
4836 return (c == NUL || c == '|' || c == '"' || c == '\n');
4837}
4838
4839/*
4840 * Like ends_excmd() but checks that a # in Vim9 script either has "cmd" equal
4841 * to "cmd_start" or has a white space character before it.
4842 */
4843 int
Bram Moolenaar21cfe502020-04-16 23:01:50 +02004844ends_excmd2(char_u *cmd_start UNUSED, char_u *cmd)
Bram Moolenaar7a092242020-04-16 22:10:49 +02004845{
4846 int c = *cmd;
4847
4848#ifdef FEAT_EVAL
Bram Moolenaara26b9702020-04-18 19:53:28 +02004849 if (c == '#' && cmd[1] != '{' && (cmd == cmd_start || VIM_ISWHITE(cmd[-1])))
Bram Moolenaar2c330432020-04-13 14:41:35 +02004850 return in_vim9script();
Bram Moolenaarfbda69b2020-04-13 15:06:53 +02004851#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004852 return (c == NUL || c == '|' || c == '"' || c == '\n');
4853}
4854
4855#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
4856 || defined(PROTO)
4857/*
4858 * Return the next command, after the first '|' or '\n'.
4859 * Return NULL if not found.
4860 */
4861 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004862find_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004863{
4864 while (*p != '|' && *p != '\n')
4865 {
4866 if (*p == NUL)
4867 return NULL;
4868 ++p;
4869 }
4870 return (p + 1);
4871}
4872#endif
4873
4874/*
Bram Moolenaar2256c992016-11-15 21:17:07 +01004875 * Check if *p is a separator between Ex commands, skipping over white space.
4876 * Return NULL if it isn't, the following character if it is.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004877 */
4878 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004879check_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004880{
Bram Moolenaar2256c992016-11-15 21:17:07 +01004881 char_u *s = skipwhite(p);
4882
4883 if (*s == '|' || *s == '\n')
4884 return (s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004885 else
4886 return NULL;
4887}
4888
4889/*
4890 * - if there are more files to edit
4891 * - and this is the last window
4892 * - and forceit not used
4893 * - and not repeated twice on a row
4894 * return FAIL and give error message if 'message' TRUE
4895 * return OK otherwise
4896 */
4897 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004898check_more(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004899 int message, // when FALSE check only, no messages
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004900 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004901{
4902 int n = ARGCOUNT - curwin->w_arg_idx - 1;
4903
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00004904 if (!forceit && only_one_window()
4905 && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004906 {
4907 if (message)
4908 {
4909#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
4910 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
4911 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02004912 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004913
Bram Moolenaarda6e8912018-08-21 15:12:14 +02004914 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
4915 NGETTEXT("%d more file to edit. Quit anyway?",
4916 "%d more files to edit. Quit anyway?", n), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004917 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
4918 return OK;
4919 return FAIL;
4920 }
4921#endif
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01004922 semsg(NGETTEXT("E173: %d more file to edit",
4923 "E173: %d more files to edit", n), n);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004924 quitmore = 2; // next try to quit is allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004925 }
4926 return FAIL;
4927 }
4928 return OK;
4929}
4930
Bram Moolenaar071d4272004-06-13 20:20:40 +00004931/*
4932 * Function given to ExpandGeneric() to obtain the list of command names.
4933 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004934 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004935get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936{
4937 if (idx >= (int)CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938 return get_user_command_name(idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004939 return cmdnames[idx].cmd_name;
4940}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004941
Bram Moolenaar071d4272004-06-13 20:20:40 +00004942 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004943ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944{
Bram Moolenaare6850792010-05-14 15:28:44 +02004945 if (*eap->arg == NUL)
4946 {
4947#ifdef FEAT_EVAL
4948 char_u *expr = vim_strsave((char_u *)"g:colors_name");
4949 char_u *p = NULL;
4950
4951 if (expr != NULL)
4952 {
4953 ++emsg_off;
Bram Moolenaarb171fb12020-06-24 20:34:03 +02004954 p = eval_to_string(expr, FALSE);
Bram Moolenaare6850792010-05-14 15:28:44 +02004955 --emsg_off;
4956 vim_free(expr);
4957 }
4958 if (p != NULL)
4959 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004960 msg((char *)p);
Bram Moolenaare6850792010-05-14 15:28:44 +02004961 vim_free(p);
4962 }
4963 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01004964 msg("default");
Bram Moolenaare6850792010-05-14 15:28:44 +02004965#else
Bram Moolenaar32526b32019-01-19 17:43:09 +01004966 msg(_("unknown"));
Bram Moolenaare6850792010-05-14 15:28:44 +02004967#endif
4968 }
4969 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004970 semsg(_("E185: Cannot find color scheme '%s'"), eap->arg);
Bram Moolenaarf58d81a2019-01-28 20:19:05 +01004971
4972#ifdef FEAT_VTP
4973 else if (has_vtp_working())
4974 {
4975 // background color change requires clear + redraw
4976 update_screen(CLEAR);
4977 redrawcmd();
4978 }
4979#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004980}
4981
4982 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004983ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004984{
4985 if (*eap->arg == NUL && eap->cmd[2] == '!')
Bram Moolenaar32526b32019-01-19 17:43:09 +01004986 msg(_("Greetings, Vim user!"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004987 do_highlight(eap->arg, eap->forceit, FALSE);
4988}
4989
4990
4991/*
4992 * Call this function if we thought we were going to exit, but we won't
4993 * (because of an error). May need to restore the terminal mode.
4994 */
4995 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004996not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004997{
4998 exiting = FALSE;
4999 settmode(TMODE_RAW);
5000}
5001
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005002 static int
5003before_quit_autocmds(win_T *wp, int quit_all, int forceit)
5004{
5005 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, wp->w_buffer);
5006
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02005007 // Bail out when autocommands closed the window.
5008 // Refuse to quit when the buffer in the last window is being closed (can
5009 // only happen in autocommands).
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005010 if (!win_valid(wp)
5011 || curbuf_locked()
5012 || (wp->w_buffer->b_nwindows == 1 && wp->w_buffer->b_locked > 0))
5013 return TRUE;
5014
5015 if (quit_all || (check_more(FALSE, forceit) == OK && only_one_window()))
5016 {
5017 apply_autocmds(EVENT_EXITPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02005018 // Refuse to quit when locked or when the window was closed or the
5019 // buffer in the last window is being closed (can only happen in
5020 // autocommands).
5021 if (!win_valid(wp) || curbuf_locked()
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005022 || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
5023 return TRUE;
5024 }
5025
5026 return FALSE;
5027}
5028
Bram Moolenaar071d4272004-06-13 20:20:40 +00005029/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01005030 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005031 * ":{nr}quit": quit window {nr}
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02005032 * Also used when closing a terminal window that's the last one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005033 */
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02005034 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005035ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005036{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005037 win_T *wp;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005038
Bram Moolenaar071d4272004-06-13 20:20:40 +00005039#ifdef FEAT_CMDWIN
5040 if (cmdwin_type != 0)
5041 {
5042 cmdwin_result = Ctrl_C;
5043 return;
5044 }
5045#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005046 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005047 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005048 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005049 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005050 return;
5051 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005052 if (eap->addr_count > 0)
5053 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01005054 int wnr = eap->line2;
5055
5056 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
5057 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005058 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005059 }
5060 else
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005061 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01005062
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005063 // Refuse to quit when locked.
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02005064 if (curbuf_locked())
5065 return;
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005066
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005067 // Trigger QuitPre and maybe ExitPre
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005068 if (before_quit_autocmds(wp, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005069 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070
5071#ifdef FEAT_NETBEANS_INTG
5072 netbeansForcedQuit = eap->forceit;
5073#endif
5074
5075 /*
5076 * If there are more files or windows we won't exit.
5077 */
5078 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
5079 exiting = TRUE;
Bram Moolenaarff930ca2017-10-19 17:12:10 +02005080 if ((!buf_hide(wp->w_buffer)
5081 && check_changed(wp->w_buffer, (p_awa ? CCGD_AW : 0)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01005082 | (eap->forceit ? CCGD_FORCEIT : 0)
5083 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005084 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01005085 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086 {
5087 not_exiting();
5088 }
5089 else
5090 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005091 // quit last window
5092 // Note: only_one_window() returns true, even so a help window is
5093 // still open. In that case only quit, if no address has been
5094 // specified. Example:
5095 // :h|wincmd w|1q - don't quit
5096 // :h|wincmd w|q - quit
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01005097 if (only_one_window() && (ONE_WINDOW || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005098 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02005099 not_exiting();
Bram Moolenaar4033c552017-09-16 20:54:51 +02005100#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005101 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005102#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005103 // close window; may free buffer
Bram Moolenaareb44a682017-08-03 22:44:55 +02005104 win_close(wp, !buf_hide(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005105 }
5106}
5107
5108/*
5109 * ":cquit".
5110 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005111 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005112ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005113{
Bram Moolenaar1860bde2020-01-06 21:47:21 +01005114 // this does not always pass on the exit code to the Manx compiler. why?
5115 getout(eap->addr_count > 0 ? (int)eap->line2 : EXIT_FAILURE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005116}
5117
5118/*
5119 * ":qall": try to quit all windows
5120 */
5121 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005122ex_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005123{
5124# ifdef FEAT_CMDWIN
5125 if (cmdwin_type != 0)
5126 {
5127 if (eap->forceit)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005128 cmdwin_result = K_XF1; // ex_window() takes care of this
Bram Moolenaar071d4272004-06-13 20:20:40 +00005129 else
5130 cmdwin_result = K_XF2;
5131 return;
5132 }
5133# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005134
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005135 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005136 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005137 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005138 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005139 return;
5140 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005141
5142 if (before_quit_autocmds(curwin, TRUE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005143 return;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005144
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01005146 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005147 getout(0);
5148 not_exiting();
5149}
5150
Bram Moolenaar071d4272004-06-13 20:20:40 +00005151/*
5152 * ":close": close current window, unless it is the last one
5153 */
5154 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005155ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005156{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005157 win_T *win;
5158 int winnr = 0;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005159#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00005160 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02005161 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005162 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005163#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005164 if (!text_locked() && !curbuf_locked())
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005165 {
5166 if (eap->addr_count == 0)
5167 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar6d41c782018-06-06 09:11:12 +02005168 else
5169 {
Bram Moolenaar29323592016-07-24 22:04:11 +02005170 FOR_ALL_WINDOWS(win)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005171 {
5172 winnr++;
5173 if (winnr == eap->line2)
5174 break;
5175 }
5176 if (win == NULL)
5177 win = lastwin;
5178 ex_win_close(eap->forceit, win, NULL);
5179 }
5180 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005181}
5182
Bram Moolenaar4033c552017-09-16 20:54:51 +02005183#ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005184/*
5185 * ":pclose": Close any preview window.
5186 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005187 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005188ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005189{
5190 win_T *win;
5191
Bram Moolenaar79648732019-07-18 21:43:07 +02005192 // First close any normal window.
Bram Moolenaar29323592016-07-24 22:04:11 +02005193 FOR_ALL_WINDOWS(win)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005194 if (win->w_p_pvw)
5195 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005196 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar79648732019-07-18 21:43:07 +02005197 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005198 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005199# ifdef FEAT_PROP_POPUP
Bram Moolenaar79648732019-07-18 21:43:07 +02005200 // Also when 'previewpopup' is empty, it might have been cleared.
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02005201 popup_close_preview();
Bram Moolenaar79648732019-07-18 21:43:07 +02005202# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005203}
Bram Moolenaar4033c552017-09-16 20:54:51 +02005204#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005205
Bram Moolenaarf740b292006-02-16 22:11:02 +00005206/*
5207 * Close window "win" and take care of handling closing the last window for a
5208 * modified buffer.
5209 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005210 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005211ex_win_close(
5212 int forceit,
5213 win_T *win,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005214 tabpage_T *tp) // NULL or the tab page "win" is in
Bram Moolenaar071d4272004-06-13 20:20:40 +00005215{
5216 int need_hide;
5217 buf_T *buf = win->w_buffer;
5218
Bram Moolenaarcf844172020-06-26 19:44:06 +02005219 // Never close the autocommand window.
5220 if (win == aucmd_win)
5221 {
5222 emsg(_(e_autocmd_close));
5223 return;
5224 }
5225
Bram Moolenaar071d4272004-06-13 20:20:40 +00005226 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02005227 if (need_hide && !buf_hide(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005228 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02005229#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005230 if ((p_confirm || cmdmod.confirm) && p_write)
5231 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005232 bufref_T bufref;
5233
5234 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005235 dialog_changed(buf, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005236 if (bufref_valid(&bufref) && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005237 return;
5238 need_hide = FALSE;
5239 }
5240 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005241#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005242 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02005243 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005244 return;
5245 }
5246 }
5247
Bram Moolenaar4033c552017-09-16 20:54:51 +02005248#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005249 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005250#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005251
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005252 // free buffer when not hiding it or when it's a scratch buffer
Bram Moolenaarf740b292006-02-16 22:11:02 +00005253 if (tp == NULL)
Bram Moolenaareb44a682017-08-03 22:44:55 +02005254 win_close(win, !need_hide && !buf_hide(buf));
Bram Moolenaarf740b292006-02-16 22:11:02 +00005255 else
Bram Moolenaareb44a682017-08-03 22:44:55 +02005256 win_close_othertab(win, !need_hide && !buf_hide(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005257}
5258
Bram Moolenaar071d4272004-06-13 20:20:40 +00005259/*
Bram Moolenaardea25702017-01-29 15:18:10 +01005260 * Handle the argument for a tabpage related ex command.
5261 * Returns a tabpage number.
5262 * When an error is encountered then eap->errmsg is set.
5263 */
5264 static int
5265get_tabpage_arg(exarg_T *eap)
5266{
5267 int tab_number;
5268 int unaccept_arg0 = (eap->cmdidx == CMD_tabmove) ? 0 : 1;
5269
5270 if (eap->arg && *eap->arg != NUL)
5271 {
5272 char_u *p = eap->arg;
5273 char_u *p_save;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005274 int relative = 0; // argument +N/-N means: go to N places to the
5275 // right/left relative to the current position.
Bram Moolenaardea25702017-01-29 15:18:10 +01005276
5277 if (*p == '-')
5278 {
5279 relative = -1;
5280 p++;
5281 }
5282 else if (*p == '+')
5283 {
5284 relative = 1;
5285 p++;
5286 }
5287
5288 p_save = p;
5289 tab_number = getdigits(&p);
5290
5291 if (relative == 0)
5292 {
5293 if (STRCMP(p, "$") == 0)
5294 tab_number = LAST_TAB_NR;
5295 else if (p == p_save || *p_save == '-' || *p != NUL
5296 || tab_number > LAST_TAB_NR)
5297 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005298 // No numbers as argument.
Bram Moolenaardea25702017-01-29 15:18:10 +01005299 eap->errmsg = e_invarg;
5300 goto theend;
5301 }
5302 }
5303 else
5304 {
5305 if (*p_save == NUL)
5306 tab_number = 1;
5307 else if (p == p_save || *p_save == '-' || *p != NUL
5308 || tab_number == 0)
5309 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005310 // No numbers as argument.
Bram Moolenaardea25702017-01-29 15:18:10 +01005311 eap->errmsg = e_invarg;
5312 goto theend;
5313 }
5314 tab_number = tab_number * relative + tabpage_index(curtab);
5315 if (!unaccept_arg0 && relative == -1)
5316 --tab_number;
5317 }
5318 if (tab_number < unaccept_arg0 || tab_number > LAST_TAB_NR)
5319 eap->errmsg = e_invarg;
5320 }
5321 else if (eap->addr_count > 0)
5322 {
5323 if (unaccept_arg0 && eap->line2 == 0)
Bram Moolenaarc6251552017-01-29 21:42:20 +01005324 {
Bram Moolenaardea25702017-01-29 15:18:10 +01005325 eap->errmsg = e_invrange;
Bram Moolenaarc6251552017-01-29 21:42:20 +01005326 tab_number = 0;
5327 }
Bram Moolenaardea25702017-01-29 15:18:10 +01005328 else
5329 {
5330 tab_number = eap->line2;
Bram Moolenaar82a12462019-01-22 22:41:42 +01005331 if (!unaccept_arg0 && *skipwhite(*eap->cmdlinep) == '-')
Bram Moolenaardea25702017-01-29 15:18:10 +01005332 {
5333 --tab_number;
5334 if (tab_number < unaccept_arg0)
5335 eap->errmsg = e_invarg;
5336 }
5337 }
5338 }
5339 else
5340 {
5341 switch (eap->cmdidx)
5342 {
5343 case CMD_tabnext:
5344 tab_number = tabpage_index(curtab) + 1;
5345 if (tab_number > LAST_TAB_NR)
5346 tab_number = 1;
5347 break;
5348 case CMD_tabmove:
5349 tab_number = LAST_TAB_NR;
5350 break;
5351 default:
5352 tab_number = tabpage_index(curtab);
5353 }
5354 }
5355
5356theend:
5357 return tab_number;
5358}
5359
5360/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00005361 * ":tabclose": close current tab page, unless it is the last one.
5362 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005363 */
5364 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005365ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005366{
Bram Moolenaarf740b292006-02-16 22:11:02 +00005367 tabpage_T *tp;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005368 int tab_number;
Bram Moolenaarf740b292006-02-16 22:11:02 +00005369
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005370# ifdef FEAT_CMDWIN
5371 if (cmdwin_type != 0)
5372 cmdwin_result = K_IGNORE;
5373 else
5374# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005375 if (first_tabpage->tp_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005376 emsg(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00005377 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005378 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005379 tab_number = get_tabpage_arg(eap);
5380 if (eap->errmsg == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005381 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005382 tp = find_tabpage(tab_number);
Bram Moolenaarf740b292006-02-16 22:11:02 +00005383 if (tp == NULL)
5384 {
5385 beep_flush();
5386 return;
5387 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005388 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005389 {
5390 tabpage_close_other(tp, eap->forceit);
5391 return;
5392 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005393 else if (!text_locked() && !curbuf_locked())
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005394 tabpage_close(eap->forceit);
5395 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005396 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005397}
5398
5399/*
5400 * ":tabonly": close all tab pages except the current one
5401 */
5402 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005403ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005404{
5405 tabpage_T *tp;
5406 int done;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005407 int tab_number;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005408
5409# ifdef FEAT_CMDWIN
5410 if (cmdwin_type != 0)
5411 cmdwin_result = K_IGNORE;
5412 else
5413# endif
5414 if (first_tabpage->tp_next == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005415 msg(_("Already only one tab page"));
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005416 else
5417 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005418 tab_number = get_tabpage_arg(eap);
5419 if (eap->errmsg == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005420 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005421 goto_tabpage(tab_number);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005422 // Repeat this up to a 1000 times, because autocommands may
5423 // mess up the lists.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005424 for (done = 0; done < 1000; ++done)
5425 {
5426 FOR_ALL_TABPAGES(tp)
5427 if (tp->tp_topframe != topframe)
5428 {
5429 tabpage_close_other(tp, eap->forceit);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005430 // if we failed to close it quit
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005431 if (valid_tabpage(tp))
5432 done = 1000;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005433 // start over, "tp" is now invalid
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005434 break;
5435 }
5436 if (first_tabpage->tp_next == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005437 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005438 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005439 }
5440 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005441}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005442
5443/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00005444 * Close the current tab page.
5445 */
5446 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005447tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005448{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005449 // First close all the windows but the current one. If that worked then
5450 // close the last window in this tab, that will close it.
Bram Moolenaar459ca562016-11-10 18:16:33 +01005451 if (!ONE_WINDOW)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005452 close_others(TRUE, forceit);
Bram Moolenaar459ca562016-11-10 18:16:33 +01005453 if (ONE_WINDOW)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005454 ex_win_close(forceit, curwin, NULL);
5455# ifdef FEAT_GUI
5456 need_mouse_correct = TRUE;
5457# endif
5458}
5459
5460/*
5461 * Close tab page "tp", which is not the current tab page.
5462 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005463 * Also takes care of the tab pages line disappearing when closing the
5464 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00005465 */
5466 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005467tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005468{
5469 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005470 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005471 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00005472
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005473 // Limit to 1000 windows, autocommands may add a window while we close
5474 // one. OK, so I'm paranoid...
Bram Moolenaarf740b292006-02-16 22:11:02 +00005475 while (++done < 1000)
5476 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005477 wp = tp->tp_firstwin;
5478 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00005479
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005480 // Autocommands may delete the tab page under our fingers and we may
5481 // fail to close a window with a modified buffer.
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005482 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005483 break;
5484 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005485
Bram Moolenaar29323592016-07-24 22:04:11 +02005486 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar29323592016-07-24 22:04:11 +02005487
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005488 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005489 if (h != tabline_height())
5490 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00005491}
5492
5493/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005494 * ":only".
5495 */
5496 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005497ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005498{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005499 win_T *wp;
5500 int wnr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005501# ifdef FEAT_GUI
5502 need_mouse_correct = TRUE;
5503# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005504 if (eap->addr_count > 0)
5505 {
5506 wnr = eap->line2;
5507 for (wp = firstwin; --wnr > 0; )
5508 {
5509 if (wp->w_next == NULL)
5510 break;
5511 else
5512 wp = wp->w_next;
5513 }
5514 win_goto(wp);
5515 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005516 close_others(TRUE, eap->forceit);
5517}
5518
Bram Moolenaar071d4272004-06-13 20:20:40 +00005519 static void
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01005520ex_hide(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005521{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005522 // ":hide" or ":hide | cmd": hide current window
Bram Moolenaar2256c992016-11-15 21:17:07 +01005523 if (!eap->skip)
5524 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02005525#ifdef FEAT_GUI
Bram Moolenaar2256c992016-11-15 21:17:07 +01005526 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005527#endif
Bram Moolenaar2256c992016-11-15 21:17:07 +01005528 if (eap->addr_count == 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005529 win_close(curwin, FALSE); // don't free buffer
Bram Moolenaar2256c992016-11-15 21:17:07 +01005530 else
5531 {
5532 int winnr = 0;
5533 win_T *win;
Bram Moolenaarf240e182014-11-27 18:33:02 +01005534
Bram Moolenaar2256c992016-11-15 21:17:07 +01005535 FOR_ALL_WINDOWS(win)
5536 {
5537 winnr++;
5538 if (winnr == eap->line2)
5539 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005540 }
Bram Moolenaar2256c992016-11-15 21:17:07 +01005541 if (win == NULL)
5542 win = lastwin;
5543 win_close(win, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005544 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005545 }
5546}
5547
5548/*
5549 * ":stop" and ":suspend": Suspend Vim.
5550 */
5551 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005552ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005553{
5554 /*
5555 * Disallow suspending for "rvim".
5556 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005557 if (!check_restricted())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005558 {
5559 if (!eap->forceit)
5560 autowrite_all();
5561 windgoto((int)Rows - 1, 0);
5562 out_char('\n');
5563 out_flush();
5564 stoptermcap();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005565 out_flush(); // needed for SUN to restore xterm buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00005566#ifdef FEAT_TITLE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005567 mch_restore_title(SAVE_RESTORE_BOTH); // restore window titles
Bram Moolenaar071d4272004-06-13 20:20:40 +00005568#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005569 ui_suspend(); // call machine specific function
Bram Moolenaar071d4272004-06-13 20:20:40 +00005570#ifdef FEAT_TITLE
5571 maketitle();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005572 resettitle(); // force updating the title
Bram Moolenaar071d4272004-06-13 20:20:40 +00005573#endif
5574 starttermcap();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005575 scroll_start(); // scroll screen before redrawing
Bram Moolenaar071d4272004-06-13 20:20:40 +00005576 redraw_later_clear();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005577 shell_resized(); // may have resized window
Bram Moolenaar071d4272004-06-13 20:20:40 +00005578 }
5579}
5580
5581/*
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005582 * ":exit", ":xit" and ":wq": Write file and quite the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005583 */
5584 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005585ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005586{
5587#ifdef FEAT_CMDWIN
5588 if (cmdwin_type != 0)
5589 {
5590 cmdwin_result = Ctrl_C;
5591 return;
5592 }
5593#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005594 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005595 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005596 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005597 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005598 return;
5599 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005600
5601 if (before_quit_autocmds(curwin, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005602 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005603
5604 /*
5605 * if more files or windows we won't exit
5606 */
5607 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
5608 exiting = TRUE;
5609 if ( ((eap->cmdidx == CMD_wq
5610 || curbufIsChanged())
5611 && do_write(eap) == FAIL)
5612 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01005613 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005614 {
5615 not_exiting();
5616 }
5617 else
5618 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005619 if (only_one_window()) // quit last window, exit Vim
Bram Moolenaar071d4272004-06-13 20:20:40 +00005620 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02005621 not_exiting();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005622# ifdef FEAT_GUI
5623 need_mouse_correct = TRUE;
5624# endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005625 // Quit current window, may free the buffer.
Bram Moolenaareb44a682017-08-03 22:44:55 +02005626 win_close(curwin, !buf_hide(curwin->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005627 }
5628}
5629
5630/*
5631 * ":print", ":list", ":number".
5632 */
5633 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005634ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005635{
Bram Moolenaardf177f62005-02-22 08:39:57 +00005636 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005637 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +00005638 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005639 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00005640 for ( ;!got_int; ui_breakcheck())
5641 {
5642 print_line(eap->line1,
5643 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
5644 || (eap->flags & EXFLAG_NR)),
5645 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
5646 if (++eap->line1 > eap->line2)
5647 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005648 out_flush(); // show one line at a time
Bram Moolenaardf177f62005-02-22 08:39:57 +00005649 }
5650 setpcmark();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005651 // put cursor at last line
Bram Moolenaardf177f62005-02-22 08:39:57 +00005652 curwin->w_cursor.lnum = eap->line2;
5653 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005654 }
5655
Bram Moolenaar071d4272004-06-13 20:20:40 +00005656 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005657}
5658
5659#ifdef FEAT_BYTEOFF
5660 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005661ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005662{
5663 goto_byte(eap->line2);
5664}
5665#endif
5666
5667/*
5668 * ":shell".
5669 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005670 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005671ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005672{
5673 do_shell(NULL, 0);
5674}
5675
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005676#if defined(HAVE_DROP_FILE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005677
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005678static int drop_busy = FALSE;
5679static int drop_filec;
5680static char_u **drop_filev = NULL;
5681static int drop_split;
5682static void (*drop_callback)(void *);
5683static void *drop_cookie;
5684
5685 static void
5686handle_drop_internal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005687{
5688 exarg_T ea;
5689 int save_msg_scroll = msg_scroll;
5690
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005691 // Setting the argument list may cause screen updates and being called
5692 // recursively. Avoid that by setting drop_busy.
5693 drop_busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005694
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005695 // Check whether the current buffer is changed. If so, we will need
5696 // to split the current window or data could be lost.
5697 // We don't need to check if the 'hidden' option is set, as in this
5698 // case the buffer won't be lost.
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005699 if (!buf_hide(curbuf) && !drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005700 {
5701 ++emsg_off;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005702 drop_split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005703 --emsg_off;
5704 }
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005705 if (drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005706 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005707 if (win_split(0, 0) == FAIL)
5708 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02005709 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005710
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005711 // When splitting the window, create a new alist. Otherwise the
5712 // existing one is overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005713 alist_unlink(curwin->w_alist);
5714 alist_new();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005715 }
5716
5717 /*
5718 * Set up the new argument list.
5719 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005720 alist_set(ALIST(curwin), drop_filec, drop_filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005721
5722 /*
5723 * Move to the first file.
5724 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005725 // Fake up a minimal "next" command for do_argfile()
Bram Moolenaara80faa82020-04-12 19:37:17 +02005726 CLEAR_FIELD(ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005727 ea.cmd = (char_u *)"next";
5728 do_argfile(&ea, 0);
5729
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005730 // do_ecmd() may set need_start_insertmode, but since we never left Insert
5731 // mode that is not needed here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005732 need_start_insertmode = FALSE;
5733
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005734 // Restore msg_scroll, otherwise a following command may cause scrolling
5735 // unexpectedly. The screen will be redrawn by the caller, thus
5736 // msg_scroll being set by displaying a message is irrelevant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005737 msg_scroll = save_msg_scroll;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005738
5739 if (drop_callback != NULL)
5740 drop_callback(drop_cookie);
5741
5742 drop_filev = NULL;
5743 drop_busy = FALSE;
5744}
5745
5746/*
5747 * Handle a file drop. The code is here because a drop is *nearly* like an
5748 * :args command, but not quite (we have a list of exact filenames, so we
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01005749 * don't want to (a) parse a command line, or (b) expand wildcards). So the
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005750 * code is very similar to :args and hence needs access to a lot of the static
5751 * functions in this file.
5752 *
5753 * The "filev" list must have been allocated using alloc(), as should each item
5754 * in the list. This function takes over responsibility for freeing the "filev"
5755 * list.
5756 */
5757 void
5758handle_drop(
5759 int filec, // the number of files dropped
5760 char_u **filev, // the list of files dropped
5761 int split, // force splitting the window
5762 void (*callback)(void *), // to be called after setting the argument
5763 // list
5764 void *cookie) // argument for "callback" (allocated)
5765{
5766 // Cannot handle recursive drops, finish the pending one.
5767 if (drop_busy)
5768 {
5769 FreeWild(filec, filev);
5770 vim_free(cookie);
5771 return;
5772 }
5773
5774 // When calling handle_drop() more than once in a row we only use the last
5775 // one.
5776 if (drop_filev != NULL)
5777 {
5778 FreeWild(drop_filec, drop_filev);
5779 vim_free(drop_cookie);
5780 }
5781
5782 drop_filec = filec;
5783 drop_filev = filev;
5784 drop_split = split;
5785 drop_callback = callback;
5786 drop_cookie = cookie;
5787
5788 // Postpone this when:
5789 // - editing the command line
5790 // - not possible to change the current buffer
5791 // - updating the screen
5792 // As it may change buffers and window structures that are in use and cause
5793 // freed memory to be used.
5794 if (text_locked() || curbuf_locked() || updating_screen)
5795 return;
5796
5797 handle_drop_internal();
5798}
5799
5800/*
5801 * To be called when text is unlocked, curbuf is unlocked or updating_screen is
5802 * reset: Handle a postponed drop.
5803 */
5804 void
5805handle_any_postponed_drop(void)
5806{
5807 if (!drop_busy && drop_filev != NULL
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02005808 && !text_locked() && !curbuf_locked() && !updating_screen)
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005809 handle_drop_internal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005810}
5811#endif
5812
Bram Moolenaar071d4272004-06-13 20:20:40 +00005813/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005814 * ":preserve".
5815 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005816 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005817ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005818{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005819 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005820 ml_preserve(curbuf, TRUE);
5821}
5822
5823/*
5824 * ":recover".
5825 */
5826 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005827ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005828{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005829 // Set recoverymode right away to avoid the ATTENTION prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005830 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01005831 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
5832 | CCGD_MULTWIN
5833 | (eap->forceit ? CCGD_FORCEIT : 0)
5834 | CCGD_EXCMD)
5835
Bram Moolenaar071d4272004-06-13 20:20:40 +00005836 && (*eap->arg == NUL
5837 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
Bram Moolenaar99499b12019-05-23 21:35:48 +02005838 ml_recover(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005839 recoverymode = FALSE;
5840}
5841
5842/*
5843 * Command modifier used in a wrong way.
5844 */
5845 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005846ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005847{
5848 eap->errmsg = e_invcmd;
5849}
5850
Bram Moolenaar071d4272004-06-13 20:20:40 +00005851/*
5852 * :sview [+command] file split window with new file, read-only
5853 * :split [[+command] file] split window with current or new file
5854 * :vsplit [[+command] file] split window vertically with current or new file
5855 * :new [[+command] file] split window with no or new file
5856 * :vnew [[+command] file] split vertically window with no or new file
5857 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005858 *
5859 * :tabedit open new Tab page with empty window
5860 * :tabedit [+command] file open new Tab page and edit "file"
5861 * :tabnew [[+command] file] just like :tabedit
5862 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005863 */
5864 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005865ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005866{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005867 win_T *old_curwin = curwin;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005868#if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005869 char_u *fname = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005870#endif
5871#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005872 int browse_flag = cmdmod.browse;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005873#endif
Bram Moolenaar39902a02018-06-21 22:10:08 +02005874 int use_tab = eap->cmdidx == CMD_tabedit
5875 || eap->cmdidx == CMD_tabfind
5876 || eap->cmdidx == CMD_tabnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005877
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005878 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005879 return;
5880
Bram Moolenaar4033c552017-09-16 20:54:51 +02005881#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005882 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005883#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005884
Bram Moolenaar4033c552017-09-16 20:54:51 +02005885#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005886 // A ":split" in the quickfix window works like ":new". Don't want two
5887 // quickfix windows. But it's OK when doing ":tab split".
Bram Moolenaar05bb9532008-07-04 09:44:11 +00005888 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005889 {
5890 if (eap->cmdidx == CMD_split)
5891 eap->cmdidx = CMD_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005892 if (eap->cmdidx == CMD_vsplit)
5893 eap->cmdidx = CMD_vnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005894 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02005895#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005896
Bram Moolenaar4033c552017-09-16 20:54:51 +02005897#ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005898 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005899 {
5900 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
5901 FNAME_MESS, TRUE, curbuf->b_ffname);
5902 if (fname == NULL)
5903 goto theend;
5904 eap->arg = fname;
5905 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005906# ifdef FEAT_BROWSE
Bram Moolenaar4033c552017-09-16 20:54:51 +02005907 else
5908# endif
5909#endif
5910#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005911 if (cmdmod.browse
Bram Moolenaar071d4272004-06-13 20:20:40 +00005912 && eap->cmdidx != CMD_vnew
Bram Moolenaar071d4272004-06-13 20:20:40 +00005913 && eap->cmdidx != CMD_new)
5914 {
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005915 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005916# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005917 !gui.in_use &&
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005918# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005919 au_has_group((char_u *)"FileExplorer"))
5920 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005921 // No browsing supported but we do have the file explorer:
5922 // Edit the directory.
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005923 if (*eap->arg == NUL || !mch_isdir(eap->arg))
5924 eap->arg = (char_u *)".";
5925 }
5926 else
5927 {
Bram Moolenaar39902a02018-06-21 22:10:08 +02005928 fname = do_browse(0, (char_u *)(use_tab
5929 ? _("Edit File in new tab page")
5930 : _("Edit File in new window")),
Bram Moolenaar071d4272004-06-13 20:20:40 +00005931 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005932 if (fname == NULL)
5933 goto theend;
5934 eap->arg = fname;
5935 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005936 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005937 cmdmod.browse = FALSE; // Don't browse again in do_ecmd().
Bram Moolenaar4033c552017-09-16 20:54:51 +02005938#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005939
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005940 /*
5941 * Either open new tab page or split the window.
5942 */
Bram Moolenaar39902a02018-06-21 22:10:08 +02005943 if (use_tab)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005944 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005945 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
5946 : eap->addr_count == 0 ? 0
5947 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005948 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00005949 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005950
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005951 // set the alternate buffer for the window we came from
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005952 if (curwin != old_curwin
5953 && win_valid(old_curwin)
5954 && old_curwin->w_buffer != curbuf
5955 && !cmdmod.keepalt)
5956 old_curwin->w_alt_fnum = curbuf->b_fnum;
5957 }
5958 }
5959 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005960 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
5961 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005962 // Reset 'scrollbind' when editing another file, but keep it when
5963 // doing ":split" without arguments.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005964 if (*eap->arg != NUL
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01005965# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005966 || cmdmod.browse
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01005967# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005968 )
Bram Moolenaar3368ea22010-09-21 16:56:35 +02005969 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005970 else
5971 do_check_scrollbind(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005972 do_exedit(eap, old_curwin);
5973 }
5974
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005975# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005976 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005977# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005978
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005979# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005980theend:
5981 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005982# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005983}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005984
5985/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00005986 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005987 */
5988 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005989tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00005990{
5991 exarg_T ea;
5992
Bram Moolenaara80faa82020-04-12 19:37:17 +02005993 CLEAR_FIELD(ea);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00005994 ea.cmdidx = CMD_tabnew;
5995 ea.cmd = (char_u *)"tabn";
5996 ea.arg = (char_u *)"";
5997 ex_splitview(&ea);
5998}
5999
6000/*
6001 * :tabnext command
6002 */
6003 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006004ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006005{
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006006 int tab_number;
6007
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02006008 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006009 return;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006010 switch (eap->cmdidx)
6011 {
6012 case CMD_tabfirst:
6013 case CMD_tabrewind:
6014 goto_tabpage(1);
6015 break;
6016 case CMD_tablast:
6017 goto_tabpage(9999);
6018 break;
6019 case CMD_tabprevious:
6020 case CMD_tabNext:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006021 if (eap->arg && *eap->arg != NUL)
6022 {
6023 char_u *p = eap->arg;
6024 char_u *p_save = p;
6025
6026 tab_number = getdigits(&p);
6027 if (p == p_save || *p_save == '-' || *p != NUL
6028 || tab_number == 0)
6029 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006030 // No numbers as argument.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006031 eap->errmsg = e_invarg;
6032 return;
6033 }
6034 }
6035 else
6036 {
6037 if (eap->addr_count == 0)
6038 tab_number = 1;
6039 else
6040 {
6041 tab_number = eap->line2;
6042 if (tab_number < 1)
6043 {
6044 eap->errmsg = e_invrange;
6045 return;
6046 }
6047 }
6048 }
6049 goto_tabpage(-tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006050 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006051 default: // CMD_tabnext
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006052 tab_number = get_tabpage_arg(eap);
6053 if (eap->errmsg == NULL)
6054 goto_tabpage(tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006055 break;
6056 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006057}
6058
6059/*
6060 * :tabmove command
6061 */
6062 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006063ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006064{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02006065 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02006066
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006067 tab_number = get_tabpage_arg(eap);
6068 if (eap->errmsg == NULL)
6069 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006070}
6071
6072/*
6073 * :tabs command: List tabs and their contents.
6074 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006075 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006076ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006077{
6078 tabpage_T *tp;
6079 win_T *wp;
6080 int tabcount = 1;
6081
6082 msg_start();
6083 msg_scroll = TRUE;
6084 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
6085 {
6086 msg_putchar('\n');
6087 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
Bram Moolenaar8820b482017-03-16 17:23:31 +01006088 msg_outtrans_attr(IObuff, HL_ATTR(HLF_T));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006089 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006090 ui_breakcheck();
6091
Bram Moolenaar030f0df2006-02-21 22:02:53 +00006092 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006093 wp = firstwin;
6094 else
6095 wp = tp->tp_firstwin;
6096 for ( ; wp != NULL && !got_int; wp = wp->w_next)
6097 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006098 msg_putchar('\n');
6099 msg_putchar(wp == curwin ? '>' : ' ');
6100 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006101 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
6102 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006103 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02006104 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006105 else
6106 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
6107 IObuff, IOSIZE, TRUE);
6108 msg_outtrans(IObuff);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006109 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006110 ui_breakcheck();
6111 }
6112 }
6113}
6114
Bram Moolenaar071d4272004-06-13 20:20:40 +00006115/*
6116 * ":mode": Set screen mode.
6117 * If no argument given, just get the screen size and redraw.
6118 */
6119 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006120ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006121{
6122 if (*eap->arg == NUL)
6123 shell_resized();
6124 else
Bram Moolenaar3c71aec2020-01-17 19:32:20 +01006125 emsg(_(e_screenmode));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006126}
6127
Bram Moolenaar071d4272004-06-13 20:20:40 +00006128/*
6129 * ":resize".
6130 * set, increment or decrement current window height
6131 */
6132 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006133ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006134{
6135 int n;
6136 win_T *wp = curwin;
6137
6138 if (eap->addr_count > 0)
6139 {
6140 n = eap->line2;
6141 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
6142 ;
6143 }
6144
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006145# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006146 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006147# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006148 n = atol((char *)eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006149 if (cmdmod.split & WSP_VERT)
6150 {
6151 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar02631462017-09-22 15:20:32 +02006152 n += curwin->w_width;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006153 else if (n == 0 && eap->arg[0] == NUL) // default is very wide
Bram Moolenaar071d4272004-06-13 20:20:40 +00006154 n = 9999;
6155 win_setwidth_win((int)n, wp);
6156 }
6157 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006158 {
6159 if (*eap->arg == '-' || *eap->arg == '+')
6160 n += curwin->w_height;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006161 else if (n == 0 && eap->arg[0] == NUL) // default is very high
Bram Moolenaar071d4272004-06-13 20:20:40 +00006162 n = 9999;
6163 win_setheight_win((int)n, wp);
6164 }
6165}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006166
6167/*
6168 * ":find [+command] <file>" command.
6169 */
6170 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006171ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006172{
6173#ifdef FEAT_SEARCHPATH
6174 char_u *fname;
6175 int count;
6176
6177 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
6178 TRUE, curbuf->b_ffname);
6179 if (eap->addr_count > 0)
6180 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006181 // Repeat finding the file "count" times. This matters when it
6182 // appears several times in the path.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006183 count = eap->line2;
6184 while (fname != NULL && --count > 0)
6185 {
6186 vim_free(fname);
6187 fname = find_file_in_path(NULL, 0, FNAME_MESS,
6188 FALSE, curbuf->b_ffname);
6189 }
6190 }
6191
6192 if (fname != NULL)
6193 {
6194 eap->arg = fname;
6195#endif
6196 do_exedit(eap, NULL);
6197#ifdef FEAT_SEARCHPATH
6198 vim_free(fname);
6199 }
6200#endif
6201}
6202
6203/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00006204 * ":open" simulation: for now just work like ":visual".
6205 */
6206 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006207ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006208{
6209 regmatch_T regmatch;
6210 char_u *p;
6211
6212 curwin->w_cursor.lnum = eap->line2;
6213 beginline(BL_SOL | BL_FIX);
6214 if (*eap->arg == '/')
6215 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006216 // ":open /pattern/": put cursor in column found with pattern
Bram Moolenaardf177f62005-02-22 08:39:57 +00006217 ++eap->arg;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006218 p = skip_regexp(eap->arg, '/', p_magic);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006219 *p = NUL;
6220 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
6221 if (regmatch.regprog != NULL)
6222 {
6223 regmatch.rm_ic = p_ic;
6224 p = ml_get_curline();
6225 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006226 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006227 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006228 emsg(_(e_nomatch));
Bram Moolenaar473de612013-06-08 18:19:48 +02006229 vim_regfree(regmatch.regprog);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006230 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006231 // Move to the NUL, ignore any other arguments.
Bram Moolenaardf177f62005-02-22 08:39:57 +00006232 eap->arg += STRLEN(eap->arg);
6233 }
6234 check_cursor();
6235
6236 eap->cmdidx = CMD_visual;
6237 do_exedit(eap, NULL);
6238}
6239
6240/*
6241 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006242 */
6243 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006244ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006245{
6246 do_exedit(eap, NULL);
6247}
6248
6249/*
Bram Moolenaar4b96df52020-01-26 22:00:26 +01006250 * ":edit <file>" command and alike.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006251 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006252 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006253do_exedit(
6254 exarg_T *eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006255 win_T *old_curwin) // curwin before doing a split or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006256{
6257 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006258 int need_hide;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006259 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006260
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01006261 if ((eap->cmdidx != CMD_pedit && ERROR_IF_POPUP_WINDOW)
6262 || ERROR_IF_TERM_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006263 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006264 /*
6265 * ":vi" command ends Ex mode.
6266 */
6267 if (exmode_active && (eap->cmdidx == CMD_visual
6268 || eap->cmdidx == CMD_view))
6269 {
6270 exmode_active = FALSE;
Bram Moolenaar158ea172020-06-18 17:28:39 +02006271 ex_pressedreturn = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006272 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006273 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006274 // Special case: ":global/pat/visual\NLvi-commands"
Bram Moolenaardf177f62005-02-22 08:39:57 +00006275 if (global_busy)
6276 {
6277 int rd = RedrawingDisabled;
6278 int nwr = no_wait_return;
6279 int ms = msg_scroll;
6280#ifdef FEAT_GUI
6281 int he = hold_gui_events;
6282#endif
6283
6284 if (eap->nextcmd != NULL)
6285 {
6286 stuffReadbuff(eap->nextcmd);
6287 eap->nextcmd = NULL;
6288 }
6289
6290 if (exmode_was != EXMODE_VIM)
6291 settmode(TMODE_RAW);
6292 RedrawingDisabled = 0;
6293 no_wait_return = 0;
6294 need_wait_return = FALSE;
6295 msg_scroll = 0;
6296#ifdef FEAT_GUI
6297 hold_gui_events = 0;
6298#endif
6299 must_redraw = CLEAR;
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01006300 pending_exmode_active = TRUE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006301
6302 main_loop(FALSE, TRUE);
6303
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01006304 pending_exmode_active = FALSE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006305 RedrawingDisabled = rd;
6306 no_wait_return = nwr;
6307 msg_scroll = ms;
6308#ifdef FEAT_GUI
6309 hold_gui_events = he;
6310#endif
6311 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006312 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006313 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006314 }
6315
6316 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006317 || eap->cmdidx == CMD_tabnew
6318 || eap->cmdidx == CMD_tabedit
Bram Moolenaar4033c552017-09-16 20:54:51 +02006319 || eap->cmdidx == CMD_vnew) && *eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006320 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006321 // ":new" or ":tabnew" without argument: edit an new empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006322 setpcmark();
6323 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006324 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
6325 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006326 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006327 else if ((eap->cmdidx != CMD_split && eap->cmdidx != CMD_vsplit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006328 || *eap->arg != NUL
6329#ifdef FEAT_BROWSE
6330 || cmdmod.browse
6331#endif
6332 )
6333 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006334 // Can't edit another file when "curbuf_lock" is set. Only ":edit"
6335 // can bring us here, others are stopped earlier.
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006336 if (*eap->arg != NUL && curbuf_locked())
6337 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006338
Bram Moolenaar071d4272004-06-13 20:20:40 +00006339 n = readonlymode;
6340 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
6341 readonlymode = TRUE;
6342 else if (eap->cmdidx == CMD_enew)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006343 readonlymode = FALSE; // 'readonly' doesn't make sense in an
6344 // empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006345 setpcmark();
6346 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
6347 NULL, eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006348 // ":edit" goes to first line if Vi compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00006349 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
6350 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
6351 ? ECMD_ONE : eap->do_ecmd_lnum,
Bram Moolenaareb44a682017-08-03 22:44:55 +02006352 (buf_hide(curbuf) ? ECMD_HIDE : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006353 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006354 // after a split we can use an existing buffer
Bram Moolenaar7b449342014-03-25 13:03:48 +01006355 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006356 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006357 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006358 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006359 // Editing the file failed. If the window was split, close it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006360 if (old_curwin != NULL)
6361 {
6362 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02006363 if (!need_hide || buf_hide(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006364 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006365#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006366 cleanup_T cs;
6367
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006368 // Reset the error/interrupt/exception state here so that
6369 // aborting() returns FALSE when closing a window.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006370 enter_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02006371#endif
6372#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006373 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006374#endif
Bram Moolenaareb44a682017-08-03 22:44:55 +02006375 win_close(curwin, !need_hide && !buf_hide(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006376
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006377#if defined(FEAT_EVAL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006378 // Restore the error/interrupt/exception state if not
6379 // discarded by a new aborting error, interrupt, or
6380 // uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006381 leave_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02006382#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006383 }
6384 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006385 }
6386 else if (readonlymode && curbuf->b_nwindows == 1)
6387 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006388 // When editing an already visited buffer, 'readonly' won't be set
6389 // but the previous value is kept. With ":view" and ":sview" we
6390 // want the file to be readonly, except when another window is
6391 // editing the same buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006392 curbuf->b_p_ro = TRUE;
6393 }
6394 readonlymode = n;
6395 }
6396 else
6397 {
6398 if (eap->do_ecmd_cmd != NULL)
6399 do_cmdline_cmd(eap->do_ecmd_cmd);
6400#ifdef FEAT_TITLE
6401 n = curwin->w_arg_idx_invalid;
6402#endif
6403 check_arg_idx(curwin);
6404#ifdef FEAT_TITLE
6405 if (n != curwin->w_arg_idx_invalid)
6406 maketitle();
6407#endif
6408 }
6409
Bram Moolenaar071d4272004-06-13 20:20:40 +00006410 /*
6411 * if ":split file" worked, set alternate file name in old window to new
6412 * file
6413 */
6414 if (old_curwin != NULL
6415 && *eap->arg != NUL
6416 && curwin != old_curwin
6417 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00006418 && old_curwin->w_buffer != curbuf
6419 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006420 old_curwin->w_alt_fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006421
6422 ex_no_reprint = TRUE;
6423}
6424
6425#ifndef FEAT_GUI
6426/*
6427 * ":gui" and ":gvim" when there is no GUI.
6428 */
6429 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006430ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006431{
6432 eap->errmsg = e_nogvim;
6433}
6434#endif
6435
Bram Moolenaar4f974752019-02-17 17:44:42 +01006436#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006437 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006438ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006439{
6440 gui_make_tearoff(eap->arg);
6441}
6442#endif
6443
Bram Moolenaar29a2c082018-03-05 21:06:23 +01006444#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
6445 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006446 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006447ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006448{
Bram Moolenaar29a2c082018-03-05 21:06:23 +01006449# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
6450 if (gui.in_use)
6451 gui_make_popup(eap->arg, eap->forceit);
6452# ifdef FEAT_TERM_POPUP_MENU
6453 else
6454# endif
6455# endif
6456# ifdef FEAT_TERM_POPUP_MENU
6457 pum_make_popup(eap->arg, eap->forceit);
6458# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006459}
6460#endif
6461
Bram Moolenaar071d4272004-06-13 20:20:40 +00006462 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006463ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006464{
6465 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006466 msg(_("No swap file"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006467 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01006468 msg((char *)curbuf->b_ml.ml_mfp->mf_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006469}
6470
6471/*
6472 * ":syncbind" forces all 'scrollbind' windows to have the same relative
6473 * offset.
6474 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
6475 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006476 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006477ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006478{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006479 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006480 win_T *save_curwin = curwin;
6481 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006482 long topline;
6483 long y;
6484 linenr_T old_linenr = curwin->w_cursor.lnum;
6485
6486 setpcmark();
6487
6488 /*
6489 * determine max topline
6490 */
6491 if (curwin->w_p_scb)
6492 {
6493 topline = curwin->w_topline;
Bram Moolenaar29323592016-07-24 22:04:11 +02006494 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006495 {
6496 if (wp->w_p_scb && wp->w_buffer)
6497 {
Bram Moolenaar375e3392019-01-31 18:26:10 +01006498 y = wp->w_buffer->b_ml.ml_line_count - get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006499 if (topline > y)
6500 topline = y;
6501 }
6502 }
6503 if (topline < 1)
6504 topline = 1;
6505 }
6506 else
6507 {
6508 topline = 1;
6509 }
6510
6511
6512 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006513 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006514 */
Bram Moolenaar29323592016-07-24 22:04:11 +02006515 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006516 {
6517 if (curwin->w_p_scb)
6518 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006519 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006520 y = topline - curwin->w_topline;
6521 if (y > 0)
6522 scrollup(y, TRUE);
6523 else
6524 scrolldown(-y, TRUE);
6525 curwin->w_scbind_pos = topline;
6526 redraw_later(VALID);
6527 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006528 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006529 }
6530 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006531 curwin = save_curwin;
6532 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006533 if (curwin->w_p_scb)
6534 {
6535 did_syncbind = TRUE;
6536 checkpcmark();
6537 if (old_linenr != curwin->w_cursor.lnum)
6538 {
6539 char_u ctrl_o[2];
6540
6541 ctrl_o[0] = Ctrl_O;
6542 ctrl_o[1] = 0;
6543 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
6544 }
6545 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006546}
6547
6548
6549 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006550ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006551{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006552 int i;
6553 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
6554 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006555
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006556 if (eap->usefilter) // :r!cmd
Bram Moolenaar071d4272004-06-13 20:20:40 +00006557 do_bang(1, eap, FALSE, FALSE, TRUE);
6558 else
6559 {
6560 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
6561 return;
6562
6563#ifdef FEAT_BROWSE
6564 if (cmdmod.browse)
6565 {
6566 char_u *browseFile;
6567
Bram Moolenaar7171abe2004-10-11 10:06:20 +00006568 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006569 NULL, NULL, NULL, curbuf);
6570 if (browseFile != NULL)
6571 {
6572 i = readfile(browseFile, NULL,
6573 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
6574 vim_free(browseFile);
6575 }
6576 else
6577 i = OK;
6578 }
6579 else
6580#endif
6581 if (*eap->arg == NUL)
6582 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006583 if (check_fname() == FAIL) // check for no file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00006584 return;
6585 i = readfile(curbuf->b_ffname, curbuf->b_fname,
6586 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
6587 }
6588 else
6589 {
6590 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
6591 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
6592 i = readfile(eap->arg, NULL,
6593 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
6594
6595 }
Bram Moolenaare13b9af2017-01-13 22:01:02 +01006596 if (i != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006597 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006598#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006599 if (!aborting())
6600#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006601 semsg(_(e_notopen), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006602 }
6603 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00006604 {
6605 if (empty && exmode_active)
6606 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006607 // Delete the empty line that remains. Historically ex does
6608 // this but vi doesn't.
Bram Moolenaardf177f62005-02-22 08:39:57 +00006609 if (eap->line2 == 0)
6610 lnum = curbuf->b_ml.ml_line_count;
6611 else
6612 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00006613 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006614 {
Bram Moolenaarca70c072020-05-30 20:30:46 +02006615 ml_delete(lnum);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00006616 if (curwin->w_cursor.lnum > 1
6617 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006618 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00006619 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006620 }
6621 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006622 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006623 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006624 }
6625}
6626
Bram Moolenaarea408852005-06-25 22:49:46 +00006627static char_u *prev_dir = NULL;
6628
6629#if defined(EXITFREE) || defined(PROTO)
6630 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006631free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00006632{
Bram Moolenaard23a8232018-02-10 18:45:26 +01006633 VIM_CLEAR(prev_dir);
6634 VIM_CLEAR(globaldir);
Bram Moolenaarea408852005-06-25 22:49:46 +00006635}
6636#endif
6637
Bram Moolenaarf4258302013-06-02 18:20:17 +02006638/*
Bram Moolenaar002bc792020-06-05 22:33:42 +02006639 * Get the previous directory for the given chdir scope.
6640 */
6641 static char_u *
6642get_prevdir(cdscope_T scope)
6643{
6644 if (scope == CDSCOPE_WINDOW)
6645 return curwin->w_prevdir;
6646 else if (scope == CDSCOPE_TABPAGE)
6647 return curtab->tp_prevdir;
6648 return prev_dir;
6649}
6650
6651/*
Bram Moolenaarf4258302013-06-02 18:20:17 +02006652 * Deal with the side effects of changing the current directory.
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006653 * When 'scope' is CDSCOPE_TABPAGE then this was after an ":tcd" command.
6654 * When 'scope' is CDSCOPE_WINDOW then this was after an ":lcd" command.
Bram Moolenaarf4258302013-06-02 18:20:17 +02006655 */
6656 void
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006657post_chdir(cdscope_T scope)
Bram Moolenaarf4258302013-06-02 18:20:17 +02006658{
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006659 if (scope != CDSCOPE_WINDOW)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006660 // Clear tab local directory for both :cd and :tcd
6661 VIM_CLEAR(curtab->tp_localdir);
Bram Moolenaard23a8232018-02-10 18:45:26 +01006662 VIM_CLEAR(curwin->w_localdir);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006663 if (scope != CDSCOPE_GLOBAL)
Bram Moolenaarf4258302013-06-02 18:20:17 +02006664 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02006665 char_u *pdir = get_prevdir(scope);
6666
6667 // If still in the global directory, need to remember current
6668 // directory as the global directory.
6669 if (globaldir == NULL && pdir != NULL)
6670 globaldir = vim_strsave(pdir);
6671
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006672 // Remember this local directory for the window.
Bram Moolenaarf4258302013-06-02 18:20:17 +02006673 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006674 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006675 if (scope == CDSCOPE_TABPAGE)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006676 curtab->tp_localdir = vim_strsave(NameBuff);
6677 else
6678 curwin->w_localdir = vim_strsave(NameBuff);
6679 }
Bram Moolenaarf4258302013-06-02 18:20:17 +02006680 }
6681 else
6682 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02006683 // We are now in the global directory, no need to remember its name.
Bram Moolenaard23a8232018-02-10 18:45:26 +01006684 VIM_CLEAR(globaldir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006685 }
6686
6687 shorten_fnames(TRUE);
6688}
6689
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006690/*
6691 * Change directory function used by :cd/:tcd/:lcd Ex commands and the
Bram Moolenaar95058722020-06-01 16:26:19 +02006692 * chdir() function.
6693 * scope == CDSCOPE_WINDOW: changes the window-local directory
6694 * scope == CDSCOPE_TABPAGE: changes the tab-local directory
6695 * Otherwise: changes the global directory
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006696 * Returns TRUE if the directory is successfully changed.
6697 */
6698 int
6699changedir_func(
6700 char_u *new_dir,
6701 int forceit,
6702 cdscope_T scope)
6703{
6704 char_u *tofree;
Bram Moolenaar002bc792020-06-05 22:33:42 +02006705 char_u *pdir = NULL;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006706 int dir_differs;
6707 int retval = FALSE;
6708
Bram Moolenaar7cc96922020-01-31 22:41:38 +01006709 if (new_dir == NULL || allbuf_locked())
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006710 return FALSE;
6711
6712 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged() && !forceit)
6713 {
6714 emsg(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
6715 return FALSE;
6716 }
6717
6718 // ":cd -": Change to previous directory
6719 if (STRCMP(new_dir, "-") == 0)
6720 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02006721 pdir = get_prevdir(scope);
6722 if (pdir == NULL)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006723 {
6724 emsg(_("E186: No previous directory"));
6725 return FALSE;
6726 }
Bram Moolenaar002bc792020-06-05 22:33:42 +02006727 new_dir = pdir;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006728 }
6729
Bram Moolenaar002bc792020-06-05 22:33:42 +02006730 // Free the previous directory
6731 tofree = get_prevdir(scope);
6732
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006733 // Save current directory for next ":cd -"
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006734 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar002bc792020-06-05 22:33:42 +02006735 pdir = vim_strsave(NameBuff);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006736 else
Bram Moolenaar002bc792020-06-05 22:33:42 +02006737 pdir = NULL;
6738 if (scope == CDSCOPE_WINDOW)
6739 curwin->w_prevdir = pdir;
6740 else if (scope == CDSCOPE_TABPAGE)
6741 curtab->tp_prevdir = pdir;
6742 else
6743 prev_dir = pdir;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006744
6745#if defined(UNIX) || defined(VMS)
6746 // for UNIX ":cd" means: go to home directory
6747 if (*new_dir == NUL)
6748 {
6749 // use NameBuff for home directory name
6750# ifdef VMS
6751 char_u *p;
6752
6753 p = mch_getenv((char_u *)"SYS$LOGIN");
6754 if (p == NULL || *p == NUL) // empty is the same as not set
6755 NameBuff[0] = NUL;
6756 else
6757 vim_strncpy(NameBuff, p, MAXPATHL - 1);
6758# else
6759 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
6760# endif
6761 new_dir = NameBuff;
6762 }
6763#endif
Bram Moolenaar002bc792020-06-05 22:33:42 +02006764 dir_differs = new_dir == NULL || pdir == NULL
6765 || pathcmp((char *)pdir, (char *)new_dir, -1) != 0;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006766 if (new_dir == NULL || (dir_differs && vim_chdir(new_dir)))
6767 emsg(_(e_failed));
6768 else
6769 {
6770 char_u *acmd_fname;
6771
6772 post_chdir(scope);
6773
6774 if (dir_differs)
6775 {
6776 if (scope == CDSCOPE_WINDOW)
6777 acmd_fname = (char_u *)"window";
6778 else if (scope == CDSCOPE_TABPAGE)
6779 acmd_fname = (char_u *)"tabpage";
6780 else
6781 acmd_fname = (char_u *)"global";
6782 apply_autocmds(EVENT_DIRCHANGED, acmd_fname, new_dir, FALSE,
6783 curbuf);
6784 }
6785 retval = TRUE;
6786 }
6787 vim_free(tofree);
6788
6789 return retval;
6790}
Bram Moolenaarea408852005-06-25 22:49:46 +00006791
Bram Moolenaar071d4272004-06-13 20:20:40 +00006792/*
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006793 * ":cd", ":tcd", ":lcd", ":chdir" ":tchdir" and ":lchdir".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006794 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00006795 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006796ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006797{
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01006798 char_u *new_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006799
6800 new_dir = eap->arg;
6801#if !defined(UNIX) && !defined(VMS)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006802 // for non-UNIX ":cd" means: print current directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006803 if (*new_dir == NUL)
6804 ex_pwd(NULL);
6805 else
6806#endif
6807 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006808 cdscope_T scope = CDSCOPE_GLOBAL;
6809
6810 if (eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir)
6811 scope = CDSCOPE_WINDOW;
6812 else if (eap->cmdidx == CMD_tcd || eap->cmdidx == CMD_tchdir)
6813 scope = CDSCOPE_TABPAGE;
6814
6815 if (changedir_func(new_dir, eap->forceit, scope))
Bram Moolenaardf177f62005-02-22 08:39:57 +00006816 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006817 // Echo the new current directory if the command was typed.
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00006818 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006819 ex_pwd(eap);
6820 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006821 }
6822}
6823
6824/*
6825 * ":pwd".
6826 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006827 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006828ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006829{
6830 if (mch_dirname(NameBuff, MAXPATHL) == OK)
6831 {
6832#ifdef BACKSLASH_IN_FILENAME
6833 slash_adjust(NameBuff);
6834#endif
Bram Moolenaar95058722020-06-01 16:26:19 +02006835 if (p_verbose > 0)
6836 {
6837 char *context = "global";
6838
6839 if (curwin->w_localdir != NULL)
6840 context = "window";
6841 else if (curtab->tp_localdir != NULL)
6842 context = "tabpage";
6843 smsg("[%s] %s", context, (char *)NameBuff);
6844 }
6845 else
6846 msg((char *)NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006847 }
6848 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006849 emsg(_("E187: Unknown"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006850}
6851
6852/*
6853 * ":=".
6854 */
6855 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006856ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006857{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006858 smsg("%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006859 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006860}
6861
6862 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006863ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006864{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006865 int n;
6866 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006867
6868 if (cursor_valid())
6869 {
6870 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
6871 if (n >= 0)
Bram Moolenaar53f81742017-09-22 14:35:51 +02006872 windgoto((int)n, curwin->w_wincol + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006873 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006874
6875 len = eap->line2;
6876 switch (*eap->arg)
6877 {
6878 case 'm': break;
6879 case NUL: len *= 1000L; break;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006880 default: semsg(_(e_invarg2), eap->arg); return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006881 }
6882 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006883}
6884
6885/*
6886 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
6887 */
6888 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006889do_sleep(long msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006890{
Bram Moolenaar52953192019-08-16 10:27:13 +02006891 long done = 0;
Bram Moolenaar975b5272016-03-15 23:10:59 +01006892 long wait_now;
Bram Moolenaar52953192019-08-16 10:27:13 +02006893# ifdef ELAPSED_FUNC
6894 elapsed_T start_tv;
6895
6896 // Remember at what time we started, so that we know how much longer we
6897 // should wait after waiting for a bit.
6898 ELAPSED_INIT(start_tv);
6899# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006900
6901 cursor_on();
Bram Moolenaarf45d7472018-09-30 18:22:26 +02006902 out_flush_cursor(FALSE, FALSE);
Bram Moolenaar52953192019-08-16 10:27:13 +02006903 while (!got_int && done < msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006904 {
Bram Moolenaar975b5272016-03-15 23:10:59 +01006905 wait_now = msec - done > 1000L ? 1000L : msec - done;
6906#ifdef FEAT_TIMERS
6907 {
6908 long due_time = check_due_timer();
6909
6910 if (due_time > 0 && due_time < wait_now)
6911 wait_now = due_time;
6912 }
6913#endif
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02006914#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar28e67e02019-08-15 23:05:49 +02006915 if (has_any_channel() && wait_now > 20L)
6916 wait_now = 20L;
6917#endif
6918#ifdef FEAT_SOUND
6919 if (has_any_sound_callback() && wait_now > 20L)
6920 wait_now = 20L;
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02006921#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +01006922 ui_delay(wait_now, TRUE);
Bram Moolenaar52953192019-08-16 10:27:13 +02006923
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02006924#ifdef FEAT_JOB_CHANNEL
6925 if (has_any_channel())
6926 ui_breakcheck_force(TRUE);
6927 else
6928#endif
6929 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006930#ifdef MESSAGE_QUEUE
Bram Moolenaar52953192019-08-16 10:27:13 +02006931 // Process the netbeans and clientserver messages that may have been
6932 // received in the call to ui_breakcheck() when the GUI is in use. This
6933 // may occur when running a test case.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006934 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02006935#endif
Bram Moolenaar52953192019-08-16 10:27:13 +02006936
6937# ifdef ELAPSED_FUNC
6938 // actual time passed
6939 done = ELAPSED_FUNC(start_tv);
6940# else
6941 // guestimate time passed (will actually be more)
6942 done += wait_now;
6943# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006944 }
Bram Moolenaar1ebff3d2018-07-07 16:18:13 +02006945
6946 // If CTRL-C was typed to interrupt the sleep, drop the CTRL-C from the
6947 // input buffer, otherwise a following call to input() fails.
6948 if (got_int)
6949 (void)vpeekc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006950}
6951
Bram Moolenaar071d4272004-06-13 20:20:40 +00006952/*
6953 * ":winsize" command (obsolete).
6954 */
6955 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006956ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006957{
6958 int w, h;
6959 char_u *arg = eap->arg;
6960 char_u *p;
6961
6962 w = getdigits(&arg);
6963 arg = skipwhite(arg);
6964 p = arg;
6965 h = getdigits(&arg);
6966 if (*p != NUL && *arg == NUL)
6967 set_shellsize(w, h, TRUE);
6968 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006969 emsg(_("E465: :winsize requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006970}
6971
Bram Moolenaar071d4272004-06-13 20:20:40 +00006972 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006973ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006974{
6975 int xchar = NUL;
6976 char_u *p;
6977
6978 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
6979 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006980 // CTRL-W g and CTRL-W CTRL-G have an extra command character
Bram Moolenaar071d4272004-06-13 20:20:40 +00006981 if (eap->arg[1] == NUL)
6982 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006983 emsg(_(e_invarg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006984 return;
6985 }
6986 xchar = eap->arg[1];
6987 p = eap->arg + 2;
6988 }
6989 else
6990 p = eap->arg + 1;
6991
6992 eap->nextcmd = check_nextcmd(p);
6993 p = skipwhite(p);
6994 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006995 emsg(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02006996 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006997 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006998 // Pass flags on for ":vertical wincmd ]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006999 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00007000 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007001 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
7002 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00007003 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007004 }
7005}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007006
Bram Moolenaar843ee412004-06-30 16:16:41 +00007007#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007008/*
7009 * ":winpos".
7010 */
7011 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007012ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007013{
7014 int x, y;
7015 char_u *arg = eap->arg;
7016 char_u *p;
7017
7018 if (*arg == NUL)
7019 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00007020# if defined(FEAT_GUI) || defined(MSWIN)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007021# ifdef VIMDLL
7022 if (gui.in_use ? gui_mch_get_winpos(&x, &y) != FAIL :
7023 mch_get_winpos(&x, &y) != FAIL)
7024# elif defined(FEAT_GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007025 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00007026# else
7027 if (mch_get_winpos(&x, &y) != FAIL)
7028# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007029 {
7030 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
Bram Moolenaar32526b32019-01-19 17:43:09 +01007031 msg((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007032 }
7033 else
7034# endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007035 emsg(_("E188: Obtaining window position not implemented for this platform"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007036 }
7037 else
7038 {
7039 x = getdigits(&arg);
7040 arg = skipwhite(arg);
7041 p = arg;
7042 y = getdigits(&arg);
7043 if (*p == NUL || *arg != NUL)
7044 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007045 emsg(_("E466: :winpos requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007046 return;
7047 }
7048# ifdef FEAT_GUI
7049 if (gui.in_use)
7050 gui_mch_set_winpos(x, y);
7051 else if (gui.starting)
7052 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007053 // Remember the coordinates for when the window is opened.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007054 gui_win_x = x;
7055 gui_win_y = y;
7056 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007057# if defined(HAVE_TGETENT) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007058 else
7059# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007060# endif
7061# if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar843ee412004-06-30 16:16:41 +00007062 mch_set_winpos(x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007063# endif
7064# ifdef HAVE_TGETENT
7065 if (*T_CWP)
7066 term_set_winpos(x, y);
7067# endif
7068 }
7069}
7070#endif
7071
7072/*
7073 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
7074 */
7075 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007076ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007077{
7078 oparg_T oa;
7079
7080 clear_oparg(&oa);
7081 oa.regname = eap->regname;
7082 oa.start.lnum = eap->line1;
7083 oa.end.lnum = eap->line2;
7084 oa.line_count = eap->line2 - eap->line1 + 1;
7085 oa.motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007086 virtual_op = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007087 if (eap->cmdidx != CMD_yank) // position cursor for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00007088 {
7089 setpcmark();
7090 curwin->w_cursor.lnum = eap->line1;
7091 beginline(BL_SOL | BL_FIX);
7092 }
7093
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007094 if (VIsual_active)
7095 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007096
Bram Moolenaar071d4272004-06-13 20:20:40 +00007097 switch (eap->cmdidx)
7098 {
7099 case CMD_delete:
7100 oa.op_type = OP_DELETE;
7101 op_delete(&oa);
7102 break;
7103
7104 case CMD_yank:
7105 oa.op_type = OP_YANK;
7106 (void)op_yank(&oa, FALSE, TRUE);
7107 break;
7108
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007109 default: // CMD_rshift or CMD_lshift
Bram Moolenaar6e707362013-06-14 19:15:58 +02007110 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007111#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02007112 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
7113#else
7114 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00007115#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02007116 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007117 oa.op_type = OP_RSHIFT;
7118 else
7119 oa.op_type = OP_LSHIFT;
7120 op_shift(&oa, FALSE, eap->amount);
7121 break;
7122 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007123 virtual_op = MAYBE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007124 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007125}
7126
7127/*
7128 * ":put".
7129 */
7130 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007131ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007132{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007133 // ":0put" works like ":1put!".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007134 if (eap->line2 == 0)
7135 {
7136 eap->line2 = 1;
7137 eap->forceit = TRUE;
7138 }
7139 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007140 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
7141 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007142}
7143
7144/*
7145 * Handle ":copy" and ":move".
7146 */
7147 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007148ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007149{
7150 long n;
7151
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02007152 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE, FALSE, 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007153 if (eap->arg == NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00007154 {
7155 eap->nextcmd = NULL;
7156 return;
7157 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00007158 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007159
7160 /*
7161 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
7162 */
7163 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
7164 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02007165 emsg(_(e_invrange));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007166 return;
7167 }
7168
7169 if (eap->cmdidx == CMD_move)
7170 {
7171 if (do_move(eap->line1, eap->line2, n) == FAIL)
7172 return;
7173 }
7174 else
7175 ex_copy(eap->line1, eap->line2, n);
7176 u_clearline();
7177 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007178 ex_may_print(eap);
7179}
7180
7181/*
7182 * Print the current line if flags were given to the Ex command.
7183 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02007184 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007185ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007186{
7187 if (eap->flags != 0)
7188 {
7189 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
7190 (eap->flags & EXFLAG_LIST));
7191 ex_no_reprint = TRUE;
7192 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007193}
7194
7195/*
7196 * ":smagic" and ":snomagic".
7197 */
7198 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007199ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007200{
7201 int magic_save = p_magic;
7202
7203 p_magic = (eap->cmdidx == CMD_smagic);
7204 do_sub(eap);
7205 p_magic = magic_save;
7206}
7207
7208/*
7209 * ":join".
7210 */
7211 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007212ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007213{
7214 curwin->w_cursor.lnum = eap->line1;
7215 if (eap->line1 == eap->line2)
7216 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007217 if (eap->addr_count >= 2) // :2,2join does nothing
Bram Moolenaar071d4272004-06-13 20:20:40 +00007218 return;
7219 if (eap->line2 == curbuf->b_ml.ml_line_count)
7220 {
7221 beep_flush();
7222 return;
7223 }
7224 ++eap->line2;
7225 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02007226 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007227 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007228 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007229}
7230
7231/*
7232 * ":[addr]@r" or ":[addr]*r": execute register
7233 */
7234 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007235ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007236{
7237 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00007238 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007239
7240 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4930a762016-09-11 14:39:53 +02007241 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007242
7243#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007244 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00007245#endif
7246
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007247 // get the register name. No name means to use the previous one
Bram Moolenaar071d4272004-06-13 20:20:40 +00007248 c = *eap->arg;
7249 if (c == NUL || (c == '*' && *eap->cmd == '*'))
7250 c = '@';
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007251 // Put the register in the typeahead buffer with the "silent" flag.
Bram Moolenaard333d1e2006-11-07 17:43:47 +00007252 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
7253 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007254 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007255 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00007256 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007257 else
7258 {
7259 int save_efr = exec_from_reg;
7260
7261 exec_from_reg = TRUE;
7262
7263 /*
7264 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00007265 * Continue until the stuff buffer is empty and all added characters
7266 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007267 */
Bram Moolenaar60462872009-11-03 11:40:19 +00007268 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007269 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
7270
7271 exec_from_reg = save_efr;
7272 }
7273}
7274
7275/*
7276 * ":!".
7277 */
7278 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007279ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007280{
7281 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
7282}
7283
7284/*
7285 * ":undo".
7286 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007287 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01007288ex_undo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007289{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007290 if (eap->addr_count == 1) // :undo 123
Bram Moolenaar730cde92010-06-27 05:18:54 +02007291 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00007292 else
7293 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007294}
7295
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007296#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02007297 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007298ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007299{
7300 char_u hash[UNDO_HASH_SIZE];
7301
7302 u_compute_hash(hash);
7303 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
7304}
7305
Bram Moolenaar6df6f472010-07-18 18:04:50 +02007306 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007307ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007308{
7309 char_u hash[UNDO_HASH_SIZE];
7310
7311 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02007312 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007313}
7314#endif
7315
Bram Moolenaar071d4272004-06-13 20:20:40 +00007316/*
7317 * ":redo".
7318 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007319 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007320ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007321{
7322 u_redo(1);
7323}
7324
7325/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007326 * ":earlier" and ":later".
7327 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007328 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007329ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007330{
7331 long count = 0;
7332 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02007333 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007334 char_u *p = eap->arg;
7335
7336 if (*p == NUL)
7337 count = 1;
7338 else if (isdigit(*p))
7339 {
7340 count = getdigits(&p);
7341 switch (*p)
7342 {
7343 case 's': ++p; sec = TRUE; break;
7344 case 'm': ++p; sec = TRUE; count *= 60; break;
7345 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02007346 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
7347 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007348 }
7349 }
7350
7351 if (*p != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007352 semsg(_(e_invarg2), eap->arg);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007353 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02007354 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
7355 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007356}
7357
7358/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007359 * ":redir": start/stop redirection.
7360 */
7361 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007362ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007363{
7364 char *mode;
7365 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00007366 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007367
Bram Moolenaarba768492016-07-08 15:32:54 +02007368#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02007369 if (redir_execute)
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007370 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007371 emsg(_("E930: Cannot use :redir inside execute()"));
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007372 return;
7373 }
Bram Moolenaarba768492016-07-08 15:32:54 +02007374#endif
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007375
Bram Moolenaar071d4272004-06-13 20:20:40 +00007376 if (STRICMP(eap->arg, "END") == 0)
7377 close_redir();
7378 else
7379 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007380 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007381 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007382 ++arg;
7383 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007384 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007385 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007386 mode = "a";
7387 }
7388 else
7389 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00007390 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007391
7392 close_redir();
7393
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007394 // Expand environment variables and "~/".
Bram Moolenaarca472992005-01-21 11:46:23 +00007395 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007396 if (fname == NULL)
7397 return;
7398#ifdef FEAT_BROWSE
7399 if (cmdmod.browse)
7400 {
7401 char_u *browseFile;
7402
Bram Moolenaar7171abe2004-10-11 10:06:20 +00007403 browseFile = do_browse(BROWSE_SAVE,
7404 (char_u *)_("Save Redirection"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +02007405 fname, NULL, NULL,
7406 (char_u *)_(BROWSE_FILTER_ALL_FILES), curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007407 if (browseFile == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007408 return; // operation cancelled
Bram Moolenaar071d4272004-06-13 20:20:40 +00007409 vim_free(fname);
7410 fname = browseFile;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007411 eap->forceit = TRUE; // since dialog already asked
Bram Moolenaar071d4272004-06-13 20:20:40 +00007412 }
7413#endif
7414
7415 redir_fd = open_exfile(fname, eap->forceit, mode);
7416 vim_free(fname);
7417 }
7418#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00007419 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007420 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007421 // redirect to a register a-z (resp. A-Z for appending)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007422 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00007423 ++arg;
7424 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007425# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00007426 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00007427 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00007428# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00007429 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007430 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007431 redir_reg = *arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007432 if (*arg == '>' && arg[1] == '>') // append
Bram Moolenaard9d30582005-05-18 22:10:28 +00007433 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00007434 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007435 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007436 // Can use both "@a" and "@a>".
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00007437 if (*arg == '>')
7438 arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007439 // Make register empty when not using @A-@Z and the
7440 // command is valid.
Bram Moolenaarc188b882007-10-19 14:20:54 +00007441 if (*arg == NUL && !isupper(redir_reg))
7442 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007443 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00007444 }
7445 if (*arg != NUL)
7446 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007447 redir_reg = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007448 semsg(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007449 }
7450 }
7451 else if (*arg == '=' && arg[1] == '>')
7452 {
7453 int append;
7454
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007455 // redirect to a variable
Bram Moolenaardf177f62005-02-22 08:39:57 +00007456 close_redir();
7457 arg += 2;
7458
7459 if (*arg == '>')
7460 {
7461 ++arg;
7462 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007463 }
7464 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00007465 append = FALSE;
7466
7467 if (var_redir_start(skipwhite(arg), append) == OK)
7468 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007469 }
7470#endif
7471
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007472 // TODO: redirect to a buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00007473
Bram Moolenaar071d4272004-06-13 20:20:40 +00007474 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007475 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007476 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00007477
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007478 // Make sure redirection is not off. Can happen for cmdline completion
7479 // that indirectly invokes a command to catch its output.
Bram Moolenaar29b2d262006-09-10 19:07:28 +00007480 if (redir_fd != NULL
7481#ifdef FEAT_EVAL
7482 || redir_reg || redir_vname
7483#endif
7484 )
7485 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007486}
7487
7488/*
7489 * ":redraw": force redraw
7490 */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01007491 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007492ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007493{
7494 int r = RedrawingDisabled;
7495 int p = p_lz;
7496
7497 RedrawingDisabled = 0;
7498 p_lz = FALSE;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01007499 validate_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007500 update_topline();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007501 update_screen(eap->forceit ? CLEAR : VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007502#ifdef FEAT_TITLE
7503 if (need_maketitle)
7504 maketitle();
7505#endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007506#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
7507# ifdef VIMDLL
7508 if (!gui.in_use)
7509# endif
7510 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01007511#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007512 RedrawingDisabled = r;
7513 p_lz = p;
7514
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007515 // Reset msg_didout, so that a message that's there is overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007516 msg_didout = FALSE;
7517 msg_col = 0;
7518
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007519 // No need to wait after an intentional redraw.
Bram Moolenaar56667a52013-07-17 11:54:28 +02007520 need_wait_return = FALSE;
7521
Bram Moolenaar071d4272004-06-13 20:20:40 +00007522 out_flush();
7523}
7524
7525/*
7526 * ":redrawstatus": force redraw of status line(s)
7527 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007528 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007529ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007530{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007531 int r = RedrawingDisabled;
7532 int p = p_lz;
7533
7534 RedrawingDisabled = 0;
7535 p_lz = FALSE;
7536 if (eap->forceit)
7537 status_redraw_all();
7538 else
7539 status_redraw_curbuf();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007540 update_screen(VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007541 RedrawingDisabled = r;
7542 p_lz = p;
7543 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007544}
7545
Bram Moolenaare12bab32019-01-08 22:02:56 +01007546/*
7547 * ":redrawtabline": force redraw of the tabline
7548 */
7549 static void
7550ex_redrawtabline(exarg_T *eap UNUSED)
7551{
7552 int r = RedrawingDisabled;
7553 int p = p_lz;
7554
7555 RedrawingDisabled = 0;
7556 p_lz = FALSE;
7557
7558 draw_tabline();
7559
7560 RedrawingDisabled = r;
7561 p_lz = p;
7562 out_flush();
7563}
7564
Bram Moolenaar071d4272004-06-13 20:20:40 +00007565 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007566close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007567{
7568 if (redir_fd != NULL)
7569 {
7570 fclose(redir_fd);
7571 redir_fd = NULL;
7572 }
7573#ifdef FEAT_EVAL
7574 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007575 if (redir_vname)
7576 {
7577 var_redir_stop();
7578 redir_vname = 0;
7579 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007580#endif
7581}
7582
Bram Moolenaarf96ae0b2019-07-28 15:21:55 +02007583#if (defined(FEAT_SESSION) || defined(FEAT_EVAL)) || defined(PROTO)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007584 int
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02007585vim_mkdir_emsg(char_u *name, int prot UNUSED)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007586{
7587 if (vim_mkdir(name, prot) != 0)
7588 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007589 semsg(_("E739: Cannot create directory: %s"), name);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007590 return FAIL;
7591 }
7592 return OK;
7593}
7594#endif
7595
Bram Moolenaar071d4272004-06-13 20:20:40 +00007596/*
7597 * Open a file for writing for an Ex command, with some checks.
7598 * Return file descriptor, or NULL on failure.
7599 */
7600 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007601open_exfile(
7602 char_u *fname,
7603 int forceit,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007604 char *mode) // "w" for create new file or "a" for append
Bram Moolenaar071d4272004-06-13 20:20:40 +00007605{
7606 FILE *fd;
7607
7608#ifdef UNIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007609 // with Unix it is possible to open a directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007610 if (mch_isdir(fname))
7611 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007612 semsg(_(e_isadir2), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007613 return NULL;
7614 }
7615#endif
7616 if (!forceit && *mode != 'a' && vim_fexists(fname))
7617 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007618 semsg(_("E189: \"%s\" exists (add ! to override)"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007619 return NULL;
7620 }
7621
7622 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007623 semsg(_("E190: Cannot open \"%s\" for writing"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007624
7625 return fd;
7626}
7627
7628/*
7629 * ":mark" and ":k".
7630 */
7631 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007632ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007633{
7634 pos_T pos;
7635
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007636 if (*eap->arg == NUL) // No argument?
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007637 emsg(_(e_argreq));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007638 else if (eap->arg[1] != NUL) // more than one character?
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007639 emsg(_(e_trailing));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007640 else
7641 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007642 pos = curwin->w_cursor; // save curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00007643 curwin->w_cursor.lnum = eap->line2;
7644 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007645 if (setmark(*eap->arg) == FAIL) // set mark
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007646 emsg(_("E191: Argument must be a letter or forward/backward quote"));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007647 curwin->w_cursor = pos; // restore curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00007648 }
7649}
7650
7651/*
7652 * Update w_topline, w_leftcol and the cursor position.
7653 */
7654 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007655update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007656{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007657 check_cursor(); // put cursor on valid line
Bram Moolenaar071d4272004-06-13 20:20:40 +00007658 update_topline();
7659 if (!curwin->w_p_wrap)
7660 validate_cursor();
7661 update_curswant();
7662}
7663
Bram Moolenaar071d4272004-06-13 20:20:40 +00007664/*
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007665 * Save the current State and go to Normal mode.
7666 * Return TRUE if the typeahead could be saved.
7667 */
7668 int
7669save_current_state(save_state_T *sst)
7670{
7671 sst->save_msg_scroll = msg_scroll;
7672 sst->save_restart_edit = restart_edit;
7673 sst->save_msg_didout = msg_didout;
7674 sst->save_State = State;
7675 sst->save_insertmode = p_im;
7676 sst->save_finish_op = finish_op;
7677 sst->save_opcount = opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01007678 sst->save_reg_executing = reg_executing;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007679
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007680 msg_scroll = FALSE; // no msg scrolling in Normal mode
7681 restart_edit = 0; // don't go to Insert mode
7682 p_im = FALSE; // don't use 'insertmode'
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007683
7684 /*
7685 * Save the current typeahead. This is required to allow using ":normal"
7686 * from an event handler and makes sure we don't hang when the argument
7687 * ends with half a command.
7688 */
7689 save_typeahead(&sst->tabuf);
7690 return sst->tabuf.typebuf_valid;
7691}
7692
7693 void
7694restore_current_state(save_state_T *sst)
7695{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007696 // Restore the previous typeahead.
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007697 restore_typeahead(&sst->tabuf);
7698
7699 msg_scroll = sst->save_msg_scroll;
7700 restart_edit = sst->save_restart_edit;
7701 p_im = sst->save_insertmode;
7702 finish_op = sst->save_finish_op;
7703 opcount = sst->save_opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01007704 reg_executing = sst->save_reg_executing;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007705 msg_didout |= sst->save_msg_didout; // don't reset msg_didout now
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007706
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007707 // Restore the state (needed when called from a function executed for
7708 // 'indentexpr'). Update the mouse and cursor, they may have changed.
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007709 State = sst->save_State;
7710#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007711 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007712#endif
7713}
7714
7715/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007716 * ":normal[!] {commands}": Execute normal mode commands.
7717 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01007718 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007719ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007720{
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007721 save_state_T save_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007722 char_u *arg = NULL;
7723 int l;
7724 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007725
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00007726 if (ex_normal_lock > 0)
7727 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007728 emsg(_(e_secure));
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00007729 return;
7730 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007731 if (ex_normal_busy >= p_mmd)
7732 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007733 emsg(_("E192: Recursive use of :normal too deep"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007734 return;
7735 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007736
Bram Moolenaar071d4272004-06-13 20:20:40 +00007737 /*
7738 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
7739 * this for the K_SPECIAL leading byte, otherwise special keys will not
7740 * work.
7741 */
7742 if (has_mbyte)
7743 {
7744 int len = 0;
7745
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007746 // Count the number of characters to be escaped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007747 for (p = eap->arg; *p != NUL; ++p)
7748 {
Bram Moolenaar13505972019-01-24 15:04:48 +01007749#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007750 if (*p == CSI) // leadbyte CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007751 len += 2;
Bram Moolenaar13505972019-01-24 15:04:48 +01007752#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007753 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007754 if (*++p == K_SPECIAL // trailbyte K_SPECIAL or CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01007755#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007756 || *p == CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01007757#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007758 )
7759 len += 2;
7760 }
7761 if (len > 0)
7762 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02007763 arg = alloc(STRLEN(eap->arg) + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007764 if (arg != NULL)
7765 {
7766 len = 0;
7767 for (p = eap->arg; *p != NUL; ++p)
7768 {
7769 arg[len++] = *p;
Bram Moolenaar13505972019-01-24 15:04:48 +01007770#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007771 if (*p == CSI)
7772 {
7773 arg[len++] = KS_EXTRA;
7774 arg[len++] = (int)KE_CSI;
7775 }
Bram Moolenaar13505972019-01-24 15:04:48 +01007776#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007777 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007778 {
7779 arg[len++] = *++p;
7780 if (*p == K_SPECIAL)
7781 {
7782 arg[len++] = KS_SPECIAL;
7783 arg[len++] = KE_FILLER;
7784 }
Bram Moolenaar13505972019-01-24 15:04:48 +01007785#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007786 else if (*p == CSI)
7787 {
7788 arg[len++] = KS_EXTRA;
7789 arg[len++] = (int)KE_CSI;
7790 }
Bram Moolenaar13505972019-01-24 15:04:48 +01007791#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007792 }
7793 arg[len] = NUL;
7794 }
7795 }
7796 }
7797 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007798
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007799 ++ex_normal_busy;
7800 if (save_current_state(&save_state))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007801 {
7802 /*
7803 * Repeat the :normal command for each line in the range. When no
7804 * range given, execute it just once, without positioning the cursor
7805 * first.
7806 */
7807 do
7808 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007809 if (eap->addr_count != 0)
7810 {
7811 curwin->w_cursor.lnum = eap->line1++;
7812 curwin->w_cursor.col = 0;
Bram Moolenaard5d37532017-03-27 23:02:07 +02007813 check_cursor_moved(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007814 }
7815
Bram Moolenaar13505972019-01-24 15:04:48 +01007816 exec_normal_cmd(arg != NULL
7817 ? arg
7818 : eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007819 }
7820 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
7821 }
7822
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007823 // Might not return to the main loop when in an event handler.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007824 update_topline_cursor();
7825
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007826 restore_current_state(&save_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007827 --ex_normal_busy;
Bram Moolenaareda73602014-11-05 09:53:23 +01007828 setmouse();
Bram Moolenaareda73602014-11-05 09:53:23 +01007829#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007830 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaareda73602014-11-05 09:53:23 +01007831#endif
7832
Bram Moolenaar071d4272004-06-13 20:20:40 +00007833 vim_free(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007834}
7835
7836/*
Bram Moolenaar64969662005-12-14 21:59:55 +00007837 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007838 */
7839 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007840ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007841{
Bram Moolenaarfd371682005-01-14 21:42:54 +00007842 if (eap->forceit)
7843 {
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007844 // cursor line can be zero on startup
Bram Moolenaar09ca9322017-09-26 17:40:45 +02007845 if (!curwin->w_cursor.lnum)
7846 curwin->w_cursor.lnum = 1;
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007847 set_cursor_for_append_to_line();
Bram Moolenaarfd371682005-01-14 21:42:54 +00007848 }
Bram Moolenaareb58a242020-04-19 18:13:19 +02007849#ifdef FEAT_TERMINAL
7850 // Ignore this when running in an active terminal.
7851 if (term_job_running(curbuf->b_term))
7852 return;
7853#endif
Bram Moolenaarfd371682005-01-14 21:42:54 +00007854
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007855 // Ignore the command when already in Insert mode. Inserting an
7856 // expression register that invokes a function can do this.
Bram Moolenaara40c5002005-01-09 21:16:21 +00007857 if (State & INSERT)
7858 return;
7859
Bram Moolenaar64969662005-12-14 21:59:55 +00007860 if (eap->cmdidx == CMD_startinsert)
7861 restart_edit = 'a';
7862 else if (eap->cmdidx == CMD_startreplace)
7863 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00007864 else
Bram Moolenaar64969662005-12-14 21:59:55 +00007865 restart_edit = 'V';
7866
7867 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007868 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00007869 if (eap->cmdidx == CMD_startinsert)
7870 restart_edit = 'i';
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007871 curwin->w_curswant = 0; // avoid MAXCOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007872 }
7873}
7874
7875/*
7876 * ":stopinsert"
7877 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007878 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007879ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007880{
7881 restart_edit = 0;
7882 stop_insert_mode = TRUE;
Bram Moolenaarfd773e92016-04-02 19:39:16 +02007883 clearmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007884}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007885
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007886/*
7887 * Execute normal mode command "cmd".
7888 * "remap" can be REMAP_NONE or REMAP_YES.
7889 */
7890 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007891exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007892{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007893 // Stuff the argument into the typeahead buffer.
Bram Moolenaar25281632016-01-21 23:32:32 +01007894 ins_typebuf(cmd, remap, 0, TRUE, silent);
Bram Moolenaar586c70c2018-10-02 16:23:58 +02007895 exec_normal(FALSE, FALSE, FALSE);
Bram Moolenaar25281632016-01-21 23:32:32 +01007896}
Bram Moolenaar25281632016-01-21 23:32:32 +01007897
Bram Moolenaar25281632016-01-21 23:32:32 +01007898/*
7899 * Execute normal_cmd() until there is no typeahead left.
Bram Moolenaar586c70c2018-10-02 16:23:58 +02007900 * When "use_vpeekc" is TRUE use vpeekc() to check for available chars.
Bram Moolenaar25281632016-01-21 23:32:32 +01007901 */
7902 void
Bram Moolenaar586c70c2018-10-02 16:23:58 +02007903exec_normal(int was_typed, int use_vpeekc, int may_use_terminal_loop UNUSED)
Bram Moolenaar25281632016-01-21 23:32:32 +01007904{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007905 oparg_T oa;
Bram Moolenaar905dd902019-04-07 14:21:47 +02007906 int c;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007907
Bram Moolenaar905dd902019-04-07 14:21:47 +02007908 // When calling vpeekc() from feedkeys() it will return Ctrl_C when there
7909 // is nothing to get, so also check for Ctrl_C.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007910 clear_oparg(&oa);
7911 finish_op = FALSE;
Bram Moolenaar586c70c2018-10-02 16:23:58 +02007912 while ((!stuff_empty()
7913 || ((was_typed || !typebuf_typed()) && typebuf.tb_len > 0)
Bram Moolenaar905dd902019-04-07 14:21:47 +02007914 || (use_vpeekc && (c = vpeekc()) != NUL && c != Ctrl_C))
Bram Moolenaar586c70c2018-10-02 16:23:58 +02007915 && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007916 {
7917 update_topline_cursor();
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02007918#ifdef FEAT_TERMINAL
Bram Moolenaar655a82a2018-05-08 22:01:07 +02007919 if (may_use_terminal_loop && term_use_loop()
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02007920 && oa.op_type == OP_NOP && oa.regname == NUL
7921 && !VIsual_active)
7922 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007923 // If terminal_loop() returns OK we got a key that is handled
7924 // in Normal model. With FAIL we first need to position the
7925 // cursor and the screen needs to be redrawn.
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02007926 if (terminal_loop(TRUE) == OK)
7927 normal_cmd(&oa, TRUE);
7928 }
7929 else
7930#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007931 // execute a Normal mode cmd
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02007932 normal_cmd(&oa, TRUE);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007933 }
7934}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007935
Bram Moolenaar071d4272004-06-13 20:20:40 +00007936#ifdef FEAT_FIND_ID
7937 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007938ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007939{
7940 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
7941 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
7942 (linenr_T)1, (linenr_T)MAXLNUM);
7943}
7944
Bram Moolenaar4033c552017-09-16 20:54:51 +02007945#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007946/*
7947 * ":psearch"
7948 */
7949 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007950ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007951{
7952 g_do_tagpreview = p_pvh;
7953 ex_findpat(eap);
7954 g_do_tagpreview = 0;
7955}
7956#endif
7957
7958 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007959ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007960{
7961 int whole = TRUE;
7962 long n;
7963 char_u *p;
7964 int action;
7965
7966 switch (cmdnames[eap->cmdidx].cmd_name[2])
7967 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007968 case 'e': // ":psearch", ":isearch" and ":dsearch"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007969 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
7970 action = ACTION_GOTO;
7971 else
7972 action = ACTION_SHOW;
7973 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007974 case 'i': // ":ilist" and ":dlist"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007975 action = ACTION_SHOW_ALL;
7976 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007977 case 'u': // ":ijump" and ":djump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007978 action = ACTION_GOTO;
7979 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007980 default: // ":isplit" and ":dsplit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007981 action = ACTION_SPLIT;
7982 break;
7983 }
7984
7985 n = 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007986 if (vim_isdigit(*eap->arg)) // get count
Bram Moolenaar071d4272004-06-13 20:20:40 +00007987 {
7988 n = getdigits(&eap->arg);
7989 eap->arg = skipwhite(eap->arg);
7990 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007991 if (*eap->arg == '/') // Match regexp, not just whole words
Bram Moolenaar071d4272004-06-13 20:20:40 +00007992 {
7993 whole = FALSE;
7994 ++eap->arg;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02007995 p = skip_regexp(eap->arg, '/', p_magic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007996 if (*p)
7997 {
7998 *p++ = NUL;
7999 p = skipwhite(p);
8000
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008001 // Check for trailing illegal characters
Bram Moolenaarfaac4102020-04-20 17:46:14 +02008002 if (!ends_excmd2(eap->arg, p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008003 eap->errmsg = e_trailing;
8004 else
8005 eap->nextcmd = check_nextcmd(p);
8006 }
8007 }
8008 if (!eap->skip)
8009 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
8010 whole, !eap->forceit,
8011 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
8012 n, action, eap->line1, eap->line2);
8013}
8014#endif
8015
Bram Moolenaar071d4272004-06-13 20:20:40 +00008016
Bram Moolenaar4033c552017-09-16 20:54:51 +02008017#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00008018/*
8019 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
8020 */
8021 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008022ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008023{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008024 g_do_tagpreview = p_pvh; // will be reset to 0 in ex_tag_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00008025 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
8026}
8027
8028/*
8029 * ":pedit"
8030 */
8031 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008032ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008033{
8034 win_T *curwin_save = curwin;
8035
Bram Moolenaar3a2505c2020-03-09 16:40:41 +01008036 if (ERROR_IF_ANY_POPUP_WINDOW)
8037 return;
8038
Bram Moolenaar026587b2019-08-17 15:08:00 +02008039 // Open the preview window or popup and make it the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008040 g_do_tagpreview = p_pvh;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02008041 prepare_tagpreview(TRUE, TRUE, FALSE);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008042
Bram Moolenaar026587b2019-08-17 15:08:00 +02008043 // Edit the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008044 do_exedit(eap, NULL);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008045
Bram Moolenaar071d4272004-06-13 20:20:40 +00008046 if (curwin != curwin_save && win_valid(curwin_save))
8047 {
Bram Moolenaar026587b2019-08-17 15:08:00 +02008048 // Return cursor to where we were
Bram Moolenaar071d4272004-06-13 20:20:40 +00008049 validate_cursor();
8050 redraw_later(VALID);
8051 win_enter(curwin_save, TRUE);
8052 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01008053# ifdef FEAT_PROP_POPUP
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008054 else if (WIN_IS_POPUP(curwin))
8055 {
8056 // can't keep focus in popup window
8057 win_enter(firstwin, TRUE);
8058 }
8059# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008060 g_do_tagpreview = 0;
8061}
Bram Moolenaar4033c552017-09-16 20:54:51 +02008062#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008063
8064/*
8065 * ":stag", ":stselect" and ":stjump".
8066 */
8067 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008068ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008069{
8070 postponed_split = -1;
8071 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008072 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008073 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
8074 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008075 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008076}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008077
8078/*
8079 * ":tag", ":tselect", ":tjump", ":tnext", etc.
8080 */
8081 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008082ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008083{
8084 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
8085}
8086
8087 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008088ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008089{
8090 int cmd;
8091
8092 switch (name[1])
8093 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008094 case 'j': cmd = DT_JUMP; // ":tjump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008095 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008096 case 's': cmd = DT_SELECT; // ":tselect"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008097 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008098 case 'p': cmd = DT_PREV; // ":tprevious"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008099 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008100 case 'N': cmd = DT_PREV; // ":tNext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008101 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008102 case 'n': cmd = DT_NEXT; // ":tnext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008103 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008104 case 'o': cmd = DT_POP; // ":pop"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008105 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008106 case 'f': // ":tfirst"
8107 case 'r': cmd = DT_FIRST; // ":trewind"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008108 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008109 case 'l': cmd = DT_LAST; // ":tlast"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008110 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008111 default: // ":tag"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008112#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +00008113 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008114 {
Bram Moolenaard4db7712016-11-12 19:16:46 +01008115 ex_cstag(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008116 return;
8117 }
8118#endif
8119 cmd = DT_TAG;
8120 break;
8121 }
8122
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00008123 if (name[0] == 'l')
8124 {
8125#ifndef FEAT_QUICKFIX
8126 ex_ni(eap);
8127 return;
8128#else
8129 cmd = DT_LTAG;
8130#endif
8131 }
8132
Bram Moolenaar071d4272004-06-13 20:20:40 +00008133 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
8134 eap->forceit, TRUE);
8135}
8136
8137/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008138 * Check "str" for starting with a special cmdline variable.
8139 * If found return one of the SPEC_ values and set "*usedlen" to the length of
8140 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
8141 */
8142 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008143find_cmdline_var(char_u *src, int *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008144{
8145 int len;
8146 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +00008147 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008148 "%",
8149#define SPEC_PERC 0
8150 "#",
Bram Moolenaar65f08472017-09-10 18:16:20 +02008151#define SPEC_HASH (SPEC_PERC + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008152 "<cword>", // cursor word
Bram Moolenaar65f08472017-09-10 18:16:20 +02008153#define SPEC_CWORD (SPEC_HASH + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008154 "<cWORD>", // cursor WORD
Bram Moolenaar65f08472017-09-10 18:16:20 +02008155#define SPEC_CCWORD (SPEC_CWORD + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008156 "<cexpr>", // expr under cursor
Bram Moolenaar65f08472017-09-10 18:16:20 +02008157#define SPEC_CEXPR (SPEC_CCWORD + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008158 "<cfile>", // cursor path name
Bram Moolenaar65f08472017-09-10 18:16:20 +02008159#define SPEC_CFILE (SPEC_CEXPR + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008160 "<sfile>", // ":so" file name
Bram Moolenaar65f08472017-09-10 18:16:20 +02008161#define SPEC_SFILE (SPEC_CFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008162 "<slnum>", // ":so" file line number
Bram Moolenaar65f08472017-09-10 18:16:20 +02008163#define SPEC_SLNUM (SPEC_SFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008164 "<afile>", // autocommand file name
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008165#define SPEC_AFILE (SPEC_SLNUM + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008166 "<abuf>", // autocommand buffer number
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008167#define SPEC_ABUF (SPEC_AFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008168 "<amatch>", // autocommand match name
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008169#define SPEC_AMATCH (SPEC_ABUF + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008170 "<sflnum>", // script file line number
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008171#define SPEC_SFLNUM (SPEC_AMATCH + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008172#ifdef FEAT_CLIENTSERVER
8173 "<client>"
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008174# define SPEC_CLIENT (SPEC_SFLNUM + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008175#endif
8176 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008177
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008178 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008179 {
8180 len = (int)STRLEN(spec_str[i]);
8181 if (STRNCMP(src, spec_str[i], len) == 0)
8182 {
8183 *usedlen = len;
8184 return i;
8185 }
8186 }
8187 return -1;
8188}
8189
8190/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008191 * Evaluate cmdline variables.
8192 *
8193 * change '%' to curbuf->b_ffname
8194 * '#' to curwin->w_altfile
8195 * '<cword>' to word under the cursor
8196 * '<cWORD>' to WORD under the cursor
Bram Moolenaar8071cb22019-07-12 17:58:01 +02008197 * '<cexpr>' to C-expression under the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00008198 * '<cfile>' to path name under the cursor
8199 * '<sfile>' to sourced file name
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008200 * '<slnum>' to sourced file line number
Bram Moolenaar071d4272004-06-13 20:20:40 +00008201 * '<afile>' to file name for autocommand
8202 * '<abuf>' to buffer number for autocommand
8203 * '<amatch>' to matching name for autocommand
8204 *
8205 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
8206 * "" for error without a message) and NULL is returned.
8207 * Returns an allocated string if a valid match was found.
8208 * Returns NULL if no match was found. "usedlen" then still contains the
8209 * number of characters to skip.
8210 */
8211 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008212eval_vars(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008213 char_u *src, // pointer into commandline
8214 char_u *srcstart, // beginning of valid memory for src
8215 int *usedlen, // characters after src that are used
8216 linenr_T *lnump, // line number for :e command, or NULL
8217 char **errormsg, // pointer to error message
8218 int *escaped) // return value has escaped white space (can
8219 // be NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008220{
8221 int i;
8222 char_u *s;
8223 char_u *result;
8224 char_u *resultbuf = NULL;
8225 int resultlen;
8226 buf_T *buf;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008227 int valid = VALID_HEAD + VALID_PATH; // assume valid result
Bram Moolenaar071d4272004-06-13 20:20:40 +00008228 int spec_idx;
Bram Moolenaarfd249462018-07-28 16:14:30 +02008229 int tilde_file = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008230 int skip_mod = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008231 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008232
8233 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +00008234 if (escaped != NULL)
8235 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008236
8237 /*
8238 * Check if there is something to do.
8239 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008240 spec_idx = find_cmdline_var(src, usedlen);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008241 if (spec_idx < 0) // no match
Bram Moolenaar071d4272004-06-13 20:20:40 +00008242 {
8243 *usedlen = 1;
8244 return NULL;
8245 }
8246
8247 /*
8248 * Skip when preceded with a backslash "\%" and "\#".
8249 * Note: In "\\%" the % is also not recognized!
8250 */
8251 if (src > srcstart && src[-1] == '\\')
8252 {
8253 *usedlen = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008254 STRMOVE(src - 1, src); // remove backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00008255 return NULL;
8256 }
8257
8258 /*
8259 * word or WORD under cursor
8260 */
Bram Moolenaar65f08472017-09-10 18:16:20 +02008261 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD
8262 || spec_idx == SPEC_CEXPR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008263 {
Bram Moolenaar65f08472017-09-10 18:16:20 +02008264 resultlen = find_ident_under_cursor(&result,
8265 spec_idx == SPEC_CWORD ? (FIND_IDENT | FIND_STRING)
8266 : spec_idx == SPEC_CEXPR ? (FIND_IDENT | FIND_STRING | FIND_EVAL)
8267 : FIND_STRING);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008268 if (resultlen == 0)
8269 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008270 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008271 return NULL;
8272 }
8273 }
8274
8275 /*
8276 * '#': Alternate file name
8277 * '%': Current file name
8278 * File name under the cursor
8279 * File name for autocommand
8280 * and following modifiers
8281 */
8282 else
8283 {
8284 switch (spec_idx)
8285 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008286 case SPEC_PERC: // '%': current file
Bram Moolenaar071d4272004-06-13 20:20:40 +00008287 if (curbuf->b_fname == NULL)
8288 {
8289 result = (char_u *)"";
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008290 valid = 0; // Must have ":p:h" to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00008291 }
8292 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008293 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008294 result = curbuf->b_fname;
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008295 tilde_file = STRCMP(result, "~") == 0;
8296 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008297 break;
8298
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008299 case SPEC_HASH: // '#' or "#99": alternate file
8300 if (src[1] == '#') // "##": the argument list
Bram Moolenaar071d4272004-06-13 20:20:40 +00008301 {
8302 result = arg_all();
8303 resultbuf = result;
8304 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +00008305 if (escaped != NULL)
8306 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008307 skip_mod = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008308 break;
8309 }
8310 s = src + 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008311 if (*s == '<') // "#<99" uses v:oldfiles
Bram Moolenaard812df62008-11-09 12:46:09 +00008312 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008313 i = (int)getdigits(&s);
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008314 if (s == src + 2 && src[1] == '-')
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008315 // just a minus sign, don't skip over it
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008316 s--;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008317 *usedlen = (int)(s - src); // length of what we expand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008318
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008319 if (src[1] == '<' && i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008320 {
Bram Moolenaard812df62008-11-09 12:46:09 +00008321 if (*usedlen < 2)
8322 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008323 // Should we give an error message for #<text?
Bram Moolenaard812df62008-11-09 12:46:09 +00008324 *usedlen = 1;
8325 return NULL;
8326 }
8327#ifdef FEAT_EVAL
8328 result = list_find_str(get_vim_var_list(VV_OLDFILES),
8329 (long)i);
8330 if (result == NULL)
8331 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008332 *errormsg = "";
Bram Moolenaard812df62008-11-09 12:46:09 +00008333 return NULL;
8334 }
8335#else
Bram Moolenaar8e481e82019-01-15 20:07:48 +01008336 *errormsg = _("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008337 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00008338#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008339 }
8340 else
Bram Moolenaard812df62008-11-09 12:46:09 +00008341 {
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008342 if (i == 0 && src[1] == '<' && *usedlen > 1)
8343 *usedlen = 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00008344 buf = buflist_findnr(i);
8345 if (buf == NULL)
8346 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008347 *errormsg = _("E194: No alternate file name to substitute for '#'");
Bram Moolenaard812df62008-11-09 12:46:09 +00008348 return NULL;
8349 }
8350 if (lnump != NULL)
8351 *lnump = ECMD_LAST;
8352 if (buf->b_fname == NULL)
8353 {
8354 result = (char_u *)"";
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008355 valid = 0; // Must have ":p:h" to be valid
Bram Moolenaard812df62008-11-09 12:46:09 +00008356 }
8357 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008358 {
Bram Moolenaard812df62008-11-09 12:46:09 +00008359 result = buf->b_fname;
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008360 tilde_file = STRCMP(result, "~") == 0;
8361 }
Bram Moolenaard812df62008-11-09 12:46:09 +00008362 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008363 break;
8364
8365#ifdef FEAT_SEARCHPATH
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008366 case SPEC_CFILE: // file name under cursor
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008367 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008368 if (result == NULL)
8369 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008370 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008371 return NULL;
8372 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008373 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00008374 break;
8375#endif
8376
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008377 case SPEC_AFILE: // file name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008378 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +00008379 if (result != NULL && !autocmd_fname_full)
8380 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008381 // Still need to turn the fname into a full path. It is
8382 // postponed to avoid a delay when <afile> is not used.
Bram Moolenaarf6dad432008-09-18 19:29:58 +00008383 autocmd_fname_full = TRUE;
8384 result = FullName_save(autocmd_fname, FALSE);
8385 vim_free(autocmd_fname);
8386 autocmd_fname = result;
8387 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008388 if (result == NULL)
8389 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008390 *errormsg = _("E495: no autocommand file name to substitute for \"<afile>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008391 return NULL;
8392 }
Bram Moolenaara0174af2008-01-02 20:08:25 +00008393 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008394 break;
8395
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008396 case SPEC_ABUF: // buffer number for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008397 if (autocmd_bufnr <= 0)
8398 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008399 *errormsg = _("E496: no autocommand buffer number to substitute for \"<abuf>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008400 return NULL;
8401 }
8402 sprintf((char *)strbuf, "%d", autocmd_bufnr);
8403 result = strbuf;
8404 break;
8405
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008406 case SPEC_AMATCH: // match name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008407 result = autocmd_match;
8408 if (result == NULL)
8409 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008410 *errormsg = _("E497: no autocommand match name to substitute for \"<amatch>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008411 return NULL;
8412 }
8413 break;
8414
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008415 case SPEC_SFILE: // file name for ":so" command
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008416 result = estack_sfile();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008417 if (result == NULL)
8418 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008419 *errormsg = _("E498: no :source file name to substitute for \"<sfile>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008420 return NULL;
8421 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008422 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00008423 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008424
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008425 case SPEC_SLNUM: // line in file for ":so" command
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008426 if (SOURCING_NAME == NULL || SOURCING_LNUM == 0)
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008427 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008428 *errormsg = _("E842: no line number to use for \"<slnum>\"");
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008429 return NULL;
8430 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008431 sprintf((char *)strbuf, "%ld", SOURCING_LNUM);
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008432 result = strbuf;
8433 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008434
8435#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008436 case SPEC_SFLNUM: // line in script file
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008437 if (current_sctx.sc_lnum + SOURCING_LNUM == 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008438 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008439 *errormsg = _("E961: no line number to use for \"<sflnum>\"");
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008440 return NULL;
8441 }
8442 sprintf((char *)strbuf, "%ld",
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008443 (long)(current_sctx.sc_lnum + SOURCING_LNUM));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008444 result = strbuf;
8445 break;
8446#endif
8447
8448#ifdef FEAT_CLIENTSERVER
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008449 case SPEC_CLIENT: // Source of last submitted input
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008450 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
8451 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008452 result = strbuf;
8453 break;
8454#endif
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008455
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02008456 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008457 result = (char_u *)""; // avoid gcc warning
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02008458 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008459 }
8460
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008461 resultlen = (int)STRLEN(result); // length of new string
8462 if (src[*usedlen] == '<') // remove the file name extension
Bram Moolenaar071d4272004-06-13 20:20:40 +00008463 {
8464 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008465 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008466 resultlen = (int)(s - result);
8467 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008468 else if (!skip_mod)
8469 {
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008470 valid |= modify_fname(src, tilde_file, usedlen, &result, &resultbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008471 &resultlen);
8472 if (result == NULL)
8473 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008474 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008475 return NULL;
8476 }
8477 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008478 }
8479
8480 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
8481 {
8482 if (valid != VALID_HEAD + VALID_PATH)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008483 // xgettext:no-c-format
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008484 *errormsg = _("E499: Empty file name for '%' or '#', only works with \":p:h\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008485 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008486 *errormsg = _("E500: Evaluates to an empty string");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008487 result = NULL;
8488 }
8489 else
8490 result = vim_strnsave(result, resultlen);
8491 vim_free(resultbuf);
8492 return result;
8493}
8494
8495/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008496 * Expand the <sfile> string in "arg".
8497 *
8498 * Returns an allocated string, or NULL for any error.
8499 */
8500 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008501expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008502{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008503 char *errormsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008504 int len;
8505 char_u *result;
8506 char_u *newres;
8507 char_u *repl;
8508 int srclen;
8509 char_u *p;
8510
8511 result = vim_strsave(arg);
8512 if (result == NULL)
8513 return NULL;
8514
8515 for (p = result; *p; )
8516 {
8517 if (STRNCMP(p, "<sfile>", 7) != 0)
8518 ++p;
8519 else
8520 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008521 // replace "<sfile>" with the sourced file name, and do ":" stuff
Bram Moolenaar63b92542007-03-27 14:57:09 +00008522 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008523 if (errormsg != NULL)
8524 {
8525 if (*errormsg)
8526 emsg(errormsg);
8527 vim_free(result);
8528 return NULL;
8529 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008530 if (repl == NULL) // no match (cannot happen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008531 {
8532 p += srclen;
8533 continue;
8534 }
8535 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
8536 newres = alloc(len);
8537 if (newres == NULL)
8538 {
8539 vim_free(repl);
8540 vim_free(result);
8541 return NULL;
8542 }
8543 mch_memmove(newres, result, (size_t)(p - result));
8544 STRCPY(newres + (p - result), repl);
8545 len = (int)STRLEN(newres);
8546 STRCAT(newres, p + srclen);
8547 vim_free(repl);
8548 vim_free(result);
8549 result = newres;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008550 p = newres + len; // continue after the match
Bram Moolenaar071d4272004-06-13 20:20:40 +00008551 }
8552 }
8553
8554 return result;
8555}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008556
Bram Moolenaar071d4272004-06-13 20:20:40 +00008557#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00008558/*
Bram Moolenaard9462e32011-04-11 21:35:11 +02008559 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +00008560 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +00008561 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008562 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008563dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008564{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008565 if (fname == NULL)
8566 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +02008567 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008568}
8569#endif
8570
8571/*
8572 * ":behave {mswin,xterm}"
8573 */
8574 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008575ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008576{
8577 if (STRCMP(eap->arg, "mswin") == 0)
8578 {
8579 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
8580 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
8581 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
8582 set_option_value((char_u *)"keymodel", 0L,
8583 (char_u *)"startsel,stopsel", 0);
8584 }
8585 else if (STRCMP(eap->arg, "xterm") == 0)
8586 {
8587 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
8588 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
8589 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
8590 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
8591 }
8592 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008593 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008594}
8595
Bram Moolenaar071d4272004-06-13 20:20:40 +00008596static int filetype_detect = FALSE;
8597static int filetype_plugin = FALSE;
8598static int filetype_indent = FALSE;
8599
8600/*
8601 * ":filetype [plugin] [indent] {on,off,detect}"
8602 * on: Load the filetype.vim file to install autocommands for file types.
8603 * off: Load the ftoff.vim file to remove all autocommands for file types.
8604 * plugin on: load filetype.vim and ftplugin.vim
8605 * plugin off: load ftplugof.vim
8606 * indent on: load filetype.vim and indent.vim
8607 * indent off: load indoff.vim
8608 */
8609 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008610ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008611{
8612 char_u *arg = eap->arg;
8613 int plugin = FALSE;
8614 int indent = FALSE;
8615
8616 if (*eap->arg == NUL)
8617 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008618 // Print current status.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008619 smsg("filetype detection:%s plugin:%s indent:%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00008620 filetype_detect ? "ON" : "OFF",
8621 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
8622 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
8623 return;
8624 }
8625
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008626 // Accept "plugin" and "indent" in any order.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008627 for (;;)
8628 {
8629 if (STRNCMP(arg, "plugin", 6) == 0)
8630 {
8631 plugin = TRUE;
8632 arg = skipwhite(arg + 6);
8633 continue;
8634 }
8635 if (STRNCMP(arg, "indent", 6) == 0)
8636 {
8637 indent = TRUE;
8638 arg = skipwhite(arg + 6);
8639 continue;
8640 }
8641 break;
8642 }
8643 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
8644 {
8645 if (*arg == 'o' || !filetype_detect)
8646 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008647 source_runtime((char_u *)FILETYPE_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008648 filetype_detect = TRUE;
8649 if (plugin)
8650 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008651 source_runtime((char_u *)FTPLUGIN_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008652 filetype_plugin = TRUE;
8653 }
8654 if (indent)
8655 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008656 source_runtime((char_u *)INDENT_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008657 filetype_indent = TRUE;
8658 }
8659 }
8660 if (*arg == 'd')
8661 {
Bram Moolenaar1610d052016-06-09 22:53:01 +02008662 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +00008663 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008664 }
8665 }
8666 else if (STRCMP(arg, "off") == 0)
8667 {
8668 if (plugin || indent)
8669 {
8670 if (plugin)
8671 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008672 source_runtime((char_u *)FTPLUGOF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008673 filetype_plugin = FALSE;
8674 }
8675 if (indent)
8676 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008677 source_runtime((char_u *)INDOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008678 filetype_indent = FALSE;
8679 }
8680 }
8681 else
8682 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008683 source_runtime((char_u *)FTOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008684 filetype_detect = FALSE;
8685 }
8686 }
8687 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008688 semsg(_(e_invarg2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008689}
8690
8691/*
Bram Moolenaar3e545692017-06-04 19:00:32 +02008692 * ":setfiletype [FALLBACK] {name}"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008693 */
8694 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008695ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008696{
8697 if (!did_filetype)
Bram Moolenaar3e545692017-06-04 19:00:32 +02008698 {
8699 char_u *arg = eap->arg;
8700
8701 if (STRNCMP(arg, "FALLBACK ", 9) == 0)
8702 arg += 9;
8703
8704 set_option_value((char_u *)"filetype", 0L, arg, OPT_LOCAL);
8705 if (arg != eap->arg)
8706 did_filetype = FALSE;
8707 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008708}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008709
Bram Moolenaar071d4272004-06-13 20:20:40 +00008710 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008711ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008712{
8713#ifdef FEAT_DIGRAPHS
8714 if (*eap->arg != NUL)
8715 putdigraph(eap->arg);
8716 else
Bram Moolenaareae8ae12018-12-14 18:53:02 +01008717 listdigraphs(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008718#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008719 emsg(_("E196: No digraphs in this version"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008720#endif
8721}
8722
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02008723#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
8724 void
8725set_no_hlsearch(int flag)
8726{
8727 no_hlsearch = flag;
8728# ifdef FEAT_EVAL
8729 set_vim_var_nr(VV_HLSEARCH, !no_hlsearch && p_hls);
8730# endif
8731}
8732
Bram Moolenaar071d4272004-06-13 20:20:40 +00008733/*
8734 * ":nohlsearch"
8735 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008736 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008737ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008738{
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02008739 set_no_hlsearch(TRUE);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00008740 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008741}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008742#endif
8743
8744#ifdef FEAT_CRYPT
8745/*
8746 * ":X": Get crypt key
8747 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008748 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008749ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008750{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +01008751 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02008752 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008753}
8754#endif
8755
8756#ifdef FEAT_FOLDING
8757 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008758ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008759{
8760 if (foldManualAllowed(TRUE))
8761 foldCreate(eap->line1, eap->line2);
8762}
8763
8764 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008765ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008766{
8767 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
8768 eap->forceit, FALSE);
8769}
8770
8771 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008772ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008773{
8774 linenr_T lnum;
8775
Bram Moolenaar4facea32019-10-12 20:17:40 +02008776# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02008777 start_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +02008778# endif
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02008779
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008780 // First set the marks for all lines closed/open.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008781 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
8782 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
8783 ml_setmarked(lnum);
8784
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008785 // Execute the command on the marked lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008786 global_exe(eap->arg);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008787 ml_clearmarked(); // clear rest of the marks
Bram Moolenaar4facea32019-10-12 20:17:40 +02008788# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02008789 end_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +02008790# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008791}
8792#endif
Bram Moolenaarf5291f32017-09-14 22:55:37 +02008793
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008794#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar39665952018-08-15 20:59:48 +02008795/*
8796 * Returns TRUE if the supplied Ex cmdidx is for a location list command
8797 * instead of a quickfix command.
8798 */
8799 int
8800is_loclist_cmd(int cmdidx)
8801{
Bram Moolenaar74c8be22018-08-23 22:51:40 +02008802 if (cmdidx < 0 || cmdidx >= CMD_SIZE)
Bram Moolenaar39665952018-08-15 20:59:48 +02008803 return FALSE;
8804 return cmdnames[cmdidx].cmd_name[0] == 'l';
8805}
8806#endif
8807
Bram Moolenaar4facea32019-10-12 20:17:40 +02008808#if defined(FEAT_TIMERS) || defined(PROTO)
Bram Moolenaarf5291f32017-09-14 22:55:37 +02008809 int
8810get_pressedreturn(void)
8811{
8812 return ex_pressedreturn;
8813}
8814
8815 void
8816set_pressedreturn(int val)
8817{
8818 ex_pressedreturn = val;
8819}
8820#endif